diff options
author | Adam Scarr <adam@vektah.net> | 2017-08-06 17:02:39 +1000 |
---|---|---|
committer | Adam Scarr <adam@vektah.net> | 2017-08-06 17:02:39 +1000 |
commit | a65a9325aaebd1499a8e523463cc023124f8536a (patch) | |
tree | 2fb31800b67a3914a7204a28319a7aeb0ac649c7 /html/html.go | |
parent | 8b343d6360d0edc065b9b62ab5e708e907b45a92 (diff) |
Get the HTML parser working
Diffstat (limited to 'html/html.go')
-rw-r--r-- | html/html.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/html/html.go b/html/html.go new file mode 100644 index 0000000..5ceb5e9 --- /dev/null +++ b/html/html.go @@ -0,0 +1,52 @@ +package html + +import . "github.com/vektah/goparsify" + +func Parse(input string) (result Node, remaining string, err error) { + return ParseString(tag, input) +} + +type Tag struct { + Name string + Attributes map[string]string + Body []Node +} + +var ( + tag Parser + + identifier = Merge(And(Range("a-z", 1, 1), Range("a-zA-Z0-9", 0))) + text = CharRunUntil("<>") + + element = Any(text, &tag) + elements = Kleene(element) + //attr := And(identifier, equal, String()) + attr = And(identifier, WS, "=", WS, `"test"`) + attrs = Map(Kleene(attr, WS), func(node Node) Node { + nodes := node.([]Node) + attr := map[string]string{} + + for _, attrNode := range nodes { + attrNodes := attrNode.([]Node) + attr[attrNodes[0].(string)] = attrNodes[2].(string) + } + + return attr + }) + + tstart = And("<", identifier, attrs, ">") + tend = And("</", identifier, ">") +) + +func init() { + tag = Map(And(tstart, elements, tend), func(node Node) Node { + nodes := node.([]Node) + openTag := nodes[0].([]Node) + return Tag{ + Name: openTag[1].(string), + Attributes: openTag[2].(map[string]string), + Body: nodes[1].([]Node), + } + + }) +} |