transform

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2011 License: Artistic-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Copyright 2010 Jeremy Wall (jeremy@marzhillstudios.com) Use of this source code is governed by the Artistic License 2.0. That License is included in the LICENSE file.

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)
 	sel1 := NewSelector("li.menuitem")
 	sel2 := NewSelector("a")
	t := NewTransform(doc)
 	t.Apply(CopyAnd(myModifiers...), sel1)
  t..Apply(Replace(Text("my new text"), sel2)
  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 HtmlString

func HtmlString(str string) []*Node

Constructs a slice of *Nodes from a string of html.

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 Text

func Text(str string) *Node

Constructs a TextNode for the string passed in

Types

type Document

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

Document is the type of a parsed html string.

func NewDoc

func NewDoc(s string) *Document

NewDoc is a constructor for a Document.

func (*Document) Clone

func (d *Document) Clone() *Document

The Clone method creates a deep copy of the Document.

func (*Document) FindAll

func (d *Document) FindAll(f func(*Node) bool) *v.Vector

The FindAll method searches the Document's node tree for anything the passed in function returns true for. Returns a vector of the found nodes.

func (*Document) Nodes

func (d *Document) Nodes() []*Node

The Nodes method returns the parsed nodes of the html string. There may be multiple nodes if the parsed string was fragment and not a full document. Returns a []*Node.

func (Document) String

func (d Document) String() string

func (*Document) Top

func (d *Document) Top() *Node

The Top Method returns the root node of the parsed html string. This node is not a parsed html node it is empty. The actual parsed nodes can be found by calling the Nodes method. This allows a Document to contain a full html document or partial fragment. Returns a *Node.

func (*Document) Walk

func (d *Document) Walk(f func(*Node))

The Walk method walks a Documents node tree running The passed in function on it.

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 *Document) []*Node

Apply the css selector to a document. Returns a Vector of nodes that the selector matched.

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 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.

type Transformer

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

Transformer encapsulates a document under transformation.

func NewTransform

func NewTransform(d *Document) *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

The Apply method applies a TransformFunc to the nodes returned from the Selector query

func (*Transformer) Doc

func (t *Transformer) Doc() *Document

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