Added attribute function generator and more element defs
This commit is contained in:
parent
8517cab0f5
commit
328e865231
22
attribs.go
Normal file
22
attribs.go
Normal 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
3
attribs.txt
Normal file
@ -0,0 +1,3 @@
|
||||
id string
|
||||
disabled bool
|
||||
src string
|
44
elements.go
44
elements.go
@ -3,6 +3,39 @@
|
||||
|
||||
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 {
|
||||
return func(children ...Term) *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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
html parent
|
||||
head parent
|
||||
body parent
|
||||
div parent
|
||||
img empty
|
||||
span parent
|
||||
html parent
|
||||
|
@ -1,17 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
h "gitlab.codemonkeysoftware.com/b/hatmill"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
document := h.Html()(
|
||||
h.Head()(),
|
||||
h.Body()(
|
||||
h.Div()(
|
||||
h.Img(h.Id("profile-photo")),
|
||||
h.Img(h.Src("./me.jpg"), h.Id("profile-photo")),
|
||||
h.Text("hello hatmill!"),
|
||||
h.Div(h.Disabled())(),
|
||||
),
|
||||
),
|
||||
)
|
||||
h.WriteDocument(os.Stdout, document)
|
||||
}
|
||||
|
35
hatmill.go
35
hatmill.go
@ -3,6 +3,7 @@ package hatmill
|
||||
import "io"
|
||||
|
||||
//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.
|
||||
type Term interface {
|
||||
@ -108,40 +109,6 @@ func (t Text) WriteTo(w io.Writer) (n int64, err error) {
|
||||
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.
|
||||
// root should probably be an <html> element.
|
||||
func WriteDocument(w io.Writer, root *ParentElement) (n int64, err error) {
|
||||
|
@ -131,7 +131,7 @@ func TestParentElement(t *testing.T) {
|
||||
|
||||
properties.Property("WriteTo writes element correctly without children", prop.ForAll(
|
||||
func(tagName string, attribs []hatmill.Attrib) bool {
|
||||
elem := &hatmill.ParentElement {
|
||||
elem := &hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
TagName: tagName,
|
||||
Attribs: attribs,
|
||||
@ -148,7 +148,7 @@ func TestParentElement(t *testing.T) {
|
||||
|
||||
properties.Property("WriteTo writes element correctly with children", prop.ForAll(
|
||||
func(tagName string, attribs []hatmill.Attrib, children []hatmill.Term) bool {
|
||||
elem := &hatmill.ParentElement {
|
||||
elem := &hatmill.ParentElement{
|
||||
EmptyElement: hatmill.EmptyElement{
|
||||
TagName: tagName,
|
||||
Attribs: attribs,
|
||||
|
87
internal/attribgen/main.go
Normal file
87
internal/attribgen/main.go
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user