Moved example, Data, and Text into html5 package
This commit is contained in:
parent
239ffa7ac2
commit
5b8983eea2
@ -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)
|
||||
}
|
@ -3,13 +3,13 @@
|
||||
|
||||
package html5
|
||||
|
||||
import . "gitlab.codemonkeysoftware.net/b/hatmill"
|
||||
import "gitlab.codemonkeysoftware.net/b/hatmill"
|
||||
|
||||
// Body creates a <body> 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 <div> 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 <head> 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 <html> 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 <img> 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 <li> 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 <span> 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 <title> 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,
|
||||
}
|
||||
|
26
html5/html5_test.go
Normal file
26
html5/html5_test.go
Normal file
@ -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>
|
||||
}
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user