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 ¶
- func ILinkAll(root *Element, refs map[string]string)
- func LinkAll(root *Element, refs map[string]string)
- func WalkElements(root *Element, fn func(e *Element))
- type Attribute
- func Action(v string) *Attribute
- func Alt(v string) *Attribute
- func Attr(name string, val interface{}) *Attribute
- func Autocomplete(v string) *Attribute
- func Charset(v string) *Attribute
- func Class(v string) *Attribute
- func Content(v string) *Attribute
- func For(v string) *Attribute
- func Formaction(v string) *Attribute
- func Href(v string) *Attribute
- func Id(v string) *Attribute
- func Lang(v string) *Attribute
- func Max(v string) *Attribute
- func Maxlength(v string) *Attribute
- func Method(v string) *Attribute
- func Min(v string) *Attribute
- func Name(v string) *Attribute
- func OnBlur(v string) *Attribute
- func OnFocus(v string) *Attribute
- func OnLoad(v string) *Attribute
- func OnMouseDown(v string) *Attribute
- func OnMouseOut(v string) *Attribute
- func OnMouseOver(v string) *Attribute
- func OnMouseUp(v string) *Attribute
- func OnMouseWheel(v string) *Attribute
- func Pattern(v string) *Attribute
- func Placeholder(v string) *Attribute
- func Rel(v string) *Attribute
- func Size(v string) *Attribute
- func Src(v string) *Attribute
- func Tabindex(v string) *Attribute
- func Type(v string) *Attribute
- func Value(v string) *Attribute
- type CSS
- func (me *CSS) Import(url string)
- func (me *CSS) Media(v string) *CSS
- func (me *CSS) SaveAs(filename string) error
- func (me *CSS) SaveTo(dir string) error
- func (me *CSS) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (me *CSS) SetMedia(v string)
- func (c *CSS) Style(selector string, propvals ...string)
- func (me *CSS) With(in ...*CSS) *CSS
- func (me *CSS) WriteTo(w io.Writer) (int64, error)
- type Element
- func A(c ...interface{}) *Element
- func Abbr(c ...interface{}) *Element
- func Acronym(c ...interface{}) *Element
- func Address(c ...interface{}) *Element
- func Article(c ...interface{}) *Element
- func Aside(c ...interface{}) *Element
- func B(c ...interface{}) *Element
- func Base(c ...interface{}) *Element
- func Big(c ...interface{}) *Element
- func Blockquote(c ...interface{}) *Element
- func Body(c ...interface{}) *Element
- func Br(c ...interface{}) *Element
- func Button(c ...interface{}) *Element
- func Cite(c ...interface{}) *Element
- func Code(c ...interface{}) *Element
- func Comment(childOrAttr ...interface{}) *Element
- func Dd(c ...interface{}) *Element
- func Del(c ...interface{}) *Element
- func Details(c ...interface{}) *Element
- func Dfn(c ...interface{}) *Element
- func Div(c ...interface{}) *Element
- func Dl(c ...interface{}) *Element
- func Dt(c ...interface{}) *Element
- func Em(c ...interface{}) *Element
- func Fieldset(c ...interface{}) *Element
- func Find(root *Element, matchers ...Matcher) []*Element
- func Footer(c ...interface{}) *Element
- func Form(c ...interface{}) *Element
- func H1(c ...interface{}) *Element
- func H2(c ...interface{}) *Element
- func H3(c ...interface{}) *Element
- func H4(c ...interface{}) *Element
- func H5(c ...interface{}) *Element
- func H6(c ...interface{}) *Element
- func Head(c ...interface{}) *Element
- func Header(c ...interface{}) *Element
- func Hgroup(c ...interface{}) *Element
- func Hr(c ...interface{}) *Element
- func Html(c ...interface{}) *Element
- func I(c ...interface{}) *Element
- func Img(c ...interface{}) *Element
- func Input(c ...interface{}) *Element
- func Ins(c ...interface{}) *Element
- func Kbd(c ...interface{}) *Element
- func Keygen(c ...interface{}) *Element
- func Label(c ...interface{}) *Element
- func Legend(c ...interface{}) *Element
- func Li(c ...interface{}) *Element
- func Link(c ...interface{}) *Element
- func Main(c ...interface{}) *Element
- func Mark(c ...interface{}) *Element
- func Menu(c ...interface{}) *Element
- func Meta(c ...interface{}) *Element
- func Meter(c ...interface{}) *Element
- func Nav(c ...interface{}) *Element
- func NewElement(name string, childOrAttr ...interface{}) *Element
- func NewSimpleElement(name string, childOrAttr ...interface{}) *Element
- func Noscript(c ...interface{}) *Element
- func Ol(c ...interface{}) *Element
- func Optgroup(c ...interface{}) *Element
- func Option(c ...interface{}) *Element
- func Output(c ...interface{}) *Element
- func P(c ...interface{}) *Element
- func Pre(c ...interface{}) *Element
- func Query(root *Element, expr string) []*Element
- func Quote(c ...interface{}) *Element
- func Script(c ...interface{}) *Element
- func Section(c ...interface{}) *Element
- func Select(c ...interface{}) *Element
- func Span(c ...interface{}) *Element
- func Style(c ...interface{}) *Element
- func Sub(c ...interface{}) *Element
- func Summary(c ...interface{}) *Element
- func Sup(c ...interface{}) *Element
- func Table(c ...interface{}) *Element
- func Tbody(c ...interface{}) *Element
- func Td(c ...interface{}) *Element
- func Textarea(c ...interface{}) *Element
- func Th(c ...interface{}) *Element
- func Thead(c ...interface{}) *Element
- func Title(c ...interface{}) *Element
- func Tr(c ...interface{}) *Element
- func U(c ...interface{}) *Element
- func Ul(c ...interface{}) *Element
- func Var(c ...interface{}) *Element
- func Wrap(v ...interface{}) *Element
- func (t *Element) Attr(name string) *Attribute
- func (t *Element) AttrVal(name string) string
- func (t *Element) HasAttr(name string) bool
- func (t *Element) String() string
- func (t *Element) Text() string
- func (t *Element) With(childOrAttr ...interface{}) *Element
- func (t *Element) WriteTo(w io.Writer) (int64, error)
- type ElementBuilder
- type Hn
- type HtmlEncoder
- type MarkdownEncoder
- type Matcher
- type Page
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ILinkAll ¶ added in v0.15.0
ILinkAll replaces key words same as LinkAll but is canse insensitive
func LinkAll ¶ added in v0.15.0
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 ¶ added in v0.10.0
Example ¶
root := Article( H1(), H2(), H3(), ) WalkElements(root, func(e *Element) { fmt.Println(e.Name) })
Output: article h1 h2 h3
Types ¶
type Attribute ¶
func Autocomplete ¶ added in v0.8.3
Autocomplete returns a autocomplete="v" attribute
func Formaction ¶ added in v0.19.0
Formaction returns a formaction="v" attribute
func OnMouseDown ¶ added in v0.9.0
OnMouseDown returns a onMouseDown="v" attribute
func OnMouseOut ¶ added in v0.9.0
OnMouseOut returns a onMouseOut="v" attribute
func OnMouseOver ¶ added in v0.9.0
OnMouseOver returns a onMouseOver="v" attribute
func OnMouseWheel ¶ added in v0.9.0
OnMouseWheel returns a onMouseWheel="v" attribute
func Placeholder ¶ added in v0.20.0
Placeholder returns a placeholder="v" attribute
func (*Attribute) SafeString ¶ added in v0.19.0
type CSS ¶ added in v0.9.0
type CSS struct { Filename string // contains filtered or unexported fields }
func (*CSS) SaveTo ¶ added in v0.16.0
SaveTo saves the page to the given directory. Fails if page.Filename is empty.
func (*CSS) ServeHTTP ¶ added in v0.16.0
func (me *CSS) ServeHTTP(w http.ResponseWriter, r *http.Request)
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 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 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 ¶ added in v0.8.3
func Em(c ...interface{}) *Element
Em returns an <em> element with optional children or attributes
func Fieldset ¶ added in v0.8.3
func Fieldset(c ...interface{}) *Element
Fieldset returns an <fieldset> element with optional children or attributes
func Footer ¶
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 ¶
func Head(c ...interface{}) *Element
Head returns an <head> element with optional children or attributes
func Header ¶
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 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 ¶
func Link(c ...interface{}) *Element
Link returns a <link/> element with optional attributes
func Main ¶ added in v0.26.0
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 ¶
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 ¶
func Nav(c ...interface{}) *Element
Nav returns an <nav> element with optional children or attributes
func NewElement ¶
func NewSimpleElement ¶
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 ¶ added in v0.23.0
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 ¶ added in v0.15.0
func Wrap(v ...interface{}) *Element
Wrap returns an element that produces no output when rendered apart from it's children.
func (*Element) AttrVal ¶ added in v0.14.0
AttrVal returns attribute value if it exists, empty string otherwise.
type ElementBuilder ¶ added in v0.20.0
type ElementBuilder interface {
BuildElement() *Element
}
ElementBuilder are used to dynamically inject elements during encoding.
type Hn ¶ added in v0.15.0
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.
type HtmlEncoder ¶ added in v0.14.0
func NewHtmlEncoder ¶ added in v0.14.0
func NewHtmlEncoder(w io.Writer) *HtmlEncoder
func NewSafeHtmlEncoder ¶ added in v0.19.0
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 ¶ added in v0.14.0
func (p *HtmlEncoder) Encode(t interface{}) error
type MarkdownEncoder ¶ added in v0.14.0
func NewMarkdownEncoder ¶ added in v0.14.0
func NewMarkdownEncoder(w io.Writer) *MarkdownEncoder
func (*MarkdownEncoder) Encode ¶ added in v0.14.0
func (p *MarkdownEncoder) Encode(t interface{}) error
type Page ¶
func NewPage ¶
NewPage returns a page ready to be rendered. Filename is empty and must be set before saving.
func NewSafePage ¶ added in v0.19.0
NewSafePage returns a page same as NewPage, only the output if written as html is escaped. See NewSafeHtmlEncoder constructor.
func (*Page) SaveTo ¶
SaveTo saves the page to the given directory. Fails if page.Filename is empty.
func (*Page) ServeHTTP ¶ added in v0.15.0
func (p *Page) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (*Page) Size ¶ added in v0.20.0
Size returns the rendered size of the page in bytes. Note! the page is rendered once to count the bytes.
func (*Page) WriteTo ¶
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 ¶
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 |