-
Notifications
You must be signed in to change notification settings - Fork 45
/
renderer.go
625 lines (564 loc) · 15 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// The package man outputs man pages from mmmark markdown.
package man
// Lots of code copied from https://github.com/cpuguy83/go-md2man, but adapated to mmark
// and made to support mmark features.
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"strconv"
"strings"
"time"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
"github.com/mmarkdown/mmark/v2/lang"
"github.com/mmarkdown/mmark/v2/mast"
)
// Flags control optional behavior of Markdown renderer.
type Flags int
// HTML renderer configuration options.
const (
FlagsNone Flags = 0
ManFragment Flags = 1 << iota // Don't generate a complete document
CommonFlags Flags = FlagsNone
)
// RendererOptions is a collection of supplementary parameters tweaking
// the behavior of various parts of Markdown renderer.
type RendererOptions struct {
Flags Flags // Flags allow customizing this renderer's behavior
Language lang.Lang // Output language for the document.
// if set, called at the start of RenderNode(). Allows replacing rendering of some nodes
RenderNodeHook html.RenderNodeFunc
// Comments is a list of comments the renderer should detect when
// parsing code blocks and detecting callouts.
Comments [][]byte
}
// Renderer implements Renderer interface for Markdown output.
type Renderer struct {
opts RendererOptions
Title *mast.Title
listLevel int
allListLevel int
}
// NewRenderer creates and configures an Renderer object, which satisfies the Renderer interface.
func NewRenderer(opts RendererOptions) *Renderer {
return &Renderer{opts: opts}
}
func (r *Renderer) hardBreak(w io.Writer, node *ast.Hardbreak) {
r.outs(w, "\n.br\n")
}
func (r *Renderer) matter(w io.Writer, node *ast.DocumentMatter, entering bool) {
// TODO: what should this output?
}
func (r *Renderer) title(w io.Writer, node *mast.Title, entering bool) {
if !entering {
return
}
if node.Date.IsZero() {
node.Date = time.Now().UTC()
}
// track back to first space and assume the rest is the section, don't parse it as a number
i := len(node.Title) - 1
for i > 0 && node.Title[i-1] != ' ' {
i--
}
section := 1
title := node.Title
switch {
case i > 0:
d, err := strconv.Atoi(node.Title[i:])
if err != nil {
log.Print("No section number found at end of title, defaulting to 1")
} else {
section = d
title = node.Title[:i-1]
}
}
if i == 0 {
log.Print("No section number found at end of title, defaulting to 1")
}
r.outs(w, fmt.Sprintf(".TH %q", strings.ToUpper(title)))
r.outs(w, fmt.Sprintf(" %d", section))
r.outs(w, fmt.Sprintf(" %q", node.Date.Format("January 2006")))
r.outs(w, fmt.Sprintf(" %q", node.Area))
r.outs(w, fmt.Sprintf(" %q", node.Workgroup))
r.outs(w, "\n")
}
func (r *Renderer) heading(w io.Writer, node *ast.Heading, entering bool) {
if entering {
switch node.Level {
case 1, 2:
r.outs(w, "\n.SH ")
default:
r.outs(w, "\n.SS ")
}
}
}
func (r *Renderer) citation(w io.Writer, node *ast.Citation, entering bool) {
r.outs(w, "[")
for i, dest := range node.Destination {
if i > 0 {
r.outs(w, ", ")
}
r.out(w, dest)
}
r.outs(w, "]")
}
func (r *Renderer) paragraph(w io.Writer, para *ast.Paragraph, entering bool) {
if entering {
// If in lists, suppress paragraphs. Unless we know the list contains
// block level elements, but then only apply this after the first paragraph.
parent := para.Parent
if parent != nil {
if _, ok := parent.(*ast.ListItem); ok {
// if we're the first para return, otherwise output a PP
c := parent.GetChildren()
i := 0
par := 0
for i = range c {
_, ok := c[i].(*ast.Paragraph)
if ok {
par++
}
if c[i] == para {
if par > 1 {
// No .PP because that messes up formatting.
r.outs(w, "\n\n")
}
}
}
return
}
}
r.outs(w, "\n.PP\n")
return
}
r.outs(w, "\n")
}
func (r *Renderer) list(w io.Writer, list *ast.List, entering bool) {
if list.IsFootnotesList {
return
}
// normal list
if entering {
r.allListLevel++
if list.ListFlags&ast.ListTypeOrdered == 0 && list.ListFlags&ast.ListTypeTerm == 0 && list.ListFlags&ast.ListTypeDefinition == 0 {
r.listLevel++
}
if r.allListLevel > 1 {
r.outs(w, "\n.RS\n")
} else {
r.outs(w, "\n")
}
return
}
if r.allListLevel > 1 {
r.outs(w, "\n.RE\n")
} else {
r.outs(w, "\n")
}
r.allListLevel--
if list.ListFlags&ast.ListTypeOrdered == 0 && list.ListFlags&ast.ListTypeTerm == 0 && list.ListFlags&ast.ListTypeDefinition == 0 {
r.listLevel--
}
}
func (r *Renderer) listItem(w io.Writer, listItem *ast.ListItem, entering bool) {
if entering {
// footnotes
if listItem.RefLink != nil {
// get number in the list
children := listItem.Parent.GetChildren()
for i := range children {
if listItem == children[i] {
r.outs(w, fmt.Sprintf("\n.IP [%d]\n", i+1))
}
}
return
}
x := listItem.ListFlags
switch {
case x&ast.ListTypeOrdered != 0:
children := listItem.GetParent().GetChildren()
i := 0
for i = 0; i < len(children); i++ {
if children[i] == listItem {
break
}
}
start := listItem.GetParent().(*ast.List).Start
r.outs(w, fmt.Sprintf(".IP %d\\. 4\n", start+i+1))
case x&ast.ListTypeTerm != 0:
r.outs(w, ".TP\n")
case x&ast.ListTypeDefinition != 0:
r.outs(w, "")
default:
if r.listLevel%2 == 0 {
r.outs(w, ".IP \\(en 4\n")
} else {
r.outs(w, ".IP \\(bu 4\n")
}
}
}
}
func (r *Renderer) codeBlock(w io.Writer, codeBlock *ast.CodeBlock, entering bool) {
if entering {
r.outs(w, "\n.PP\n.RS\n\n.nf\n")
escapeSpecialChars(r, w, codeBlock.Literal)
r.outs(w, "\n.fi\n.RE\n")
}
}
func (r *Renderer) table(w io.Writer, tab *ast.Table, entering bool) {
// The tbl renderer want to see the entire table's columns, rows first
if entering {
r.outs(w, "\n.RS\n.TS\nallbox;\n")
cells := rows(tab)
for r1 := 0; r1 < len(cells); r1++ {
align := ""
for c := 0; c < len(cells[r1]); c++ {
x := cells[r1][c]
switch x.Align {
case ast.TableAlignmentLeft:
align += "l "
case ast.TableAlignmentRight:
align += "r "
case ast.TableAlignmentCenter:
fallthrough
default:
align += "c "
}
if x.ColSpan > 0 {
align += strings.Repeat("s ", x.ColSpan-1)
}
}
r.outs(w, strings.TrimSpace(align)+"\n")
}
r.outs(w, ".\n")
return
}
r.outs(w, ".TE\n.RE\n\n")
}
func (r *Renderer) tableRow(w io.Writer, tableRow *ast.TableRow, entering bool) {
if !entering {
r.outs(w, "\n")
}
}
func (r *Renderer) tableCell(w io.Writer, tableCell *ast.TableCell, entering bool) {
if tableCell.IsHeader {
r.outOneOf(w, entering, "\\fB", "\\fP")
}
parent := tableCell.Parent
if tableCell == ast.GetFirstChild(parent) {
return
}
if entering {
r.outs(w, "\t")
return
}
}
func (r *Renderer) htmlSpan(w io.Writer, span *ast.HTMLSpan) {}
func (r *Renderer) crossReference(w io.Writer, cr *ast.CrossReference, entering bool) {
if !entering {
return
}
r.out(w, bytes.ToUpper(cr.Destination))
}
func (r *Renderer) index(w io.Writer, index *ast.Index, entering bool) {}
func (r *Renderer) link(w io.Writer, link *ast.Link, entering bool) {
if link.Footnote != nil {
if entering {
r.outs(w, fmt.Sprintf("\\u[%d]\\d", link.NoteID))
}
return
}
// !entering so the URL comes after the link text.
if !entering {
r.outs(w, "\n\\[la]")
r.out(w, link.Destination)
r.outs(w, "\\[ra]")
}
}
func (r *Renderer) image(w io.Writer, node *ast.Image, entering bool) {
// only works with `ascii-art` images
if !bytes.HasSuffix(node.Destination, []byte(".ascii-art")) {
// remove from tree, we can use RemoveFromTree, because that re-shuffles things and can
// make us output things twice.
node.SetChildren(nil)
node = nil
return
}
if !entering {
r.outs(w, "\n.fi\n.RE\n")
return
}
node.SetChildren(nil) // remove Title, if any, we can type set it.
r.outs(w, "\n.PP\n.RS\n\n.nf\n")
img, err := ioutil.ReadFile(string(node.Destination)) // markdown, doens't err, this can be an empty image, log maybe??
if err != nil {
img = []byte(err.Error())
}
escapeSpecialChars(r, w, img)
}
func (r *Renderer) mathBlock(w io.Writer, mathBlock *ast.MathBlock, entering bool) {
// may indent it?
}
func (r *Renderer) captionFigure(w io.Writer, figure *ast.CaptionFigure, entering bool) {
// check what we have here and throw away any non ascii-art figures
// CaptionFigure
// Paragraph
// Text
// Image 'url=array-vs-slice.svg'
// Text 'Array versus Slice svg'
// Text '\n'
// Image 'url=array-vs-slice.ascii-art'
// Text 'Array versus Slice ascii-art'
//
// The image with svg will be removed as child and then we continue to walk the AST.
for _, child := range figure.GetChildren() {
// for figures/images, these are wrapped below a paragraph.
// TODO: can there be more than 1 paragraph??
if p, ok := child.(*ast.Paragraph); ok {
for _, img := range p.GetChildren() {
x, ok := img.(*ast.Image)
if !ok {
continue
}
// if not ascii-art, remove entirely
if !bytes.HasSuffix(x.Destination, []byte(".ascii-art")) {
ast.RemoveFromTree(img) // this is save because we're not accessing any of the children just yet.
continue
}
img.SetChildren(nil) // remove alt text
}
}
}
}
func (r *Renderer) caption(w io.Writer, caption *ast.Caption, entering bool) {
what := ast.GetFirstChild(caption.Parent)
if !entering {
switch what.(type) {
case *ast.Table:
r.outs(w, "\n.RE\n")
case *ast.CodeBlock, *ast.Paragraph: // Paragraph is here because that wrap an image.
r.outs(w, "\n.RE\n")
case *ast.BlockQuote:
r.outs(w, "\n.RE\n")
}
return
}
// get parent, get first child for type
switch what.(type) {
case *ast.Table:
r.outs(w, "\n.RS\n")
case *ast.CodeBlock, *ast.Paragraph:
r.outs(w, "\n.RS\n")
case *ast.BlockQuote:
r.outs(w, "\n.RS\n")
r.outs(w, "\\(en ")
}
}
func (r *Renderer) blockQuote(w io.Writer, block *ast.BlockQuote, entering bool) {
if entering {
r.outs(w, "\n.PP\n.RS\n")
} else {
r.outs(w, "\n.RE\n")
}
}
func (r *Renderer) aside(w io.Writer, block *ast.Aside, entering bool) {
if entering {
r.outs(w, "\n.PP\n.RS\n")
} else {
r.outs(w, "\n.RE\n")
}
}
// RenderNode renders a markdown node to markdown.
func (r *Renderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.WalkStatus {
if r.opts.RenderNodeHook != nil {
status, didHandle := r.opts.RenderNodeHook(w, node, entering)
if didHandle {
return status
}
}
if attr := mast.AttributeFromNode(node); attr != nil && entering {
}
switch node := node.(type) {
case *ast.Document:
// do nothing
case *mast.Title:
r.title(w, node, entering)
r.Title = node // save for later.
case *mast.Authors:
r.authors(w, node, entering)
case *mast.Bibliography, *mast.BibliographyWrapper:
if entering {
r.outs(w, "\n.SH \"")
r.outs(w, strings.ToUpper(r.opts.Language.Bibliography()))
r.outs(w, "\"\n")
}
case *mast.BibliographyItem:
r.bibliographyItem(w, node, entering)
case *mast.DocumentIndex, *mast.IndexLetter, *mast.IndexItem, *mast.IndexSubItem, *mast.IndexLink:
case *mast.ReferenceBlock:
// ignore
case *ast.Footnotes:
r.footnotes(w, node, entering)
case *ast.Text:
r.text(w, node, entering)
case *ast.Softbreak:
// TODO
case *ast.Hardbreak:
r.hardBreak(w, node)
case *ast.NonBlockingSpace:
r.outs(w, "\\0")
case *ast.Callout:
r.callout(w, node, entering)
case *ast.Emph:
r.outOneOf(w, entering, "\\fI", "\\fP")
case *ast.Strong:
r.outOneOf(w, entering, "\\fB", "\\fP")
case *ast.Del:
r.outOneOf(w, entering, "~~", "~~")
case *ast.Citation:
r.citation(w, node, entering)
case *ast.DocumentMatter:
r.matter(w, node, entering)
case *ast.Heading:
r.heading(w, node, entering)
case *ast.HorizontalRule:
if entering {
r.outs(w, "\n.ti 0\n\\l'\\n(.l─'\n")
}
case *ast.Paragraph:
r.paragraph(w, node, entering)
case *ast.HTMLSpan:
r.out(w, node.Literal)
case *ast.HTMLBlock:
r.out(w, node.Literal)
case *ast.List:
r.list(w, node, entering)
case *ast.ListItem:
r.listItem(w, node, entering)
case *ast.CodeBlock:
r.codeBlock(w, node, entering)
case *ast.Caption:
r.caption(w, node, entering)
case *ast.CaptionFigure:
r.captionFigure(w, node, entering)
case *ast.Table:
r.table(w, node, entering)
case *ast.TableCell:
r.tableCell(w, node, entering)
case *ast.TableHeader:
case *ast.TableBody:
case *ast.TableFooter:
case *ast.TableRow:
r.tableRow(w, node, entering)
case *ast.BlockQuote:
r.blockQuote(w, node, entering)
case *ast.Aside:
r.aside(w, node, entering)
case *ast.CrossReference:
r.crossReference(w, node, entering)
case *ast.Index:
r.index(w, node, entering)
case *ast.Link:
r.link(w, node, entering)
case *ast.Math:
if entering {
r.out(w, node.Literal)
}
case *ast.Image:
r.image(w, node, entering)
case *ast.Code:
r.outs(w, "\\fB\\fC")
r.out(w, node.Literal)
r.outs(w, "\\fR")
case *ast.MathBlock:
r.mathBlock(w, node, entering)
case *ast.Subscript:
r.outOneOf(w, true, "\\d", "\\u")
if entering {
r.out(w, node.Literal)
}
r.outOneOf(w, false, "\\d", "\\u")
case *ast.Superscript:
r.outOneOf(w, true, "\\u", "\\d")
if entering {
r.out(w, node.Literal)
}
r.outOneOf(w, false, "\\u", "\\d")
default:
panic(fmt.Sprintf("Unknown node %T", node))
}
return ast.GoToNext
}
func (r *Renderer) callout(w io.Writer, node *ast.Callout, entering bool) {
if entering {
r.outs(w, "\\fB")
r.out(w, node.ID)
r.outs(w, "\\fP")
return
}
}
func (r *Renderer) text(w io.Writer, node *ast.Text, entering bool) {
if !entering {
return
}
text := node.Literal
parent := node.Parent
if parent != nil {
if _, ok := parent.(*ast.Heading); ok {
text = bytes.ToUpper(text)
text = append(text, byte('"'))
text = append([]byte{byte('"')}, text...)
}
}
// A hyphen must be escaped otherwise it will be an em/en-dash.
text = bytes.Replace(text, []byte("-"), []byte("\\-"), -1)
r.out(w, text)
}
func (r *Renderer) footnotes(w io.Writer, node ast.Node, entering bool) {
if !entering {
return
}
r.outs(w, "\n.SH \""+strings.ToUpper(r.opts.Language.Footnotes())+"\"\n")
}
func (r *Renderer) RenderHeader(w io.Writer, _ ast.Node) {
if r.opts.Flags&ManFragment != 0 {
return
}
r.outs(w, `.\" Generated by Mmark Markdown Processer - mmark.miek.nl`+"\n")
}
func (r *Renderer) RenderFooter(w io.Writer, node ast.Node) {}
func (r *Renderer) bibliographyItem(w io.Writer, bib *mast.BibliographyItem, entering bool) {
if !entering {
return
}
if bib.Reference == nil {
return
}
r.outs(w, ".TP\n")
r.outs(w, fmt.Sprintf("[%s]\n", bib.Anchor))
for _, author := range bib.Reference.Front.Authors {
writeNonEmptyString(w, author.Fullname)
if author.Organization != nil {
writeNonEmptyString(w, author.Organization.Value)
}
}
writeNonEmptyString(w, bib.Reference.Front.Title)
if bib.Reference.Target != "" {
r.outs(w, "\\[la]")
r.outs(w, bib.Reference.Target)
r.outs(w, "\\[ra]")
}
writeNonEmptyString(w, bib.Reference.Front.Date.Year)
r.outs(w, "\n")
}
func writeNonEmptyString(w io.Writer, s string) {
if s == "" {
return
}
io.WriteString(w, s)
io.WriteString(w, "\n")
}