go-html-transform: github.com/rtfb/go-html-transform/html/transform Index | Files

package transform

import "github.com/rtfb/go-html-transform/html/transform"

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")


Package Files

doc.go transform.go

type Collector Uses

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 Uses

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

func FirstMatch Uses

func FirstMatch(cs ...Collector) CollectorFunc

func (CollectorFunc) Find Uses

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

type Transform Uses

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 Uses

func MustTrans(f TransformFunc, sel string) *Transform

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

func Trans Uses

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 Uses

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 Uses

type TransformFunc func(*html.Node)

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

func AppendChildren Uses

func AppendChildren(cs ...*html.Node) TransformFunc

AppendChildren creates a TransformFunc that appends the Children passed in.

func CopyAnd Uses

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 Uses

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 Uses

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 Uses

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 Uses

func PrependChildren(cs ...*html.Node) TransformFunc

PrependChildren creates a TransformFunc that prepends the Children passed in.

func RemoveChildren Uses

func RemoveChildren() TransformFunc

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

func Replace Uses

func Replace(ns ...*html.Node) TransformFunc

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

func ReplaceChildren Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

type Transformer struct {
    // contains filtered or unexported fields

Transformer encapsulates a document under transformation.

func New Uses

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 Uses

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

func (*Transformer) Apply Uses

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 Uses

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 Uses

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

func (*Transformer) ApplyWithCollector Uses

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

ApplyWithCollector applies a TransformFunc to the tree using a Collector.

func (*Transformer) Clone Uses

func (t *Transformer) Clone() *Transformer

func (*Transformer) Doc Uses

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

The Doc method returns the document under transformation.

func (*Transformer) Render Uses

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

func (*Transformer) String Uses

func (t *Transformer) String() string

Package transform imports 5 packages (graph) and is imported by 1 packages. Updated 2018-01-23. Refresh now. Tools for package owners.