Moved example, Data, and Text into html5 package
This commit is contained in:
@ -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())
|
||||
|
Reference in New Issue
Block a user