-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
223 lines (202 loc) · 5.75 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
package md2roff
import (
"bytes"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"github.com/russross/blackfriday"
)
// https://github.com/russross/blackfriday/blob/v2/markdown.go
const (
PARSER_EXTENSIONS = blackfriday.NoIntraEmphasis |
blackfriday.FencedCode |
blackfriday.SpaceHeadings |
blackfriday.AutoHeadingIDs |
blackfriday.DefinitionLists
)
var (
backslash = []byte{'\\'}
enterVar = []byte("<var>")
closeVar = []byte("</var>")
tilde = []byte(`\(ti`)
htmlEscape = regexp.MustCompile(`<([A-Za-z][A-Za-z0-9_-]*)>`)
roffEscape = regexp.MustCompile(`[&\_-]`)
headingEscape = regexp.MustCompile(`["]`)
titleRe = regexp.MustCompile(`(?P<name>[A-Za-z][A-Za-z0-9_-]+)\((?P<num>\d)\) -- (?P<title>.+)`)
)
func escape(src []byte, re *regexp.Regexp) []byte {
return re.ReplaceAllFunc(src, func(c []byte) []byte {
return append(backslash, c...)
})
}
func roffText(src []byte) []byte {
return bytes.Replace(escape(src, roffEscape), []byte{'~'}, tilde, -1)
}
type RoffRenderer struct {
Manual string
Version string
Date string
Title string
Name string
Section uint8
listWasTerm bool
}
func (r *RoffRenderer) RenderHeader(buf io.Writer, ast *blackfriday.Node) {
}
func (r *RoffRenderer) RenderFooter(buf io.Writer, ast *blackfriday.Node) {
}
func (r *RoffRenderer) RenderNode(buf io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
if entering {
switch node.Type {
case blackfriday.Emph:
io.WriteString(buf, `\fI`)
case blackfriday.Strong:
io.WriteString(buf, `\fB`)
case blackfriday.Link:
io.WriteString(buf, `\[la]`)
case blackfriday.Code:
io.WriteString(buf, `\fB\fC`)
case blackfriday.Hardbreak:
io.WriteString(buf, "\n.br\n")
case blackfriday.Paragraph:
if node.Parent.Type != blackfriday.Item {
io.WriteString(buf, ".P\n")
} else if node.Parent.FirstChild != node {
io.WriteString(buf, ".sp\n")
if node.Prev.Type == blackfriday.List {
io.WriteString(buf, ".PP\n")
}
}
case blackfriday.CodeBlock:
io.WriteString(buf, ".PP\n.RS 4\n.nf\n")
case blackfriday.Item:
if node.ListFlags&blackfriday.ListTypeDefinition == 0 {
if node.Parent.ListData.Tight && node.Parent.FirstChild != node {
io.WriteString(buf, ".sp -1\n")
}
if node.Parent.ListData.Tight {
io.WriteString(buf, ".IP \\(bu 2.3\n")
} else {
io.WriteString(buf, ".IP \\(bu 4\n")
}
} else {
if node.ListFlags&blackfriday.ListTypeTerm != 0 {
io.WriteString(buf, ".PP\n")
} else {
io.WriteString(buf, ".RS 4\n")
}
}
case blackfriday.Heading:
r.renderHeading(buf, node)
return blackfriday.SkipChildren
}
}
leaf := len(node.Literal) > 0
if leaf {
if bytes.Compare(node.Literal, enterVar) == 0 {
io.WriteString(buf, `\fI`)
} else if bytes.Compare(node.Literal, closeVar) == 0 {
io.WriteString(buf, `\fP`)
} else {
buf.Write(roffText(node.Literal))
}
}
if !entering || leaf {
switch node.Type {
case blackfriday.Emph,
blackfriday.Strong:
io.WriteString(buf, `\fP`)
case blackfriday.Link:
io.WriteString(buf, `\[ra]`)
case blackfriday.Code:
io.WriteString(buf, `\fR`)
case blackfriday.CodeBlock:
io.WriteString(buf, ".fi\n.RE\n")
case blackfriday.HTMLSpan,
blackfriday.Del,
blackfriday.Image:
case blackfriday.List:
io.WriteString(buf, ".br\n")
case blackfriday.Item:
if node.ListFlags&blackfriday.ListTypeDefinition != 0 &&
node.ListFlags&blackfriday.ListTypeTerm == 0 {
io.WriteString(buf, ".RE\n")
}
default:
if !leaf {
io.WriteString(buf, "\n")
}
}
}
return blackfriday.GoToNext
}
func textContent(node *blackfriday.Node) []byte {
var buf bytes.Buffer
node.Walk(func(n *blackfriday.Node, entering bool) blackfriday.WalkStatus {
if entering && len(n.Literal) > 0 {
buf.Write(n.Literal)
}
return blackfriday.GoToNext
})
return buf.Bytes()
}
func (r *RoffRenderer) renderHeading(buf io.Writer, node *blackfriday.Node) {
text := textContent(node)
switch node.HeadingData.Level {
case 1:
var name []byte
var num []byte
if match := titleRe.FindAllSubmatch(text, 1); match != nil {
name, num, text = match[0][1], match[0][2], match[0][3]
r.Name = string(name)
if sectionNum, err := strconv.Atoi(string(num)); err == nil {
r.Section = uint8(sectionNum)
}
r.Title = string(text)
}
fmt.Fprintf(buf, ".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
escape(name, headingEscape),
num,
escape([]byte(r.Date), headingEscape),
escape([]byte(r.Version), headingEscape),
escape([]byte(r.Manual), headingEscape),
)
io.WriteString(buf, ".nh\n") // disable hyphenation
io.WriteString(buf, ".ad l\n") // disable justification
io.WriteString(buf, ".SH \"NAME\"\n")
fmt.Fprintf(buf, "%s \\- %s\n",
roffText(name),
roffText(text),
)
case 2:
fmt.Fprintf(buf, ".SH \"%s\"\n", strings.ToUpper(string(escape(text, headingEscape))))
case 3:
fmt.Fprintf(buf, ".SS \"%s\"\n", escape(text, headingEscape))
}
}
func sanitizeInput(src []byte) []byte {
return htmlEscape.ReplaceAllFunc(src, func(match []byte) []byte {
res := append(enterVar, match[1:len(match)-1]...)
return append(res, closeVar...)
})
}
type renderOption struct {
renderer blackfriday.Renderer
buffer io.Writer
}
func Opt(buffer io.Writer, renderer blackfriday.Renderer) *renderOption {
return &renderOption{renderer, buffer}
}
func Generate(src []byte, opts ...*renderOption) {
parser := blackfriday.New(blackfriday.WithExtensions(PARSER_EXTENSIONS))
ast := parser.Parse(sanitizeInput(src))
for _, opt := range opts {
opt.renderer.RenderHeader(opt.buffer, ast)
ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
return opt.renderer.RenderNode(opt.buffer, node, entering)
})
opt.renderer.RenderFooter(opt.buffer, ast)
}
}