Skip to content

Commit 1a6a5f2

Browse files
committed
added html error checker
1 parent d4818d0 commit 1a6a5f2

File tree

3 files changed

+53
-23
lines changed

3 files changed

+53
-23
lines changed

bot.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Bot struct {
1111
Inputs map[string]*Input
1212
Menus map[string]*Menu
1313
Dialogs map[string]*Dialog
14+
Errors []string
1415
}
1516

1617
type Menu struct {

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func main() {
2424
flag.Parse()
2525

2626
// open the specified html file to be parsed
27+
log.Println("Compiling from", *HTML_FILE, " ...")
2728
file, err := os.Open(*HTML_FILE)
2829
if err != nil {
2930
log.Fatal(err)
@@ -61,13 +62,15 @@ func main() {
6162
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
6263
}
6364
go (func() {
65+
log.Println("Start serving HTTPS traffic on", *HTTPS_SERVER)
6466
errchan <- s.ListenAndServeTLS("", "")
6567
})()
6668
}
6769

6870
// starts the HTTP server if required
6971
if *HTTP_SERVER != "" {
7072
go (func() {
73+
log.Println("Start serving HTTP traffic on", *HTTP_SERVER)
7174
errchan <- http.ListenAndServe(*HTTP_SERVER, handler)
7275
})()
7376
}

parser.go

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"io"
6+
"log"
67
"strings"
78

89
"github.com/PuerkitoBio/goquery"
@@ -61,29 +62,6 @@ func NewBotFromReader(r io.Reader) (*Bot, error) {
6162
})
6263
})
6364

64-
// populate the navs
65-
doc.Find("menu,nav").Each(func(i int, m *goquery.Selection) {
66-
menu := &Menu{}
67-
menu.ID = m.AttrOr("id", fmt.Sprintf("menu%d", i))
68-
menu.Title = m.AttrOr("title", "Choose")
69-
menu.Buttons = []*Button{}
70-
menu.Inline = (m.AttrOr("inline", "false") == "true")
71-
m.Find("a,button").Each(func(i int, b *goquery.Selection) {
72-
btn := &Button{}
73-
btn.ID = b.AttrOr("id", fmt.Sprintf("button%d", len(bot.Buttons)+1))
74-
btn.Title = b.Text()
75-
btn.Href = b.AttrOr("href", "")
76-
btn.Embed = b.AttrOr("embed", "false")
77-
btn.Reset = (b.AttrOr("reset", "false") == "true")
78-
menu.Buttons = append(menu.Buttons, btn)
79-
bot.Buttons[btn.ID] = btn
80-
b.SetAttr("id", btn.ID)
81-
})
82-
m.SetAttr("id", menu.ID)
83-
m.SetAttr("title", menu.Title)
84-
bot.Menus[menu.ID] = menu
85-
})
86-
8765
// populate forms
8866
doc.Find("dialog,form").Each(func(i int, d *goquery.Selection) {
8967
dialog := &Dialog{}
@@ -133,5 +111,53 @@ func NewBotFromReader(r io.Reader) (*Bot, error) {
133111
d.SetAttr("id", dialog.ID)
134112
})
135113

114+
// populate the navs
115+
doc.Find("menu,nav").Each(func(i int, m *goquery.Selection) {
116+
menu := &Menu{}
117+
menu.ID = m.AttrOr("id", fmt.Sprintf("menu%d", i))
118+
menu.Title = m.AttrOr("title", "Choose ...")
119+
menu.Buttons = []*Button{}
120+
menu.Inline = (m.AttrOr("inline", "false") == "true")
121+
m.Find("a,button").Each(func(i int, b *goquery.Selection) {
122+
btn := &Button{}
123+
btn.ID = b.AttrOr("id", fmt.Sprintf("button%d", len(bot.Buttons)+1))
124+
btn.Title = b.Text()
125+
btn.Href = b.AttrOr("href", "")
126+
btn.Embed = b.AttrOr("embed", "false")
127+
btn.Reset = (b.AttrOr("reset", "false") == "true")
128+
menu.Buttons = append(menu.Buttons, btn)
129+
bot.Buttons[btn.ID] = btn
130+
b.SetAttr("id", btn.ID)
131+
})
132+
m.SetAttr("id", menu.ID)
133+
m.SetAttr("title", menu.Title)
134+
bot.Menus[menu.ID] = menu
135+
})
136+
137+
// finding errors
138+
doc.Find("menu,nav").Find("a,button").Each(func(i int, b *goquery.Selection) {
139+
href := b.AttrOr("href", "")
140+
title := b.AttrOr("title", b.Text())
141+
btnErr := "Invalid `href` (href=" + href + ") attribute of the button(" + title + ") in the Menu/Nav(" + b.Parent().AttrOr("id", "") + ")"
142+
143+
if href == "" {
144+
bot.Errors = append(bot.Errors, "[EMPTY] "+btnErr)
145+
}
146+
147+
// log.Println(doc.Find(href).Length())
148+
149+
if string(href[0]) == "#" && doc.Find(href).Length() == 0 {
150+
bot.Errors = append(bot.Errors, "[NOT FOUND] "+btnErr)
151+
}
152+
})
153+
154+
if len(bot.Errors) > 0 {
155+
for _, msg := range bot.Errors {
156+
log.Println("[CompilerError]", msg)
157+
}
158+
} else {
159+
log.Println("No errors found ...")
160+
}
161+
136162
return bot, nil
137163
}

0 commit comments

Comments
 (0)