diff --git a/attribute/customdata.go b/attribute/customdata.go new file mode 100644 index 0000000..ee7c542 --- /dev/null +++ b/attribute/customdata.go @@ -0,0 +1,11 @@ +package attribute + +import "gitlab.codemonkeysoftware.net/b/hatmill" + +// CustomData creates an attribute of the form data-suffix='value'. +func CustomData(suffix, value string) hatmill.Attrib { + return hatmill.Attrib{ + Key: "data-" + suffix, + Value: value, + } +} diff --git a/attribute/generated.go b/attribute/generated.go new file mode 100644 index 0000000..0e28556 --- /dev/null +++ b/attribute/generated.go @@ -0,0 +1,29 @@ +// GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/internal/codegen +// DO NOT EDIT! + +package attribute + +import "gitlab.codemonkeysoftware.net/b/hatmill" + +// Disabled creates a "disabled" attribute +func Disabled() hatmill.Attrib { + return hatmill.Attrib{ + Key: "disabled", + } +} + +// Id creates a "id" attribute +func Id(value string) hatmill.Attrib { + return hatmill.Attrib{ + Key: "id", + Value: value, + } +} + +// Src creates a "src" attribute +func Src(value string) hatmill.Attrib { + return hatmill.Attrib{ + Key: "src", + Value: value, + } +} diff --git a/html5/defs.json b/defs.json similarity index 100% rename from html5/defs.json rename to defs.json diff --git a/html5/generated.go b/element/generated.go similarity index 98% rename from html5/generated.go rename to element/generated.go index affa45e..fe81e56 100644 --- a/html5/generated.go +++ b/element/generated.go @@ -1,7 +1,7 @@ // GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/internal/codegen // DO NOT EDIT! -package html5 +package element import "gitlab.codemonkeysoftware.net/b/hatmill" @@ -1416,26 +1416,3 @@ func Wbr(attribs ...hatmill.Attrib) hatmill.VoidElement { Attribs: attribs, } } - -// Disabled creates a "disabled" attribute -func Disabled() hatmill.Attrib { - return hatmill.Attrib{ - Key: "disabled", - } -} - -// Id creates a "id" attribute -func Id(value string) hatmill.Attrib { - return hatmill.Attrib{ - Key: "id", - Value: value, - } -} - -// Src creates a "src" attribute -func Src(value string) hatmill.Attrib { - return hatmill.Attrib{ - Key: "src", - Value: value, - } -} diff --git a/hatmill.go b/hatmill.go index 2890b65..a80e0f7 100644 --- a/hatmill.go +++ b/hatmill.go @@ -1,5 +1,7 @@ package hatmill + +//go:generate go run internal/codegen/codegen.go -input defs.json -elemfile element/generated.go -elempkg element -attribfile attribute/generated.go -attribpkg attribute import "io" // Term represents a fragment of HTML markup, and is one of VoidElement, ParentElement, or Text. diff --git a/hatmill_test.go b/hatmill_test.go index 493d8b7..a5a250b 100644 --- a/hatmill_test.go +++ b/hatmill_test.go @@ -3,7 +3,9 @@ package hatmill_test import ( "bytes" "fmt" + "html" "io" + "os" "reflect" "strings" "testing" @@ -12,6 +14,8 @@ import ( "github.com/leanovate/gopter/gen" "github.com/leanovate/gopter/prop" "gitlab.codemonkeysoftware.net/b/hatmill" + ha "gitlab.codemonkeysoftware.net/b/hatmill/attribute" + he "gitlab.codemonkeysoftware.net/b/hatmill/element" ) type NopWriter struct{} @@ -173,3 +177,20 @@ func TestParentElement(t *testing.T) { properties.TestingRun(t) } + +func Example() { + userInput := "" + + document := he.Html()( + he.Head()(), + he.Body()( + he.Div()( + he.Img(ha.Src("./me.jpg"), ha.Id("profile-photo")), + hatmill.Text(html.EscapeString(userInput)), + he.Div(ha.Disabled(), ha.CustomData("coolness", "awesome"))(), + ), + ), + ) + hatmill.WriteDocument(os.Stdout, document) + // Output: