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
|
package html5
|
||||||
|
|
||||||
import . "gitlab.codemonkeysoftware.net/b/hatmill"
|
import "gitlab.codemonkeysoftware.net/b/hatmill"
|
||||||
|
|
||||||
// Body creates a <body> element.
|
// Body creates a <body> element.
|
||||||
func Body(attribs ...Attrib) func(children ...Term) *ParentElement {
|
func Body(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return func(children ...Term) *ParentElement {
|
return func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return &ParentElement{
|
return &hatmill.ParentElement{
|
||||||
EmptyElement: EmptyElement{
|
EmptyElement: hatmill.EmptyElement{
|
||||||
TagName: "body",
|
TagName: "body",
|
||||||
Attribs: attribs,
|
Attribs: attribs,
|
||||||
},
|
},
|
||||||
@ -19,10 +19,10 @@ func Body(attribs ...Attrib) func(children ...Term) *ParentElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Div creates a <div> element.
|
// Div creates a <div> element.
|
||||||
func Div(attribs ...Attrib) func(children ...Term) *ParentElement {
|
func Div(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return func(children ...Term) *ParentElement {
|
return func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return &ParentElement{
|
return &hatmill.ParentElement{
|
||||||
EmptyElement: EmptyElement{
|
EmptyElement: hatmill.EmptyElement{
|
||||||
TagName: "div",
|
TagName: "div",
|
||||||
Attribs: attribs,
|
Attribs: attribs,
|
||||||
},
|
},
|
||||||
@ -32,10 +32,10 @@ func Div(attribs ...Attrib) func(children ...Term) *ParentElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Head creates a <head> element.
|
// Head creates a <head> element.
|
||||||
func Head(attribs ...Attrib) func(children ...Term) *ParentElement {
|
func Head(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return func(children ...Term) *ParentElement {
|
return func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return &ParentElement{
|
return &hatmill.ParentElement{
|
||||||
EmptyElement: EmptyElement{
|
EmptyElement: hatmill.EmptyElement{
|
||||||
TagName: "head",
|
TagName: "head",
|
||||||
Attribs: attribs,
|
Attribs: attribs,
|
||||||
},
|
},
|
||||||
@ -45,10 +45,10 @@ func Head(attribs ...Attrib) func(children ...Term) *ParentElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Html creates a <html> element.
|
// Html creates a <html> element.
|
||||||
func Html(attribs ...Attrib) func(children ...Term) *ParentElement {
|
func Html(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return func(children ...Term) *ParentElement {
|
return func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return &ParentElement{
|
return &hatmill.ParentElement{
|
||||||
EmptyElement: EmptyElement{
|
EmptyElement: hatmill.EmptyElement{
|
||||||
TagName: "html",
|
TagName: "html",
|
||||||
Attribs: attribs,
|
Attribs: attribs,
|
||||||
},
|
},
|
||||||
@ -58,18 +58,18 @@ func Html(attribs ...Attrib) func(children ...Term) *ParentElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Img creates a <img> element.
|
// Img creates a <img> element.
|
||||||
func Img(attribs ...Attrib) EmptyElement {
|
func Img(attribs ...hatmill.Attrib) hatmill.EmptyElement {
|
||||||
return EmptyElement{
|
return hatmill.EmptyElement{
|
||||||
TagName: "img",
|
TagName: "img",
|
||||||
Attribs: attribs,
|
Attribs: attribs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Li creates a <li> element.
|
// Li creates a <li> element.
|
||||||
func Li(attribs ...Attrib) func(children ...Term) *ParentElement {
|
func Li(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return func(children ...Term) *ParentElement {
|
return func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return &ParentElement{
|
return &hatmill.ParentElement{
|
||||||
EmptyElement: EmptyElement{
|
EmptyElement: hatmill.EmptyElement{
|
||||||
TagName: "li",
|
TagName: "li",
|
||||||
Attribs: attribs,
|
Attribs: attribs,
|
||||||
},
|
},
|
||||||
@ -79,10 +79,10 @@ func Li(attribs ...Attrib) func(children ...Term) *ParentElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Span creates a <span> element.
|
// Span creates a <span> element.
|
||||||
func Span(attribs ...Attrib) func(children ...Term) *ParentElement {
|
func Span(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return func(children ...Term) *ParentElement {
|
return func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return &ParentElement{
|
return &hatmill.ParentElement{
|
||||||
EmptyElement: EmptyElement{
|
EmptyElement: hatmill.EmptyElement{
|
||||||
TagName: "span",
|
TagName: "span",
|
||||||
Attribs: attribs,
|
Attribs: attribs,
|
||||||
},
|
},
|
||||||
@ -92,10 +92,10 @@ func Span(attribs ...Attrib) func(children ...Term) *ParentElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Title creates a <title> element.
|
// Title creates a <title> element.
|
||||||
func Title(attribs ...Attrib) func(children ...Term) *ParentElement {
|
func Title(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return func(children ...Term) *ParentElement {
|
return func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return &ParentElement{
|
return &hatmill.ParentElement{
|
||||||
EmptyElement: EmptyElement{
|
EmptyElement: hatmill.EmptyElement{
|
||||||
TagName: "title",
|
TagName: "title",
|
||||||
Attribs: attribs,
|
Attribs: attribs,
|
||||||
},
|
},
|
||||||
@ -105,10 +105,10 @@ func Title(attribs ...Attrib) func(children ...Term) *ParentElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ul creates a <ul> element.
|
// Ul creates a <ul> element.
|
||||||
func Ul(attribs ...Attrib) func(children ...Term) *ParentElement {
|
func Ul(attribs ...hatmill.Attrib) func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return func(children ...Term) *ParentElement {
|
return func(children ...hatmill.Term) *hatmill.ParentElement {
|
||||||
return &ParentElement{
|
return &hatmill.ParentElement{
|
||||||
EmptyElement: EmptyElement{
|
EmptyElement: hatmill.EmptyElement{
|
||||||
TagName: "ul",
|
TagName: "ul",
|
||||||
Attribs: attribs,
|
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",
|
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",
|
Key: "id",
|
||||||
Value: value,
|
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",
|
Key: "src",
|
||||||
Value: value,
|
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 {
|
func fileHeader(packageName string, needImport bool) string {
|
||||||
header := fmt.Sprintf(headerFmt, packageName)
|
header := fmt.Sprintf(headerFmt, packageName)
|
||||||
if needImport {
|
if needImport {
|
||||||
header += `import . "gitlab.codemonkeysoftware.net/b/hatmill"` + "\n"
|
header += `import "gitlab.codemonkeysoftware.net/b/hatmill"` + "\n"
|
||||||
}
|
}
|
||||||
return header
|
return header
|
||||||
}
|
}
|
||||||
@ -58,26 +58,32 @@ type AttribDef struct {
|
|||||||
Type AttribType `json:"type"`
|
Type AttribType `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (def AttribDef) String() string {
|
func (def AttribDef) Generate(qualified bool) string {
|
||||||
const (
|
const (
|
||||||
boolType = "bool"
|
boolType = "bool"
|
||||||
stringType = "string"
|
stringType = "string"
|
||||||
|
|
||||||
stringTemplate = `func %s(value string) Attrib {
|
stringTemplate = `// %[1]s creates a %[2]s attribute
|
||||||
return Attrib{
|
func %[1]s(value string) %[3]sAttrib {
|
||||||
Key: "%s",
|
return %[3]sAttrib{
|
||||||
|
Key: "%[2]s",
|
||||||
Value: value,
|
Value: value,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
boolTemplate = `func %s() Attrib {
|
boolTemplate = `// %[1]s creates a %[2]s attribute
|
||||||
return Attrib{
|
func %[1]s() %[3]sAttrib {
|
||||||
Key: "%s",
|
return %[3]sAttrib{
|
||||||
|
Key: "%[2]s",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var pkg string
|
||||||
|
if qualified {
|
||||||
|
pkg = "hatmill."
|
||||||
|
}
|
||||||
var template string
|
var template string
|
||||||
switch def.Type {
|
switch def.Type {
|
||||||
case Bool:
|
case Bool:
|
||||||
@ -85,10 +91,10 @@ func (def AttribDef) String() string {
|
|||||||
case String:
|
case String:
|
||||||
template = stringTemplate
|
template = stringTemplate
|
||||||
default:
|
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 {
|
type ElemDef struct {
|
||||||
@ -96,13 +102,13 @@ type ElemDef struct {
|
|||||||
Empty bool `json:"empty"`
|
Empty bool `json:"empty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (def ElemDef) String() string {
|
func (def ElemDef) Generate(qualified bool) string {
|
||||||
const (
|
const (
|
||||||
parentTemplate = `// %[1]s creates a <%[2]s> element.
|
parentTemplate = `// %[1]s creates a <%[2]s> element.
|
||||||
func %[1]s(attribs ...Attrib) func(children ...Term) *ParentElement {
|
func %[1]s(attribs ...%[3]sAttrib) func(children ...%[3]sTerm) *%[3]sParentElement {
|
||||||
return func(children ...Term) *ParentElement {
|
return func(children ...%[3]sTerm) *%[3]sParentElement {
|
||||||
return &ParentElement{
|
return &%[3]sParentElement{
|
||||||
EmptyElement: EmptyElement{
|
EmptyElement: %[3]sEmptyElement{
|
||||||
TagName: "%[2]s",
|
TagName: "%[2]s",
|
||||||
Attribs: attribs,
|
Attribs: attribs,
|
||||||
},
|
},
|
||||||
@ -112,8 +118,8 @@ func (def ElemDef) String() string {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
emptyTemplate = `// %[1]s creates a <%[2]s> element.
|
emptyTemplate = `// %[1]s creates a <%[2]s> element.
|
||||||
func %[1]s(attribs ...Attrib) EmptyElement {
|
func %[1]s(attribs ...%[3]sAttrib) %[3]sEmptyElement {
|
||||||
return EmptyElement{
|
return %[3]sEmptyElement{
|
||||||
TagName: "%[2]s",
|
TagName: "%[2]s",
|
||||||
Attribs: attribs,
|
Attribs: attribs,
|
||||||
}
|
}
|
||||||
@ -121,11 +127,15 @@ func (def ElemDef) String() string {
|
|||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var pkg string
|
||||||
|
if qualified {
|
||||||
|
pkg = "hatmill."
|
||||||
|
}
|
||||||
template := parentTemplate
|
template := parentTemplate
|
||||||
if def.Empty {
|
if def.Empty {
|
||||||
template = emptyTemplate
|
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 {
|
type Defs struct {
|
||||||
@ -154,10 +164,10 @@ func main() {
|
|||||||
output := new(bytes.Buffer)
|
output := new(bytes.Buffer)
|
||||||
output.WriteString(fileHeader(*packageName, *addImport))
|
output.WriteString(fileHeader(*packageName, *addImport))
|
||||||
for _, elemDef := range defs.Elements {
|
for _, elemDef := range defs.Elements {
|
||||||
output.WriteString(elemDef.String())
|
output.WriteString(elemDef.Generate(*addImport))
|
||||||
}
|
}
|
||||||
for _, attribDef := range defs.Attributes {
|
for _, attribDef := range defs.Attributes {
|
||||||
output.WriteString(attribDef.String())
|
output.WriteString(attribDef.Generate(*addImport))
|
||||||
}
|
}
|
||||||
|
|
||||||
formatted, err := format.Source(output.Bytes())
|
formatted, err := format.Source(output.Bytes())
|
||||||
|
Loading…
Reference in New Issue
Block a user