Added attribute function generator and more element defs

This commit is contained in:
Brandon Dyck 2019-03-21 21:49:26 -06:00
parent 8517cab0f5
commit 328e865231
9 changed files with 284 additions and 178 deletions

22
attribs.go Normal file
View File

@ -0,0 +1,22 @@
// GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/internal/attribgen
// DO NOT EDIT!
package hatmill
func Id(value string) Attrib {
return Attrib{
Key: "id",
Value: value,
}
}
func Disabled() Attrib {
return Attrib{
Key: "disabled",
}
}
func Src(value string) Attrib {
return Attrib{
Key: "src",
Value: value,
}
}

3
attribs.txt Normal file
View File

@ -0,0 +1,3 @@
id string
disabled bool
src string

View File

@ -3,6 +3,39 @@
package hatmill package hatmill
func Html(attribs ...Attrib) func(children ...Term) *ParentElement {
return func(children ...Term) *ParentElement {
return &ParentElement{
EmptyElement: EmptyElement{
TagName: "html",
Attribs: attribs,
},
Children: children,
}
}
}
func Head(attribs ...Attrib) func(children ...Term) *ParentElement {
return func(children ...Term) *ParentElement {
return &ParentElement{
EmptyElement: EmptyElement{
TagName: "head",
Attribs: attribs,
},
Children: children,
}
}
}
func Body(attribs ...Attrib) func(children ...Term) *ParentElement {
return func(children ...Term) *ParentElement {
return &ParentElement{
EmptyElement: EmptyElement{
TagName: "body",
Attribs: attribs,
},
Children: children,
}
}
}
func Div(attribs ...Attrib) func(children ...Term) *ParentElement { func Div(attribs ...Attrib) func(children ...Term) *ParentElement {
return func(children ...Term) *ParentElement { return func(children ...Term) *ParentElement {
return &ParentElement{ return &ParentElement{
@ -31,14 +64,3 @@ func Span(attribs ...Attrib) func(children ...Term) *ParentElement {
} }
} }
} }
func Html(attribs ...Attrib) func(children ...Term) *ParentElement {
return func(children ...Term) *ParentElement {
return &ParentElement{
EmptyElement: EmptyElement{
TagName: "html",
Attribs: attribs,
},
Children: children,
}
}
}

View File

@ -1,4 +1,6 @@
html parent
head parent
body parent
div parent div parent
img empty img empty
span parent span parent
html parent

View File

@ -1,17 +1,20 @@
package main package main
import ( import (
"os"
h "gitlab.codemonkeysoftware.com/b/hatmill" h "gitlab.codemonkeysoftware.com/b/hatmill"
"os"
) )
func main() { func main() {
document := h.Html()( document := h.Html()(
h.Head()(),
h.Body()(
h.Div()( h.Div()(
h.Img(h.Id("profile-photo")), h.Img(h.Src("./me.jpg"), h.Id("profile-photo")),
h.Text("hello hatmill!"), h.Text("hello hatmill!"),
h.Div(h.Disabled())(), h.Div(h.Disabled())(),
), ),
),
) )
h.WriteDocument(os.Stdout, document) h.WriteDocument(os.Stdout, document)
} }

View File

@ -3,6 +3,7 @@ package hatmill
import "io" import "io"
//go:generate go run ./internal/elementgen/main.go //go:generate go run ./internal/elementgen/main.go
//go:generate go run ./internal/attribgen/main.go
// Term represents a fragment of HTML markup, and is one of EmptyElement, ParentElement, or Text. // Term represents a fragment of HTML markup, and is one of EmptyElement, ParentElement, or Text.
type Term interface { type Term interface {
@ -108,40 +109,6 @@ func (t Text) WriteTo(w io.Writer) (n int64, err error) {
return return
} }
// Html5 functions
// func Div(attribs ...Attrib) func(children ...Term) *ParentElement {
// return func(children ...Term) *ParentElement {
// return &ParentElement{
// EmptyElement: EmptyElement{
// TagName: "div",
// Attribs: attribs,
// },
// Children: children,
// }
// }
// }
//
// func Img(attribs ...Attrib) EmptyElement {
// return EmptyElement{
// TagName: "img",
// Attribs: attribs,
// }
// }
func Id(id string) Attrib {
return Attrib{
Key: "id",
Value: id,
}
}
func Disabled() Attrib {
return Attrib{
Key: "disabled",
}
}
// WriteDocument writes an HTML5 doctype declaration, followed by root. // WriteDocument writes an HTML5 doctype declaration, followed by root.
// root should probably be an <html> element. // root should probably be an <html> element.
func WriteDocument(w io.Writer, root *ParentElement) (n int64, err error) { func WriteDocument(w io.Writer, root *ParentElement) (n int64, err error) {

View File

@ -131,7 +131,7 @@ func TestParentElement(t *testing.T) {
properties.Property("WriteTo writes element correctly without children", prop.ForAll( properties.Property("WriteTo writes element correctly without children", prop.ForAll(
func(tagName string, attribs []hatmill.Attrib) bool { func(tagName string, attribs []hatmill.Attrib) bool {
elem := &hatmill.ParentElement { elem := &hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ EmptyElement: hatmill.EmptyElement{
TagName: tagName, TagName: tagName,
Attribs: attribs, Attribs: attribs,
@ -148,7 +148,7 @@ func TestParentElement(t *testing.T) {
properties.Property("WriteTo writes element correctly with children", prop.ForAll( properties.Property("WriteTo writes element correctly with children", prop.ForAll(
func(tagName string, attribs []hatmill.Attrib, children []hatmill.Term) bool { func(tagName string, attribs []hatmill.Attrib, children []hatmill.Term) bool {
elem := &hatmill.ParentElement { elem := &hatmill.ParentElement{
EmptyElement: hatmill.EmptyElement{ EmptyElement: hatmill.EmptyElement{
TagName: tagName, TagName: tagName,
Attribs: attribs, Attribs: attribs,

View File

@ -0,0 +1,87 @@
package main
import (
"bufio"
"bytes"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"strings"
)
const (
inputPath = "attribs.txt"
outputPath = "attribs.go"
packageName = "hatmill"
boolType = "bool"
stringType = "string"
stringTemplate = `func %s(value string) Attrib {
return Attrib{
Key: "%s",
Value: value,
}
}
`
boolTemplate = `func %s() Attrib {
return Attrib{
Key: "%s",
}
}
`
)
func main() {
inputFile, err := os.Open(inputPath)
if err != nil {
log.Fatal(err)
}
defer inputFile.Close()
var output bytes.Buffer
fmt.Fprintln(&output, "// GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/internal/attribgen")
fmt.Fprintln(&output, "// DO NOT EDIT!\n")
fmt.Fprintln(&output, "package ", packageName, "\n")
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
var attribName, attribType string
_, err = fmt.Sscanf(line, "%s %s", &attribName, &attribType)
if err != nil {
log.Fatalf("error parsing input line: %s", err)
}
var template string
switch attribType {
case boolType:
template = boolTemplate
case stringType:
template = stringTemplate
default:
log.Fatal("unknown attribute type: ", attribType)
}
fmt.Fprintf(&output, template, strings.Title(attribName), attribName)
}
if err := scanner.Err(); err != nil {
log.Fatalf("error scanning input: %s", err)
}
formatted, err := format.Source(output.Bytes())
if err != nil {
log.Fatalf("error formatting output: %s", err)
}
err = ioutil.WriteFile(outputPath, formatted, 0644)
if err != nil {
log.Fatalf("error writing output: %s", err)
}
}