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:
<script>launchMissiles();</script>
+} diff --git a/html5/html5.go b/html5/html5.go deleted file mode 100644 index 01186ea..0000000 --- a/html5/html5.go +++ /dev/null @@ -1,19 +0,0 @@ -package html5 - -//go:generate go run ../internal/codegen/codegen.go -input defs.json -output generated.go -package html5 - -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, - } -} - -// Text converts a string to a hatmill.Text. Text(s) is functionally identical -// to hatmill.Text(s) and is reproduced here for convenience. -func Text(s string) hatmill.Text { - return hatmill.Text(s) -} diff --git a/html5/html5_test.go b/html5/html5_test.go deleted file mode 100644 index 23232a4..0000000 --- a/html5/html5_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package html5_test - -import ( - "gitlab.codemonkeysoftware.net/b/hatmill" - html5 "gitlab.codemonkeysoftware.net/b/hatmill/html5" - - "html" - "os" -) - -func Example() { - userInput := "" - - 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.CustomData("coolness", "awesome"))(), - ), - ), - ) - hatmill.WriteDocument(os.Stdout, document) - // Output:
<script>launchMissiles();</script>
-} diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 19df4e5..b9dc1b6 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -133,10 +133,23 @@ type Defs struct { Elements []ElemDef `json:"elements"` } +func writeFormatted(fileName, packageName string, writeDefs func(*bytes.Buffer)) error { + src := new(bytes.Buffer) + src.WriteString(fileHeader(packageName)) + writeDefs(src) + formatted, err := format.Source(src.Bytes()) + if err != nil { + return err + } + return ioutil.WriteFile(fileName, formatted, 0644) +} + func main() { inputPath := flag.String("input", "", "JSON input file") - outputPath := flag.String("output", "", ".go output file") - packageName := flag.String("package", "", "output package name") + elemPath := flag.String("elemfile", "", "generated element .go file") + elemPkg := flag.String("elempkg", "", "generated element package name") + attribPath := flag.String("attribfile", "", "generated attribute .go file") + attribPkg := flag.String("attribpkg", "", "generated attribute package name") flag.Parse() input, err := ioutil.ReadFile(*inputPath) @@ -150,22 +163,21 @@ func main() { log.Fatal(err) } - output := new(bytes.Buffer) - output.WriteString(fileHeader(*packageName)) - for _, elemDef := range defs.Elements { - output.WriteString(elemDef.Generate()) - } - for _, attribDef := range defs.Attributes { - output.WriteString(attribDef.Generate()) - } + err = writeFormatted(*attribPath, *attribPkg, func(buf *bytes.Buffer) { + for _, attribDef := range defs.Attributes { + buf.WriteString(attribDef.Generate()) + } + }) + if err != nil { + log.Fatal(err) + } - formatted, err := format.Source(output.Bytes()) - if err != nil { - log.Fatal(err) - } - - err = ioutil.WriteFile(*outputPath, formatted, 0644) - if err != nil { - log.Fatal(err) - } + err = writeFormatted(*elemPath, *elemPkg, func(buf *bytes.Buffer) { + for _, elemDef := range defs.Elements { + buf.WriteString(elemDef.Generate()) + } + }) + if err != nil { + log.Fatal(err) + } }