-
Notifications
You must be signed in to change notification settings - Fork 54
/
docuowl_extensions.go
240 lines (205 loc) · 5.35 KB
/
docuowl_extensions.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
package markdown
import (
"fmt"
"strings"
"github.com/heyvito/docuowl/markdown/ast"
"github.com/heyvito/docuowl/markdown/html"
"github.com/heyvito/docuowl/markdown/parser"
)
func convertToAST(input []string) (ast.Node, error) {
buf := []byte(strings.Join(input, "\n"))
node := Parse(buf, parser.New())
if _, ok := node.(*ast.Document); !ok {
return nil, fmt.Errorf("parser did not return a Document object")
}
return node, nil
}
func convertToHTML(node ast.Node) string {
renderer := html.NewRenderer(html.RendererOptions{
Flags: html.CommonFlags,
})
return string(Render(node, renderer))
}
func isList(node ast.Node) bool {
_, ok := node.(*ast.List)
return ok
}
func isParagraph(node ast.Node) bool {
_, ok := node.(*ast.Paragraph)
return ok
}
func unlinkNode(n ast.Node) {
if p := n.GetParent(); p != nil {
newChildren := make([]ast.Node, 0, len(p.GetChildren()))
for _, nn := range p.GetChildren() {
if nn != n {
newChildren = append(newChildren, nn)
}
}
p.SetChildren(newChildren)
}
n.SetParent(nil)
}
func stringifyParagraph(node *ast.Paragraph) string {
var text []string
for _, c := range node.Children {
switch v := c.(type) {
case *ast.Text:
text = append(text, string(v.Literal))
default:
fmt.Printf("WARNING: stringifyParagraph found unexpected type %t\n", c)
}
}
return strings.Join(text, "")
}
//goland:noinspection ALL
func processListAsOwlElement(node *ast.List) []ast.Node {
unlinkNode(node)
// Node should be a list
if !isList(node) {
fmt.Printf("[WARNING] Non-list node reached processListAsOwlElement: %#v\n", node)
return []ast.Node{}
}
var result []ast.Node
var item *ast.DocuowlListItem
for idx, child := range node.Children {
unlinkNode(child)
if idx%2 == 0 {
if item != nil {
result = append(result, item)
}
switch len(child.GetChildren()) {
case 2:
first := child.GetChildren()[0]
second := child.GetChildren()[1]
if isParagraph(first) && isList(second) {
item := &ast.DocuowlList{Title: stringifyParagraph(first.(*ast.Paragraph)), HasTitle: true}
unlinkNode(first)
second.SetParent(item)
item.Children = processListAsOwlElement(second.(*ast.List))
result = append(result, item)
} else {
item = &ast.DocuowlListItem{Title: []ast.Node{first}}
item.Children = []ast.Node{second}
}
case 1:
first := child.GetChildren()[0]
if isList(first) {
item.Children = append(item.Children, processListAsOwlElement(first.(*ast.List))...)
break
}
fallthrough
default:
item = &ast.DocuowlListItem{Title: child.GetChildren()}
}
} else {
for _, c := range child.GetChildren() {
unlinkNode(c)
if isList(c) {
processedData := processListAsOwlElement(c.(*ast.List))
if len(processedData) == 1 && isOwlList(processedData[0]) {
item.Children = append(item.Children, processedData[0])
} else {
otherList := &ast.DocuowlList{
HasTitle: false,
}
otherList.Children = processedData
item.Children = append(item.Children, otherList)
}
} else {
item.Children = append(item.Children, c)
}
c.SetParent(item)
}
}
}
if item != nil {
result = append(result, item)
}
return result
}
func isOwlList(node ast.Node) bool {
_, ok := node.(*ast.DocuowlList)
return ok
}
func shouldProcessChild(node ast.Node) bool {
_, isBox := node.(*ast.DocuowlBox)
_, isHR := node.(*ast.HorizontalRule)
return !isBox && !isHR || isList(node)
}
func ProcessContent(input []string) string {
root, err := convertToAST(input)
if err != nil {
panic(err)
}
isProcessingList := false
var currentList *ast.DocuowlList
ast.WalkFunc(root, func(node ast.Node, entering bool) ast.WalkStatus {
if shouldProcessChild(node) && entering && isProcessingList {
list, ok := node.(*ast.List)
if !ok {
// Not a list. Just stop processing it as a list, and continue.
isProcessingList = false
return ast.GoToNext
}
ast.AppendChildren(currentList, processListAsOwlElement(list)...)
return ast.SkipChildren
}
if !entering {
return ast.GoToNext
}
if header, ok := node.(*ast.Heading); ok {
if header.Level < 3 {
header.Level = 3
}
}
if _, ok := node.(*ast.HorizontalRule); ok && isProcessingList {
isProcessingList = false
ast.RemoveFromTree(node)
return ast.GoToNext
}
if l, ok := node.(*ast.DocuowlList); ok {
currentList = l
isProcessingList = true
}
return ast.GoToNext
})
return convertToHTML(root)
}
func ProcessSideNotes(input []string) string {
root, err := convertToAST(input)
if err != nil {
panic(err)
}
isProcessingBox := false
var currentBox *ast.DocuowlBox
ast.WalkFunc(root, func(node ast.Node, entering bool) ast.WalkStatus {
if shouldProcessChild(node) && entering && isProcessingBox {
// ast.RemoveFromTree does weird things, and we have no time.
unlinkNode(node)
ast.AppendChild(currentBox, node)
node.SetParent(currentBox)
return ast.SkipChildren
}
if !entering {
return ast.GoToNext
}
if h, ok := node.(*ast.Heading); ok {
h.Level = 2
return ast.GoToNext
}
if _, ok := node.(*ast.HorizontalRule); ok {
if isProcessingBox {
isProcessingBox = false
ast.RemoveFromTree(node)
}
return ast.GoToNext
}
if b, ok := node.(*ast.DocuowlBox); ok {
isProcessingBox = true
currentBox = b
}
return ast.GoToNext
})
return convertToHTML(root)
}