callouts.go

 1package html
 2
 3import (
 4	"bytes"
 5	"io"
 6
 7	"github.com/gomarkdown/markdown/ast"
 8	"github.com/gomarkdown/markdown/parser"
 9)
10
11// EscapeHTMLCallouts writes html-escaped d to w. It escapes &, <, > and " characters, *but*
12// expands callouts <<N>> with the callout HTML, i.e. by calling r.callout() with a newly created
13// ast.Callout node.
14func (r *Renderer) EscapeHTMLCallouts(w io.Writer, d []byte) {
15	ld := len(d)
16Parse:
17	for i := 0; i < ld; i++ {
18		for _, comment := range r.opts.Comments {
19			if !bytes.HasPrefix(d[i:], comment) {
20				break
21			}
22
23			lc := len(comment)
24			if i+lc < ld {
25				if id, consumed := parser.IsCallout(d[i+lc:]); consumed > 0 {
26					// We have seen a callout
27					callout := &ast.Callout{ID: id}
28					r.callout(w, callout)
29					i += consumed + lc - 1
30					continue Parse
31				}
32			}
33		}
34
35		escSeq := Escaper[d[i]]
36		if escSeq != nil {
37			w.Write(escSeq)
38		} else {
39			w.Write([]byte{d[i]})
40		}
41	}
42}