-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
170 lines (166 loc) · 3.85 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"errors"
"strings"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
type Element struct {
Id string `json:id`
Children []Element `json:children`
Data string `json:"#data"`
Scalar interface{} `json:"#scalar"`
Base64 string `json:"#base64"`
Style map[string]interface{} `json:style`
}
func (eself *Element)parseStyle() []html.Attribute{
var retval []html.Attribute
if len(eself.Style) == 0 {
return retval
}
retval = append(retval, html.Attribute{Key:"style"})
for key, val := range eself.Style {
if key[0] == '#' {
continue
}
var valStr string
if vals, ok := val.(string); ok {
valStr = vals
} else {
valStr = fmt.Sprintf("%v", val)
}
retval[0].Val += key + "=" + strings.Replace(valStr, "\x00", "", -1) + ";"
}
return retval
}
func (eself *Element)hasText() bool{
if eself.Id == "#text" {
return true
}
for _, child := range eself.Children {
if child.hasText() {
return true
}
}
return false
}
func (eself *Element)toHtml() *html.Node{
if len(eself.Id) != 0 {
if eself.Id == "#text" {
return &html.Node {
Data:eself.Data,
Type:html.TextNode,
}
} else {
var retval html.Node
if eself.Id == "li" && !eself.hasText() { // HAX
return nil
}
retval.Data = eself.Id
retval.Type = html.ElementNode
retval.DataAtom = atom.Lookup([]byte(eself.Id)) // FIXME tolower?
retval.Attr = append(retval.Attr,
eself.parseStyle()...)
for _, child := range eself.Children {
htmlChild := child.toHtml()
if htmlChild != nil {
retval.AppendChild(htmlChild)
}
}
return &retval
}
}
return nil
}
const emptyDocument string = `<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<!--<link rel="stylesheet" href="style.css">-->
</head><body></body></html>
`
func findHeadBody(root *html.Node) (head,body *html.Node, err error){
cur := root
stack := []*html.Node{}
for cur != nil && (body == nil || head == nil) {
if cur.DataAtom == atom.Body {
body = cur
}
if cur.DataAtom == atom.Head {
head = cur
}
if cur.NextSibling != nil {
stack = append(stack, cur.NextSibling)
}
if cur.FirstChild != nil {
cur = cur.FirstChild
} else if len(stack) != 0{
cur = stack[len(stack) - 1]
stack = stack[:len(stack) - 1]
} else {
cur = nil
}
}
if body == nil {
err = errors.New("Could not locate body")
}
if head == nil {
err = errors.New("Could not locate head")
}
return
}
func main() {
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
var dat []Element
err = json.Unmarshal(input, &dat)
if err != nil {
panic(err)
}
root, parseErr := html.Parse(strings.NewReader(emptyDocument))
if parseErr != nil {
panic(parseErr)
}
head, body, findErr := findHeadBody(root)
if findErr != nil {
panic(findErr)
}
/*
head.AppendChild(&html.Node {
Type:html.ElementNode,
DataAtom:atom.Link,
Data:"link",
Attr:[]html.Attribute {
html.Attribute{
Key:"rel",
Val:"stylesheet",
},
html.Attribute{
Key:"href",
Val:"style.css",
},
},
})
body.AppendChild(&html.Node {
Type:html.TextNode,
Data:"HELLOTE",
})
*/
_ = head
for _, ele := range dat {
htmlElement := ele.toHtml()
if htmlElement != nil {
body.AppendChild(htmlElement)
}
}
err = html.Render(os.Stdout, root)
if err != nil {
panic(err)
}
_,_ = os.Stdout.Write([]byte{'\n'})
}