MCPcopy Create free account
hub / github.com/gogs/gogs / postProcessHTML

Function postProcessHTML

internal/markup/markup.go:245–317  ·  view source on GitHub ↗

postProcessHTML treats different types of HTML differently, and only renders special links for plain text blocks.

(rawHTML []byte, urlPrefix string, metas map[string]string)

Source from the content-addressed store, hash-verified

243// postProcessHTML treats different types of HTML differently,
244// and only renders special links for plain text blocks.
245func postProcessHTML(rawHTML []byte, urlPrefix string, metas map[string]string) []byte {
246 startTags := make([]string, 0, 5)
247 buf := bytes.NewBuffer(nil)
248 tokenizer := html.NewTokenizer(bytes.NewReader(rawHTML))
249
250outerLoop:
251 for html.ErrorToken != tokenizer.Next() {
252 token := tokenizer.Token()
253 switch token.Type {
254 case html.TextToken:
255 buf.Write(RenderSpecialLink([]byte(token.String()), urlPrefix, metas))
256
257 case html.StartTagToken:
258 tagName := token.Data
259
260 if tagName == "img" {
261 wrapImgWithLink(urlPrefix, buf, token)
262 continue outerLoop
263 }
264
265 buf.WriteString(token.String())
266 // If this is an excluded tag, we skip processing all output until a close tag is encountered.
267 if strings.EqualFold("a", tagName) || strings.EqualFold("code", tagName) || strings.EqualFold("pre", tagName) {
268 stackNum := 1
269 for html.ErrorToken != tokenizer.Next() {
270 token = tokenizer.Token()
271
272 // Copy the token to the output verbatim
273 buf.WriteString(token.String())
274
275 // Stack number doesn't increase for tags without end tags.
276 if token.Type == html.StartTagToken && !com.IsSliceContainsStr(noEndTags, token.Data) {
277 stackNum++
278 }
279
280 // If this is the close tag to the outer-most, we are done
281 if token.Type == html.EndTagToken {
282 stackNum--
283 if stackNum <= 0 && strings.EqualFold(tagName, token.Data) {
284 break
285 }
286 }
287 }
288 continue outerLoop
289 }
290
291 if !com.IsSliceContainsStr(noEndTags, tagName) {
292 startTags = append(startTags, tagName)
293 }
294
295 case html.EndTagToken:
296 if len(startTags) == 0 {
297 buf.WriteString(token.String())
298 break
299 }
300
301 buf.Write(leftAngleBracket)
302 buf.WriteString(startTags[len(startTags)-1])

Callers 1

RenderFunction · 0.85

Calls 7

RenderSpecialLinkFunction · 0.85
wrapImgWithLinkFunction · 0.85
appendFunction · 0.85
WriteMethod · 0.80
BytesMethod · 0.80
NextMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected