diff --git a/example/example.go b/example/example.go deleted file mode 100644 index e22ca84..0000000 --- a/example/example.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "gitlab.codemonkeysoftware.net/b/hatmill" - h "gitlab.codemonkeysoftware.net/b/hatmill/html5" - "os" -) - -func main() { - document := h.Html()( - h.Head()(), - h.Body()( - h.Div()( - h.Img(h.Src("./me.jpg"), h.Id("profile-photo")), - h.Text("hello hatmill!"), - h.Div(h.Disabled(), h.Data("coolness", "awesome"))(), - ), - ), - ) - hatmill.WriteDocument(os.Stdout, document) -} diff --git a/html5/generated.go b/html5/generated.go index a40339d..ae26696 100644 --- a/html5/generated.go +++ b/html5/generated.go @@ -3,13 +3,13 @@ package html5 -import . "gitlab.codemonkeysoftware.net/b/hatmill" +import "gitlab.codemonkeysoftware.net/b/hatmill" // Body creates a element. -func Body(attribs ...Attrib) func(children ...Term) *ParentElement { - return func(children ...Term) *ParentElement { - return &ParentElement{ - EmptyElement: EmptyElement{ +func Body(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement { + return func(children ...hatmill.Term) *hatmill.ParentElement { + return &hatmill.ParentElement{ + EmptyElement: hatmill.EmptyElement{ TagName: "body", Attribs: attribs, }, @@ -19,10 +19,10 @@ func Body(attribs ...Attrib) func(children ...Term) *ParentElement { } // Div creates a
element. -func Div(attribs ...Attrib) func(children ...Term) *ParentElement { - return func(children ...Term) *ParentElement { - return &ParentElement{ - EmptyElement: EmptyElement{ +func Div(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement { + return func(children ...hatmill.Term) *hatmill.ParentElement { + return &hatmill.ParentElement{ + EmptyElement: hatmill.EmptyElement{ TagName: "div", Attribs: attribs, }, @@ -32,10 +32,10 @@ func Div(attribs ...Attrib) func(children ...Term) *ParentElement { } // Head creates a element. -func Head(attribs ...Attrib) func(children ...Term) *ParentElement { - return func(children ...Term) *ParentElement { - return &ParentElement{ - EmptyElement: EmptyElement{ +func Head(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement { + return func(children ...hatmill.Term) *hatmill.ParentElement { + return &hatmill.ParentElement{ + EmptyElement: hatmill.EmptyElement{ TagName: "head", Attribs: attribs, }, @@ -45,10 +45,10 @@ func Head(attribs ...Attrib) func(children ...Term) *ParentElement { } // Html creates a element. -func Html(attribs ...Attrib) func(children ...Term) *ParentElement { - return func(children ...Term) *ParentElement { - return &ParentElement{ - EmptyElement: EmptyElement{ +func Html(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement { + return func(children ...hatmill.Term) *hatmill.ParentElement { + return &hatmill.ParentElement{ + EmptyElement: hatmill.EmptyElement{ TagName: "html", Attribs: attribs, }, @@ -58,18 +58,18 @@ func Html(attribs ...Attrib) func(children ...Term) *ParentElement { } // Img creates a element. -func Img(attribs ...Attrib) EmptyElement { - return EmptyElement{ +func Img(attribs ...hatmill.Attrib) hatmill.EmptyElement { + return hatmill.EmptyElement{ TagName: "img", Attribs: attribs, } } // Li creates a
  • element. -func Li(attribs ...Attrib) func(children ...Term) *ParentElement { - return func(children ...Term) *ParentElement { - return &ParentElement{ - EmptyElement: EmptyElement{ +func Li(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement { + return func(children ...hatmill.Term) *hatmill.ParentElement { + return &hatmill.ParentElement{ + EmptyElement: hatmill.EmptyElement{ TagName: "li", Attribs: attribs, }, @@ -79,10 +79,10 @@ func Li(attribs ...Attrib) func(children ...Term) *ParentElement { } // Span creates a element. -func Span(attribs ...Attrib) func(children ...Term) *ParentElement { - return func(children ...Term) *ParentElement { - return &ParentElement{ - EmptyElement: EmptyElement{ +func Span(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement { + return func(children ...hatmill.Term) *hatmill.ParentElement { + return &hatmill.ParentElement{ + EmptyElement: hatmill.EmptyElement{ TagName: "span", Attribs: attribs, }, @@ -92,10 +92,10 @@ func Span(attribs ...Attrib) func(children ...Term) *ParentElement { } // Title creates a element. -func Title(attribs ...Attrib) func(children ...Term) *ParentElement { - return func(children ...Term) *ParentElement { - return &ParentElement{ - EmptyElement: EmptyElement{ +func Title(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement { + return func(children ...hatmill.Term) *hatmill.ParentElement { + return &hatmill.ParentElement{ + EmptyElement: hatmill.EmptyElement{ TagName: "title", Attribs: attribs, }, @@ -105,10 +105,10 @@ func Title(attribs ...Attrib) func(children ...Term) *ParentElement { } // Ul creates a <ul> element. -func Ul(attribs ...Attrib) func(children ...Term) *ParentElement { - return func(children ...Term) *ParentElement { - return &ParentElement{ - EmptyElement: EmptyElement{ +func Ul(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement { + return func(children ...hatmill.Term) *hatmill.ParentElement { + return &hatmill.ParentElement{ + EmptyElement: hatmill.EmptyElement{ TagName: "ul", Attribs: attribs, }, @@ -116,19 +116,25 @@ func Ul(attribs ...Attrib) func(children ...Term) *ParentElement { } } } -func Disabled() Attrib { - return Attrib{ + +// Disabled creates a disabled attribute +func Disabled() hatmill.Attrib { + return hatmill.Attrib{ Key: "disabled", } } -func Id(value string) Attrib { - return Attrib{ + +// Id creates a id attribute +func Id(value string) hatmill.Attrib { + return hatmill.Attrib{ Key: "id", Value: value, } } -func Src(value string) Attrib { - return Attrib{ + +// Src creates a src attribute +func Src(value string) hatmill.Attrib { + return hatmill.Attrib{ Key: "src", Value: value, } diff --git a/html5/html5_test.go b/html5/html5_test.go new file mode 100644 index 0000000..7750007 --- /dev/null +++ b/html5/html5_test.go @@ -0,0 +1,26 @@ +package html5_test + +import ( + "gitlab.codemonkeysoftware.net/b/hatmill" + html5 "gitlab.codemonkeysoftware.net/b/hatmill/html5" + + "html" + "os" +) + +func Example() { + userInput := "<script>launchMissiles();</script>" + + document := html5.Html()( + html5.Head()(), + html5.Body()( + html5.Div()( + html5.Img(html5.Src("./me.jpg"), html5.Id("profile-photo")), + html5.Text(html.EscapeString(userInput)), + html5.Div(html5.Disabled(), html5.Data("coolness", "awesome"))(), + ), + ), + ) + hatmill.WriteDocument(os.Stdout, document) + // Output: <!DOCTYPE html><html><head></head><body><div><img src='./me.jpg' id='profile-photo'><script>launchMissiles();</script><div disabled data-coolness='awesome'></div></div></body></html> +} diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 9f8e582..854d2c1 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -20,7 +20,7 @@ package %s func fileHeader(packageName string, needImport bool) string { header := fmt.Sprintf(headerFmt, packageName) if needImport { - header += `import . "gitlab.codemonkeysoftware.net/b/hatmill"` + "\n" + header += `import "gitlab.codemonkeysoftware.net/b/hatmill"` + "\n" } return header } @@ -58,26 +58,32 @@ type AttribDef struct { Type AttribType `json:"type"` } -func (def AttribDef) String() string { +func (def AttribDef) Generate(qualified bool) string { const ( boolType = "bool" stringType = "string" - stringTemplate = `func %s(value string) Attrib { - return Attrib{ - Key: "%s", + stringTemplate = `// %[1]s creates a %[2]s attribute + func %[1]s(value string) %[3]sAttrib { + return %[3]sAttrib{ + Key: "%[2]s", Value: value, } } ` - boolTemplate = `func %s() Attrib { - return Attrib{ - Key: "%s", + boolTemplate = `// %[1]s creates a %[2]s attribute + func %[1]s() %[3]sAttrib { + return %[3]sAttrib{ + Key: "%[2]s", } } ` ) + var pkg string + if qualified { + pkg = "hatmill." + } var template string switch def.Type { case Bool: @@ -85,10 +91,10 @@ func (def AttribDef) String() string { case String: template = stringTemplate default: - panic(fmt.Errorf("unknown attribute type: %s", def.Type)) + panic(fmt.Errorf("unknown attribute type: %v", def.Type)) } - return fmt.Sprintf(template, strings.Title(def.Name), def.Name) + return fmt.Sprintf(template, strings.Title(def.Name), def.Name, pkg) } type ElemDef struct { @@ -96,13 +102,13 @@ type ElemDef struct { Empty bool `json:"empty"` } -func (def ElemDef) String() string { +func (def ElemDef) Generate(qualified bool) string { const ( parentTemplate = `// %[1]s creates a <%[2]s> element. - func %[1]s(attribs ...Attrib) func(children ...Term) *ParentElement { - return func(children ...Term) *ParentElement { - return &ParentElement{ - EmptyElement: EmptyElement{ + func %[1]s(attribs ...%[3]sAttrib) func(children ...%[3]sTerm) *%[3]sParentElement { + return func(children ...%[3]sTerm) *%[3]sParentElement { + return &%[3]sParentElement{ + EmptyElement: %[3]sEmptyElement{ TagName: "%[2]s", Attribs: attribs, }, @@ -112,8 +118,8 @@ func (def ElemDef) String() string { } ` emptyTemplate = `// %[1]s creates a <%[2]s> element. - func %[1]s(attribs ...Attrib) EmptyElement { - return EmptyElement{ + func %[1]s(attribs ...%[3]sAttrib) %[3]sEmptyElement { + return %[3]sEmptyElement{ TagName: "%[2]s", Attribs: attribs, } @@ -121,11 +127,15 @@ func (def ElemDef) String() string { ` ) + var pkg string + if qualified { + pkg = "hatmill." + } template := parentTemplate if def.Empty { template = emptyTemplate } - return fmt.Sprintf(template, strings.Title(def.Name), def.Name) + return fmt.Sprintf(template, strings.Title(def.Name), def.Name, pkg) } type Defs struct { @@ -154,10 +164,10 @@ func main() { output := new(bytes.Buffer) output.WriteString(fileHeader(*packageName, *addImport)) for _, elemDef := range defs.Elements { - output.WriteString(elemDef.String()) + output.WriteString(elemDef.Generate(*addImport)) } for _, attribDef := range defs.Attributes { - output.WriteString(attribDef.String()) + output.WriteString(attribDef.Generate(*addImport)) } formatted, err := format.Source(output.Bytes())