Documentation
¶
Overview ¶
Package gidom converts HTML and MD into GoGi DOM widget trees.
Index ¶
- Variables
- func ExtractText(ctx Context) string
- func Get(ctx Context, url string) (*http.Response, error)
- func GetAttr(n *html.Node, attr string) string
- func HandleElement(ctx Context)
- func HandleLabel(ctx Context) *gi.Label
- func HandleLabelTag(ctx Context) *gi.Label
- func HasAttr(n *html.Node, attr string) bool
- func IsText(n *html.Node) bool
- func IsURL(u *url.URL) bool
- func New[T gi.Widget](ctx Context) T
- func NewValue(ctx Context, val any) giv.Value
- func NodeString(n *html.Node) (start, end string)
- func NormalizeURL(u *url.URL)
- func ParseRelativeURL(rawURL, base string) (*url.URL, error)
- func ParseURL(rawURL string) (*url.URL, bool)
- func ReadHTML(ctx Context, par gi.Widget, r io.Reader) error
- func ReadHTMLNode(ctx Context, par gi.Widget, n *html.Node) error
- func ReadHTMLString(ctx Context, par gi.Widget, s string) error
- func ReadMD(ctx Context, par gi.Widget, b []byte) error
- func ReadMDString(ctx Context, par gi.Widget, s string) error
- func RootNode(n *html.Node) *html.Node
- type Context
- type ContextBase
- func (cb *ContextBase) AddStyle(style string)
- func (cb *ContextBase) BlockParent() gi.Widget
- func (cb *ContextBase) Config(w gi.Widget)
- func (cb *ContextBase) InlineParent() gi.Widget
- func (cb *ContextBase) NewParent() gi.Widget
- func (cb *ContextBase) Node() *html.Node
- func (cb *ContextBase) OpenURL(url string)
- func (cb *ContextBase) PageURL() string
- func (cb *ContextBase) Parent() gi.Widget
- func (cb *ContextBase) SetBlockParent(pw gi.Widget)
- func (cb *ContextBase) SetInlineParent(pw gi.Widget)
- func (cb *ContextBase) SetNewParent(pw gi.Widget)
- func (cb *ContextBase) SetNode(node *html.Node)
- func (cb *ContextBase) Style() []*css.Rule
Constants ¶
This section is empty.
Variables ¶
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).
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.
var UserAgentStyles string
UserAgentStyles contains the default user agent styles.
Functions ¶
func ExtractText ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 IsText ¶
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 ¶
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
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
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 ¶
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 ¶
NormalizeURL sets the scheme of the URL to "https" if it is unset.
func ParseRelativeURL ¶
ParseRelativeURL parses the given raw URL relative to the given base URL.
func ParseURL ¶
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 ¶
ReadHTML reads HTML from the given io.Reader and adds corresponding GoGi widgets to the given gi.Widget, using the given context.
func ReadHTMLNode ¶
ReadHTMLNode reads HTML from the given *html.Node and adds corresponding GoGi widgets to the given gi.Widget, using the given context.
func ReadHTMLString ¶
ReadHTMLString reads HTML from the given string and adds corresponding GoGi widgets to the given gi.Widget, using the given context.
func ReadMD ¶
ReadMD reads MD (markdown) from the given bytes and adds corresponding GoGi widgets to the given gi.Widget, using the given context.
func ReadMDString ¶
ReadMDString reads MD (markdown) from the given string and adds corresponding GoGi widgets to the given gi.Widget, using the given context.
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