/
renderer.go
228 lines (183 loc) · 5.61 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
package main
import (
"bytes"
"fmt"
"github.com/russross/blackfriday"
"github.com/samfoo/ansi"
"html"
"regexp"
"strings"
)
// Index corresponds to the heading level (e.g. h1, h2, h3...)
var headerStyles = [...]string{
ansi.ColorCode("yellow+bhu"),
ansi.ColorCode("yellow+bh"),
ansi.ColorCode("yellow"),
ansi.ColorCode("yellow"),
}
var emphasisStyles = [...]string{
ansi.ColorCode("cyan+bh"),
ansi.ColorCode("cyan+bhu"),
ansi.ColorCode("cyan+bhi"),
}
var linkStyle = ansi.ColorCode("015+u")
const (
UNORDERED = 1 << iota
ORDERED
)
type list struct {
kind int
index int
}
type Console struct {
lists []*list
}
func (options *Console) BlockCode(out *bytes.Buffer, text []byte, lang string) {
s := string(text)
reg, _ := regexp.Compile("\\n")
out.WriteString("\n ")
out.WriteString(ansi.ColorCode("015"))
out.WriteString(reg.ReplaceAllString(s, "\n "))
out.WriteString(ansi.ColorCode("reset"))
out.WriteString("\n")
}
func (options *Console) BlockQuote(out *bytes.Buffer, text []byte) {
s := strings.TrimSpace(string(text))
reg, _ := regexp.Compile("\\n")
out.WriteString("\n | ")
out.WriteString(reg.ReplaceAllString(s, "\n | "))
out.WriteString("\n\n")
}
func (options *Console) BlockHtml(out *bytes.Buffer, text []byte) {
out.Write(text)
}
func (options *Console) Header(out *bytes.Buffer, text func() bool, level int, id string) {
out.WriteString("\n")
out.WriteString(headerStyles[level-1])
marker := out.Len()
if !text() {
out.Truncate(marker)
return
}
out.WriteString(ansi.ColorCode("reset"))
out.WriteString("\n\n")
}
func (options *Console) HRule(out *bytes.Buffer) {
out.WriteString("\n\u2015\u2015\u2015\u2015\u2015\n\n")
}
func (options *Console) List(out *bytes.Buffer, text func() bool, flags int) {
out.WriteString("\n")
kind := UNORDERED
if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
kind = ORDERED
}
options.lists = append(options.lists, &list{kind, 1})
text()
options.lists = options.lists[:len(options.lists)-1]
out.WriteString("\n")
}
func (options *Console) ListItem(out *bytes.Buffer, text []byte, flags int) {
current := options.lists[len(options.lists)-1]
for i := 0; i < len(options.lists); i++ {
out.WriteString(" ")
}
if current.kind == ORDERED {
out.WriteString(fmt.Sprintf("%d. ", current.index))
current.index += 1
} else {
out.WriteString(ansi.ColorCode("red+bh"))
out.WriteString("* ")
out.WriteString(ansi.ColorCode("reset"))
}
out.Write(text)
out.WriteString("\n\n")
}
func (options *Console) Paragraph(out *bytes.Buffer, text func() bool) {
marker := out.Len()
if !text() {
out.Truncate(marker)
return
}
out.WriteString("\n\n")
}
func (options *Console) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {}
func (options *Console) TableRow(out *bytes.Buffer, text []byte) {}
func (options *Console) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {}
func (options *Console) TableCell(out *bytes.Buffer, text []byte, flags int) {}
func (options *Console) Footnotes(out *bytes.Buffer, text func() bool) {}
func (options *Console) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {}
func (options *Console) TitleBlock(out *bytes.Buffer, text []byte) {
out.WriteString("\n")
out.WriteString(headerStyles[0])
out.Write(text)
out.WriteString(ansi.ColorCode("reset"))
out.WriteString("\n\n")
}
func (options *Console) AutoLink(out *bytes.Buffer, link []byte, kind int) {
out.WriteString(linkStyle)
out.Write(link)
out.WriteString(ansi.ColorCode("reset"))
}
func (options *Console) CodeSpan(out *bytes.Buffer, text []byte) {
out.WriteString(ansi.ColorCode("015+b"))
out.Write(text)
out.WriteString(ansi.ColorCode("reset"))
}
func (options *Console) DoubleEmphasis(out *bytes.Buffer, text []byte) {
out.WriteString(emphasisStyles[1])
out.Write(text)
out.WriteString(ansi.ColorCode("reset"))
}
func (options *Console) Emphasis(out *bytes.Buffer, text []byte) {
out.WriteString(emphasisStyles[0])
out.Write(text)
out.WriteString(ansi.ColorCode("reset"))
}
func (options *Console) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
out.WriteString(" [ image ] ")
}
func (options *Console) LineBreak(out *bytes.Buffer) {
out.WriteString("\n")
}
func (options *Console) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
out.Write(content)
out.WriteString(" (")
out.WriteString(linkStyle)
out.Write(link)
out.WriteString(ansi.ColorCode("reset"))
out.WriteString(")")
}
func (options *Console) RawHtmlTag(out *bytes.Buffer, tag []byte) {
out.WriteString(ansi.ColorCode("magenta"))
out.Write(tag)
out.WriteString(ansi.ColorCode("reset"))
}
func (options *Console) TripleEmphasis(out *bytes.Buffer, text []byte) {
out.WriteString(emphasisStyles[2])
out.Write(text)
out.WriteString(ansi.ColorCode("reset"))
}
func (options *Console) StrikeThrough(out *bytes.Buffer, text []byte) {
out.WriteString(ansi.ColorCode("008+s"))
out.WriteString("\u2015")
out.Write(text)
out.WriteString("\u2015")
out.WriteString(ansi.ColorCode("reset"))
}
func (options *Console) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
}
func (options *Console) Entity(out *bytes.Buffer, entity []byte) {
out.WriteString(html.UnescapeString(string(entity)))
}
func (options *Console) NormalText(out *bytes.Buffer, text []byte) {
s := string(text)
reg, _ := regexp.Compile("\\s+")
out.WriteString(reg.ReplaceAllString(s, " "))
}
func (options *Console) DocumentHeader(out *bytes.Buffer) {
}
func (options *Console) DocumentFooter(out *bytes.Buffer) {
}
func (options *Console) GetFlags() int {
return 0
}