-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.go
166 lines (151 loc) · 4.77 KB
/
parser.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
package main
import (
"fmt"
"io"
"log"
"strings"
"github.com/PuerkitoBio/goquery"
)
// parse the specified io.Reader
func NewBotFromReader(r io.Reader) (*Bot, error) {
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
return nil, err
}
// initialize the bot container
bot := new(Bot)
bot.Document = doc
bot.Configs = map[string]string{}
bot.Buttons = map[string]*Button{}
bot.Menus = map[string]*Menu{}
bot.Dialogs = map[string]*Dialog{}
bot.Inputs = map[string]*Input{}
// compile the head.meta to configs hashmap
doc.Find("meta").Each(func(_ int, s *goquery.Selection) {
key, _ := s.Attr("name")
value, _ := s.Attr("content")
if key == "" {
return
}
bot.Configs[key] = value
})
// paginate the menu/navs buttons
// messanger maximum navigation links are 3 - 4, we will make them 3 by maximum.
doc.Find("menu,nav").Each(func(_ int, m *goquery.Selection) {
if m.AttrOr("id", "") == bot.Configs["main-menu"] {
return
}
p, cnt := m, 1
more := bot.Configs["pager-more"]
if more == "" {
more = "More"
}
m.Find("a,button").Each(func(i int, b *goquery.Selection) {
if (i > 0) && (i%2 == 0) && m.Find("a,button").Length() > 3 {
newId := fmt.Sprintf("%s-%d", m.AttrOr("id", ""), cnt)
p.AppendHtml(fmt.Sprintf("<a href='#%s'>%s</a>", newId, more))
p.AfterHtml(fmt.Sprintf("<menu id='%s' title='%s'></menu>", newId, m.AttrOr("title", "")))
p = bot.Document.Find("#" + newId)
cnt++
}
if p != m {
p.AppendSelection(b.Clone())
b.Remove()
}
})
})
// populate forms
doc.Find("dialog,form").Each(func(i int, d *goquery.Selection) {
dialog := &Dialog{}
dialog.Node = d
dialog.ID = d.AttrOr("id", fmt.Sprintf("dialog%d", i))
dialog.Action = d.AttrOr("action", "")
dialog.Help = d.AttrOr("help", "")
fn := func(i int, n *goquery.Selection) {
input := &Input{}
input.NS = dialog.ID
input.Title = n.AttrOr("title", "Fill/Choose ...")
input.ID = n.AttrOr("id", fmt.Sprintf("input%d", len(bot.Inputs)+1))
input.Name = n.AttrOr("name", input.ID)
input.Value = n.AttrOr("value", "")
input.If = n.AttrOr("if", "")
input.Options = []*Button{}
input.Type = n.AttrOr("type", "text")
n.Find("option").Each(func(i int, o *goquery.Selection) {
btn := &Button{}
btn.ID = fmt.Sprintf("input-%s-%d", input.ID, len(bot.Inputs)+1)
btn.Href = fmt.Sprintf("input=%s&answer=yes&value=%s", input.ID, o.AttrOr("value", ""))
btn.Embed = "no"
btn.Title = o.Text()
btn.Reset = false
input.Options = append(input.Options, btn)
o.SetAttr("id", btn.ID)
})
bot.Inputs[input.ID] = input
n.SetAttr("id", input.ID)
n.SetAttr("title", input.Title)
}
d.Find("div").Children().Each(func(i int, s *goquery.Selection) {
s.SetAttr("if", s.Parent().AttrOr("if", ""))
s.Parent().BeforeSelection(s.Clone())
})
d.Find("div").Remove()
d.Find("input,select").Each(func(i int, n *goquery.Selection) {
switch strings.ToLower(goquery.NodeName(n)) {
case "input":
fn(i, n)
case "select":
n.SetAttr("type", "options")
fn(i, n)
}
})
bot.Dialogs[dialog.ID] = dialog
d.SetAttr("id", dialog.ID)
})
// populate the navs
doc.Find("menu,nav").Each(func(i int, m *goquery.Selection) {
menu := &Menu{}
menu.ID = m.AttrOr("id", fmt.Sprintf("menu%d", i))
menu.Title = m.AttrOr("title", "Choose ...")
menu.Buttons = []*Button{}
menu.Inline = (m.AttrOr("inline", "false") == "true")
m.Find("a,button").Each(func(i int, b *goquery.Selection) {
btn := &Button{}
btn.ID = b.AttrOr("id", fmt.Sprintf("button%d", len(bot.Buttons)+1))
btn.Title = b.Text()
btn.Href = b.AttrOr("href", "")
btn.Embed = b.AttrOr("embed", "false")
btn.Reset = (b.AttrOr("reset", "false") == "true")
menu.Buttons = append(menu.Buttons, btn)
bot.Buttons[btn.ID] = btn
b.SetAttr("id", btn.ID)
})
m.SetAttr("id", menu.ID)
m.SetAttr("title", menu.Title)
bot.Menus[menu.ID] = menu
})
// finding errors
doc.Find("menu,nav").Find("a,button").Each(func(i int, b *goquery.Selection) {
href := b.AttrOr("href", "")
title := b.AttrOr("title", b.Text())
btnErr := "Invalid `href` (href=" + href + ") attribute of the button(" + title + ") in the Menu/Nav(" + b.Parent().AttrOr("id", "") + ")"
if string(href[0]) != "#" && !strings.HasPrefix(href, "http://") && !strings.HasPrefix(href, "https://") {
bot.Errors = append(bot.Errors, "[WrongID Format] "+btnErr)
}
if href == "" {
bot.Errors = append(bot.Errors, "[EMPTY] "+btnErr)
}
if string(href[0]) == "#" && doc.Find(href).Length() == 0 {
bot.Errors = append(bot.Errors, "[NOT FOUND] "+btnErr)
}
})
if len(bot.Errors) > 0 {
for _, msg := range bot.Errors {
log.Println("[CompilerError]", msg)
}
log.Fatal("Please fix the above errors")
} else {
log.Println("No errors found ...")
}
return bot, nil
}