-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
confluence.go
347 lines (318 loc) · 6.91 KB
/
confluence.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
package confluence
import (
"bytes"
"io"
bf "github.com/russross/blackfriday/v2"
)
// Renderer is the rendering interface for confluence wiki output.
type Renderer struct {
w bytes.Buffer
// Flags allow customizing this renderer's behavior.
Flags Flag
lastOutputLen int
}
// Flag control optional behavior of this renderer.
type Flag int
const (
// FlagsNone does not allow customizing this renderer's behavior.
FlagsNone Flag = 0
// InformationMacros allow using info, tip, note, and warning macros.
InformationMacros Flag = 1 << iota
// IgnoreMacroEscaping will not escape any text that contains starts with `{`
// in a block of text.
IgnoreMacroEscaping
)
var (
quoteTag = []byte("{quote}")
codeTag = []byte("code")
infoTag = []byte("info")
noteTag = []byte("note")
tipTag = []byte("tip")
warningTag = []byte("warning")
imageTag = []byte("!")
strongTag = []byte("*")
strikethroughTag = []byte("-")
emTag = []byte("_")
linkTag = []byte("[")
linkCloseTag = []byte("]")
liTag = []byte("*")
olTag = []byte("#")
hrTag = []byte("----")
tableTag = []byte("|")
h1Tag = []byte("h1.")
h2Tag = []byte("h2.")
h3Tag = []byte("h3.")
h4Tag = []byte("h4.")
h5Tag = []byte("h5.")
h6Tag = []byte("h6.")
)
var (
nlBytes = []byte{'\n'}
spaceBytes = []byte{' '}
)
var itemLevel = 0
var confluenceEscaper = [256][]byte{
'*': []byte(`\*`),
'_': []byte(`\_`),
'-': []byte(`\-`),
'+': []byte(`\+`),
'^': []byte(`\^`),
'~': []byte(`\~`),
'{': []byte(`\{`),
'!': []byte(`\!`),
'[': []byte(`\[`),
']': []byte(`\]`),
'(': []byte(`\(`),
')': []byte(`\)`),
}
func (r *Renderer) esc(w io.Writer, text []byte) {
var start, end int
for end < len(text) {
if escSeq := confluenceEscaper[text[end]]; escSeq != nil {
// do not escape blocks that start with `{` if request.
// this will allow people to declare macros in `wiki` format.
if r.Flags&IgnoreMacroEscaping != 0 && text[end] == '{' {
escSeq = escSeq[1:]
}
w.Write(text[start:end])
w.Write(escSeq)
start = end + 1
}
end++
}
if start < len(text) && end <= len(text) {
w.Write(text[start:end])
}
}
func (r *Renderer) cr(w io.Writer) {
if r.lastOutputLen > 0 {
r.out(w, nlBytes)
}
}
func (r *Renderer) out(w io.Writer, text []byte) {
w.Write(text)
r.lastOutputLen = len(text)
}
func headingTagFromLevel(level int) []byte {
switch level {
case 1:
return h1Tag
case 2:
return h2Tag
case 3:
return h3Tag
case 4:
return h4Tag
case 5:
return h5Tag
default:
return h6Tag
}
}
// RenderNode is a confluence renderer of a single node of a syntax tree.
func (r *Renderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus {
switch node.Type {
case bf.Text:
r.esc(w, node.Literal)
case bf.Softbreak:
break
case bf.Hardbreak:
w.Write(nlBytes)
case bf.BlockQuote:
if entering {
r.out(w, quoteTag)
r.cr(w)
} else {
r.out(w, quoteTag)
r.cr(w)
r.cr(w)
}
case bf.CodeBlock:
r.out(w, []byte("{"))
if len(node.Info) > 0 {
if r.Flags&InformationMacros != 0 {
language := string(node.Info)
switch language {
case "info":
r.out(w, infoTag)
case "tip":
r.out(w, tipTag)
case "note":
r.out(w, noteTag)
case "warning":
r.out(w, warningTag)
default:
r.out(w, []byte(codeTag))
r.out(w, []byte(":language="))
r.out(w, node.Info)
}
r.out(w, []byte("}"))
r.cr(w)
w.Write(node.Literal)
r.out(w, []byte("{"))
switch language {
case "info":
r.out(w, infoTag)
case "tip":
r.out(w, tipTag)
case "note":
r.out(w, noteTag)
case "warning":
r.out(w, warningTag)
default:
r.out(w, []byte(codeTag))
}
} else {
r.out(w, []byte(codeTag))
r.out(w, []byte(":language="))
r.out(w, node.Info)
r.out(w, []byte("}"))
r.cr(w)
w.Write(node.Literal)
r.out(w, []byte("{"))
r.out(w, []byte(codeTag))
}
} else {
r.out(w, codeTag)
r.out(w, []byte("}"))
r.cr(w)
w.Write(node.Literal)
r.out(w, []byte("{"))
r.out(w, codeTag)
}
r.out(w, []byte("}"))
r.cr(w)
r.cr(w)
case bf.Code:
r.out(w, []byte("{{"))
r.esc(w, node.Literal)
r.out(w, []byte("}}"))
case bf.Emph:
r.out(w, emTag)
case bf.Heading:
headingTag := headingTagFromLevel(node.Level)
if entering {
r.out(w, headingTag)
w.Write(spaceBytes)
} else {
r.cr(w)
}
case bf.Image:
if entering {
dest := node.LinkData.Destination
r.out(w, imageTag)
r.out(w, dest)
} else {
r.out(w, imageTag)
}
case bf.Item:
if entering {
itemTag := liTag
if node.ListFlags&bf.ListTypeOrdered != 0 {
itemTag = olTag
}
for i := 0; i < itemLevel; i++ {
r.out(w, itemTag)
}
w.Write(spaceBytes)
}
case bf.Link:
if entering {
r.out(w, linkTag)
} else {
if dest := node.LinkData.Destination; dest != nil {
r.out(w, []byte("|"))
r.out(w, dest)
}
r.out(w, linkCloseTag)
}
case bf.HorizontalRule:
r.cr(w)
r.out(w, hrTag)
r.cr(w)
case bf.List:
if entering {
itemLevel++
} else {
itemLevel--
if itemLevel == 0 {
r.cr(w)
}
}
case bf.Document:
break
case bf.HTMLBlock:
break
case bf.HTMLSpan:
break
case bf.Paragraph:
if !entering {
if node.Next != nil && node.Next.Type == bf.Paragraph {
w.Write(nlBytes)
w.Write(nlBytes)
} else {
if node.Parent.Type != bf.Item {
r.cr(w)
}
r.cr(w)
}
}
case bf.Strong:
r.out(w, strongTag)
case bf.Del:
r.out(w, strikethroughTag)
case bf.Table:
if !entering {
r.cr(w)
}
case bf.TableCell:
if node.IsHeader {
r.out(w, tableTag)
} else {
if entering {
r.out(w, tableTag)
}
}
case bf.TableHead:
break
case bf.TableBody:
break
case bf.TableRow:
if node.Parent.Type == bf.TableHead {
r.out(w, tableTag)
if !entering {
r.cr(w)
}
} else if node.Parent.Type == bf.TableBody {
if !entering {
r.out(w, tableTag)
r.cr(w)
}
}
default:
panic("Unknown node type " + node.Type.String())
}
return bf.GoToNext
}
// Render prints out the whole document from the ast.
func (r *Renderer) Render(ast *bf.Node) []byte {
ast.Walk(func(node *bf.Node, entering bool) bf.WalkStatus {
return r.RenderNode(&r.w, node, entering)
})
return r.w.Bytes()
}
// RenderHeader writes document header (unused).
func (r *Renderer) RenderHeader(w io.Writer, ast *bf.Node) {
}
// RenderFooter writes document footer (unused).
func (r *Renderer) RenderFooter(w io.Writer, ast *bf.Node) {
}
// Run prints out the confluence document.
func Run(input []byte, opts ...bf.Option) []byte {
r := &Renderer{Flags: InformationMacros}
optList := []bf.Option{bf.WithRenderer(r), bf.WithExtensions(bf.CommonExtensions)}
optList = append(optList, opts...)
parser := bf.New(optList...)
ast := parser.Parse([]byte(input))
return r.Render(ast)
}