2017-08-06 09:02:39 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import . "github.com/vektah/goparsify"
|
|
|
|
|
2017-08-06 15:32:10 +02:00
|
|
|
func Parse(input string) (result interface{}, remaining string, err error) {
|
2017-08-06 09:02:39 +02:00
|
|
|
return ParseString(tag, input)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tag struct {
|
|
|
|
Name string
|
|
|
|
Attributes map[string]string
|
2017-08-06 15:32:10 +02:00
|
|
|
Body []interface{}
|
2017-08-06 09:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
tag Parser
|
|
|
|
|
2017-08-06 15:32:10 +02:00
|
|
|
identifier = Merge(And(Chars("a-z", 1, 1), Chars("a-zA-Z0-9", 0)))
|
|
|
|
text = NotChars("<>")
|
2017-08-06 09:02:39 +02:00
|
|
|
|
|
|
|
element = Any(text, &tag)
|
|
|
|
elements = Kleene(element)
|
|
|
|
//attr := And(identifier, equal, String())
|
2017-08-06 09:28:34 +02:00
|
|
|
attr = And(WS, identifier, WS, "=", WS, Any(String('"'), String('\'')))
|
2017-08-06 15:32:10 +02:00
|
|
|
attrs = Map(Kleene(attr, WS), func(node interface{}) interface{} {
|
|
|
|
nodes := node.([]interface{})
|
2017-08-06 09:02:39 +02:00
|
|
|
attr := map[string]string{}
|
|
|
|
|
|
|
|
for _, attrNode := range nodes {
|
2017-08-06 15:32:10 +02:00
|
|
|
attrNodes := attrNode.([]interface{})
|
2017-08-06 09:02:39 +02:00
|
|
|
attr[attrNodes[0].(string)] = attrNodes[2].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return attr
|
|
|
|
})
|
|
|
|
|
|
|
|
tstart = And("<", identifier, attrs, ">")
|
|
|
|
tend = And("</", identifier, ">")
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-08-06 15:32:10 +02:00
|
|
|
tag = Map(And(tstart, elements, tend), func(node interface{}) interface{} {
|
|
|
|
nodes := node.([]interface{})
|
|
|
|
openTag := nodes[0].([]interface{})
|
2017-08-06 09:02:39 +02:00
|
|
|
return Tag{
|
|
|
|
Name: openTag[1].(string),
|
|
|
|
Attributes: openTag[2].(map[string]string),
|
2017-08-06 15:32:10 +02:00
|
|
|
Body: nodes[1].([]interface{}),
|
2017-08-06 09:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|