Documentation
¶
Overview ¶
Package htmlcore converts HTML and MD into Cogent Core widget trees.
Index ¶
- Variables
- func ExtractText(ctx *Context) string
- func ExtractTextExclude(ctx *Context, excludeSubs ...string) (string, *html.Node)
- func Get(ctx *Context, url string) (*http.Response, error)
- func GetAttr(n *html.Node, attr string) string
- func GetURLFromFS(fsys fs.FS, rawURL string) (*http.Response, error)
- func HasAttr(n *html.Node, attr string) bool
- func MDGetAttr(n ast.Node, attr string) string
- func MDSetAttr(n ast.Node, attr, value string)
- func New[T tree.NodeValue](ctx *Context) *T
- func ReadHTML(ctx *Context, parent core.Widget, r io.Reader) error
- func ReadHTMLString(ctx *Context, parent core.Widget, s string) error
- func ReadMD(ctx *Context, parent core.Widget, b []byte) error
- func ReadMDString(ctx *Context, parent core.Widget, s string) error
- type Context
- type WikilinkHandler
Constants ¶
This section is empty.
Variables ¶
BindTextEditor is a function set to github.com/MobinYengejehi/core/yaegicore.BindTextEditor when importing yaegicore, which provides interactive editing functionality for Go code blocks in text editors.
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 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 ExtractTextExclude ¶
ExtractTextExclude 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], except those types listed in the excludeSubs list. If one of those types is encountered, it is returned.
func Get ¶
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 GetAttr ¶
GetAttr gets the given attribute from the given node, returning "" if the attribute is not found.
func GetURLFromFS ¶
GetURLFromFS can be used for [Context.GetURL] to get resources from the given file system.
func MDGetAttr ¶
MDGetAttr gets the given attribute from the given markdown node, returning "" if the attribute is not found.
func New ¶
New adds a new widget of the given type to the context parent. It automatically calls [Context.config] on the resulting widget.
func ReadHTML ¶
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 ¶
ReadHTMLString reads HTML from the given string and adds corresponding Cogent Core widgets to the given core.Widget, using the given context.
func ReadMD ¶
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 ¶
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
// TableParent is the current table being generated.
TableParent *core.Frame
// 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)
// 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.
ElementHandlers map[string]func(ctx *Context) bool
// WikilinkHandlers is a list of handlers to use for wikilinks.
// If one returns "", "", the next ones will be tried instead.
// The functions are tried in sequential ascending order.
// See [Context.AddWikilinkHandler] to add a new handler.
WikilinkHandlers []WikilinkHandler
// AttributeHandlers is a map of markdown render handler functions
// for custom attribute values that are specified in {tag: value}
// attributes prior to markdown elements in the markdown source.
// The map key is the tag in the attribute, which is then passed
// to the function, along with the markdown node being rendered.
// Alternative or additional HTML output can be written to the given writer.
// If the handler function returns true, then the default HTML code
// will not be generated.
AttributeHandlers map[string]func(ctx *Context, w io.Writer, node ast.Node, entering bool, tag, value string) bool
// 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 (*Context) AddWikilinkHandler ¶
func (c *Context) AddWikilinkHandler(h WikilinkHandler)
AddWikilinkHandler adds a new WikilinkHandler to [Context.WikilinkHandlers]. If it returns "", "", the next handlers will be tried instead. The functions are tried in sequential ascending order.
func (*Context) InlineParent ¶
InlineParent returns the current parent widget that inline elements should be added to.
func (*Context) LinkButton ¶
LinkButton is a helper function that makes the given button open the given link when clicked on, using [Context.OpenURL]. The advantage of using this is that it does tree.NodeBase.SetProperty of "href" to the given url, allowing generatehtml to create an <a> element for HTML preview and SEO purposes. It also sets the tooltip to the URL.
See also Context.LinkButtonUpdating for a dynamic version.
func (*Context) LinkButtonUpdating ¶
LinkButtonUpdating is a version of Context.LinkButton that is robust to a changing/dynamic URL, using an Updater and a URL function instead of a variable.
type WikilinkHandler ¶
WikilinkHandler is a function that converts wikilink text to a corresponding URL and label text. If it returns "", "", the handler will be skipped in favor of the next possible handlers. Wikilinks are of the form [[wikilink text]]. Only the text inside of the brackets is passed to the handler. If there is additional text directly after the closing brackets without spaces or punctuation, it will be appended to the label text after the handler is run (ex: [[widget]]s).
func GoDocWikilink ¶
func GoDocWikilink(prefix string, pkg string) WikilinkHandler
GoDocWikilink returns a WikilinkHandler that converts wikilinks of the form [[prefix:identifier]] to a pkg.go.dev URL starting at pkg. For example, with prefix="doc" and pkg="github.com/MobinYengejehi/core", the wikilink [[doc:core.Button]] will result in the URL "https://pkg.go.dev/github.com/MobinYengejehi/core/core#Button".