hatmill

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2020 License: MIT Imports: 4 Imported by: 0

README

hatmill - HTML generation DSL for Go

License Gitlab pipeline status Go Report Card GoDoc Badge count

hatmill provides a simple set of types and helper functions for writing HTML in plain Go code, without having to deal with any template languages. It is not spectacularly fast, but is comparable in simple cases to the html/template package; run go test -bench=. -benchmem for proof. hatmill “templates” are arguably easier to read and write than many template languages.

hatmill uses semantic versioning.

Installation

There are three necessary packages:

  • gitlab.codemonkeysoftware.net/b/hatmill
  • gitlab.codemonkeysoftware.net/b/hatmill/attribute
  • gitlab.codemonkeysoftware.net/b/hatmill/element

Install them as you would any other Go package, with go get or whatever. gitlab.codemonkeysoftware.net/b/hatmill is a Go module and so will play nicely with Go ≥ 1.11.

Usage

Basic types are in gitlab.codemonkeysoftware.net/b/hatmill. The attribute and element subpackages contain helper functions for HTML5 attributes and elements. See the Example function in hatmill_test.go.

Hacking

If there is a missing attribute or element helper function, describe it in defs.json and run go generate in the repository root. If you can't figure out what you need to know by reading defs.json, internal/codegen/*, attribute/*, and element/*, then pester me to improve the documentation.

Documentation

Overview

Example
package main

import (
	"os"

	"gitlab.codemonkeysoftware.net/b/hatmill"

	ha "gitlab.codemonkeysoftware.net/b/hatmill/attribute"

	he "gitlab.codemonkeysoftware.net/b/hatmill/element"
)

func main() {
	userInput := "<script>launchMissiles();</script>"

	document := he.Html()(
		he.Body()(
			he.Img(ha.Src("./photo.jpg"), ha.Contenteditable(true)),
			hatmill.Text(userInput),
			he.Div(ha.Disabled(true), ha.CustomData("coolness", "awesome"))(),
			he.Textarea(ha.Rows(25))(),
			he.Meter(ha.Min(-1.3), ha.Max(5.5e12))(),
		),
	)
	hatmill.WriteDocument(os.Stdout, document)
}
Output:

<!DOCTYPE html><html><body><img src='./photo.jpg' contenteditable='true'>&lt;script&gt;launchMissiles();&lt;/script&gt;<div disabled data-coolness='awesome'></div><textarea rows='25'></textarea><meter min='-1.3' max='5.5E+12'></meter></body></html>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteDocument

func WriteDocument(w io.Writer, root ParentElement) (n int64, err error)

WriteDocument writes an HTML5 DOCTYPE declaration, followed by root. root should probably be an <html> element.

Types

type Attrib

type Attrib struct {
	Key   string
	Value fmt.Stringer
}

Attrib represents an HTML attribute.

func (Attrib) Empty added in v0.0.6

func (a Attrib) Empty() bool

func (Attrib) WriteTo

func (a Attrib) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes a to w as an HTML attribute in the form key="value", or simply key if value is empty. Special characters in value are replaced with HTML entities. It returns the number of bytes written and any error encountered.

type ParentElement

type ParentElement struct {
	VoidElement
	Children Terms
}

ParentElement represents an HTML element that can have children.

func (ParentElement) WriteTo

func (e ParentElement) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes the HTML markup represented by e to w, returning the number of bytes written and any error encountered.

type RawText added in v0.0.4

type RawText string

RawText represents an HTML text node. Unlike Text, its contents are not escaped when written, so it should be used with care. It is intended mainly for use in <style> and <script> elements.

Example
package main

import (
	"os"

	"gitlab.codemonkeysoftware.net/b/hatmill"

	he "gitlab.codemonkeysoftware.net/b/hatmill/element"
)

func main() {
	he.Style()(
		hatmill.RawText(`div > p::before {content: "Words & stuff: ";}`),
	).WriteTo(os.Stdout)
}
Output:

<style>div > p::before {content: "Words & stuff: ";}</style>

func (RawText) WriteTo added in v0.0.4

func (t RawText) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes the contents of t to w, returning the number of bytes written and any error encountered. It does not escape special characters.

type Term

type Term interface {
	io.WriterTo
	// contains filtered or unexported methods
}

Term represents a fragment of HTML markup, and is one of VoidElement, ParentElement, Terms, or Text.

type Terms added in v0.0.2

type Terms []Term

Terms is a list of HTML nodes.

func (Terms) WriteTo added in v0.0.2

func (ts Terms) WriteTo(w io.Writer) (n int64, err error)

type Text

type Text string

Text represents an HTML text node.

func (Text) WriteTo

func (t Text) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes the contents of t to w, returning the number of bytes written and any error encountered. It replaces special characters with HTML entities.

type VoidElement

type VoidElement struct {
	TagName string
	Attribs []Attrib
}

VoidElement represents a void HTML element, that is one that cannot have children.

func (VoidElement) WriteTo

func (e VoidElement) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes the HTML markup represented by e to w, returning the number of bytes written and any error encountered.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL