-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.go
231 lines (212 loc) · 6.47 KB
/
renderer.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// This file is part of gmnhg.
// gmnhg is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gmnhg is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gmnhg. If not, see <https://www.gnu.org/licenses/>.
package renderer
import (
"bytes"
"fmt"
"io"
"regexp"
"github.com/gomarkdown/markdown/ast"
)
var (
lineBreak = []byte{'\n'}
space = []byte{' '}
linkPrefix = []byte("=> ")
codeDelimiter = []byte("`")
emphDelimiter = []byte("*")
strongDelimiter = []byte("**")
delDelimiter = []byte("~~")
)
// matches a FULL string that contains no non-whitespace characters
var emptyLineRegex = regexp.MustCompile(`\A[\s]*\z`)
var lineBreakCharacters = regexp.MustCompile(`[\n\r]+`)
// Renderer implements markdown.Renderer.
type Renderer struct{}
// NewRenderer returns a new Renderer.
func NewRenderer() Renderer {
return Renderer{}
}
func getNodeDelimiter(node ast.Node) []byte {
switch node.(type) {
case *ast.Code:
return codeDelimiter
case *ast.Emph:
return emphDelimiter
case *ast.Strong:
return strongDelimiter
case *ast.Del:
return delDelimiter
default:
return []byte{}
}
}
func textWithNewlineReplacement(node ast.Node, replacement []byte, unescapeHtml bool) []byte {
buf := bytes.Buffer{}
delimiter := getNodeDelimiter(node)
// special case for footnotes: we want them in the text
if node, ok := node.(*ast.Link); ok && node.Footnote != nil {
fmt.Fprintf(&buf, "[^%d]", node.NoteID)
}
if leaf := node.AsLeaf(); leaf != nil {
// replace all newlines in text with preferred symbols; this may
// be spaces for general text, allowing for soft wrapping, which
// is recommended as per Gemini spec p. 5.4.1, or line breaks
// with a blockquote symbols for blockquotes, or just nothing
buf.Write(delimiter)
switch node := node.(type) {
case *ast.Hardbreak:
buf.Write(lineBreak)
// If the blockquote ends with a double space, the parser will
// not create a Hardbreak at the end, so this works.
if _, ok := leaf.Parent.(*ast.BlockQuote); !ok {
buf.Write(quotePrefix)
}
case *ast.HTMLSpan:
if isHardBreak(leaf.Literal) {
buf.Write(lineBreak)
}
buf.Write(leaf.Content)
case *ast.HTMLBlock:
buf.Write([]byte(stripHtml(node, quotePrefix)))
default:
textWithoutBreaks := lineBreakCharacters.ReplaceAll(leaf.Literal, replacement)
if unescapeHtml {
buf.Write(unescapeHtmlText(textWithoutBreaks))
} else {
buf.Write(textWithoutBreaks)
}
}
buf.Write(delimiter)
}
if node := node.AsContainer(); node != nil {
buf.Write(delimiter)
for _, child := range node.Children {
// skip non-text child elements from rendering
switch child := child.(type) {
case *ast.List:
default:
buf.Write(textWithNewlineReplacement(child, replacement, unescapeHtml))
}
}
buf.Write(delimiter)
}
return buf.Bytes()
}
func (r Renderer) text(w io.Writer, node ast.Node, unescapeHtml bool) {
w.Write(textWithNewlineReplacement(node, space, unescapeHtml))
}
func extractLinks(node ast.Node) (stack []ast.Node) {
if node := node.AsContainer(); node != nil {
for _, subnode := range node.Children {
stack = append(stack, extractLinks(subnode)...)
}
}
switch node := node.(type) {
case *ast.Image:
stack = append(stack, node)
case *ast.Link:
stack = append(stack, node)
// footnotes are represented as links which embed an extra node
// containing footnote text; the link itself is not considered a
// container
if node.Footnote != nil {
stack = append(stack, extractLinks(node.Footnote)...)
}
}
return stack
}
func isLinksOnlyList(node *ast.List) bool {
for _, child := range node.Children {
child, ok := child.(*ast.ListItem)
if !ok {
return false // should never happen
}
for _, liChild := range child.Children {
liChild, ok := liChild.(*ast.Paragraph)
if !ok {
return false // sublist, etc
}
if !isLinksOnlyParagraph(liChild) {
return false
}
}
}
return true
}
// RenderNode implements Renderer.RenderNode().
func (r Renderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.WalkStatus {
// entering in gomarkdown was made to have elements of type switch
// to enclose themselves within the second pass with entering =
// false, as Markdown is quite similar to HTML in its structure.
// As Gemtext is line-oriented, and not tag-oriented, most of
// container subroutines have to handle their subelements on
// themselves.
noNewLine := true
fetchLinks := false
switch node := node.(type) {
case *ast.BlockQuote:
r.blockquote(w, node, entering)
fetchLinks = true
case *ast.HorizontalRule:
r.hr(w, node, entering)
case *ast.Heading:
r.heading(w, node, entering)
noNewLine = false
case *ast.Paragraph:
switch node.Parent.(type) {
// these (should) handle underlying paragraphs themselves
case *ast.BlockQuote, *ast.ListItem, *ast.Footnotes:
default:
noNewLine = r.paragraph(w, node, entering)
fetchLinks = true
}
case *ast.CodeBlock:
r.code(w, node)
// code block is not considered a wrapping element
w.Write(lineBreak)
case *ast.List:
// lists of level >= 2 are rendered recursively along with the
// first level; the list is a container
_, parentIsDocument := node.Parent.(*ast.Document)
// footnotes are rendered as links after the parent paragraph
if !node.IsFootnotesList && parentIsDocument && !entering {
if !isLinksOnlyList(node) {
r.list(w, node, 0)
noNewLine = false
}
fetchLinks = true
}
case *ast.Table:
r.table(w, node, entering)
noNewLine = false
fetchLinks = true
case *ast.HTMLBlock:
// Do not render if already rendered as part of a blockquote
if _, ok := node.Parent.(*ast.BlockQuote); !ok {
r.htmlBlock(w, node, entering)
}
}
if !noNewLine && !entering {
w.Write(lineBreak)
}
if fetchLinks && !entering {
links := extractLinks(node)
if len(links) > 0 {
r.linksList(w, links)
}
}
return ast.GoToNext
}
// RenderHeader implements Renderer.RenderHeader().
func (r Renderer) RenderHeader(w io.Writer, node ast.Node) {}
// RenderFooter implements Renderer.RenderFooter().
func (r Renderer) RenderFooter(w io.Writer, node ast.Node) {}