hatmill

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2019 License: MIT Imports: 1 Imported by: 0

README

hatmill - HTML generation DSL for Go

License Gitlab pipeline status Go Report Card GoDoc Badge count

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 (
	"html"
	"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(html.EscapeString(userInput)),
			he.Div(ha.Disabled(), 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 string
}

Attrib represents an HTML attribute.

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. It returns the number of bytes written and any error encountered.

type ParentElement

type ParentElement struct {
	VoidElement
	Children []Term
}

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.

See the warning about sanitization in the (Attrib).WriteTo documentation.

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, or Text.

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 does not replace special characters with HTML entities; use html.EscapeString for this purpose.

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.

See the warning about sanitization in the (Attrib).WriteTo documentation.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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