-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderhtml.go
161 lines (145 loc) · 3.56 KB
/
renderhtml.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
package stork
import (
"errors"
"fmt"
"strings"
"golang.org/x/net/html"
)
var (
TabStr = " "
NewStr = "\n"
)
// Html renders the article in html format.
func (a *Article) Html() (string, error) {
var b strings.Builder
if err := renderHtml(&b, a.output, 0, true); err != nil {
return "", err
}
return b.String(), nil
}
// renderHtml is a simplest version of the one implemented in
// golang.org/x/net/html.Render.
// It does not escape special characters and adds newline
// characters after block elements.
func renderHtml(b *strings.Builder, n *html.Node, depth int, indent bool) error {
// Render non-element nodes; these are the easy cases.
switch n.Type {
case html.ErrorNode:
return errors.New("cannot render an ErrorNode node")
case html.TextNode:
_, err := b.WriteString(n.Data)
return err
case html.DocumentNode:
for c := n.FirstChild; c != nil; c = c.NextSibling {
if err := renderHtml(b, c, depth, indent); err != nil {
return err
}
}
return nil
case html.ElementNode:
// No-op.
case html.CommentNode:
// We don't render comment nodes
return nil
case html.DoctypeNode:
if _, err := b.WriteString("<!DOCTYPE "); err != nil {
return err
}
if _, err := b.WriteString(n.Data); err != nil {
return err
}
if err := b.WriteByte('>'); err != nil {
return err
}
// We don't render attributes
_, err := b.WriteString(NewStr)
return err
default:
return errors.New("unknown node type")
}
// Render the <xxx> opening tag.
if err := b.WriteByte('<'); err != nil {
return err
}
if _, err := b.WriteString(n.Data); err != nil {
return err
}
for _, a := range n.Attr {
if err := b.WriteByte(' '); err != nil {
return err
}
if a.Namespace != "" {
if _, err := b.WriteString(a.Namespace); err != nil {
return err
}
if err := b.WriteByte(':'); err != nil {
return err
}
}
if _, err := b.WriteString(a.Key); err != nil {
return err
}
if _, err := b.WriteString(`="`); err != nil {
return err
}
if _, err := b.WriteString(a.Val); err != nil {
return err
}
if err := b.WriteByte('"'); err != nil {
return err
}
}
if voidTags[n.Data] {
if n.FirstChild != nil {
return fmt.Errorf("void element <%s> has child nodes", n.Data)
}
_, err := b.WriteString("/>")
return err
}
if err := b.WriteByte('>'); err != nil {
return err
}
if inlineTags[n.Data] || n.Data == "pre" {
indent = false
}
if n.FirstChild == nil {
indent = false
}
// Render any child nodes.
collapse := false
for c := n.FirstChild; c != nil; c = c.NextSibling {
if singleTxt := (c == n.FirstChild && c.NextSibling == nil && c.Type == html.TextNode); singleTxt {
indent = false
}
if c.PrevSibling != nil {
isPrevInline := c.PrevSibling.Type == html.ElementNode && inlineTags[c.PrevSibling.Data]
isPrevTxt := c.PrevSibling.Type == html.TextNode
isCurInline := c.Type == html.ElementNode && inlineTags[c.Data]
isCurTxt := c.Type == html.TextNode
if isCurTxt && isPrevInline || isCurInline && isPrevTxt {
collapse = true
}
}
if indent && !collapse {
if _, err := b.WriteString(fmt.Sprint(NewStr, strings.Repeat(TabStr, depth+1))); err != nil {
return err
}
}
if err := renderHtml(b, c, depth+1, indent); err != nil {
return err
}
}
if indent {
if _, err := b.WriteString(fmt.Sprint(NewStr, strings.Repeat(TabStr, depth))); err != nil {
return err
}
}
// Render the </xxx> closing tag.
if _, err := b.WriteString("</"); err != nil {
return err
}
if _, err := b.WriteString(n.Data); err != nil {
return err
}
return b.WriteByte('>')
}