forked from bytesparadise/libasciidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quoted_text.go
64 lines (59 loc) · 1.92 KB
/
quoted_text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package html5
import (
"bytes"
"html/template"
texttemplate "text/template"
"github.com/bytesparadise/libasciidoc/pkg/renderer"
"github.com/bytesparadise/libasciidoc/pkg/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
var boldTextTmpl texttemplate.Template
var italicTextTmpl texttemplate.Template
var monospaceTextTmpl texttemplate.Template
var subscriptTextTmpl texttemplate.Template
var superscriptTextTmpl texttemplate.Template
// initializes the templates
func init() {
boldTextTmpl = newTextTemplate("bold text", "<strong>{{.}}</strong>")
italicTextTmpl = newTextTemplate("italic text", "<em>{{.}}</em>")
monospaceTextTmpl = newTextTemplate("monospace text", "<code>{{.}}</code>")
subscriptTextTmpl = newTextTemplate("subscript text", "<sub>{{.}}</sub>")
superscriptTextTmpl = newTextTemplate("superscript text", "<sup>{{.}}</sup>")
}
func renderQuotedText(ctx *renderer.Context, t types.QuotedText) ([]byte, error) {
elementsBuffer := bytes.NewBuffer(nil)
for _, element := range t.Elements {
b, err := renderElement(ctx, element)
if err != nil {
return nil, errors.Wrapf(err, "unable to render text quote")
}
_, err = elementsBuffer.Write(b)
if err != nil {
return nil, errors.Wrapf(err, "unable to render text quote")
}
}
result := bytes.NewBuffer(nil)
var tmpl texttemplate.Template
kind := t.Attributes[types.AttrKind]
switch kind {
case types.Bold:
tmpl = boldTextTmpl
case types.Italic:
tmpl = italicTextTmpl
case types.Monospace:
tmpl = monospaceTextTmpl
case types.Subscript:
tmpl = subscriptTextTmpl
case types.Superscript:
tmpl = superscriptTextTmpl
default:
return nil, errors.Errorf("unsupported quoted text kind: '%v'", kind)
}
err := tmpl.Execute(result, template.HTML(elementsBuffer.String()))
if err != nil {
return nil, errors.Wrapf(err, "unable to render monospaced quote")
}
log.Debugf("rendered bold quote: %s", result.Bytes())
return result.Bytes(), nil
}