htmlg

package module
v0.0.0-...-a97fae3 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: MIT Imports: 5 Imported by: 52

README

htmlg

Go Reference

Package htmlg contains helper funcs for generating HTML nodes and rendering them. Context-aware escaping is done just like in html/template, making it safe against code injection.

Note: This package is quite experimental in nature, so its API is susceptible to more frequent changes than the average package. This is necessary in order to keep this package useful.

Installation

go get github.com/shurcooL/htmlg

License

Documentation

Overview

Package htmlg contains helper funcs for generating HTML nodes and rendering them. Context-aware escaping is done just like in html/template, making it safe against code injection.

Note: This package is quite experimental in nature, so its API is susceptible to more frequent changes than the average package. This is necessary in order to keep this package useful.

Example
package main

import (
	"fmt"
	"os"

	"github.com/shurcooL/htmlg"
)

func main() {
	// Context-aware escaping is done just like in html/template.
	html := htmlg.Render(
		htmlg.Text("Hi & how are you, "),
		htmlg.A("Gophers", "https://golang.org/"),
		htmlg.Text("? <script> is a cool gopher."),
	)

	fmt.Fprintln(os.Stdout, html)

}
Output:

Hi &amp; how are you, <a href="https://golang.org/">Gophers</a>? &lt;script&gt; is a cool gopher.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func A

func A(s, href string) *html.Node

A returns an anchor element <a href="{{.href}}">{{.s}}</a>.

func AppendChildren

func AppendChildren(n *html.Node, cs ...*html.Node)

AppendChildren adds nodes cs as children of n.

It will panic if any of cs already has a parent or siblings.

Example
package main

import (
	"os"

	"github.com/shurcooL/component"
	"github.com/shurcooL/htmlg"
	"golang.org/x/net/html"
)

func main() {
	div := htmlg.Div(
		htmlg.Text("Go "),
	)
	htmlg.AppendChildren(div, component.Link{
		Text:   "there",
		URL:    "https://golang.org",
		NewTab: true,
	}.Render()...)
	div.AppendChild(
		htmlg.Text("for more!"),
	)

	err := html.Render(os.Stdout, div)
	if err != nil {
		panic(err)
	}

}
Output:

<div>Go <a href="https://golang.org" target="_blank">there</a>for more!</div>

func Code

func Code(nodes ...*html.Node) *html.Node

Code returns a code element <code>{{range .nodes}}{{.}}{{end}}</code>.

func DD

func DD(nodes ...*html.Node) *html.Node

DD returns a dd element <dd>{{range .nodes}}{{.}}{{end}}</dd>.

func DL

func DL(nodes ...*html.Node) *html.Node

DL returns a dl element <dl>{{range .nodes}}{{.}}{{end}}</dl>.

func DT

func DT(nodes ...*html.Node) *html.Node

DT returns a dt element <dt>{{range .nodes}}{{.}}{{end}}</dt>.

func Div

func Div(nodes ...*html.Node) *html.Node

Div returns a div element <div>{{range .nodes}}{{.}}{{end}}</div>.

Div is experimental and may be changed or removed.

func DivClass

func DivClass(class string, nodes ...*html.Node) *html.Node

DivClass returns a div element <div class="{{.class}}">{{range .nodes}}{{.}}{{end}}</div>.

DivClass is experimental and may be changed or removed.

func H1

func H1(nodes ...*html.Node) *html.Node

H1 returns a h1 element <h1>{{range .nodes}}{{.}}{{end}}</h1>.

func H2

func H2(nodes ...*html.Node) *html.Node

H2 returns a h2 element <h2>{{range .nodes}}{{.}}{{end}}</h2>.

func H3

func H3(nodes ...*html.Node) *html.Node

H3 returns a h3 element <h3>{{range .nodes}}{{.}}{{end}}</h3>.

func H4

func H4(nodes ...*html.Node) *html.Node

H4 returns a h4 element <h4>{{range .nodes}}{{.}}{{end}}</h4>.

func LI

func LI(nodes ...*html.Node) *html.Node

LI returns a li element <li>{{range .nodes}}{{.}}{{end}}</li>.

func LIClass

func LIClass(class string, nodes ...*html.Node) *html.Node

LIClass returns a div element <li class="{{.class}}">{{range .nodes}}{{.}}{{end}}</li>.

LIClass is experimental and may be changed or removed.

func P

func P(nodes ...*html.Node) *html.Node

P returns a p element <p>{{range .nodes}}{{.}}{{end}}</p>.

func Pre

func Pre(nodes ...*html.Node) *html.Node

Pre returns a pre element <pre>{{range .nodes}}{{.}}{{end}}</pre>.

func Render

func Render(nodes ...*html.Node) string

Render renders HTML nodes, returning result as a string. Context-aware escaping is done just like in html/template when rendering nodes.

func RenderComponents

func RenderComponents(w io.Writer, components ...Component) error

RenderComponents renders components into HTML, writing result to w. Context-aware escaping is done just like in html/template when rendering nodes.

func RenderComponentsString

func RenderComponentsString(components ...Component) string

RenderComponentsString renders components into HTML, returning result as a string. Context-aware escaping is done just like in html/template when rendering nodes.

func Span

func Span(nodes ...*html.Node) *html.Node

Span returns a span element <span>{{range .nodes}}{{.}}{{end}}</span>.

Span is experimental and may be changed or removed.

func SpanClass

func SpanClass(class string, nodes ...*html.Node) *html.Node

SpanClass returns a span element <span class="{{.class}}">{{range .nodes}}{{.}}{{end}}</span>.

SpanClass is experimental and may be changed or removed.

func Strong

func Strong(s string) *html.Node

Strong returns a strong text node.

func TD

func TD(nodes ...*html.Node) *html.Node

TD returns a td element <td>{{range .nodes}}{{.}}{{end}}</td>.

func TR

func TR(nodes ...*html.Node) *html.Node

TR returns a tr element <tr>{{range .nodes}}{{.}}{{end}}</tr>.

func Text

func Text(s string) *html.Node

Text returns a plain text node.

func UL

func UL(nodes ...*html.Node) *html.Node

UL returns a ul element <ul>{{range .nodes}}{{.}}{{end}}</ul>.

func ULClass

func ULClass(class string, nodes ...*html.Node) *html.Node

ULClass returns a div element <ul class="{{.class}}">{{range .nodes}}{{.}}{{end}}</ul>.

ULClass is experimental and may be changed or removed.

Types

type Component

type Component interface {
	Render() []*html.Node
}

Component is anything that can render itself into HTML nodes.

type NodeComponent

type NodeComponent html.Node

NodeComponent is a wrapper that makes a Component from a single html.Node.

Example
package main

import (
	"os"

	"github.com/shurcooL/htmlg"
	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
)

func main() {
	heading := htmlg.NodeComponent{
		Type: html.ElementNode, Data: atom.H2.String(),
		FirstChild: htmlg.Text("This heading is an htmlg.Component"),
	}

	err := htmlg.RenderComponents(os.Stdout, heading)
	if err != nil {
		panic(err)
	}

}
Output:

<h2>This heading is an htmlg.Component</h2>

func (NodeComponent) Render

func (n NodeComponent) Render() []*html.Node

type Nodes

type Nodes []*html.Node

Nodes implements the Component interface from a slice of HTML nodes.

The Render method always returns the same references to existing nodes, and as a result, it is unsuitable to be rendered and attached to other HTML nodes more than once. It is suitable to rendered directly into HTML multiple times, or attached to an existing node once.

Nodes is experimental and may be changed or removed.

Example
package main

import (
	"os"

	"github.com/shurcooL/htmlg"
)

func main() {
	err := htmlg.RenderComponents(os.Stdout, htmlg.Nodes{
		htmlg.Text("Hi & how are you, "),
		htmlg.A("Gophers", "https://golang.org/"),
		htmlg.Text("? <script> is a cool gopher."),
	})
	if err != nil {
		panic(err)
	}

}
Output:

Hi &amp; how are you, <a href="https://golang.org/">Gophers</a>? &lt;script&gt; is a cool gopher.

func (Nodes) Render

func (ns Nodes) Render() []*html.Node

Jump to

Keyboard shortcuts

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