kiteroot

package module
v0.0.0-...-caa194c Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2017 License: MIT Imports: 6 Imported by: 0

README

kiteroot

kiteroot is a convenient tool to play with html tag for Go.

The project is inspired by BeautifulSoup written for Python.

To select tags after parse, you can use these functions: Find, FindAll, FindWithAttrs, FindAllWithAttrs.

var r io.Reader
...

doc, _ := kiteroot.Parse(r) 

// you can get urls of all links in the document you parse like this:
links := doc.FindAll("a")
for _, link := links {
  fmt.Println(link.Attribute("href"))
}

// you can also find a tag containing specific attributes.
title := doc.FindWithAttrs("span", kiteroot.MakeAttrs("class", "title"))
// And it is same with: title := doc.Find("span", "class", "title")

Documentation

Index

Constants

View Source
const LineSeparator = "\n"

LineSeparator is the carrage return.

Variables

View Source
var ErrInvalidPair = errors.New("open/close tag mismatched")

ErrInvalidPair is error returned by failures to parse an HTML.

Functions

This section is empty.

Types

type Attributes

type Attributes map[string]string

An Attributes stores key/value attribute pairs in the tag.

func MakeAttrs

func MakeAttrs(s ...string) (attrs Attributes)

MakeAttrs returns Attribute consisting of pairs. this function needs the even number of strings to make key/value pairs. so, if length of string slice is odd number, the last is dropped.

type Element

type Element struct {
	Type     ElementType
	Content  string
	Attrs    Attributes
	Children []*Element
	// contains filtered or unexported fields
}

An Element consists of ElementType and Content

func NewDocument

func NewDocument() *Element

NewDocument returns a new document type element.

func NewTag

func NewTag(name string, selfClosing bool) *Element

NewTag returns tag typed element for the given name and selfClosing.

func NewText

func NewText(content string) *Element

NewText returns a new text type element for the given content.

func Parse

func Parse(r io.Reader) (*Element, error)

Parse returns an document element tree for the HTML from the given Reader.

func (*Element) Append

func (e *Element) Append(elem *Element)

Append appends an element into its children slice in this element.

func (*Element) Attribute

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

Attribute returns the value matching with the key. if the Attrs doesn't have the key, empty string is returned.

func (*Element) Find

func (e *Element) Find(tagName string, attrs ...string) *Element

Find returns an element containing attrs with the same tag throughout its subelements. if no element is found, this function returns nil. attrs is mapped to Attributes by calling MakeAttrs

func (*Element) FindAll

func (e *Element) FindAll(tagName string, attrs ...string) []*Element

FindAll returns all elements containing attrs with the same tag in the subelements. attrs is mapped to Attributes by calling MakeAttrs

func (*Element) FindAllWithAttrs

func (e *Element) FindAllWithAttrs(tagName string, attrs Attributes) []*Element

FindAllWithAttrs returns all elements containing attrs with the same tag in the subelements.

func (*Element) FindWithAttrs

func (e *Element) FindWithAttrs(tagName string, attrs Attributes) *Element

FindWithAttrs returns an element containing attrs with the same tag in the subelements. if no such element exists, returns nil.

func (*Element) SetAttribute

func (e *Element) SetAttribute(key, value string)

SetAttribute puts the given key-value pair into the attribute map in this element.

func (*Element) String

func (e *Element) String() string

String returns a content according to its type.

func (*Element) Text

func (e *Element) Text() string

Text returns a concatenated text of TextType elements in the children.

type ElementType

type ElementType uint32

An ElementType is the type of Element.

const (
	DocumentType ElementType = iota
	TagType
	TextType
)

These are types for Element

type Stack

type Stack []*Element

Stack implements the stack container

func (*Stack) Len

func (s *Stack) Len() int

Len returns the number of element currently in the stack.

func (*Stack) Pop

func (s *Stack) Pop() (e *Element)

Pop removes top element from stack and returns it. if stack is empty, nil is returned.

func (*Stack) Push

func (s *Stack) Push(e *Element)

Push adds the given element to the stack.

func (*Stack) Top

func (s *Stack) Top() (e *Element)

Top returns the last element in the stack if it is not empty. Otherwise, nil is returned.

Jump to

Keyboard shortcuts

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