htmlutil

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2022 License: MIT Imports: 6 Imported by: 0

README

htmlutil

html を雑に抽出できるようにする go ライブラリ

Install

$ go get github.com/thamaji/htmlutil

Example

Code

package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/thamaji/htmlutil"
)

var html = `<table>
	<tbody>
		<tr>
			<td>1</td>
			<td>2</td>
			<td>3</td>
		</tr>
		<tr>
			<td>A</td>
			<td>B</td>
			<td>C</td>
		</tr>
		<tr>
			<td>ひとつめ</td>
			<td>ふたつめ</td>
			<td>みっつめ</td>
		</tr>
	</tbody>
</table>
`

func main() {
	doc, err := htmlutil.ParseFragment(strings.NewReader(html), htmlutil.ContextElem("body"))
	if err != nil {
		log.Fatal(err)
	}

	doc.FindAll(htmlutil.Tree(htmlutil.Elem("table"), htmlutil.Elem("tbody"), htmlutil.Elem("tr"))).Each(func(tr htmlutil.Selection) error {
		values, _ := tr.FindAll(htmlutil.Elem("td")).MapString(func(td htmlutil.Selection) (string, error) {
			return td.Text(htmlutil.DefaultFormatter), nil
		})

		fmt.Println(values)

		return nil
	})
}

Output

[1 2 3]
[A B C]
[ひとつめ ふたつめ みっつめ]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfluenceStorageFormatter

func ConfluenceStorageFormatter(node *html.Node) (string, bool)

func ConfluenceViewFormatter

func ConfluenceViewFormatter(node *html.Node) (string, bool)

func ContextAtom

func ContextAtom(atom atom.Atom) *html.Node

func ContextElem

func ContextElem(elem string) *html.Node

func DefaultFormatter

func DefaultFormatter(node *html.Node) (string, bool)

func MapSelection

func MapSelection[T any](selection Selection, f func(Selection) (T, error)) ([]T, error)

Types

type Formatter

type Formatter func(*html.Node) (string, bool)

type Selection

type Selection []*html.Node

func Parse

func Parse(r io.Reader) (Selection, error)

func ParseFragment

func ParseFragment(r io.Reader, context *html.Node) (Selection, error)

func (Selection) AppendChild

func (selection Selection) AppendChild(nodes ...*html.Node)

func (Selection) AppendNext

func (selection Selection) AppendNext(nodes ...*html.Node)

func (Selection) AppendPrev

func (selection Selection) AppendPrev(nodes ...*html.Node)

func (Selection) Children

func (selection Selection) Children() Selection

func (Selection) Contains

func (selection Selection) Contains(selector Selector) bool

func (Selection) Each

func (selection Selection) Each(f func(Selection) error) error

func (Selection) Filter

func (selection Selection) Filter(selector Selector) Selection

func (Selection) Find

func (selection Selection) Find(selector Selector) Selection

func (Selection) FindAll

func (selection Selection) FindAll(selector Selector) Selection

func (Selection) First

func (selection Selection) First() Selection

func (Selection) Get

func (selection Selection) Get(index int) Selection

func (Selection) Last

func (selection Selection) Last() Selection

func (Selection) Len

func (selection Selection) Len() int

func (Selection) MapBool

func (selection Selection) MapBool(f func(Selection) (bool, error)) ([]bool, error)

func (Selection) MapByte

func (selection Selection) MapByte(f func(Selection) (byte, error)) ([]byte, error)

func (Selection) MapBytes

func (selection Selection) MapBytes(f func(Selection) ([]byte, error)) ([][]byte, error)

func (Selection) MapComplex128

func (selection Selection) MapComplex128(f func(Selection) (complex128, error)) ([]complex128, error)

func (Selection) MapComplex64

func (selection Selection) MapComplex64(f func(Selection) (complex64, error)) ([]complex64, error)

func (Selection) MapFloat32

func (selection Selection) MapFloat32(f func(Selection) (float32, error)) ([]float32, error)

func (Selection) MapFloat64

func (selection Selection) MapFloat64(f func(Selection) (float64, error)) ([]float64, error)

func (Selection) MapInt

func (selection Selection) MapInt(f func(Selection) (int, error)) ([]int, error)

func (Selection) MapInt16

func (selection Selection) MapInt16(f func(Selection) (int16, error)) ([]int16, error)

func (Selection) MapInt32

func (selection Selection) MapInt32(f func(Selection) (int32, error)) ([]int32, error)

func (Selection) MapInt64

func (selection Selection) MapInt64(f func(Selection) (int64, error)) ([]int64, error)

func (Selection) MapInt8

func (selection Selection) MapInt8(f func(Selection) (int8, error)) ([]int8, error)

func (Selection) MapRune

func (selection Selection) MapRune(f func(Selection) (rune, error)) ([]rune, error)

func (Selection) MapRunes

func (selection Selection) MapRunes(f func(Selection) ([]rune, error)) ([][]rune, error)

func (Selection) MapString

func (selection Selection) MapString(f func(Selection) (string, error)) ([]string, error)

func (Selection) MapUint

func (selection Selection) MapUint(f func(Selection) (uint, error)) ([]uint, error)

func (Selection) MapUint16

func (selection Selection) MapUint16(f func(Selection) (uint16, error)) ([]uint16, error)

func (Selection) MapUint32

func (selection Selection) MapUint32(f func(Selection) (uint32, error)) ([]uint32, error)

func (Selection) MapUint64

func (selection Selection) MapUint64(f func(Selection) (uint64, error)) ([]uint64, error)

func (Selection) MapUint8

func (selection Selection) MapUint8(f func(Selection) (uint8, error)) ([]uint8, error)

func (Selection) MapUintptr

func (selection Selection) MapUintptr(f func(Selection) (uintptr, error)) ([]uintptr, error)

func (Selection) Nodes

func (selection Selection) Nodes() []*html.Node

func (Selection) Parents

func (selection Selection) Parents() Selection

func (Selection) Render

func (selection Selection) Render(w io.Writer) error

func (Selection) String

func (selection Selection) String() string

func (Selection) Text

func (selection Selection) Text(formatter Formatter) string

type Selector

type Selector func(*html.Node) bool

func And

func And(selector Selector, selectors ...Selector) Selector

func Atom

func Atom(atom atom.Atom) Selector

func Attr

func Attr(key string, val string) Selector

func Class

func Class(class string) Selector

func Elem

func Elem(elem string) Selector

func HasAttr

func HasAttr(key string) Selector

func ID

func ID(id string) Selector

func Includes

func Includes(selector1 Selector, selector2 ...Selector) Selector

func Not

func Not(selector Selector) Selector

func Or

func Or(selector Selector, selectors ...Selector) Selector

func Tree

func Tree(selector1 Selector, selector2 ...Selector) Selector

func (Selector) After

func (selector Selector) After() Selector

func (Selector) And

func (selector Selector) And(selector2 Selector) Selector

func (Selector) Before

func (selector Selector) Before() Selector

func (Selector) FirstChild

func (selector Selector) FirstChild() Selector

func (Selector) Not

func (selector Selector) Not() Selector

func (Selector) Or

func (selector Selector) Or(selector2 Selector) Selector

type Table

type Table [][]string

func ParseTables

func ParseTables(selection Selection, formatter Formatter) []Table

func (Table) Node

func (table Table) Node() *html.Node

func (Table) Render

func (table Table) Render(w io.Writer) error

Jump to

Keyboard shortcuts

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