haat

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2024 License: MIT Imports: 9 Imported by: 0

README

haat (Html as a Template)

Wrapper Library for x/net/html and github.com/ericchiang/css

package main

import (
	"log"
	"os"

	"github.com/turutcrane/haat"
)

func main() {

	h, err := haat.ParseHTML(strings.NewReader(`
<!DOCTYPE html>
<html>
<head>
<title>Hello haat</title>
</head>
<body>
Hello <span id="pkgname"></span>!!
</body></html>`))
	if err != nil {
		log.Panicln(err)
	}
	for _, span := range h.Query("span#pkgname") {
		span.C(haat.T("haat"))
	}
	h.Render(os.Stdout)
}

Output:

<!DOCTYPE html><html><head>
<title>Hello haat</title>
</head>
<body>
Hello <span id="pkgname">haat</span>!!
</body></html>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpDocument

func DumpDocument(d *Document, indent int, mark string)

DumpDocument prints the node to the standard output.

func DumpDocument2

func DumpDocument2(d *Document, indent int, mark string)

DumpNode2 prints the node to the standard output.

func DumpElement

func DumpElement(e *Element, indent int, mark string)

func IDDuplicateCheck

func IDDuplicateCheck(e *Element) error

IDDuplicateCheck checks if the node has duplicate id attributes.

func IDHasBlankCheck

func IDHasBlankCheck(e *Element) error

IDHasBlankCheck checks if the node has id with blank.

func IDMissingCheck

func IDMissingCheck(e *Element) error

IDMissingCheck checks if the node has id without value.

func Lf

func Lf() string

Lf creates a new text node with a line feed.

func Remove

func Remove(n Node)

Remove removes the node from the parent node.

Types

type Attribute

type Attribute html.Attribute

func Attr

func Attr(key, value string) Attribute

Attr creates a new attribute with the given key and value.

func AttrHref

func AttrHref(u url.URL) Attribute

AttrHref creates a new attribute with the key "href" and the value of the given URL.

func AttrID

func AttrID(id string) Attribute

AttrID creates a new attribute with the key "id" and the given value.

type Checker

type Checker func(*Element) error

Checker is a function that checks the node.

type Comment

type Comment html.Node

func (*Comment) Clone

func (c *Comment) Clone() *Comment

func (*Comment) NodeType

func (c *Comment) NodeType() html.NodeType

func (*Comment) ParentElement

func (c *Comment) ParentElement() *Element

type Doctype

type Doctype html.Node

func (*Doctype) Clone

func (d *Doctype) Clone() *Doctype

func (*Doctype) NodeType

func (d *Doctype) NodeType() html.NodeType

type Document

type Document html.Node

func ParseHTML added in v0.1.1

func ParseHTML(s io.Reader) (*Document, error)

ParseHTML parses the HTML page from the given reader.

func (*Document) Clone

func (d *Document) Clone() *Document

Clone creates a deep copy of the node.

func (*Document) InputText

func (d *Document) InputText(selector string) []*Element

InputText return the input text nodes that match the selector.

func (*Document) NodeType

func (d *Document) NodeType() html.NodeType

NodeType returns the node type.

func (*Document) Query

func (d *Document) Query(selector string) []*Element

Query return the nodes that match the selector.

func (*Document) QuerySelector

func (d *Document) QuerySelector(selector *Selector) []*Element

QeurySelector return the all nodes that match the selector.

func (*Document) Render

func (d *Document) Render(w io.Writer, checker ...Checker) error

Render renders the node to the given writer.

type Element

type Element html.Node

func Elem

func Elem(a atom.Atom) *Element

Elem creates a new element node with the given atom.

func ParseHTMLFragment added in v0.1.1

func ParseHTMLFragment(s io.Reader, node *Element) ([]*Element, error)

ParseHTMLFragment parses the HTML fragment with node context from the given reader.

func (*Element) A

func (e *Element) A(attrs ...Attribute) *Element

A replaces all attributes with the attributes specified in the arguments. If there are duplicate keys, it sets the latter value.

func (*Element) AppendC

func (e *Element) AppendC(childs ...ElementChild) *Element

AppendC appends the given nodes to the children of the node.

func (*Element) C

func (e *Element) C(childs ...ElementChild) *Element

C sets the children of the node to the given nodes.

func (*Element) ClearContents

func (e *Element) ClearContents() *Element

ClearContents removes all children of the node.

func (*Element) Clone

func (e *Element) Clone() *Element

func (*Element) GetAttr

func (e *Element) GetAttr(key string) string

GetAttr returns the value of the attribute with the given key.

func (*Element) HasAttrValueLower

func (e *Element) HasAttrValueLower(key, val string) bool

HasAttrValue returns true if the node has an attribute with the given key and value.

func (*Element) HasRoot

func (e *Element) HasRoot(root *Element) bool

HasRoot returns true if the node has the given root node as parent.

func (*Element) ID

func (e *Element) ID() string

ID returns the value of the id attribute.

func (*Element) InputText

func (e *Element) InputText(selector string) []*Element

func (*Element) NodeType

func (e *Element) NodeType() html.NodeType

func (*Element) ParentElement

func (e *Element) ParentElement() *Element

ParentElement returns the parent element of the node.

func (*Element) Query

func (e *Element) Query(selector string) []*Element

func (*Element) QuerySelector

func (e *Element) QuerySelector(selector *Selector) []*Element

func (*Element) RemoveAttr

func (e *Element) RemoveAttr(key string) *Element

RemoveAttr removes the attribute with the given key from the node.

func (*Element) RemoveClass

func (e *Element) RemoveClass(class string) *Element

RemoveClass removes the class from the class attribute of the node.

func (*Element) Render

func (e *Element) Render(w io.Writer, checker ...Checker) error

func (*Element) SetA

func (e *Element) SetA(attr ...Attribute) *Element

SetA appends the given attributes to the attributes of the node. If keys are already in the attributes of the node, the values are overwritten.

func (*Element) SetBoolA

func (e *Element) SetBoolA(key string, v bool) *Element

SetBoolA appends the given boolean attributes to the attributes of the node or removes them.

func (*Element) SetClasses

func (e *Element) SetClasses(classes ...string) *Element

SetClasses sets the given class to the class attribute of the node. Class duplication check is performed.

type ElementChild

type ElementChild interface {
	ParentElement() *Element
}

type Node

type Node interface {
	NodeType() html.NodeType
}

type RawText

type RawText html.Node

func RawT

func RawT(text ...string) *RawText

RawT creates a new text node with the given text.

func (*RawText) NodeType

func (t *RawText) NodeType() html.NodeType

func (*RawText) ParentElement

func (t *RawText) ParentElement() *Element

type Selector

type Selector css.Selector

func SelectorMustParse

func SelectorMustParse(s string) *Selector

SelectorMustParse parses the given CSS selector and panics if it fails.

func SelectorParse

func SelectorParse(s string) (*Selector, error)

SelectorParse parse the givent CSS selector

type Text

type Text html.Node

func T

func T(text ...string) *Text

T creates a new text node with the given text.

func (*Text) Clone

func (t *Text) Clone() *Text

func (*Text) NodeType

func (t *Text) NodeType() html.NodeType

func (*Text) ParentElement

func (t *Text) ParentElement() *Element

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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