forked from cammellos/markdown
-
Notifications
You must be signed in to change notification settings - Fork 4
/
md_renderer.go
401 lines (366 loc) · 9.06 KB
/
md_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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package md
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/status-im/markdown/ast"
)
// Renderer renders to markdown. Allows to convert to a canonnical
// form
type Renderer struct {
orderedListCounter map[int]int
// used to keep track of whether a given list item uses a paragraph
// for large spacing.
paragraph map[int]bool
lastOutputLen int
listDepth int
lastNormalText string
}
// NewRenderer returns a Markdown renderer.
func NewRenderer() *Renderer {
return &Renderer{
orderedListCounter: map[int]int{},
paragraph: map[int]bool{},
}
}
func (r *Renderer) out(w io.Writer, d []byte) {
r.lastOutputLen = len(d)
w.Write(d)
}
func (r *Renderer) outs(w io.Writer, s string) {
r.lastOutputLen = len(s)
io.WriteString(w, s)
}
func (r *Renderer) cr(w io.Writer) {
if r.lastOutputLen > 0 {
r.outs(w, "\n")
}
}
func (r *Renderer) doubleSpace(w io.Writer) {
// TODO: need to remember number of written bytes
//if out.Len() > 0 {
r.outs(w, "\n")
//}
}
func (r *Renderer) list(w io.Writer, node *ast.List, entering bool) {
if entering {
r.listDepth++
flags := node.ListFlags
if flags&ast.ListTypeOrdered != 0 {
r.orderedListCounter[r.listDepth] = 1
}
} else {
r.listDepth--
}
}
func (r *Renderer) listItem(w io.Writer, node *ast.ListItem, entering bool) {
flags := node.ListFlags
text := node.Literal
if entering {
if flags&ast.ListTypeOrdered != 0 {
fmt.Fprintf(w, "%d.", r.orderedListCounter[r.listDepth])
//indentwriter.New(w, 1).Write(text)
fmt.Fprintf(w, "%s", text)
r.orderedListCounter[r.listDepth]++
} else {
r.outs(w, "-")
//indentwriter.New(w, 1).Write(text)
fmt.Fprintf(w, "%s", text)
}
if false && !node.Tight {
r.outs(w, "\n")
}
if r.paragraph[r.listDepth] {
if flags&ast.ListItemEndOfList == 0 {
r.outs(w, "\n")
}
r.paragraph[r.listDepth] = false
}
}
}
func (r *Renderer) para(w io.Writer, node *ast.Paragraph, entering bool) {
if entering {
// marker := out.Len()
//r.doubleSpace(w)
r.paragraph[r.listDepth] = true
r.outs(w, " ")
} else {
r.cr(w)
}
}
// escape replaces instances of backslash with escaped backslash in text.
func escape(text []byte) []byte {
return bytes.Replace(text, []byte(`\`), []byte(`\\`), -1)
}
func isNumber(data []byte) bool {
for _, b := range data {
if b < '0' || b > '9' {
return false
}
}
return true
}
func needsEscaping(text []byte, lastNormalText string) bool {
switch string(text) {
case `\`,
"`",
"*",
"_",
"{", "}",
"[", "]",
"(", ")",
"#",
"+",
"-":
return true
case "!":
return false
case ".":
// Return true if number, because a period after a number must be escaped to not get parsed as an ordered list.
return isNumber([]byte(lastNormalText))
case "<", ">":
return true
default:
return false
}
}
// cleanWithoutTrim is like clean, but doesn't trim blanks.
func cleanWithoutTrim(s string) string {
var b []byte
var p byte
for i := 0; i < len(s); i++ {
q := s[i]
if q == '\n' || q == '\r' || q == '\t' {
q = ' '
}
if q != ' ' || p != ' ' {
b = append(b, q)
p = q
}
}
return string(b)
}
func (r *Renderer) skipSpaceIfNeededNormalText(w io.Writer, cleanString string) bool {
if cleanString[0] != ' ' {
return false
}
return false
// TODO: what did it mean to do?
// we no longer use *bytes.Buffer for out, so whatever this tracked,
// it has to be done in a different wy
/*
if _, ok := r.normalTextMarker[out]; !ok {
r.normalTextMarker[out] = -1
}
return r.normalTextMarker[out] == out.Len()
*/
}
func (r *Renderer) text(w io.Writer, text *ast.Text) {
lit := text.Literal
normalText := string(text.Literal)
if needsEscaping(lit, r.lastNormalText) {
lit = append([]byte("\\"), lit...)
}
r.lastNormalText = normalText
if r.listDepth > 0 && string(lit) == "\n" {
// TODO: See if this can be cleaned up... It's needed for lists.
return
}
cleanString := cleanWithoutTrim(string(lit))
if cleanString == "" {
return
}
// Skip first space if last character is already a space (i.e., no need for a 2nd space in a row).
if r.skipSpaceIfNeededNormalText(w, cleanString) {
cleanString = cleanString[1:]
}
r.outs(w, cleanString)
// If it ends with a space, make note of that.
if len(cleanString) >= 1 && cleanString[len(cleanString)-1] == ' ' {
// TODO: write equivalent of this
// r.normalTextMarker[out] = out.Len()
}
}
func (r *Renderer) del(w io.Writer, node *ast.Del) {
r.outs(w, "~~")
r.out(w, node.Literal)
r.outs(w, "~~")
}
func (r *Renderer) strong(w io.Writer, node *ast.Strong) {
text := node.Literal
r.outs(w, "**")
r.out(w, text)
r.outs(w, "**")
}
func (r *Renderer) emphasis(w io.Writer, node *ast.Emph) {
text := node.Literal
if len(text) == 0 {
return
}
r.outs(w, "*")
r.out(w, text)
r.outs(w, "*")
}
func (r *Renderer) htmlSpan(w io.Writer, node *ast.HTMLSpan) {
r.out(w, node.Literal)
}
func (r *Renderer) htmlBlock(w io.Writer, node *ast.HTMLBlock) {
r.doubleSpace(w)
r.out(w, node.Literal)
r.outs(w, "\n")
}
func (r *Renderer) codeBlock(w io.Writer, node *ast.CodeBlock) {
r.doubleSpace(w)
text := node.Literal
lang := string(node.Info)
// Parse out the language name.
count := 0
for _, elt := range strings.Fields(lang) {
if elt[0] == '.' {
elt = elt[1:]
}
if len(elt) == 0 {
continue
}
r.outs(w, "```")
r.outs(w, elt)
count++
break
}
if count == 0 {
r.outs(w, "```")
}
r.outs(w, "\n")
r.out(w, text)
r.outs(w, "```\n")
}
func (r *Renderer) code(w io.Writer, node *ast.Code) {
r.outs(w, "`")
r.out(w, node.Literal)
r.outs(w, "`")
}
func (r *Renderer) image(w io.Writer, node *ast.Image) {
link := node.Destination
title := node.Title
// alt := node. ??
var alt []byte
r.outs(w, "![")
r.out(w, alt)
r.outs(w, "](")
r.out(w, escape(link))
if len(title) != 0 {
r.outs(w, ` "`)
r.out(w, title)
r.outs(w, `"`)
}
r.outs(w, ")")
}
func (r *Renderer) link(w io.Writer, node *ast.Link) {
link := string(escape(node.Destination))
title := string(node.Title)
content := string(node.Literal)
r.outs(w, "[")
r.outs(w, content)
r.outs(w, "](")
r.outs(w, link)
if len(title) != 0 {
r.outs(w, ` "`)
r.outs(w, title)
r.outs(w, `"`)
}
r.outs(w, ")")
}
// RenderNode renders markdown node
func (r *Renderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.WalkStatus {
switch node := node.(type) {
case *ast.Text:
r.text(w, node)
case *ast.Softbreak:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Hardbreak:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Emph:
r.emphasis(w, node)
case *ast.Strong:
r.strong(w, node)
case *ast.Del:
r.del(w, node)
case *ast.BlockQuote:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Aside:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Link:
r.link(w, node)
case *ast.CrossReference:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Citation:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Image:
r.image(w, node)
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Code:
r.code(w, node)
case *ast.CodeBlock:
r.codeBlock(w, node)
case *ast.Caption:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.CaptionFigure:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Document:
// do nothing
case *ast.Paragraph:
r.para(w, node, entering)
case *ast.HTMLSpan:
r.htmlSpan(w, node)
case *ast.HTMLBlock:
r.htmlBlock(w, node)
case *ast.Heading:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.HorizontalRule:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.List:
r.list(w, node, entering)
case *ast.ListItem:
r.listItem(w, node, entering)
case *ast.Table:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.TableCell:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.TableHeader:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.TableBody:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.TableRow:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.TableFooter:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Math:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.MathBlock:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.DocumentMatter:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Callout:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Index:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Subscript:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Superscript:
panic(fmt.Sprintf("node %T NYI", node))
case *ast.Footnotes:
// nothing by default; just output the list.
default:
panic(fmt.Sprintf("Unknown node %T", node))
}
return ast.GoToNext
}
// RenderHeader renders header
func (r *Renderer) RenderHeader(w io.Writer, ast ast.Node) {
// do nothing
}
// RenderFooter renders footer
func (r *Renderer) RenderFooter(w io.Writer, ast ast.Node) {
// do nothing
}