-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
157 lines (144 loc) · 3.31 KB
/
parser.go
File metadata and controls
157 lines (144 loc) · 3.31 KB
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
package ham
import (
"encoding/json"
"fmt"
"log"
"strings"
"golang.org/x/net/html"
)
type Layout struct {
ID string `json:"id"`
Src string `json:"layout"`
Path string `json:"path"`
CSS []string `json:"css,omitempty"`
Js []string `json:"js,omitempty"`
JsMod []string `json:"js-mod,omitempty"`
Embeds []Embed
}
type Page struct {
Layout *Layout
Embeds []Embed
}
type Embed struct {
Type string
Src string
Replace string
}
func ParseLayout(doc *html.Node) Layout {
var layout Layout
parseLayout(doc, &layout)
return layout
}
func parseLayout(start *html.Node, layout *Layout) {
if layout == nil {
panic("layout is nil")
}
// fmt.Println("layout transversing ", start.Data)
if start.Type == html.ElementNode {
switch start.Data {
case "link":
for _, attr := range start.Attr {
if attr.Key == "type" && attr.Val == "ham/layout-css" {
start.Data = "{ham:css}"
start.Type = 1
layout.Embeds = append(layout.Embeds, Embed{Type: attr.Val})
}
}
case "embed":
em := Embed{}
for _, attr := range start.Attr {
switch attr.Key {
case "type":
em.Type = attr.Val
case "src":
em.Src = attr.Val
case "data-ham-replace":
em.Replace = attr.Val
}
}
layout.Embeds = append(layout.Embeds, em)
start.Type = 1
switch em.Type {
case "ham/partial":
start.Data = embedPlaceholder(em.Src)
case "ham/page":
start.Data = "{ham:page}"
case "ham/layout-js":
start.Data = "{ham:js}"
case "ham/layout-css":
start.Data = "{ham:css}"
}
}
}
for start = start.FirstChild; start != nil; start = start.NextSibling {
parseLayout(start, layout)
}
}
func ParsePage(doc *html.Node) Page {
var page Page
parsePage(doc, &page)
return page
}
func parsePage(start *html.Node, page *Page) {
if page.Layout == nil {
page.Layout = &Layout{}
}
// fmt.Println("page transversing ", start.Data)
if start.Type == html.ElementNode {
for _, attr := range start.Attr {
if attr.Key == "class" && strings.Contains(attr.Val, "ham-remove") {
start.Parent.RemoveChild(start)
}
}
switch start.Data {
case "embed":
em := Embed{}
for _, attr := range start.Attr {
switch attr.Key {
case "type":
em.Type = attr.Val
case "src":
em.Src = attr.Val
case "data-ham-replace":
em.Replace = attr.Val
}
}
start.Type = 1
switch em.Type {
case "ham/partial":
// replace <embed> tag with placeholders
start.Data = embedPlaceholder(em.Src)
page.Embeds = append(page.Embeds, em)
case "ham/page":
start.Data = "{ham:page}"
case "ham/layout-js":
start.Data = "{ham:js}"
case "ham/layout-css":
start.Data = "{ham:css}"
}
case "div":
var newAttr []html.Attribute
for _, attr := range start.Attr {
switch attr.Key {
case "data-ham-page-config":
if err := json.Unmarshal([]byte(attr.Val), page.Layout); err != nil {
log.Println("config decode error", err.Error())
continue
}
default:
newAttr = append(newAttr, attr)
}
start.Attr = newAttr
}
}
}
for start = start.FirstChild; start != nil; start = start.NextSibling {
parsePage(start, page)
}
}
func embedPlaceholder(src string) string {
return "{embed:" + src + "}"
}
func embedReplaceKey(key string) string {
return fmt.Sprintf("__%s__", key)
}