transform

package
v0.0.0-...-3f75658 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2014 License: Artistic-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Package transform implements a html css selector and transformer.

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

tree, _ := h5.New(rdr)
t := transform.New(Tree)
t.Apply(CopyAnd(myModifiers...), "li.menuitem")
t.Apply(Replace(Text("my new text"), "a")

You can use the building blocks in this package as an html templating engine. the basic principle behind html transform is that your template is just data the same as your fill data. You use functions to mix and munge these two types of data and get a third type of data back out which you can use to render.

You can also use this package to slice up and retrieve data out of an html page. You can build scrapers and even mash up two different html documents.

How to do common templating actions.

How do I loop over input data?

Just because you don't have a for statement in your templating language doesn't mean you can't loop over your data. You just have to loop over before you insert it.

If we have template that looks like this:

<html><body><ul id="people"><li>some guy</ul></body></html>

Then we can turn that list of people into a list of Me, Myself, and I like so:

// We are going to want to set up the transformations for each person
// in our list.
liTransfroms := []TransformFuncs
for i, item := range []string{"Me", "Myself", "I"} {
    liTransforms = append(menuTransforms, ReplaceChildren(Text(item)))
}
// Find the li in the ul element with the id='people' and make one copy of
// it for each item from above with the text content replaced with that
// item
 t.Apply(CopyAnd(liTransforms...), "ul#people", "li")

How do I change an html elements contents?

t.Apply(ReplaceChildren(Text("some text contents"), "#SomeElement")

How do I remove/replace an html element.

// Remove the element
T.Apply(Replace(), "#SomeElement")
// Replace the element
node, _ := NewDoc("<span>hello</span>")
T.Apply(Replace(node), "#SomeElement")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

type Collector interface {
	// Find searches a tree rooted at n and returns a slice of nodes
	// that match a criteria.
	Find(n *html.Node) []*html.Node
}

Collector defines an interface for html node collectors.

type CollectorFunc

type CollectorFunc func(n *html.Node) []*html.Node

func FirstMatch

func FirstMatch(cs ...Collector) CollectorFunc

func (CollectorFunc) Find

func (f CollectorFunc) Find(n *html.Node) []*html.Node

type Transform

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

Transform is a bundle of selectors and a transform func. It forms a self contained Transfrom on an html document that can be reused.

func MustTrans

func MustTrans(f TransformFunc, sel string) *Transform

MustTrans creates a Transform. Panics if the selector wasn't valid.

func Trans

func Trans(f TransformFunc, sel string) (*Transform, error)

Trans creates a Transform that you can apply using ApplyAll. It takes a TransformFunc and a valid CSS3 Selector. It returns a *Transform or an error if the selector wasn't valid

func TransCollector

func TransCollector(f TransformFunc, coll Collector) *Transform

TransCollector creates a Transform that you can apply using ApplyAll. It takes a TransformFunc and a Collector

type TransformFunc

type TransformFunc func(*html.Node)

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

func AppendChildren

func AppendChildren(cs ...*html.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 html.Nodes.

func DoAll

func DoAll(fs ...TransformFunc) TransformFunc

DoAll returns a TransformFunc that combines all the TransformFuncs that are passed in. Doing each transform in order.

func ModifyAttrib

func ModifyAttrib(key string, val string) TransformFunc

ModifyAttrb creates a TransformFunc that modifies the attributes of the node it operates on. If an Attribute with the same name as the key doesn't exist it creates it.

func MustSubtransform

func MustSubtransform(f TransformFunc, sel string) TransformFunc

MustSubtransform constructs a TransformFunc that runs a TransformFunc on any nodes in the tree rooted by the node the the TransformFunc is run against. Panics if the selector string is malformed.

func PrependChildren

func PrependChildren(cs ...*html.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 ...*html.Node) TransformFunc

Replace constructs a TransformFunc that replaces a node with the nodes passed in.

func ReplaceChildren

func ReplaceChildren(ns ...*html.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, sel string) (TransformFunc, error)

SubTransform constructs a TransformFunc that runs a TransformFunc on any nodes in the tree rooted by the node the the TransformFunc is run against. This is useful for creating self contained Transforms that are meant to work on subtrees of the html document.

func SubtransformCollector

func SubtransformCollector(f TransformFunc, coll Collector) TransformFunc

SubTransformSelector constructs a TransformFunc that runs a TransformFunc on any nodes collected, using the passed in collector, from the subtree the TransformFunc is run on. This is useful for creating self contained Transforms that are meant to work on subtrees of the html document.

func Trace

func Trace(f TransformFunc, traceFunc func(msg string, args ...interface{}), msg string, args ...interface{}) TransformFunc

Trace is a debugging wrapper for transform funcs. It calls traceFunc with debugging information before and after the TransformFunc is applied.

func TransformAttrib

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

TransformAttrib returns a TransformFunc that transforms an attribute on the node it operates on using the provided func. It only transforms the attribute if it exists.

type Transformer

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

Transformer encapsulates a document under transformation.

func New

func New(t *h5.Tree) *Transformer

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

func NewFromReader

func NewFromReader(rdr io.Reader) (*Transformer, error)

func (*Transformer) Apply

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

The ApplyWithSelector method applies a TransformFunc to the nodes matched by the CSS3 Selector.

func (*Transformer) ApplyAll

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

ApplyAll applies a series of Transforms to a document.

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

func (*Transformer) ApplyToFirstMatch

func (t *Transformer) ApplyToFirstMatch(f TransformFunc, sels ...string) error

func (*Transformer) ApplyWithCollector

func (t *Transformer) ApplyWithCollector(f TransformFunc, coll Collector)

ApplyWithCollector applies a TransformFunc to the tree using a Collector.

func (*Transformer) Clone

func (t *Transformer) Clone() *Transformer

func (*Transformer) Doc

func (t *Transformer) Doc() *html.Node

The Doc method returns the document under transformation.

func (*Transformer) Render

func (t *Transformer) Render(w io.Writer) error

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