summaryrefslogtreecommitdiff
path: root/html/html.go
blob: 847af96d6303593c298faed4081a491cb1b77f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package html

import . "github.com/vektah/goparsify"

func Parse(input string) (result interface{}, remaining string, err error) {
	return ParseString(tag, input)
}

type Tag struct {
	Name       string
	Attributes map[string]string
	Body       []interface{}
}

var (
	tag Parser

	identifier = Merge(And(Chars("a-z", 1, 1), Chars("a-zA-Z0-9", 0)))
	text       = NotChars("<>")

	element  = Any(text, &tag)
	elements = Kleene(element)
	//attr := And(identifier, equal, String())
	attr  = And(WS, identifier, WS, "=", WS, Any(String('"'), String('\'')))
	attrs = Map(Kleene(attr, WS), func(node interface{}) interface{} {
		nodes := node.([]interface{})
		attr := map[string]string{}

		for _, attrNode := range nodes {
			attrNodes := attrNode.([]interface{})
			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 interface{}) interface{} {
		nodes := node.([]interface{})
		openTag := nodes[0].([]interface{})
		return Tag{
			Name:       openTag[1].(string),
			Attributes: openTag[2].(map[string]string),
			Body:       nodes[1].([]interface{}),
		}

	})
}