lolhtml

package module
v0.0.0-...-7fe2ad7 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2020 License: BSD-3-Clause Imports: 4 Imported by: 0

README

lolhtml

Go Report Card

Go bindings for cloudflare/lol-html, the Low Output Latency streaming HTML rewriter/parser with CSS-selector based API.

Status: All abilities provided by C-API implemented, except for customized user data in handlers. The code is at its early stage and the API is therefore subject to change. If you have any ideas on how API can be better structured, feel free to open a PR or an issue.

Installation

Rust is required to build the lol-html library.

For Linux:

git clone --recursive https://github.com/coolspring8/lolhtml.git
cargo build --release --manifest-path ./lol-html/c-api/ --target-dir ./
go intall

For Windows users, as Rust relies on MSVC toolchain by default, one more step is needed between cargo build and go install: create a .a file from compiled artifacts. This snippet works for me:

gendef ./release/lolhtml.dll
dlltool --as-flags=--64 -m i386:x86-64 -k --output-lib ./lolhtml.a --input-def lolhtml.def
cp ./release/lolhtml.dll ./

Now let's initialize a project and create main.go:

package main

import (
    "fmt"
    "github.com/coolspring8/lolhtml"
)

func main() {
	rb := lolhtml.NewRewriterBuilder()
	defer rb.Free()
	s, _ := lolhtml.NewSelector("span")
	defer s.Free()
	rb.AddElementContentHandlers(
		s,
		func(e *lolhtml.Element) lolhtml.RewriterDirective {
			e.SetInnerContentAsRaw("LOL-HTML")
			return lolhtml.Continue
		},
		nil,
		func(*lolhtml.TextChunk) lolhtml.RewriterDirective {
			return lolhtml.Continue
		},
	)
	r, _ := rb.Build(
		lolhtml.Config{
			Encoding: "utf-8",
			Memory: &lolhtml.MemorySettings{
				PreallocatedParsingBufferSize: 1024,
				MaxAllowedMemoryUsage:         1<<63 - 1,
			},
			Sink:   func(s string) { fmt.Print(s) },
			Strict: true,
		},
	)
	defer r.Free()
	r.WriteString("<p>Hello <span>")
	r.WriteString("World</span>!</p>")
	r.End()
}

The above program takes chunked input <p>Hello <span>World</span>!</p>, rewrites texts in span tags to "LOL-HTML" and prints the result to standard output. The result is <p>Hello <span>LOL-HTML</span>!</p> .

Documentation

Available at pkg.go.dev. (WIP)

Known Issue

  • For now, to use Rewriter.End() without causing panic, you will probably need to assign a stub DocEndHandler function when calling AddDocumentContentHandlers().

Other Bindings

License

BSD 3-Clause "New" or "Revised" License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCannotGetErrorMessage = errors.New("cannot get error message from underlying lol-html lib")

Functions

This section is empty.

Types

type Attribute

type Attribute C.lol_html_attribute_t

Attribute as declared in include/lol_html.h:30

func (*Attribute) GetName

func (a *Attribute) GetName() string

func (*Attribute) GetValue

func (a *Attribute) GetValue() string

type AttributeIterator

type AttributeIterator C.lol_html_attributes_iterator_t

AttributeIterator as declared in include/lol_html.h:29

func (*AttributeIterator) Free

func (ai *AttributeIterator) Free()

func (*AttributeIterator) Next

func (ai *AttributeIterator) Next() *Attribute

type Comment

type Comment C.lol_html_comment_t

Comment as declared in include/lol_html.h:26

func (*Comment) GetText

func (c *Comment) GetText() string

func (*Comment) InsertAfterAsHtml

func (c *Comment) InsertAfterAsHtml(content string) error

func (*Comment) InsertAfterAsRaw

func (c *Comment) InsertAfterAsRaw(content string) error

func (*Comment) InsertBeforeAsHtml

func (c *Comment) InsertBeforeAsHtml(content string) error

func (*Comment) InsertBeforeAsRaw

func (c *Comment) InsertBeforeAsRaw(content string) error

func (*Comment) IsRemoved

func (c *Comment) IsRemoved() bool

func (*Comment) Remove

func (c *Comment) Remove()

func (*Comment) ReplaceAsHtml

func (c *Comment) ReplaceAsHtml(content string) error

func (*Comment) ReplaceAsRaw

func (c *Comment) ReplaceAsRaw(content string) error

func (*Comment) SetText

func (c *Comment) SetText(text string) error

type CommentHandler

type CommentHandler func(*Comment) RewriterDirective

CommentHandler type as declared in include/lol_html.h:91

type Config

type Config struct {
	Encoding string
	Memory   *MemorySettings
	Sink     OutputSink
	//UserData interface{}
	Strict bool
}

func NewDefaultConfig

func NewDefaultConfig() Config

type DocEnd

type DocEnd C.lol_html_doc_end_t

DocEnd as declared in include/lol_html.h:25

func (*DocEnd) AppendAsHtml

func (d *DocEnd) AppendAsHtml(content string) error

func (*DocEnd) AppendAsRaw

func (d *DocEnd) AppendAsRaw(content string) error

type DocEndHandler

type DocEndHandler func(*DocEnd) RewriterDirective

DocEndHandler type as declared in include/lol_html.h:106

type Doctype

type Doctype C.lol_html_doctype_t

Doctype as declared in include/lol_html.h:24

func (*Doctype) GetName

func (d *Doctype) GetName() string

func (*Doctype) GetPublicId

func (d *Doctype) GetPublicId() string

func (*Doctype) GetSystemId

func (d *Doctype) GetSystemId() string

type DoctypeHandler

type DoctypeHandler func(*Doctype) RewriterDirective

DoctypeHandler type as declared in include/lol_html.h:86

type Element

type Element C.lol_html_element_t

Element as declared in include/lol_html.h:28

func (*Element) GetAttributeIterator

func (e *Element) GetAttributeIterator() *AttributeIterator

func (*Element) GetAttributeValue

func (e *Element) GetAttributeValue(name string) (string, error)

func (*Element) GetNamespaceUri

func (e *Element) GetNamespaceUri() string

func (*Element) GetTagName

func (e *Element) GetTagName() string

func (*Element) HasAttribute

func (e *Element) HasAttribute(name string) (bool, error)

func (*Element) InsertAfterEndTagAsHtml

func (e *Element) InsertAfterEndTagAsHtml(content string) error

func (*Element) InsertAfterEndTagAsRaw

func (e *Element) InsertAfterEndTagAsRaw(content string) error

func (*Element) InsertAfterStartTagAsHtml

func (e *Element) InsertAfterStartTagAsHtml(content string) error

func (*Element) InsertAfterStartTagAsRaw

func (e *Element) InsertAfterStartTagAsRaw(content string) error

func (*Element) InsertBeforeEndTagAsHtml

func (e *Element) InsertBeforeEndTagAsHtml(content string) error

func (*Element) InsertBeforeEndTagAsRaw

func (e *Element) InsertBeforeEndTagAsRaw(content string) error

func (*Element) InsertBeforeStartTagAsHtml

func (e *Element) InsertBeforeStartTagAsHtml(content string) error

func (*Element) InsertBeforeStartTagAsRaw

func (e *Element) InsertBeforeStartTagAsRaw(content string) error

func (*Element) IsRemoved

func (e *Element) IsRemoved() bool

func (*Element) Remove

func (e *Element) Remove()

func (*Element) RemoveAndKeepContent

func (e *Element) RemoveAndKeepContent()

func (*Element) RemoveAttribute

func (e *Element) RemoveAttribute(name string) error

func (*Element) ReplaceAsHtml

func (e *Element) ReplaceAsHtml(content string) error

func (*Element) ReplaceAsRaw

func (e *Element) ReplaceAsRaw(content string) error

func (*Element) SetAttribute

func (e *Element) SetAttribute(name string, value string) error

func (*Element) SetInnerContentAsHtml

func (e *Element) SetInnerContentAsHtml(content string) error

func (*Element) SetInnerContentAsRaw

func (e *Element) SetInnerContentAsRaw(content string) error

func (*Element) SetTagName

func (e *Element) SetTagName(name string) error

type ElementHandler

type ElementHandler func(*Element) RewriterDirective

ElementHandler type as declared in include/lol_html.h:101

type MemorySettings

type MemorySettings struct {
	PreallocatedParsingBufferSize int
	MaxAllowedMemoryUsage         int
}

type OutputSink

type OutputSink func(string)

type Rewriter

type Rewriter C.lol_html_rewriter_t

Rewriter as declared in include/lol_html.h:23

func (*Rewriter) End

func (r *Rewriter) End() error

func (*Rewriter) Free

func (r *Rewriter) Free()

func (*Rewriter) WriteString

func (r *Rewriter) WriteString(chunk string) error

type RewriterBuilder

type RewriterBuilder C.lol_html_rewriter_builder_t

RewriterBuilder as declared in include/lol_html.h:22

func NewRewriterBuilder

func NewRewriterBuilder() *RewriterBuilder

func (*RewriterBuilder) AddDocumentContentHandlers

func (rb *RewriterBuilder) AddDocumentContentHandlers(
	doctypeHandler DoctypeHandler,
	commentHandler CommentHandler,
	textChunkHandler TextChunkHandler,
	docEndHandler DocEndHandler,
)

TODO: BUG? For now, to use *Rewriter.End() without causing panic, you will probably need to assign a stub handler function to it.

func (*RewriterBuilder) AddElementContentHandlers

func (rb *RewriterBuilder) AddElementContentHandlers(
	selector *Selector,
	elementHandler ElementHandler,
	commentHandler CommentHandler,
	textChunkHandler TextChunkHandler,
)

func (*RewriterBuilder) Build

func (rb *RewriterBuilder) Build(config Config) (*Rewriter, error)

func (*RewriterBuilder) Free

func (rb *RewriterBuilder) Free()

type RewriterDirective

type RewriterDirective int

RewriterDirective as declared in include/lol_html.h:84

const (
	Continue RewriterDirective = iota
	Stop
)

RewriterDirective enumeration from include/lol_html.h:84

type Selector

type Selector C.lol_html_selector_t

Selector as declared in include/lol_html.h:31

func NewSelector

func NewSelector(selector string) (*Selector, error)

func (*Selector) Free

func (s *Selector) Free()

type TextChunk

type TextChunk C.lol_html_text_chunk_t

TextChunk as declared in include/lol_html.h:27

func (*TextChunk) GetContent

func (t *TextChunk) GetContent() string

func (*TextChunk) InsertAfterAsHtml

func (t *TextChunk) InsertAfterAsHtml(content string) error

func (*TextChunk) InsertAfterAsRaw

func (t *TextChunk) InsertAfterAsRaw(content string) error

func (*TextChunk) InsertBeforeAsHtml

func (t *TextChunk) InsertBeforeAsHtml(content string) error

func (*TextChunk) InsertBeforeAsRaw

func (t *TextChunk) InsertBeforeAsRaw(content string) error

func (*TextChunk) IsLastInTextNode

func (t *TextChunk) IsLastInTextNode() bool

func (*TextChunk) IsRemoved

func (t *TextChunk) IsRemoved() bool

func (*TextChunk) Remove

func (t *TextChunk) Remove()

func (*TextChunk) ReplaceAsHtml

func (t *TextChunk) ReplaceAsHtml(content string) error

func (*TextChunk) ReplaceAsRaw

func (t *TextChunk) ReplaceAsRaw(content string) error

type TextChunkContent

type TextChunkContent C.lol_html_text_chunk_content_t

TextChunkContent as declared in include/lol_html.h:60

type TextChunkHandler

type TextChunkHandler func(*TextChunk) RewriterDirective

TextChunkHandler type as declared in include/lol_html.h:96

Jump to

Keyboard shortcuts

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