-
Notifications
You must be signed in to change notification settings - Fork 23
/
footnote_reference.go
102 lines (97 loc) · 2.66 KB
/
footnote_reference.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package sgml
import (
"strings"
"github.com/bytesparadise/libasciidoc/pkg/types"
"github.com/pkg/errors"
)
func (r *sgmlRenderer) renderFootnoteReference(note *types.FootnoteReference) (string, error) {
result := &strings.Builder{}
if note.ID != types.InvalidFootnoteReference && !note.Duplicate {
// valid case for a footnote with content, with our without an explicit reference
tmpl, err := r.footnote()
if err != nil {
return "", errors.Wrap(err, "unable to load footnote template")
}
if err := tmpl.Execute(result, struct {
ID int
Ref string
}{
ID: note.ID,
Ref: note.Ref,
}); err != nil {
return "", errors.Wrap(err, "unable to render footnote")
}
} else if note.Duplicate {
// valid case for a footnote with content, with our without an explicit reference
tmpl, err := r.footnoteRef()
if err != nil {
return "", errors.Wrap(err, "unable to load footnote template")
}
if err := tmpl.Execute(result, struct {
ID int
Ref string
}{
ID: note.ID,
Ref: note.Ref,
}); err != nil {
return "", errors.Wrap(err, "unable to render footnote")
}
} else {
// invalid footnote
tmpl, err := r.invalidFootnote()
if err != nil {
return "", errors.Wrap(err, "unable to load missing footnote template")
}
if err := tmpl.Execute(result, struct {
Ref string
}{
Ref: note.Ref,
}); err != nil {
return "", errors.Wrap(err, "unable to render missing footnote")
}
}
return result.String(), nil
}
func (r *sgmlRenderer) renderFootnotes(ctx *context, notes []*types.Footnote) (string, error) {
// skip if there's no foot note in the doc
if len(notes) == 0 {
return "", nil
}
content := &strings.Builder{}
for _, note := range notes {
renderedNote, err := r.renderFootnoteElement(ctx, note)
if err != nil {
return "", errors.Wrap(err, "failed to render footnote element")
}
content.WriteString(renderedNote)
}
return r.execute(r.footnotes, struct {
Context *context
Content string
Footnotes []*types.Footnote
}{
Context: ctx,
Content: content.String(),
Footnotes: notes,
})
}
func (r *sgmlRenderer) renderFootnoteElement(ctx *context, note *types.Footnote) (string, error) {
content, err := r.renderInlineElements(ctx, note.Elements)
if err != nil {
return "", errors.Wrapf(err, "unable to render foot note content")
}
content = strings.TrimSpace(content)
// Note: Asciidoctor will render the footnote content on a single line
content = strings.ReplaceAll(content, "\n", " ")
return r.execute(r.footnoteElement, struct {
Context *context
ID int
Ref string
Content string
}{
Context: ctx,
ID: note.ID,
Ref: note.Ref,
Content: string(content),
})
}