gidom

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: BSD-3-Clause Imports: 30 Imported by: 2

Documentation

Overview

Package gidom converts HTML and MD into GoGi DOM widget trees.

Index

Constants

This section is empty.

Variables

View Source
var ElementHandlers = map[string]func(ctx Context){}

ElementHandlers is a map of handler functions for each HTML element type (eg: "button", "input", "p"). It is empty by default, but can be used by anyone in need of behavior different than the default behavior defined in HandleElement (for example, for custom elements).

View Source
var TextTags = []string{
	"a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "data", "dfn",
	"em", "i", "kbd", "mark", "q", "rp", "rt", "ruby", "s", "samp", "small",
	"span", "strong", "sub", "sup", "time", "u", "var", "wbr",
}

TextTags are all of the node tags that result in a true return value for IsText.

View Source
var UserAgentStyles string

UserAgentStyles contains the default user agent styles.

Functions

func ExtractText

func ExtractText(ctx Context) string

ExtractText recursively extracts all of the text from the children of the given *html.Node, adding any appropriate inline markup for formatted text. It adds any non-text elements to the given gi.Widget using ReadHTMLNode. It should not be called on text nodes themselves; for that, you can directly access the html.Node.Data field. It uses the given page URL for context when resolving URLs, but it can be omitted if not available.

func Get

func Get(ctx Context, url string) (*http.Response, error)

Get is a helper function that calls http.Get with the given URL, parsed relative to the page URL of the given context. It also checks the status code of the response and closes the response body and returns an error if it is not http.StatusOK. If the error is nil, then the response body is not closed and must be closed by the caller.

func GetAttr

func GetAttr(n *html.Node, attr string) string

GetAttr gets the given attribute from the given node, returning "" if the attribute is not found.

func HandleElement

func HandleElement(ctx Context)

HandleELement calls the handler in ElementHandlers associated with the current node using the given context. If there is no handler associated with it, it uses default hardcoded configuration code.

func HandleLabel

func HandleLabel(ctx Context) *gi.Label

HandleLabel creates a new label from the given information, setting the text and the label click function so that URLs are opened according to [Context.OpenURL].

func HandleLabelTag

func HandleLabelTag(ctx Context) *gi.Label

HandleLabelTag creates a new label from the given information, setting the text and the label click function so that URLs are opened according to [Context.OpenURL]. Also, it wraps the label text with the NodeString of the given node, meaning that it should be used for standalone elements that are meant to only exist in labels (eg: a, span, b, code, etc).

func HasAttr

func HasAttr(n *html.Node, attr string) bool

HasAttr returns whether the given node has the given attribute defined.

func IsText

func IsText(n *html.Node) bool

IsText returns true if the given node is a html.TextNode or an html.ElementNode designed for holding text (a, span, b, code, etc), and false otherwise.

func IsURL

func IsURL(u *url.URL) bool

IsURL returns whether the given url.URL is probably a URL (as opposed to just a normal string of text)

func New added in v0.0.2

func New[T gi.Widget](ctx Context) T

New adds a new widget of the given type to the context parent. It automatically calls [Context.Config] on the resulting widget.

func NewValue added in v0.0.2

func NewValue(ctx Context, val any) giv.Value

NewValue adds a new giv.Value with the given value to the context parent. It automatically calls [Context.Config] on the resulting value widget.

func NodeString

func NodeString(n *html.Node) (start, end string)

NodeString returns the given node as starting and ending strings in the format:

<tag attr0="value0" attr1="value1">

and

</tag>

It returns "", "" if the given node is not an html.ElementNode

func NormalizeURL

func NormalizeURL(u *url.URL)

NormalizeURL sets the scheme of the URL to "https" if it is unset.

func ParseRelativeURL

func ParseRelativeURL(rawURL, base string) (*url.URL, error)

ParseRelativeURL parses the given raw URL relative to the given base URL.

func ParseURL

func ParseURL(rawURL string) (*url.URL, bool)

ParseURL parses and normalized the given raw URL. If the given RawURL is actually a URL (as specified by IsURL), it returns the URL and true. Otherwise, it returns nil, false.

func ReadHTML

func ReadHTML(ctx Context, par gi.Widget, r io.Reader) error

ReadHTML reads HTML from the given io.Reader and adds corresponding GoGi widgets to the given gi.Widget, using the given context.

func ReadHTMLNode

func ReadHTMLNode(ctx Context, par gi.Widget, n *html.Node) error

ReadHTMLNode reads HTML from the given *html.Node and adds corresponding GoGi widgets to the given gi.Widget, using the given context.

func ReadHTMLString

func ReadHTMLString(ctx Context, par gi.Widget, s string) error

ReadHTMLString reads HTML from the given string and adds corresponding GoGi widgets to the given gi.Widget, using the given context.

func ReadMD

func ReadMD(ctx Context, par gi.Widget, b []byte) error

ReadMD reads MD (markdown) from the given bytes and adds corresponding GoGi widgets to the given gi.Widget, using the given context.

func ReadMDString

func ReadMDString(ctx Context, par gi.Widget, s string) error

ReadMDString reads MD (markdown) from the given string and adds corresponding GoGi widgets to the given gi.Widget, using the given context.

func RootNode added in v0.0.2

func RootNode(n *html.Node) *html.Node

RootNode returns the root node of the given node.

Types

type Context

type Context interface {
	// Node returns the node that is currently being read.
	Node() *html.Node

	// SetNode sets the node that is currently being read.
	SetNode(node *html.Node)

	// Parent returns the current parent widget that a widget
	// associated with the current node should be added to.
	// It may make changes to the widget tree, so the widget
	// must be added to the resulting parent immediately.
	Parent() gi.Widget

	// Config configures the given widget. It needs to be called
	// on all widgets that are not configured through the [New]
	// pathway.
	Config(w gi.Widget)

	// NewParent returns the current parent widget that children of
	// the previously read element should be added to, if any.
	NewParent() gi.Widget

	// SetNewParent sets the current parent widget that children of
	// the previous read element should be added to, if any.
	SetNewParent(pw gi.Widget)

	// BlockParent returns the current parent widget that non-inline elements
	// should be added to.
	BlockParent() gi.Widget

	// SetBlockParent sets the current parent widget that non-inline elements
	// should be added to.
	SetBlockParent(pw gi.Widget)

	// InlineParent returns the current parent widget that inline
	// elements should be added to.
	InlineParent() gi.Widget

	// SetInlineParent sets the current parent widget that inline elements
	// should be added to.
	SetInlineParent(pw gi.Widget)

	// PageURL returns the URL of the current page, and "" if there
	// is no current page.
	PageURL() string

	// OpenURL opens the given URL.
	OpenURL(url string)

	// Style returns the styling rules for the node that is currently being read.
	Style() []*css.Rule

	// AddStyle adds the given CSS style string to the page's compiled styles.
	AddStyle(style string)
}

Context contains context information about the current state of a gidom reader and its surrounding context.

func BaseContext

func BaseContext() Context

BaseContext returns a Context with basic implementations of all functions.

type ContextBase

type ContextBase struct {
	Nd *html.Node

	Rules map[*html.Node][]*css.Rule

	WidgetsForNodes map[*html.Node]gi.Widget
	BlockPw         gi.Widget
	InlinePw        gi.Widget
	NewPw           gi.Widget
}

ContextBase contains basic implementations of all Context functions.

func (*ContextBase) AddStyle added in v0.0.2

func (cb *ContextBase) AddStyle(style string)

func (*ContextBase) BlockParent added in v0.0.2

func (cb *ContextBase) BlockParent() gi.Widget

func (*ContextBase) Config added in v0.0.2

func (cb *ContextBase) Config(w gi.Widget)

func (*ContextBase) InlineParent added in v0.0.2

func (cb *ContextBase) InlineParent() gi.Widget

func (*ContextBase) NewParent added in v0.0.2

func (cb *ContextBase) NewParent() gi.Widget

func (*ContextBase) Node added in v0.0.2

func (cb *ContextBase) Node() *html.Node

func (*ContextBase) OpenURL

func (cb *ContextBase) OpenURL(url string)

func (*ContextBase) PageURL

func (cb *ContextBase) PageURL() string

func (*ContextBase) Parent added in v0.0.2

func (cb *ContextBase) Parent() gi.Widget

func (*ContextBase) SetBlockParent added in v0.0.2

func (cb *ContextBase) SetBlockParent(pw gi.Widget)

func (*ContextBase) SetInlineParent added in v0.0.2

func (cb *ContextBase) SetInlineParent(pw gi.Widget)

func (*ContextBase) SetNewParent added in v0.0.2

func (cb *ContextBase) SetNewParent(pw gi.Widget)

func (*ContextBase) SetNode added in v0.0.2

func (cb *ContextBase) SetNode(node *html.Node)

func (*ContextBase) Style added in v0.0.2

func (cb *ContextBase) Style() []*css.Rule

Jump to

Keyboard shortcuts

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