diff options
author | Adam Scarr <adam@vektah.net> | 2017-08-13 17:30:10 +1000 |
---|---|---|
committer | Adam Scarr <adam@vektah.net> | 2017-08-13 18:56:23 +1000 |
commit | 5716ddb5e7ca3cb4ee445bdd4958c37aeb033baa (patch) | |
tree | a5d5017dbf1078220ceef24d9ea9432b33972291 /html | |
parent | 0dc37ae5bc10cc0669f88ab9febbc039a28f23d1 (diff) |
Pass result in instead of returning
Diffstat (limited to 'html')
-rw-r--r-- | html/html.go | 19 | ||||
-rw-r--r-- | html/html_test.go | 3 |
2 files changed, 11 insertions, 11 deletions
diff --git a/html/html.go b/html/html.go index 33e5cc4..05e091b 100644 --- a/html/html.go +++ b/html/html.go @@ -18,28 +18,26 @@ var ( tag Parser identifier = Regex("[a-zA-Z][a-zA-Z0-9]*") - text = NotChars("<>").Map(func(n Result) Result { - return Result{Result: n.Token} - }) + text = NotChars("<>").Map(func(n *Result) { n.Result = n.Token }) element = Any(text, &tag) - elements = Some(element).Map(func(n Result) Result { + elements = Some(element).Map(func(n *Result) { ret := []interface{}{} for _, child := range n.Child { ret = append(ret, child.Result) } - return Result{Result: ret} + n.Result = ret }) attr = Seq(identifier, "=", StringLit(`"'`)) - attrs = Some(attr).Map(func(node Result) Result { + attrs = Some(attr).Map(func(node *Result) { attr := map[string]string{} for _, attrNode := range node.Child { attr[attrNode.Child[0].Token] = attrNode.Child[2].Result.(string) } - return Result{Result: attr} + node.Result = attr }) tstart = Seq("<", identifier, Cut(), attrs, ">") @@ -47,13 +45,12 @@ var ( ) func init() { - tag = Seq(tstart, Cut(), elements, tend).Map(func(node Result) Result { + tag = Seq(tstart, Cut(), elements, tend).Map(func(node *Result) { openTag := node.Child[0] - return Result{Result: htmlTag{ + node.Result = htmlTag{ Name: openTag.Child[1].Token, Attributes: openTag.Child[3].Result.(map[string]string), Body: node.Child[2].Result.([]interface{}), - }} - + } }) } diff --git a/html/html_test.go b/html/html_test.go index 7649fdd..79b0191 100644 --- a/html/html_test.go +++ b/html/html_test.go @@ -1,12 +1,15 @@ package html import ( + "os" "testing" "github.com/stretchr/testify/require" + "github.com/vektah/goparsify" ) func TestParse(t *testing.T) { + goparsify.EnableLogging(os.Stdout) result, err := parse(`<body>hello <p color="blue">world</p></body>`) require.NoError(t, err) require.Equal(t, htmlTag{Name: "body", Attributes: map[string]string{}, Body: []interface{}{ |