-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnode.go
415 lines (351 loc) · 10.6 KB
/
node.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
package commonmark
/*
#include <stdlib.h>
#include <string.h>
#include "cmark.h"
*/
import "C"
import (
"unsafe"
)
//Maps to a cmark_node_type enum in cmark.h
type NodeType int
const (
//Error Status
CMARK_NODE_NONE NodeType = iota
//Block
CMARK_NODE_DOCUMENT
CMARK_NODE_BLOCK_QUOTE
CMARK_NODE_LIST
CMARK_NODE_ITEM
CMARK_NODE_CODE_BLOCK
CMARK_NODE_HTML_BLOCK
CMARK_NODE_CUSTOM_BLOCK
CMARK_NODE_PARAGRAPH
CMARK_NODE_HEADING
CMARK_NODE_THEMATIC_BREAK
//Inline
CMARK_NODE_TEXT
CMARK_NODE_SOFTBREAK
CMARK_NODE_LINEBREAK
CMARK_NODE_CODE
CMARK_NODE_HTML_INLINE
CMARK_NODE_CUSTOM_INLINE
CMARK_NODE_EMPH
CMARK_NODE_STRONG
CMARK_NODE_LINK
CMARK_NODE_IMAGE
//Block
CMARK_NODE_FIRST_BLOCK = CMARK_NODE_DOCUMENT
CMARK_NODE_LAST_BLOCK = CMARK_NODE_THEMATIC_BREAK
//Inline
CMARK_NODE_FIRST_INLINE = CMARK_NODE_TEXT
CMARK_NODE_LAST_INLINE = CMARK_NODE_IMAGE
)
//Maps to a cmark_list_type in cmark.h
type ListType int
const (
CMARK_NO_LIST ListType = iota
CMARK_BULLET_LIST
CMARK_ORDERED_LIST
)
type DelimType int
const (
CMARK_NO_DELIM = iota
CMARK_PERIOD_DELIM
CMARK_PAREN_DELIM
)
//CMark writer options for render functions
const CMARK_OPT_DEFAULT = 0
const CMARK_OPT_SOURCEPOS = 1
const CMARK_OPT_HARDBREAKS = 2
const CMARK_OPT_NORMALIZE = 4
const CMARK_OPT_SMART = 8
const CMARK_OPT_VALIDATE_UTF8 = 16
const CMARK_OPT_SAFE = 32
//converts C int return codes to True/False :)
func success(code C.int) bool {
if int(code) > 0 {
return true
} else {
return false
}
}
//Wraps the cmark_node.
//CommonMark nodes are represented as Trees in memory.
type CMarkNode struct {
node *C.struct_cmark_node
parent *CMarkNode
}
//Creates a new node of the specified type
func NewCMarkNode(nt NodeType) *CMarkNode {
n := &CMarkNode{
node: C.cmark_node_new(C.cmark_node_type(nt)),
}
return n
}
// print structure as XML
func (node *CMarkNode) RenderXML(options int) string {
result := C.cmark_render_xml(node.node, C.int(options))
defer C.free(unsafe.Pointer(result))
return C.GoString(result)
}
// Renders the document as HTML.
// Returns an HTML string.
func (node *CMarkNode) RenderHtml(options int) string {
result := C.cmark_render_html(node.node, C.int(options))
defer C.free(unsafe.Pointer(result))
return C.GoString(result)
}
// Renders the document as a groff man page,
// without the header
func (node *CMarkNode) RenderMan(options int, width int) string {
result := C.cmark_render_man(node.node, C.int(options), C.int(width))
defer C.free(unsafe.Pointer(result))
return C.GoString(result)
}
// Renders node tree as commonmark text.
func (node *CMarkNode) RenderCMark(options int, width int) string {
result := C.cmark_render_commonmark(node.node, C.int(options), C.int(width))
defer C.free(unsafe.Pointer(result))
return C.GoString(result)
}
// Renders node tree as a LaTeX document
func (node *CMarkNode) RenderLatex(options int, width int) string {
result := C.cmark_render_latex(node.node, C.int(options), C.int(width))
defer C.free(unsafe.Pointer(result))
return C.GoString(result)
}
// Cleanup a node, including any children.
// Unlinks a node from the tree and frees it.
func (node *CMarkNode) Free() {
if node.node != nil {
C.cmark_node_free(node.node)
}
node.node = nil
}
//Node traversal functions
//Get next node
func (node *CMarkNode) Next() *CMarkNode {
return &CMarkNode{
node: C.cmark_node_next(node.node),
}
}
//Get previous node
func (node *CMarkNode) Previous() *CMarkNode {
return &CMarkNode{
node: C.cmark_node_previous(node.node),
}
}
//Get parent node
func (node *CMarkNode) Parent() *CMarkNode {
return &CMarkNode{
node: C.cmark_node_parent(node.node),
}
}
//Get first child node
func (node *CMarkNode) FirstChild() *CMarkNode {
return &CMarkNode{
node: C.cmark_node_first_child(node.node),
}
}
//Get last child node
func (node *CMarkNode) LastChild() *CMarkNode {
return &CMarkNode{
node: C.cmark_node_last_child(node.node),
}
}
//Accessor functions
//Sets arbitrary user data for node
func (node *CMarkNode) SetNodeUserData(userData string) bool {
cstr := C.CString(userData)
res := C.cmark_node_set_user_data(node.node, unsafe.Pointer(&cstr))
return success(res)
}
//Returns the user data of the node as an
//unsafe.Pointer. Hope you know what you're doing.
func (node *CMarkNode) GetNodeUserData() string {
data := C.cmark_node_get_user_data(node.node)
return C.GoString((*C.char)(*(*unsafe.Pointer)(data)))
}
//Get the node type
func (node *CMarkNode) GetNodeType() NodeType {
nt := C.cmark_node_get_type(node.node)
return NodeType(nt)
}
//Get the node type as a string
func (node *CMarkNode) GetNodeTypeString() string {
cstr := C.cmark_node_get_type_string(node.node)
return C.GoString(cstr)
}
//Get the node's string content
func (node *CMarkNode) GetLiteral() string {
cstr := C.cmark_node_get_literal(node.node)
return C.GoString(cstr)
}
//Set the node's string content
func (node *CMarkNode) SetLiteral(content string) bool {
cstr := C.CString(content)
defer C.free(unsafe.Pointer(cstr))
return success(C.cmark_node_set_literal(node.node, cstr))
}
//Get a Header node's level
func (node *CMarkNode) GetHeaderLevel() int {
level := C.cmark_node_get_header_level(node.node)
return int(level)
}
//Set a Header node's level (1,2, etc.)
func (node *CMarkNode) SetHeaderLevel(level int) bool {
return success(C.cmark_node_set_header_level(node.node, C.int(level)))
}
//Get a List node's list type
func (node *CMarkNode) GetListType() ListType {
lt := C.cmark_node_get_list_type(node.node)
return ListType(lt)
}
//Set a List node's list type
func (node *CMarkNode) SetListType(lt ListType) bool {
return success(C.cmark_node_set_list_type(node.node, C.cmark_list_type(lt)))
}
//Returns the list delimiter type of node, or CMARK_NO_DELIM if node is not a list
func (node *CMarkNode) GetListDelim() DelimType {
dt := C.cmark_node_get_list_delim(node.node)
return DelimType(dt)
}
//Sets the list delimeter type of the node, returns true on success
func (node *CMarkNode) SetListDelim(dt DelimType) bool {
return success(C.cmark_node_set_list_delim(node.node, C.cmark_delim_type(dt)))
}
//Get a list's start
func (node *CMarkNode) GetListStart() int {
ls := C.cmark_node_get_list_start(node.node)
return int(ls)
}
//Set a list's start
func (node *CMarkNode) SetListStart(start int) bool {
return success(C.cmark_node_set_list_start(node.node, C.int(start)))
}
//Get list 'tight'
func (node *CMarkNode) GetListTight() bool {
return success(C.cmark_node_get_list_tight(node.node))
}
//Set list 'tight'
func (node *CMarkNode) SetListTight(isTight bool) bool {
ti := 0
if isTight == true {
ti = 1
}
return success(C.cmark_node_set_list_tight(node.node, C.int(ti)))
}
//Get Fence info
func (node *CMarkNode) GetFenceInfo() string {
cstr := C.cmark_node_get_fence_info(node.node)
return C.GoString(cstr)
}
//Set Fence info
func (node *CMarkNode) SetFenceInfo(fenceInfo string) bool {
cstr := C.CString(fenceInfo)
defer C.free(unsafe.Pointer(cstr))
return success(C.cmark_node_set_fence_info(node.node, cstr))
}
//Get a node's url
func (node *CMarkNode) GetUrl() string {
cstr := C.cmark_node_get_url(node.node)
return C.GoString(cstr)
}
//Set a node's url
func (node *CMarkNode) SetUrl(url string) bool {
cstr := C.CString(url)
defer C.free(unsafe.Pointer(cstr))
return success(C.cmark_node_set_url(node.node, cstr))
}
//Set a node's title
func (node *CMarkNode) SetTitle(title string) bool {
cstr := C.CString(title)
defer C.free(unsafe.Pointer(cstr))
return success(C.cmark_node_set_title(node.node, cstr))
}
//Get a node's title
func (node *CMarkNode) GetTitle() string {
cstr := C.cmark_node_get_title(node.node)
return C.GoString(cstr)
}
//Returns the literal "on enter" text for a custom node, or an empty
//string if no on_enter is set
func (node *CMarkNode) GetOnEnter() string {
cstr := C.cmark_node_get_on_enter(node.node)
return C.GoString(cstr)
}
//Sets the literal text to render "on enter" for a custom node.
//Any children of the node will be rendered after this text.
//Returns true on success and false on failure
func (node *CMarkNode) SetOnEnter(onEnter string) bool {
cstr := C.CString(onEnter)
defer C.free(unsafe.Pointer(cstr))
return success(C.cmark_node_set_on_enter(node.node, cstr))
}
//Returns the literal "on exit" text for a custom node, or an empty
//string if no on_exit is set
func (node *CMarkNode) GetOnExit() string {
cstr := C.cmark_node_get_on_exit(node.node)
return C.GoString(cstr)
}
//Sets the literal text to render "on exit" for a custom node.
//Any children of the node will be rendered before this text.
//Returns true on success and false on failure
func (node *CMarkNode) SetOnExit(onExit string) bool {
cstr := C.CString(onExit)
defer C.free(unsafe.Pointer(cstr))
return success(C.cmark_node_set_on_exit(node.node, cstr))
}
//Returns the line on which 'node' begins
func (node *CMarkNode) GetStartLine() int {
return int(C.cmark_node_get_start_line(node.node))
}
//Returns the column at which 'node' begins
func (node *CMarkNode) GetStartColumn() int {
return int(C.cmark_node_get_start_column(node.node))
}
//Returns the line on which 'node' ends
func (node *CMarkNode) GetEndLine() int {
return int(C.cmark_node_get_end_line(node.node))
}
//Returns the column at which 'node' ends
func (node *CMarkNode) GetEndColumn() int {
return int(C.cmark_node_get_end_column(node.node))
}
// Tree manipulation functions
//Unlink a node from the tree
func (node *CMarkNode) Unlink() {
C.cmark_node_unlink(node.node)
}
// InsertBefore can cause a panic quite readily :)
// Hint: Both nodes had better already be in the 'tree'
// Insert a node before another 'sibling' node
func (node *CMarkNode) InsertBefore(sibling *CMarkNode) bool {
return success(C.cmark_node_insert_before(node.node, sibling.node))
}
// InsertAfter can cause a panic quite readily :)
// Hint: Both nodes had better already be in the 'tree'
//Insert a node after another 'sibling' node
func (node *CMarkNode) InsertAfter(sibling *CMarkNode) bool {
return success(C.cmark_node_insert_after(node.node, sibling.node))
}
// Replaces 'oldNode' with 'newNode' and unlinks 'oldnode' (but does
// not free its memory).
// Returns true on success, false on failure.
func (newNode *CMarkNode) Replace(oldNode *CMarkNode) bool {
return success(C.cmark_node_replace(oldNode.node, newNode.node))
}
//Prepend a child node
func (node *CMarkNode) PrependChild(child *CMarkNode) bool {
return success(C.cmark_node_prepend_child(node.node, child.node))
}
//Append a child node
func (node *CMarkNode) AppendChild(child *CMarkNode) bool {
return success(C.cmark_node_append_child(node.node, child.node))
}
//Consolidates adjacent text nodes.
func (node *CMarkNode) ConsolidateTextNodes() {
C.cmark_consolidate_text_nodes(node.node)
}