@@ -3,6 +3,7 @@ package main
33import (
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