transform

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2012 License: Artistic-2.0 Imports: 4 Imported by: 0

Documentation

Overview

The html transform package implements a html css selector and transformer.

An html doc can be inspected and queried using css selectors as well as transformed.

doc := NewDoc(str)
t := NewTransform(doc)
t.Apply(CopyAnd(myModifiers...), "li.menuitem")
t.Apply(Replace(Text("my new text"), "a")
newDoc := t.Doc()

Index

Constants

View Source
const (
	TAGNAME byte = iota // Tagname Selector Type
	CLASS   byte = '.'  // Class SelectorPart Type
	ID      byte = '#'  // Id  SelectorPart Type
	PSEUDO  byte = ':'  // Pseudo SelectoPart Type
	ANY     byte = '*'  // Any tag Selector Type
	ATTR    byte = '['  // Attr Selector Type
)
View Source
const (
	// Important characters in a Selector string
	SELECTOR_CHARS string = ".:#["
)

Variables

This section is empty.

Functions

func MergeSelectors

func MergeSelectors(sel1 *Selector, sel2 *Selector)

MergeSelectors merges two *Selector types into one *Selector. It merges the second selector into the first modifying the first selector.

func NewDoc

func NewDoc(str string) (*Node, error)

func NewDocFromReader

func NewDocFromReader(rdr io.Reader) (*Node, error)

Types

type Selector

type Selector struct {
	TagName string // "*" for any tag otherwise the name of the tag
	Parts   []SelectorPart
	Attr    map[string]string
}

Selector is the type of a single selector in a selector query. A slice of Selectors makes up a SelectorQuery.

func NewSelector

func NewSelector(str string) *Selector

NewSelector is a constructor for a *Selector type. It creates a Selector by parsing the string passed in.

func (*Selector) Match

func (sel *Selector) Match(node *Node) bool

The Match method tests if a Selector matches a Node. Returns true for a match false otherwise.

type SelectorPart

type SelectorPart struct {
	Type byte   // a bitmask of the selector types
	Val  string // the value we are matching against
}

SelectorPart is the type of a single Selector's Class, ID or Pseudo part.

type SelectorQuery

type SelectorQuery []*Selector

SelectorQuery is the type of a CSS Selector Query. Each Selector in the slice is operated on order with subsequent selectors matching against the descendants of the previous selectors match.

func NewSelectorQuery

func NewSelectorQuery(sel ...string) SelectorQuery

NewSelectorQuery is a constructor for a SelectorQuery It creates the query using the strings passed in.

func (SelectorQuery) Apply

func (sel SelectorQuery) Apply(doc *Node) []*Node

TODO(jwall): Apply multiple Selectors to a node tree in one pass. Apply the css selector to a document. Returns a Vector of nodes that the selector matched.

type Transform

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

func Trans

func Trans(f TransformFunc, sel1 string, sel ...string) Transform

Spec creates a Transform that you can apply using ApplyAll.

type TransformFunc

type TransformFunc func(*Node)

The TransformFunc type is the type of a Node transformation function.

func AppendChildren

func AppendChildren(cs ...*Node) TransformFunc

AppendChildren creates a TransformFunc that appends the Children passed in.

func Compose

func Compose(fs ...TransformFunc) TransformFunc

Compose a set of TransformFuncs into a single TransformFunc

func CopyAnd

func CopyAnd(fns ...TransformFunc) TransformFunc

CopyAnd will construct a TransformFunc that will make a copy of the node for each passed in TransformFunc And replace the passed in node with the resulting transformed Nodes. Returns a TransformFunc

func DoAll

func DoAll(fs ...TransformFunc) TransformFunc

func ForEach

func ForEach(f interface{}, ns ...*Node) TransformFunc

ForEach takes a function and a list of Nodes and performs that function for each node in the list. The function should be of a type either func(...*Node) TransformFunc or func(*Node) TransformFunc. Any other type will panic. Returns a TransformFunc.

func ModifyAttrib

func ModifyAttrib(key string, val string) TransformFunc

ModifyAttrb creates a TransformFunc that modifies the attributes of the node it operates on.

func PrependChildren

func PrependChildren(cs ...*Node) TransformFunc

PrependChildren creates a TransformFunc that prepends the Children passed in.

func RemoveChildren

func RemoveChildren() TransformFunc

RemoveChildren creates a TransformFunc that removes the Children of the node it operates on.

func Replace

func Replace(ns ...*Node) TransformFunc

func ReplaceChildren

func ReplaceChildren(ns ...*Node) TransformFunc

ReplaceChildren creates a TransformFunc that replaces the Children of the node it operates on with the Children passed in.

func SubTransform

func SubTransform(f TransformFunc, sel1 string, sels ...string) TransformFunc

func TransformAttrib

func TransformAttrib(key string, f func(string) string) TransformFunc

type Transformer

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

Transformer encapsulates a document under transformation.

func NewTransform

func NewTransform(d *Node) *Transformer

Constructor for a Transformer. It makes a copy of the document and transforms that instead of the original.

func (*Transformer) Apply

func (t *Transformer) Apply(f TransformFunc, sel ...string) *Transformer

TODO(jwall): TransformApplication type that we can process the doc in one pass. The Apply method applies a TransformFunc to the nodes returned from the Selector query

func (*Transformer) ApplyAll

func (t *Transformer) ApplyAll(ts ...Transform) *Transformer

ApplyAll applies a series of Transforms to a document.

t.ApplyAll(Trans(f, sel1, sel2), Trans(f2, sel3, sel4))

func (*Transformer) Clone

func (t *Transformer) Clone() *Transformer

func (*Transformer) Doc

func (t *Transformer) Doc() *Node

The Doc method returns the document under transformation.

func (*Transformer) String

func (t *Transformer) String() string

Jump to

Keyboard shortcuts

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