Skip to content

Commit

Permalink
added html error checker
Browse files Browse the repository at this point in the history
  • Loading branch information
alash3al committed Mar 10, 2018
1 parent d4818d0 commit 1a6a5f2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
1 change: 1 addition & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Bot struct {
Inputs map[string]*Input
Menus map[string]*Menu
Dialogs map[string]*Dialog
Errors []string
}

type Menu struct {
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func main() {
flag.Parse()

// open the specified html file to be parsed
log.Println("Compiling from", *HTML_FILE, " ...")
file, err := os.Open(*HTML_FILE)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -61,13 +62,15 @@ func main() {
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
}
go (func() {
log.Println("Start serving HTTPS traffic on", *HTTPS_SERVER)
errchan <- s.ListenAndServeTLS("", "")
})()
}

// starts the HTTP server if required
if *HTTP_SERVER != "" {
go (func() {
log.Println("Start serving HTTP traffic on", *HTTP_SERVER)
errchan <- http.ListenAndServe(*HTTP_SERVER, handler)
})()
}
Expand Down
72 changes: 49 additions & 23 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"io"
"log"
"strings"

"github.com/PuerkitoBio/goquery"
Expand Down Expand Up @@ -61,29 +62,6 @@ func NewBotFromReader(r io.Reader) (*Bot, error) {
})
})

// 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
})

// populate forms
doc.Find("dialog,form").Each(func(i int, d *goquery.Selection) {
dialog := &Dialog{}
Expand Down Expand Up @@ -133,5 +111,53 @@ func NewBotFromReader(r io.Reader) (*Bot, error) {
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 href == "" {
bot.Errors = append(bot.Errors, "[EMPTY] "+btnErr)
}

// log.Println(doc.Find(href).Length())

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)
}
} else {
log.Println("No errors found ...")
}

return bot, nil
}

0 comments on commit 1a6a5f2

Please sign in to comment.