-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (138 loc) · 2.66 KB
/
main.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
package main
import (
"log"
"os"
"strings"
. "html"
"github.com/taylorchu/h5/pretty"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
type Node struct {
*html.Node
}
func (n *Node) Children() []pretty.Node {
var ns []pretty.Node
for c := n.FirstChild; c != nil; c = c.NextSibling {
ns = append(ns, &Node{c})
}
return ns
}
func (n *Node) Start() string {
switch n.Type {
case html.CommentNode:
return "<!--"
case html.DoctypeNode:
return "<!DOCTYPE html>"
case html.ElementNode:
s := "<" + n.Data
for _, a := range n.Attr {
s += " " + a.Key
if a.Val != "" {
s += `="` + EscapeString(a.Val) + `"`
}
}
if voidElements[n.Data] {
s += " />"
} else {
s += ">"
}
return s
default:
return ""
}
}
func (n *Node) End() string {
switch n.Type {
case html.CommentNode:
return "-->"
case html.ElementNode:
if voidElements[n.Data] {
return ""
}
return "</" + n.Data + ">"
default:
return ""
}
}
func (n *Node) Parent() pretty.Node {
if n.Node.Parent == nil {
return nil
}
return &Node{n.Node.Parent}
}
func (n *Node) Text() []string {
switch n.Type {
case html.CommentNode:
fallthrough
case html.TextNode:
s := n.Data
if n.Node.Parent != nil {
if n.Node.Parent.DataAtom != atom.Script {
s = EscapeString(s)
}
if n.Node.Parent.DataAtom == atom.Pre {
return []string{s}
}
}
return strings.Split(strings.Trim(s, " \t\r\n"), "\n")
default:
return nil
}
}
func (n *Node) Inline() bool {
switch n.DataAtom {
case atom.Pre:
return true
}
if n.Type == html.ElementNode && n.FirstChild == nil {
return true
}
if n.FirstChild != nil && n.FirstChild.NextSibling == nil &&
n.FirstChild.Type == html.TextNode &&
!strings.ContainsAny(n.FirstChild.Data, "\r\n") {
return true
}
return n.Parent() != nil && n.Parent().Inline()
}
// Section 12.1.2, "Elements", gives this list of void elements. Void elements
// are those that can't have any contents.
var voidElements = map[string]bool{
"area": true,
"base": true,
"br": true,
"col": true,
"command": true,
"embed": true,
"hr": true,
"img": true,
"input": true,
"keygen": true,
"link": true,
"meta": true,
"param": true,
"source": true,
"track": true,
"wbr": true,
}
func clean(n *html.Node) {
for c := n.FirstChild; c != nil; {
// c.NextSibling is nil once it is removed
tmp := c.NextSibling
if strings.Trim(c.Data, " \t\r\n") == "" {
n.RemoveChild(c)
}
c = tmp
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
clean(c)
}
}
func main() {
doc, err := html.Parse(os.Stdin)
if err != nil {
log.Fatal(err)
}
clean(doc)
pretty.Print(os.Stdout, &Node{doc})
}