diff options
Diffstat (limited to 'html')
-rw-r--r-- | html/README.md | 4 | ||||
-rw-r--r-- | html/html.go | 52 | ||||
-rw-r--r-- | html/html_test.go | 17 |
3 files changed, 73 insertions, 0 deletions
diff --git a/html/README.md b/html/README.md new file mode 100644 index 0000000..2e72f20 --- /dev/null +++ b/html/README.md @@ -0,0 +1,4 @@ +example html parser +=== + +This is a **very** rudimentary html parser that should be used as an example only. 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), + } + + }) +} diff --git a/html/html_test.go b/html/html_test.go new file mode 100644 index 0000000..6dca6d4 --- /dev/null +++ b/html/html_test.go @@ -0,0 +1,17 @@ +package html + +import ( + "testing" + + "github.com/stretchr/testify/require" + . "github.com/vektah/goparsify" +) + +func TestParse(t *testing.T) { + result, _, err := Parse("<body>hello <b>world</b></body>") + require.NoError(t, err) + require.Equal(t, Tag{Name: "body", Attributes: map[string]string{}, Body: []Node{ + "hello ", + Tag{Name: "b", Attributes: map[string]string{}, Body: []Node{"world"}}, + }}, result) +} |