web

package module
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package web provides html writing capabilities.

In most cases when writing interactive webapplications you should stick to html/template package. This package however can be suitable when writing documentation or generating websites.

article := Article(
   Class("fancy"),
   H1("Title of my article"),
)
page := NewPage("", Html(Body(article)))
page.WriteTo(os.Stdout)

By default the page is written as html, expected.

<!DOCTYPE html>

<html>
<body>
<article class="fancy">
<h1>Title of my article</h1>

</article>
</body>
</html>
Example
c := NewCSS()
c.Import("https://fonts.googleapis.com/css?family=Open+Sans")
c.Style("#x", "margin: 0 0")

p := c.Media("print")
p.Style("footer", "display: none")

c.WriteTo(os.Stdout)
Output:

@import url('https://fonts.googleapis.com/css?family=Open+Sans');
#x {
margin: 0 0;
}

@media print{
footer {
display: none;
}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ILinkAll

func ILinkAll(root *Element, refs map[string]string)

ILinkAll replaces key words same as LinkAll but is canse insensitive

func LinkAll

func LinkAll(root *Element, refs map[string]string)

LinkAll replaces key words found in the root and it's children with links defined in the map. The map should be TEXT -> HREF

func WalkElements

func WalkElements(root *Element, fn func(e *Element))
Example
root := Article(
	H1(),
	H2(),
	H3(),
)
WalkElements(root, func(e *Element) {
	fmt.Println(e.Name)
})
Output:

article
h1
h2
h3

Types

type Attribute

type Attribute struct {
	Name string
	Val  string
}

func Action

func Action(v string) *Attribute

Action returns a action="v" attribute

func Alt

func Alt(v string) *Attribute

Alt returns a alt="v" attribute

func Attr

func Attr(name string, val interface{}) *Attribute

Attr creates a new named attribute with the given value.

func Autocomplete

func Autocomplete(v string) *Attribute

Autocomplete returns a autocomplete="v" attribute

func Charset

func Charset(v string) *Attribute

Charset returns a charset="v" attribute

func Class

func Class(v string) *Attribute

Class returns a class="v" attribute

func Content

func Content(v string) *Attribute

Content returns a content="v" attribute

func For

func For(v string) *Attribute

For returns a for="v" attribute

func Formaction

func Formaction(v string) *Attribute

Formaction returns a formaction="v" attribute

func Href

func Href(v string) *Attribute

Href returns a href="v" attribute

func Id

func Id(v string) *Attribute

Id returns a id="v" attribute

func Lang

func Lang(v string) *Attribute

Lang returns a lang="v" attribute

func Max

func Max(v string) *Attribute

Max returns a max="v" attribute

func Maxlength

func Maxlength(v string) *Attribute

Maxlength returns a maxlength="v" attribute

func Method

func Method(v string) *Attribute

Method returns a method="v" attribute

func Min

func Min(v string) *Attribute

Min returns a min="v" attribute

func Name

func Name(v string) *Attribute

Name returns a name="v" attribute

func OnBlur

func OnBlur(v string) *Attribute

OnBlur returns a onBlur="v" attribute

func OnFocus

func OnFocus(v string) *Attribute

OnFocus returns a onFocus="v" attribute

func OnLoad

func OnLoad(v string) *Attribute

OnLoad returns a onLoad="v" attribute

func OnMouseDown

func OnMouseDown(v string) *Attribute

OnMouseDown returns a onMouseDown="v" attribute

func OnMouseOut

func OnMouseOut(v string) *Attribute

OnMouseOut returns a onMouseOut="v" attribute

func OnMouseOver

func OnMouseOver(v string) *Attribute

OnMouseOver returns a onMouseOver="v" attribute

func OnMouseUp

func OnMouseUp(v string) *Attribute

OnMouseUp returns a onMouseUp="v" attribute

func OnMouseWheel

func OnMouseWheel(v string) *Attribute

OnMouseWheel returns a onMouseWheel="v" attribute

func Pattern

func Pattern(v string) *Attribute

Pattern returns a pattern="v" attribute

func Placeholder

func Placeholder(v string) *Attribute

Placeholder returns a placeholder="v" attribute

func Rel

func Rel(v string) *Attribute

Rel returns a rel="v" attribute

func Size

func Size(v string) *Attribute

Size returns a size="v" attribute

func Src

func Src(v string) *Attribute

Src returns a src="v" attribute

func Tabindex

func Tabindex(v string) *Attribute

Tabindex returns a tabindex="v" attribute

func Type

func Type(v string) *Attribute

Type returns a type="v" attribute

func Value

func Value(v string) *Attribute

Value returns a value="v" attribute

func (*Attribute) SafeString

func (a *Attribute) SafeString() string

func (*Attribute) String

func (a *Attribute) String() string

type CSS

type CSS struct {
	Filename string
	// contains filtered or unexported fields
}

func NewCSS

func NewCSS() *CSS

func (*CSS) Import

func (me *CSS) Import(url string)

Import adds the url to list of imports.

func (*CSS) Media

func (me *CSS) Media(v string) *CSS

Media adds a media section returning a new css for styling

func (*CSS) SaveAs

func (me *CSS) SaveAs(filename string) error

SaveAs sets filename and then save to the current directory.

func (*CSS) SaveTo

func (me *CSS) SaveTo(dir string) error

SaveTo saves the page to the given directory. Fails if page.Filename is empty.

func (*CSS) ServeHTTP

func (me *CSS) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*CSS) SetMedia

func (me *CSS) SetMedia(v string)

SetMedia sets the media

func (*CSS) Style

func (c *CSS) Style(selector string, propvals ...string)

func (*CSS) With

func (me *CSS) With(in ...*CSS) *CSS

With combines a css with another, returning the combined

Example
a := NewCSS()
a.Style("body", "margin: 0 0")

b := NewCSS()
b.Style("p", "color:red")

a.With(b)

a.WriteTo(os.Stdout)
Output:

body {
margin: 0 0;
}
p {
color:red;
}

func (*CSS) WriteTo

func (me *CSS) WriteTo(w io.Writer) (int64, error)

type Element

type Element struct {
	Children   []interface{}
	Name       string
	Attributes []*Attribute
	// contains filtered or unexported fields
}

func A

func A(c ...interface{}) *Element

A returns an <a> element with optional children or attributes

func Abbr

func Abbr(c ...interface{}) *Element

Abbr returns an <abbr> element with optional children or attributes

func Acronym

func Acronym(c ...interface{}) *Element

Acronym returns an <acronym> element with optional children or attributes

func Address

func Address(c ...interface{}) *Element

Address returns an <address> element with optional children or attributes

func Article

func Article(c ...interface{}) *Element

Article returns an <article> element with optional children or attributes

func Aside

func Aside(c ...interface{}) *Element

Aside returns an <aside> element with optional children or attributes

func B

func B(c ...interface{}) *Element

B returns an <b> element with optional children or attributes

func Base

func Base(c ...interface{}) *Element

Base returns a <base/> element with optional attributes

func Big

func Big(c ...interface{}) *Element

Big returns an <big> element with optional children or attributes

func Blockquote

func Blockquote(c ...interface{}) *Element

Blockquote returns an <blockquote> element with optional children or attributes

func Body

func Body(c ...interface{}) *Element

Body returns an <body> element with optional children or attributes

func Br

func Br(c ...interface{}) *Element

Br returns a <br/> element with optional attributes

func Button

func Button(c ...interface{}) *Element

Button returns an <button> element with optional children or attributes

func Cite

func Cite(c ...interface{}) *Element

Cite returns an <cite> element with optional children or attributes

func Code

func Code(c ...interface{}) *Element

Code returns an <code> element with optional children or attributes

func Comment

func Comment(childOrAttr ...interface{}) *Element

func Dd

func Dd(c ...interface{}) *Element

Dd returns an <dd> element with optional children or attributes

func Del

func Del(c ...interface{}) *Element

Del returns an <del> element with optional children or attributes

func Details

func Details(c ...interface{}) *Element

Details returns an <details> element with optional children or attributes

func Dfn

func Dfn(c ...interface{}) *Element

Dfn returns an <dfn> element with optional children or attributes

func Div

func Div(c ...interface{}) *Element

Div returns an <div> element with optional children or attributes

func Dl

func Dl(c ...interface{}) *Element

Dl returns an <dl> element with optional children or attributes

func Dt

func Dt(c ...interface{}) *Element

Dt returns an <dt> element with optional children or attributes

func Em

func Em(c ...interface{}) *Element

Em returns an <em> element with optional children or attributes

func Fieldset

func Fieldset(c ...interface{}) *Element

Fieldset returns an <fieldset> element with optional children or attributes

func Find

func Find(root *Element, matchers ...Matcher) []*Element

Find returns the matching elements of the expression

func Footer(c ...interface{}) *Element

Footer returns an <footer> element with optional children or attributes

func Form

func Form(c ...interface{}) *Element

Form returns an <form> element with optional children or attributes

func H1

func H1(c ...interface{}) *Element

H1 returns an <h1> element with optional children or attributes

func H2

func H2(c ...interface{}) *Element

H2 returns an <h2> element with optional children or attributes

func H3

func H3(c ...interface{}) *Element

H3 returns an <h3> element with optional children or attributes

func H4

func H4(c ...interface{}) *Element

H4 returns an <h4> element with optional children or attributes

func H5

func H5(c ...interface{}) *Element

H5 returns an <h5> element with optional children or attributes

func H6

func H6(c ...interface{}) *Element

H6 returns an <h6> element with optional children or attributes

func Head(c ...interface{}) *Element

Head returns an <head> element with optional children or attributes

func Header(c ...interface{}) *Element

Header returns an <header> element with optional children or attributes

func Hgroup

func Hgroup(c ...interface{}) *Element

Hgroup returns an <hgroup> element with optional children or attributes

func Hr

func Hr(c ...interface{}) *Element

Hr returns a <hr/> element with optional attributes

func Html

func Html(c ...interface{}) *Element

Html returns an <html> element with optional children or attributes

func I

func I(c ...interface{}) *Element

I returns an <i> element with optional children or attributes

func Img

func Img(c ...interface{}) *Element

Img returns a <img/> element with optional attributes

func Input

func Input(c ...interface{}) *Element

Input returns a <input/> element with optional attributes

func Ins

func Ins(c ...interface{}) *Element

Ins returns an <ins> element with optional children or attributes

func Kbd

func Kbd(c ...interface{}) *Element

Kbd returns an <kbd> element with optional children or attributes

func Keygen

func Keygen(c ...interface{}) *Element

Keygen returns a <keygen/> element with optional attributes

func Label

func Label(c ...interface{}) *Element

Label returns an <label> element with optional children or attributes

func Legend

func Legend(c ...interface{}) *Element

Legend returns an <legend> element with optional children or attributes

func Li

func Li(c ...interface{}) *Element

Li returns an <li> element with optional children or attributes

func Link(c ...interface{}) *Element

Link returns a <link/> element with optional attributes

func Main

func Main(c ...interface{}) *Element

Main returns an <main> element with optional children or attributes

func Mark

func Mark(c ...interface{}) *Element

Mark returns an <mark> element with optional children or attributes

func Menu(c ...interface{}) *Element

Menu returns an <menu> element with optional children or attributes

func Meta

func Meta(c ...interface{}) *Element

Meta returns a <meta/> element with optional attributes

func Meter

func Meter(c ...interface{}) *Element

Meter returns an <meter> element with optional children or attributes

func Nav(c ...interface{}) *Element

Nav returns an <nav> element with optional children or attributes

func NewElement

func NewElement(name string, childOrAttr ...interface{}) *Element

func NewSimpleElement

func NewSimpleElement(name string, childOrAttr ...interface{}) *Element

func Noscript

func Noscript(c ...interface{}) *Element

Noscript returns an <noscript> element with optional children or attributes

func Ol

func Ol(c ...interface{}) *Element

Ol returns an <ol> element with optional children or attributes

func Optgroup

func Optgroup(c ...interface{}) *Element

Optgroup returns an <optgroup> element with optional children or attributes

func Option

func Option(c ...interface{}) *Element

Option returns an <option> element with optional children or attributes

func Output

func Output(c ...interface{}) *Element

Output returns an <output> element with optional children or attributes

func P

func P(c ...interface{}) *Element

P returns an <p> element with optional children or attributes

func Pre

func Pre(c ...interface{}) *Element

Pre returns an <pre> element with optional children or attributes

func Query

func Query(root *Element, expr string) []*Element

Query returns elements matching the given css selector style expression. See ParseExpr for supported expressions.

Example
doc := Article(
	H1("1"),
	H2("1.1"),
	H2("1.2"),
	H2("1.3", Class("mark")),
	H2("1.4"),
)
for _, el := range Query(doc, "h1") {
	el.With(Class("mark"))
}

for _, el := range Query(doc, "h2.mark") {
	el.WriteTo(os.Stdout)
}

fmt.Println()
for _, el := range Query(doc, ".mark") {
	el.WriteTo(os.Stdout)
}
Output:

<h2 class="mark">1.3</h2>

<h1 class="mark">1</h1>
<h2 class="mark">1.3</h2>

func Quote

func Quote(c ...interface{}) *Element

Quote returns an <quote> element with optional children or attributes

func Script

func Script(c ...interface{}) *Element

Script returns an <script> element with optional children or attributes

func Section

func Section(c ...interface{}) *Element

Section returns an <section> element with optional children or attributes

func Select

func Select(c ...interface{}) *Element

Select returns an <select> element with optional children or attributes

func Span

func Span(c ...interface{}) *Element

Span returns an <span> element with optional children or attributes

func Style

func Style(c ...interface{}) *Element

Style returns an <style> element with optional children or attributes

func Sub

func Sub(c ...interface{}) *Element

Sub returns an <sub> element with optional children or attributes

func Summary

func Summary(c ...interface{}) *Element

Summary returns an <summary> element with optional children or attributes

func Sup

func Sup(c ...interface{}) *Element

Sup returns an <sup> element with optional children or attributes

func Table

func Table(c ...interface{}) *Element

Table returns an <table> element with optional children or attributes

func Tbody

func Tbody(c ...interface{}) *Element

Tbody returns an <tbody> element with optional children or attributes

func Td

func Td(c ...interface{}) *Element

Td returns an <td> element with optional children or attributes

func Textarea

func Textarea(c ...interface{}) *Element

Textarea returns an <textarea> element with optional children or attributes

func Th

func Th(c ...interface{}) *Element

Th returns an <th> element with optional children or attributes

func Thead

func Thead(c ...interface{}) *Element

Thead returns an <thead> element with optional children or attributes

func Title

func Title(c ...interface{}) *Element

Title returns an <title> element with optional children or attributes

func Tr

func Tr(c ...interface{}) *Element

Tr returns an <tr> element with optional children or attributes

func U

func U(c ...interface{}) *Element

U returns an <u> element with optional children or attributes

func Ul

func Ul(c ...interface{}) *Element

Ul returns an <ul> element with optional children or attributes

func Var

func Var(c ...interface{}) *Element

Var returns an <var> element with optional children or attributes

func Wrap

func Wrap(v ...interface{}) *Element

Wrap returns an element that produces no output when rendered apart from it's children.

func (*Element) Attr

func (t *Element) Attr(name string) *Attribute

Attr returns the named attribute or nil.

func (*Element) AttrVal

func (t *Element) AttrVal(name string) string

AttrVal returns attribute value if it exists, empty string otherwise.

func (*Element) HasAttr

func (t *Element) HasAttr(name string) bool

HasAttr returns true if the named attribute is found on the element.

func (*Element) String

func (t *Element) String() string

func (*Element) Text

func (t *Element) Text() string

func (*Element) With

func (t *Element) With(childOrAttr ...interface{}) *Element

func (*Element) WriteTo

func (t *Element) WriteTo(w io.Writer) (int64, error)

type ElementBuilder

type ElementBuilder interface {
	BuildElement() *Element
}

ElementBuilder are used to dynamically inject elements during encoding.

type Hn

type Hn struct {
	// contains filtered or unexported fields
}

Hn allows for creating headings starting at different levels. Zero value object of Hn is usable, where H1 method matches H1 heading.

func NewHn

func NewHn(start int) *Hn

NewHn returns a Hn starting at the given level. 1 meaning H1 produces H1, whereas 2 means H1 produces H2 and so on.

func (*Hn) H1

func (me *Hn) H1(v ...interface{}) *Element

func (*Hn) H2

func (me *Hn) H2(v ...interface{}) *Element

func (*Hn) H3

func (me *Hn) H3(v ...interface{}) *Element

func (*Hn) H4

func (me *Hn) H4(v ...interface{}) *Element

func (*Hn) H5

func (me *Hn) H5(v ...interface{}) *Element

func (*Hn) H6

func (me *Hn) H6(v ...interface{}) *Element

type HtmlEncoder

type HtmlEncoder struct {
	*nexus.Printer
	// contains filtered or unexported fields
}

func NewHtmlEncoder

func NewHtmlEncoder(w io.Writer) *HtmlEncoder

func NewSafeHtmlEncoder

func NewSafeHtmlEncoder(w io.Writer) *HtmlEncoder

NewSafeHtmlEncoder returns a HtmlEncoder that escapes plain values. Values from io.Reader elements or those implementing WriterTo are not escaped.

func (*HtmlEncoder) Encode

func (p *HtmlEncoder) Encode(t interface{}) error

type MarkdownEncoder

type MarkdownEncoder struct {
	*nexus.Printer
	// contains filtered or unexported fields
}

func NewMarkdownEncoder

func NewMarkdownEncoder(w io.Writer) *MarkdownEncoder

func (*MarkdownEncoder) Encode

func (p *MarkdownEncoder) Encode(t interface{}) error

type Matcher

type Matcher func(*Element) bool

func ParseExpr

func ParseExpr(expr string) []Matcher

ParseExpr returns a list of matchers to use in func Find.

Valid expressions:

name
name.class
.class
#id

type Page

type Page struct {
	Filename string
	*Element
	// contains filtered or unexported fields
}

func NewFile

func NewFile(filename string, el *Element) *Page

NewFile returns a page with filename set, ready to be saved.

func NewPage

func NewPage(el *Element) *Page

NewPage returns a page ready to be rendered. Filename is empty and must be set before saving.

func NewSafePage

func NewSafePage(el *Element) *Page

NewSafePage returns a page same as NewPage, only the output if written as html is escaped. See NewSafeHtmlEncoder constructor.

func (*Page) SaveAs

func (me *Page) SaveAs(filename string) error

SaveAs sets filename and then save to the current directory.

func (*Page) SaveTo

func (p *Page) SaveTo(dir string) error

SaveTo saves the page to the given directory. Fails if page.Filename is empty.

func (*Page) ServeHTTP

func (p *Page) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Page) Size

func (me *Page) Size() int

Size returns the rendered size of the page in bytes. Note! the page is rendered once to count the bytes.

func (*Page) WriteTo

func (p *Page) WriteTo(w io.Writer) (int64, error)

WriteTo writes the page using the given writer. Page.Filename extension decides format. .md for markdown, otherwise HTML.

Example
article := Article(
	Class("fancy"),
	H1("Title of my article"),
	P(
		"before ", A(Href("http://example.com"), "example"),
		" after",
	),
)
page := NewPage(Html(Body(article)))
page.WriteTo(os.Stdout)
Output:

<!DOCTYPE html>

<html>
<body>
<article class="fancy">
<h1>Title of my article</h1>
<p>before <a href="http://example.com">example</a> after</p>
</article>
</body>
</html>

Source Files

  • css.go
  • doc.go
  • element.go
  • html.go
  • htmlencoder.go
  • hx.go
  • links.go
  • markdown.go
  • page.go
  • query.go
  • wrap.go

Directories

Path Synopsis
Package apidoc provides html document builder for http requests and responses.
Package apidoc provides html document builder for http requests and responses.
Package files provides file loading utils.
Package files provides file loading utils.
Package theme provides some basic web page themes
Package theme provides some basic web page themes

Jump to

Keyboard shortcuts

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