/
ext_outline.go
147 lines (130 loc) · 2.73 KB
/
ext_outline.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
////////////////////////////////////////////////////////////////////////////
// Program: extractor.go
// Purpose: HTML Extractor
// Authors: Antonio Sun (c) 2018, All rights reserved
////////////////////////////////////////////////////////////////////////////
package main
import (
"fmt"
"io"
"strings"
"golang.org/x/net/html"
)
////////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions
type extOutliner struct {
*extractor
}
var attrPick []string = []string{"id", "name", "css", "type", "onclick"}
////////////////////////////////////////////////////////////////////////////
// Function definitions
func NewExtOutliner(i io.Reader) *extOutliner {
return &extOutliner{NewExtractor(i)}
}
////////////////////////////////////////////////////////////////////////////
// Method definitions
func (e *extOutliner) VisitToken(z *html.Tokenizer, tt html.TokenType, w io.Writer) {
tag := TagParse(z)
verbose(2, ">: %d (%v: %s)", e.depth, tt, tag.Name)
switch tt {
case html.StartTagToken, html.SelfClosingTagToken:
if tag.Name == "body" {
e.depth = 1
e.outputstart = true
fmt.Fprintf(w, "{\n")
}
if !e.justOpenUsed && e.justOpenSet {
fmt.Fprintln(w)
e.justOpenUsed = true
}
if tt == html.SelfClosingTagToken {
e.depth++
}
e.PrintTag(w, tag)
if tt == html.SelfClosingTagToken {
fmt.Fprintf(w, "}},\n")
e.depth--
} else {
e.justOpenSet = true
e.justOpenUsed = false
}
case html.EndTagToken:
if e.outputstart {
fmt.Fprintf(w, "}},\n")
e.justOpenUsed = true
}
}
verbose(2, "<: %d", e.depth)
}
func (e *extOutliner) PrintTag(w io.Writer, tag Tag) {
if !e.outputstart {
return
}
fmt.Fprintf(w, "%*s\"%s\": {\n%*s\"=\": \"",
(e.depth-1)*2, "", tag.Name, e.depth*2, "")
e.PrintAttr(w, tag.Attr)
fmt.Fprintf(w, "\",\n%*s\"_\": {", e.depth*2, "")
}
func (e *extOutliner) PrintAttr(w io.Writer, am attributes) {
if len(am) == 0 {
return
}
for _, p := range attrPick {
if p == "css" {
a, ok := am["class"]
if ok {
fmt.Fprintf(w, "%s=%s ", p, "."+strings.Join(strings.Fields(a), "."))
}
} else {
a, ok := am[p]
if ok {
fmt.Fprintf(w, "%s=%s ", p, a)
}
}
}
}
/*
JSON layout:
{
"body": {
"A": "",
"C": {
"h1": {
"A": "",
"C": {}
},
"h2": {
"A": "",
"C": {}
},
"ul": {
"A": "",
"C": {
"li": {
"A": "",
"C": {}
},
"li2": {
"A": "",
"C": {}
}
}
}
"span": {
"A": "",
"C": {
"img": {
"A": "",
"C": {}
},
"a": {
"A": "",
"C": {}
}
}
}
}
}
Note the "li2" can't be "li" again? Otherwise, will get
"Duplicate key" error
*/