-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmd_citations.go
135 lines (111 loc) · 2.93 KB
/
md_citations.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package website
import (
"context"
"errors"
"fmt"
"regexp"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
var citationRegExp = regexp.MustCompile(`\\cite{([A-Za-z0-9-]+)}`)
const citationRefIndex = 1
var _ goldmark.Extender = (*citationExt)(nil)
type citationExt struct {
References references
}
func (e *citationExt) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(parser.WithInlineParsers(
util.Prioritized(&citationParser{}, 0),
))
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(&citationRenderer{
References: e.References,
}, 500),
))
}
var citationNodeKind = ast.NewNodeKind("Citation")
var _ ast.Node = (*citationNode)(nil)
// citationNode is a Goldmark AST node for a citation.
type citationNode struct {
ast.BaseInline
Reference string
}
func (n *citationNode) Dump(source []byte, level int) {
ast.DumpHelper(n, source, level, nil, nil)
}
func (n *citationNode) Kind() ast.NodeKind {
return citationNodeKind
}
var _ parser.InlineParser = (*citationParser)(nil)
// citationParser is a Goldmark inline parser for citations.
// It finds citations in the source markdown and replaces them with citation
// nodes.
type citationParser struct{}
func (p *citationParser) Trigger() []byte {
return []byte{'\\'}
}
func (p *citationParser) Parse(
parent ast.Node,
block text.Reader,
pc parser.Context,
) ast.Node {
line, _ := block.PeekLine()
matches := citationRegExp.FindSubmatch(line)
if len(matches) == 0 {
return nil
}
citeFull := matches[0]
block.Advance(len(citeFull))
citeRef := matches[citationRefIndex]
return &citationNode{
Reference: string(citeRef),
}
// for next := block.Peek(); next != '}'; {
// fmt.Printf("CITATION: %s\n", string(next))
// block.Advance(1)
// next = block.Peek()
// }
// block.Advance(1)
// fmt.Printf("CITATION: %s\n", string(block.Peek()))
// block.Advance(1)
// return &NodeCitation{}
}
var _ renderer.NodeRenderer = (*citationRenderer)(nil)
// citationRenderer renders citation nodes to HTML.
type citationRenderer struct {
References references
}
func (r *citationRenderer) RegisterFuncs(
reg renderer.NodeRendererFuncRegisterer,
) {
reg.Register(citationNodeKind, r.renderCitation)
}
func (r *citationRenderer) renderCitation(
w util.BufWriter,
source []byte,
n ast.Node,
entering bool,
) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
citeNode, ok := n.(*citationNode)
if !ok {
return ast.WalkStop, errors.New("not a citation node")
}
ref, ok := r.References.cite(citeNode.Reference)
if !ok {
return ast.WalkStop, fmt.Errorf(
"reference not found for %q",
citeNode.Reference,
)
}
if err := citationTemplate(ref).Render(context.TODO(), w); err != nil {
return ast.WalkStop, fmt.Errorf("rendering citation: %w", err)
}
return ast.WalkContinue, nil
}