Rearranged generated code

This commit is contained in:
Brandon Dyck 2019-04-02 21:42:16 -06:00
parent 94a7c075c7
commit e43b8aa6ee
9 changed files with 95 additions and 88 deletions

11
attribute/customdata.go Normal file
View File

@ -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,
}
}

29
attribute/generated.go Normal file
View File

@ -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,
}
}

View File

@ -1,7 +1,7 @@
// GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/internal/codegen // GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/internal/codegen
// DO NOT EDIT! // DO NOT EDIT!
package html5 package element
import "gitlab.codemonkeysoftware.net/b/hatmill" import "gitlab.codemonkeysoftware.net/b/hatmill"
@ -1416,26 +1416,3 @@ func Wbr(attribs ...hatmill.Attrib) hatmill.VoidElement {
Attribs: attribs, 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,
}
}

View File

@ -1,5 +1,7 @@
package hatmill 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" import "io"
// Term represents a fragment of HTML markup, and is one of VoidElement, ParentElement, or Text. // Term represents a fragment of HTML markup, and is one of VoidElement, ParentElement, or Text.

View File

@ -3,7 +3,9 @@ package hatmill_test
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"html"
"io" "io"
"os"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
@ -12,6 +14,8 @@ import (
"github.com/leanovate/gopter/gen" "github.com/leanovate/gopter/gen"
"github.com/leanovate/gopter/prop" "github.com/leanovate/gopter/prop"
"gitlab.codemonkeysoftware.net/b/hatmill" "gitlab.codemonkeysoftware.net/b/hatmill"
ha "gitlab.codemonkeysoftware.net/b/hatmill/attribute"
he "gitlab.codemonkeysoftware.net/b/hatmill/element"
) )
type NopWriter struct{} type NopWriter struct{}
@ -173,3 +177,20 @@ func TestParentElement(t *testing.T) {
properties.TestingRun(t) properties.TestingRun(t)
} }
func Example() {
userInput := "<script>launchMissiles();</script>"
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: <!DOCTYPE html><html><head></head><body><div><img src='./me.jpg' id='profile-photo'>&lt;script&gt;launchMissiles();&lt;/script&gt;<div disabled data-coolness='awesome'></div></div></body></html>
}

View File

@ -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)
}

View File

@ -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 := "<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.CustomData("coolness", "awesome"))(),
),
),
)
hatmill.WriteDocument(os.Stdout, document)
// Output: <!DOCTYPE html><html><head></head><body><div><img src='./me.jpg' id='profile-photo'>&lt;script&gt;launchMissiles();&lt;/script&gt;<div disabled data-coolness='awesome'></div></div></body></html>
}

View File

@ -133,10 +133,23 @@ type Defs struct {
Elements []ElemDef `json:"elements"` 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() { func main() {
inputPath := flag.String("input", "", "JSON input file") inputPath := flag.String("input", "", "JSON input file")
outputPath := flag.String("output", "", ".go output file") elemPath := flag.String("elemfile", "", "generated element .go file")
packageName := flag.String("package", "", "output package name") 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() flag.Parse()
input, err := ioutil.ReadFile(*inputPath) input, err := ioutil.ReadFile(*inputPath)
@ -150,22 +163,21 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
output := new(bytes.Buffer) err = writeFormatted(*attribPath, *attribPkg, func(buf *bytes.Buffer) {
output.WriteString(fileHeader(*packageName)) for _, attribDef := range defs.Attributes {
for _, elemDef := range defs.Elements { buf.WriteString(attribDef.Generate())
output.WriteString(elemDef.Generate()) }
} })
for _, attribDef := range defs.Attributes { if err != nil {
output.WriteString(attribDef.Generate()) log.Fatal(err)
} }
formatted, err := format.Source(output.Bytes()) err = writeFormatted(*elemPath, *elemPkg, func(buf *bytes.Buffer) {
if err != nil { for _, elemDef := range defs.Elements {
log.Fatal(err) buf.WriteString(elemDef.Generate())
} }
})
err = ioutil.WriteFile(*outputPath, formatted, 0644) if err != nil {
if err != nil { log.Fatal(err)
log.Fatal(err) }
}
} }