htmlcore

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: BSD-3-Clause Imports: 29 Imported by: 5

Documentation

Overview

Package htmlcore converts HTML and MD into Cogent Core widget trees.

Index

Constants

This section is empty.

Variables

View Source
var BindTextEditor func(ed *texteditor.Editor, parent core.Widget)

BindTextEditor is a function set to cogentcore.org/core/yaegicore.BindTextEditor when importing yaegicore, which provides interactive editing functionality for Go code blocks in text editors.

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

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). If the handler for an element returns false, then the default behavior for an element is used.

View Source
var WikilinkBaseURL = "cogentcore.org/core"

WikilinkBaseURL is the base URL to use for wiki links

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 core.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 [Context.GetURL] 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 New

func New[T tree.NodeValue](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 ReadHTML

func ReadHTML(ctx *Context, parent core.Widget, r io.Reader) error

ReadHTML reads HTML from the given io.Reader and adds corresponding Cogent Core widgets to the given core.Widget, using the given context.

func ReadHTMLString

func ReadHTMLString(ctx *Context, parent core.Widget, s string) error

ReadHTMLString reads HTML from the given string and adds corresponding Cogent Core widgets to the given core.Widget, using the given context.

func ReadMD

func ReadMD(ctx *Context, parent core.Widget, b []byte) error

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

func ReadMDString

func ReadMDString(ctx *Context, parent core.Widget, s string) error

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

Types

type Context

type Context struct {

	// Node is the node that is currently being read.
	Node *html.Node

	// NewParent is the current parent widget that children of
	// the previously read element should be added to, if any.
	NewParent core.Widget

	// BlockParent is the current parent widget that non-inline elements
	// should be added to.
	BlockParent core.Widget

	// PageURL, if not "", is the URL of the current page.
	// Otherwise, there is no current page.
	PageURL string

	// OpenURL is the function used to open URLs,
	// which defaults to [system.App.OpenURL].
	OpenURL func(url string)

	// GetURL is the function used to get resources from URLs,
	// which defaults to [http.Get].
	GetURL func(url string) (*http.Response, error)
	// contains filtered or unexported fields
}

Context contains context information about the current state of a htmlcore reader and its surrounding context. It should be created with NewContext.

func NewContext

func NewContext() *Context

NewContext returns a new Context with basic defaults.

func (*Context) InlineParent

func (c *Context) InlineParent() core.Widget

InlineParent returns the current parent widget that inline elements should be added to.

func (*Context) Parent

func (c *Context) Parent() core.Widget

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.

Directories

Path Synopsis
examples
md

Jump to

Keyboard shortcuts

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