143 lines
3.4 KiB
Go
143 lines
3.4 KiB
Go
package hatmill
|
|
|
|
import "io"
|
|
|
|
//go:generate go run ./internal/codegen/codegen.go -input htmldefs.json -output html5/generated.go -package html5 -import
|
|
|
|
// Term represents a fragment of HTML markup, and is one of VoidElement, ParentElement, or Text.
|
|
type Term interface {
|
|
io.WriterTo
|
|
|
|
// isHtml is a no-op method to prevent external Term implementations, because
|
|
// Go doesn't have sum types.
|
|
isHtml()
|
|
}
|
|
|
|
// writeStringsTo attempts to write the strings in ss to w, incrementing n by the
|
|
// number of bytes written.
|
|
func writeStringsTo(w io.Writer, n *int64, ss ...string) error {
|
|
for _, s := range ss {
|
|
nCurr, err := io.WriteString(w, s)
|
|
*n += int64(nCurr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Attrib represents an HTML attribute.
|
|
type Attrib struct {
|
|
Key string
|
|
Value string
|
|
}
|
|
|
|
// WriteTo writes a to w as an HTML attribute in the form key="value", or
|
|
// simply key if value is empty. It returns the number of bytes written and any
|
|
// error encountered.
|
|
func (a Attrib) WriteTo(w io.Writer) (n int64, err error) {
|
|
err = writeStringsTo(w, &n, a.Key)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if a.Value != "" {
|
|
err = writeStringsTo(w, &n, "='", a.Value, "'")
|
|
}
|
|
return
|
|
}
|
|
|
|
// VoidElement represents a void HTML element, that is one that cannot have
|
|
// children.
|
|
type VoidElement struct {
|
|
TagName string
|
|
Attribs []Attrib
|
|
}
|
|
|
|
func (VoidElement) isHtml() {}
|
|
|
|
// WriteTo writes the HTML markup represented by e to w, returning the number
|
|
// of bytes written and any error encountered.
|
|
//
|
|
// See the warning about sanitization in the (Attrib).WriteTo documentation.
|
|
func (e VoidElement) WriteTo(w io.Writer) (n int64, err error) {
|
|
err = writeStringsTo(w, &n, "<", e.TagName)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
for _, attrib := range e.Attribs {
|
|
err = writeStringsTo(w, &n, " ")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var nCurr int64
|
|
nCurr, err = attrib.WriteTo(w)
|
|
n += int64(nCurr)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
err = writeStringsTo(w, &n, ">")
|
|
return
|
|
}
|
|
|
|
// ParentElement represents an HTML element that can have children.
|
|
type ParentElement struct {
|
|
VoidElement
|
|
Children []Term
|
|
}
|
|
|
|
func (e ParentElement) isHtml() {}
|
|
|
|
// WriteTo writes the HTML markup represented by e to w, returning the number
|
|
// of bytes written and any error encountered.
|
|
//
|
|
// See the warning about sanitization in the (Attrib).WriteTo documentation.
|
|
func (e ParentElement) WriteTo(w io.Writer) (n int64, err error) {
|
|
n, err = e.VoidElement.WriteTo(w)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
for _, child := range e.Children {
|
|
var nCurr int64
|
|
nCurr, err = child.WriteTo(w)
|
|
n += int64(nCurr)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
err = writeStringsTo(w, &n, "</", e.TagName, ">")
|
|
return
|
|
}
|
|
|
|
// Text represents an HTML text node.
|
|
type Text string
|
|
|
|
func (Text) isHtml() {}
|
|
|
|
// WriteTo writes the contents of t to w, returning the number of bytes written
|
|
// and any error encountered. It does not replace special characters with HTML
|
|
// entities; use html.EscapeString for this purpose.
|
|
func (t Text) WriteTo(w io.Writer) (n int64, err error) {
|
|
err = writeStringsTo(w, &n, string(t))
|
|
return
|
|
}
|
|
|
|
// 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) {
|
|
err = writeStringsTo(w, &n, "<!DOCTYPE html>")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var nroot int64
|
|
nroot, err = root.WriteTo(w)
|
|
n += nroot
|
|
return
|
|
}
|