-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
262 lines (228 loc) · 6.43 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
package htmldown
import (
"fmt"
"strings"
"golang.org/x/net/html"
)
type (
// Node defines a simple description iof html node
Node interface {
New(t html.Token, parent Node) Node
Tag() string // node's tag name
Text(str ...string) string // get or append node's text
Attr() map[string]string // node's attributes
Parent() Node // node's parent
Children(nodes ...Node) []Node // append or get node's children nodes
Markdown() string // render nodes to markdown
}
baseNode struct {
tag string
text string
attributes map[string]string
parent Node
children []Node
}
// RootNode is root node of html document
RootNode struct {
baseNode
}
// CommonNode is a node that print as normal html, not markdown
CommonNode struct {
baseNode
}
)
// New returns new baseNode
func (bn *baseNode) New(t html.Token, parent Node) Node {
b := &baseNode{
tag: strings.ToLower(t.Data),
attributes: make(map[string]string),
parent: parent,
}
for _, attr := range t.Attr {
b.attributes[attr.Key] = attr.Val
}
return b
}
func (bn *baseNode) Tag() string { return bn.tag }
func (bn *baseNode) Attr() map[string]string { return bn.attributes }
func (bn *baseNode) Text(str ...string) string {
if len(str) > 0 {
bn.text += strings.Join(str, "")
}
return bn.text
}
func (bn *baseNode) Parent() Node { return bn.parent }
func (bn *baseNode) Children(nodes ...Node) []Node {
if len(nodes) > 0 {
bn.children = append(bn.children, nodes...)
bn.Text(strings.Repeat("@node@", len(nodes)))
}
return bn.children
}
func (bn *baseNode) Markdown() string {
content := bn.text
for _, child := range bn.children {
content = strings.Replace(content, "@node@", child.Markdown(), 1)
}
return content
}
// Tag returns RootNode's tag ,"root"
func (rt *RootNode) Tag() string { return "root" }
// Markdown renders CommonNode as html tag string ,<tag>text</tag>
func (cn *CommonNode) Markdown() string {
return fmt.Sprintf("<%s>%s</%s>", cn.Tag(), cn.baseNode.Markdown(), cn.Tag())
}
// New returns new CommonNode
func (cn *CommonNode) New(t html.Token, parent Node) Node {
bs := cn.baseNode.New(t, parent)
return &CommonNode{
baseNode: *(bs.(*baseNode)),
}
}
type (
// ParagraphNode is used to <p>
ParagraphNode struct{ baseNode }
// HrefNode is used to <a>
HrefNode struct{ baseNode }
// ImageNode is used to <img>
ImageNode struct{ baseNode }
// HorizontalNode is used to <hr/>
HorizontalNode struct{ baseNode }
// BreakNode is used to <br/>
BreakNode struct{ baseNode }
// BlockquoteNode is used to <blockquote>
BlockquoteNode struct{ baseNode }
// CodeNode is used to <code>
CodeNode struct{ baseNode }
// PreNode is used to <pre>
PreNode struct {
baseNode
closePrint bool
}
// HeaderNode is used to <h1-6>
HeaderNode struct{ baseNode }
// StrongNode is used to <strong> and <em>
StrongNode struct{ baseNode }
)
// Markdown renders ParagraphNode with \n
func (pn *ParagraphNode) Markdown() string {
if pn.Text() == "" {
return ""
}
return pn.baseNode.Markdown() + "\n\n"
}
// New returns new ParagraphNode
func (pn *ParagraphNode) New(t html.Token, parent Node) Node {
bs := pn.baseNode.New(t, parent)
return &ParagraphNode{
baseNode: *(bs.(*baseNode)),
}
}
// Markdown renders HrefNode as [text](href)
func (hn *HrefNode) Markdown() string {
return fmt.Sprintf("[%s](%s)", hn.baseNode.Markdown(), hn.attributes["href"])
}
// New returns new HrefNode
func (hn *HrefNode) New(t html.Token, parent Node) Node {
bs := hn.baseNode.New(t, parent)
return &HrefNode{
baseNode: *(bs.(*baseNode)),
}
}
// Markdown renders ImageNode as ![alt](src)
func (in *ImageNode) Markdown() string {
return fmt.Sprintf("![%s](%s)", in.attributes["alt"], in.attributes["src"])
}
// New returns new ImageNode
func (in *ImageNode) New(t html.Token, parent Node) Node {
bs := in.baseNode.New(t, parent)
return &ImageNode{
baseNode: *(bs.(*baseNode)),
}
}
// Markdown renders HorizontalNode as `---\n`
func (hn *HorizontalNode) Markdown() string {
return "---\n\n"
}
// New returns HorizontalNode
func (hn *HorizontalNode) New(t html.Token, parent Node) Node {
bs := hn.baseNode.New(t, parent)
return &HorizontalNode{
baseNode: *(bs.(*baseNode)),
}
}
// Markdown renders BreakNode as "\n\n"
func (bn *BreakNode) Markdown() string {
return "\n\n"
}
// New returns new BreakNode
func (bn *BreakNode) New(t html.Token, parent Node) Node {
bs := bn.baseNode.New(t, parent)
return &BreakNode{
baseNode: *(bs.(*baseNode)),
}
}
// Markdown renders BlockquoteNode as \n```\n text \n```\n
func (bn *BlockquoteNode) Markdown() string {
return fmt.Sprintf("\n```\n%s\n```\n", bn.baseNode.Markdown())
}
// New returns new BlockquoteNode
func (bn *BlockquoteNode) New(t html.Token, parent Node) Node {
bs := bn.baseNode.New(t, parent)
return &BlockquoteNode{
baseNode: *(bs.(*baseNode)),
}
}
// Markdown renders CodeNode as <code>text</code>
// if element class exist, render as <code class="class">text</code>
func (cn *CodeNode) Markdown() string {
if cn.Parent().Tag() == "pre" {
cn.Parent().(*PreNode).closePrint = true // close <pre> print
classes := strings.Split(cn.attributes["class"], " ")
for _, class := range classes {
if strings.HasPrefix(class, "language") {
return fmt.Sprintf("```%s\n%s\n```\n", strings.TrimPrefix(class, "language-"), cn.baseNode.Markdown())
}
}
return fmt.Sprintf("```\n%s\n```\n", cn.baseNode.Markdown())
}
return fmt.Sprintf("`%s`", cn.baseNode.Markdown())
}
// New returns new CodeNode
func (cn *CodeNode) New(t html.Token, parent Node) Node {
bs := cn.baseNode.New(t, parent)
return &CodeNode{
baseNode: *(bs.(*baseNode)),
}
}
func (pn *PreNode) Markdown() string {
if pn.closePrint {
return ""
}
return pn.baseNode.Markdown()
}
func (pn *PreNode) New(t html.Token, parent Node) Node {
bs := pn.baseNode.New(t, parent)
return &PreNode{
baseNode: *(bs.(*baseNode)),
}
}
func (hn *HeaderNode) Markdown() string {
repeat := int(hn.Tag()[len(hn.Tag())-1]) - 48
return strings.Repeat("#", repeat) + " " + hn.baseNode.Markdown()
}
func (hn *HeaderNode) New(t html.Token, parent Node) Node {
bs := hn.baseNode.New(t, parent)
return &HeaderNode{
baseNode: *(bs.(*baseNode)),
}
}
func (sn *StrongNode) Markdown() string {
return fmt.Sprintf("**%s**", sn.baseNode.Markdown())
}
func (sn *StrongNode) New(t html.Token, parent Node) Node {
bs := sn.baseNode.New(t, parent)
return &StrongNode{
baseNode: *(bs.(*baseNode)),
}
}