forked from shurcooL/markdownfmt
/
main.go
476 lines (442 loc) · 12.2 KB
/
main.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
// Package markdown provides a Markdown renderer.
package markdown
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"strings"
"github.com/mattn/go-runewidth"
"github.com/russross/blackfriday"
"github.com/shurcooL/go/indentwriter"
)
type markdownRenderer struct {
normalTextMarker map[*bytes.Buffer]int
orderedListCounter map[int]int
paragraph map[int]bool // Used to keep track of whether a given list item uses a paragraph for large spacing.
listDepth int
lastNormalText string
// TODO: Clean these up.
headers []string
columnAligns []int
columnWidths []int
cells []string
}
func formatCode(lang string, text []byte) (formattedCode []byte, ok bool) {
switch lang {
case "Go", "go":
gofmt, err := format.Source(text)
if err != nil {
return nil, false
}
return gofmt, true
default:
return nil, false
}
}
// Block-level callbacks.
func (_ *markdownRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) {
doubleSpace(out)
// parse out the language name
count := 0
for _, elt := range strings.Fields(lang) {
if elt[0] == '.' {
elt = elt[1:]
}
if len(elt) == 0 {
continue
}
out.WriteString("```")
out.WriteString(elt)
count++
break
}
if count == 0 {
out.WriteString("```")
}
out.WriteString("\n")
if formattedCode, ok := formatCode(lang, text); ok {
out.Write(formattedCode)
} else {
out.Write(text)
}
out.WriteString("```\n")
}
func (_ *markdownRenderer) BlockQuote(out *bytes.Buffer, text []byte) {
doubleSpace(out)
lines := bytes.Split(text, []byte("\n"))
for i, line := range lines {
if i == len(lines)-1 {
continue
}
out.WriteString(">")
if len(line) != 0 {
out.WriteString(" ")
out.Write(line)
}
out.WriteString("\n")
}
}
func (_ *markdownRenderer) BlockHtml(out *bytes.Buffer, text []byte) {
doubleSpace(out)
out.Write(text)
out.WriteByte('\n')
}
func (_ *markdownRenderer) TitleBlock(out *bytes.Buffer, text []byte) {
}
func (_ *markdownRenderer) Header(out *bytes.Buffer, text func() bool, level int, id string) {
marker := out.Len()
doubleSpace(out)
if level >= 3 {
fmt.Fprint(out, strings.Repeat("#", level), " ")
}
textMarker := out.Len()
if !text() {
out.Truncate(marker)
return
}
switch level {
case 1:
len := runewidth.StringWidth(out.String()[textMarker:])
fmt.Fprint(out, "\n", strings.Repeat("=", len))
case 2:
len := runewidth.StringWidth(out.String()[textMarker:])
fmt.Fprint(out, "\n", strings.Repeat("-", len))
}
out.WriteString("\n")
}
func (_ *markdownRenderer) HRule(out *bytes.Buffer) {
doubleSpace(out)
out.WriteString("---\n")
}
func (mr *markdownRenderer) List(out *bytes.Buffer, text func() bool, flags int) {
marker := out.Len()
doubleSpace(out)
mr.listDepth++
defer func() { mr.listDepth-- }()
if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
mr.orderedListCounter[mr.listDepth] = 1
}
if !text() {
out.Truncate(marker)
return
}
}
func (mr *markdownRenderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
/*if flags&blackfriday.LIST_ITEM_CONTAINS_BLOCK != 0 {
doubleSpace(out)
}*/
//text = bytes.Replace(text, []byte("\n"), append([]byte("\n"), bytes.Repeat([]byte("\t"), 1)...), -1)
if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
fmt.Fprintf(out, "%d.", mr.orderedListCounter[mr.listDepth])
indentwriter.New(out, 1).Write(text)
mr.orderedListCounter[mr.listDepth]++
} else if flags&blackfriday.LIST_TYPE_TERM != 0 {
out.WriteString("\n")
indentwriter.New(out, 0).Write(text)
} else if flags&blackfriday.LIST_TYPE_DEFINITION != 0 {
out.WriteString(":")
indentwriter.New(out, 1).Write(text)
} else {
out.WriteString("-")
indentwriter.New(out, 1).Write(text)
}
out.WriteString("\n")
if mr.paragraph[mr.listDepth] {
if flags&blackfriday.LIST_ITEM_END_OF_LIST == 0 {
out.WriteString("\n")
}
mr.paragraph[mr.listDepth] = false
}
}
func (mr *markdownRenderer) Paragraph(out *bytes.Buffer, text func() bool) {
marker := out.Len()
doubleSpace(out)
mr.paragraph[mr.listDepth] = true
if !text() {
out.Truncate(marker)
return
}
out.WriteString("\n")
}
func (mr *markdownRenderer) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
doubleSpace(out)
/*out.WriteString(goon.SdumpExpr(mr.headers))
out.WriteString(goon.SdumpExpr(mr.columnAligns))
out.WriteString(goon.SdumpExpr(mr.columnWidths))
out.WriteString(goon.SdumpExpr(mr.cells))*/
for column, cell := range mr.headers {
out.WriteByte('|')
out.WriteByte(' ')
out.WriteString(cell)
for i := runewidth.StringWidth(string(cell)); i < mr.columnWidths[column]; i++ {
out.WriteByte(' ')
}
out.WriteByte(' ')
}
out.WriteString("|\n")
for column, width := range mr.columnWidths {
out.WriteByte('|')
if mr.columnAligns[column]&blackfriday.TABLE_ALIGNMENT_LEFT != 0 {
out.WriteByte(':')
} else {
out.WriteByte('-')
}
for ; width > 0; width-- {
out.WriteByte('-')
}
if mr.columnAligns[column]&blackfriday.TABLE_ALIGNMENT_RIGHT != 0 {
out.WriteByte(':')
} else {
out.WriteByte('-')
}
}
out.WriteString("|\n")
for i := 0; i < len(mr.cells); {
for column := range mr.headers {
cell := []byte(mr.cells[i])
i++
out.WriteByte('|')
out.WriteByte(' ')
switch mr.columnAligns[column] {
default:
fallthrough
case blackfriday.TABLE_ALIGNMENT_LEFT:
out.Write(cell)
for i := runewidth.StringWidth(string(cell)); i < mr.columnWidths[column]; i++ {
out.WriteByte(' ')
}
case blackfriday.TABLE_ALIGNMENT_CENTER:
spaces := mr.columnWidths[column] - runewidth.StringWidth(string(cell))
for i := 0; i < spaces/2; i++ {
out.WriteByte(' ')
}
out.Write(cell)
for i := 0; i < spaces-(spaces/2); i++ {
out.WriteByte(' ')
}
case blackfriday.TABLE_ALIGNMENT_RIGHT:
for i := runewidth.StringWidth(string(cell)); i < mr.columnWidths[column]; i++ {
out.WriteByte(' ')
}
out.Write(cell)
}
out.WriteByte(' ')
}
out.WriteString("|\n")
}
mr.headers = nil
mr.columnAligns = nil
mr.columnWidths = nil
mr.cells = nil
}
func (_ *markdownRenderer) TableRow(out *bytes.Buffer, text []byte) {
}
func (mr *markdownRenderer) TableHeaderCell(out *bytes.Buffer, text []byte, align int) {
mr.columnAligns = append(mr.columnAligns, align)
columnWidth := runewidth.StringWidth(string(text))
mr.columnWidths = append(mr.columnWidths, columnWidth)
mr.headers = append(mr.headers, string(text))
}
func (mr *markdownRenderer) TableCell(out *bytes.Buffer, text []byte, align int) {
columnWidth := runewidth.StringWidth(string(text))
column := len(mr.cells) % len(mr.headers)
if columnWidth > mr.columnWidths[column] {
mr.columnWidths[column] = columnWidth
}
mr.cells = append(mr.cells, string(text))
}
func (_ *markdownRenderer) Footnotes(out *bytes.Buffer, text func() bool) {
out.WriteString("<Footnotes: Not implemented.>") // TODO
}
func (_ *markdownRenderer) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
out.WriteString("<FootnoteItem: Not implemented.>") // TODO
}
// Span-level callbacks.
func (_ *markdownRenderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {
out.Write(link)
}
func (_ *markdownRenderer) CodeSpan(out *bytes.Buffer, text []byte) {
out.WriteByte('`')
out.Write(text)
out.WriteByte('`')
}
func (_ *markdownRenderer) DoubleEmphasis(out *bytes.Buffer, text []byte) {
out.WriteString("**")
out.Write(text)
out.WriteString("**")
}
func (_ *markdownRenderer) Emphasis(out *bytes.Buffer, text []byte) {
if len(text) == 0 {
return
}
out.WriteByte('*')
out.Write(text)
out.WriteByte('*')
}
func (_ *markdownRenderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
out.WriteString("![")
out.Write(alt)
out.WriteString("](")
out.Write(link)
out.WriteString(")")
}
func (_ *markdownRenderer) LineBreak(out *bytes.Buffer) {
out.WriteString(" \n")
}
func (_ *markdownRenderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
out.WriteString("[")
out.Write(content)
out.WriteString("](")
out.Write(link)
out.WriteString(")")
}
func (_ *markdownRenderer) RawHtmlTag(out *bytes.Buffer, tag []byte) {
out.Write(tag)
}
func (_ *markdownRenderer) TripleEmphasis(out *bytes.Buffer, text []byte) {
out.WriteString("***")
out.Write(text)
out.WriteString("***")
}
func (_ *markdownRenderer) StrikeThrough(out *bytes.Buffer, text []byte) {
out.WriteString("~~")
out.Write(text)
out.WriteString("~~")
}
func (_ *markdownRenderer) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
out.WriteString("<FootnoteRef: Not implemented.>") // TODO
}
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
}
}
// Low-level callbacks
func (_ *markdownRenderer) Entity(out *bytes.Buffer, entity []byte) {
out.Write(entity)
}
func (mr *markdownRenderer) NormalText(out *bytes.Buffer, text []byte) {
normalText := string(text)
if needsEscaping(text, mr.lastNormalText) {
text = append([]byte("\\"), text...)
}
mr.lastNormalText = normalText
if mr.listDepth > 0 && string(text) == "\n" { // TODO: See if this can be cleaned up... It's needed for lists.
return
}
cleanString := cleanWithoutTrim(string(text))
if cleanString == "" {
return
}
if mr.skipSpaceIfNeededNormalText(out, cleanString) { // Skip first space if last character is already a space (i.e., no need for a 2nd space in a row).
cleanString = cleanString[1:]
}
out.WriteString(cleanString)
if len(cleanString) >= 1 && cleanString[len(cleanString)-1] == ' ' { // If it ends with a space, make note of that.
mr.normalTextMarker[out] = out.Len()
}
}
// Header and footer.
func (_ *markdownRenderer) DocumentHeader(out *bytes.Buffer) {}
func (_ *markdownRenderer) DocumentFooter(out *bytes.Buffer) {}
func (_ *markdownRenderer) GetFlags() int { return 0 }
func (mr *markdownRenderer) skipSpaceIfNeededNormalText(out *bytes.Buffer, cleanString string) bool {
if cleanString[0] != ' ' {
return false
}
if _, ok := mr.normalTextMarker[out]; !ok {
mr.normalTextMarker[out] = -1
}
return mr.normalTextMarker[out] == out.Len()
}
// 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 doubleSpace(out *bytes.Buffer) {
if out.Len() > 0 {
out.WriteByte('\n')
}
}
// NewRenderer returns a Markdown renderer.
func NewRenderer() blackfriday.Renderer {
return &markdownRenderer{
normalTextMarker: make(map[*bytes.Buffer]int),
orderedListCounter: make(map[int]int),
paragraph: make(map[int]bool),
}
}
// Options specifies options for formatting.
type Options struct {
// Currently none.
}
// Process formats Markdown.
// If opt is nil the defaults are used.
// Error can only occur when reading input from filename rather than src.
func Process(filename string, src []byte, opt *Options) ([]byte, error) {
// Get source.
text, err := readSource(filename, src)
if err != nil {
return nil, err
}
// GitHub Flavored Markdown-like extensions.
extensions := 0
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
extensions |= blackfriday.EXTENSION_TABLES
extensions |= blackfriday.EXTENSION_FENCED_CODE
extensions |= blackfriday.EXTENSION_AUTOLINK
extensions |= blackfriday.EXTENSION_STRIKETHROUGH
extensions |= blackfriday.EXTENSION_SPACE_HEADERS
extensions |= blackfriday.EXTENSION_DEFINITION_LISTS
//extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
output := blackfriday.Markdown(text, NewRenderer(), extensions)
return output, nil
}
// If src != nil, readSource returns src.
// If src == nil, readSource returns the result of reading the file specified by filename.
func readSource(filename string, src []byte) ([]byte, error) {
if src != nil {
return src, nil
}
return ioutil.ReadFile(filename)
}