htmlcore

package
v0.5.51 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: BSD-3-Clause Imports: 8 Imported by: 6 modules, 8 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package htmlcore converts HTML and Markdown into Cogent Core widgets.

It is the API for apps and content sites: make a Context with NewContext, set it up with its handlers and settings, and read HTML or Markdown into a parent widget with ReadHTML, ReadHTMLString, ReadMD or ReadMDString.

The engine that does the reading (the CSS cascade, layout of the elements, re-reading and restyling changed documents) is package htmlengine, which apps that show web pages, as a browser does, use directly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractText

func ExtractText(ctx *Context) string

ExtractText returns the text of the children of the current node of the context (see Context.SetNode), with inline markup for formatted text.

func GetAttr

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

GetAttr returns the value of the given attribute of the given node, or "" if it has none.

func GetLanguage added in v0.3.13

func GetLanguage(class string) string

GetLanguage returns the 'x' in a `language-x` class from the given string of class(es).

func GetURLFromFS added in v0.3.8

func GetURLFromFS(fsys fs.FS, rawURL string) (*http.Response, error)

GetURLFromFS returns the response for the given URL from the given filesystem, as for a GetURL function of the files of an app.

func HasAttr

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

HasAttr returns whether the given node has the given attribute.

func MDGetAttr added in v0.3.11

func MDGetAttr(n ast.Node, attr string) string

MDGetAttr returns the value of the given attribute of the given Markdown node, or "" if it has none.

func MDSetAttr added in v0.3.11

func MDSetAttr(n ast.Node, attr, value string)

MDSetAttr sets the given attribute of the given Markdown node.

func MDToHTML added in v0.3.13

func MDToHTML(ctx *Context, md []byte) []byte

MDToHTML converts the given Markdown to HTML, with the attribute and wikilink handlers of the context.

func NodeOf added in v0.5.28

func NodeOf(w core.Widget) *html.Node

NodeOf returns the HTML node of the element that the given widget is for, or that of the nearest widget that it is in that has one (as for the text of an element), or nil if there is none.

func ParseHTML added in v0.5.28

func ParseHTML(ctx *Context, r io.Reader) (*html.Node, error)

ParseHTML parses HTML from the given reader into a document of nodes, as browsers parse it, which ReadDocument reads.

func ReadDocument added in v0.5.28

func ReadDocument(ctx *Context, parent core.Widget, n *html.Node) error

ReadDocument adds the widgets of the elements of the given parsed document (see ParseHTML) to the given parent widget.

func ReadHTML

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

ReadHTML reads HTML from the given reader and adds the widgets of its elements to the given parent widget.

func ReadHTMLString

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

ReadHTMLString reads HTML from the given string and adds the widgets of its elements to the given parent widget.

func ReadMD

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

ReadMD reads Markdown from the given bytes and adds the widgets of its elements to the given parent widget.

func ReadMDString

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

ReadMDString reads Markdown from the given string and adds the widgets of its elements to the given parent widget.

func TagTexts added in v0.5.24

func TagTexts(w core.Widget)

TagTexts gives each text in the given widget the "tag" and "id" properties of the HTML element that it is the text of, if it does not have its own, as for printing it apart from the widget of its element (as in a PDF, which styles headings by their tags, and links to their ids).

func TextTag added in v0.5.24

func TextTag(tx *core.Text) string

TextTag returns the name of the HTML element that the given text is the text of, such as "h2" for the text of a heading, or "" if there is none.

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context is the context for reading HTML and Markdown into widgets: its settings, its handlers of elements, attributes, wikilinks and widgets, and, in the handlers, the state of the current read (the current Context.Node and the parents that its widgets are added to).

func NewContext

func NewContext() *Context

NewContext returns a new Context with basic defaults.

func (*Context) AddAttributeHandler

func (c *Context) AddAttributeHandler(tag string, f func(ctx *Context, w io.Writer, node ast.Node, entering bool, tag, value string) bool) *Context

AddAttributeHandler adds a handler of the given tag of the custom {tag: value} attributes of Markdown elements, which is called with the Markdown node as it is converted to HTML. It can write alternative or additional HTML to the given writer; if it returns true, the default HTML of the node is not written.

func (*Context) AddElementHandler

func (c *Context) AddElementHandler(tag string, f func(ctx *Context) bool) *Context

AddElementHandler adds a handler of the elements with the given tag (as for custom elements), which makes their widgets, as in the Context.BlockParent, in place of those that would be made for them. If it returns false, the default widgets of the element are made.

func (*Context) AddWidgetHandler added in v0.3.13

func (c *Context) AddWidgetHandler(f func(w core.Widget)) *Context

AddWidgetHandler adds a function that is called for each widget that is made, after it is configured, as for styling widgets of a type.

func (*Context) AddWikilinkHandler added in v0.3.8

func (c *Context) AddWikilinkHandler(h WikilinkHandler) *Context

AddWikilinkHandler adds a handler of wikilinks. If it returns "", "", the handlers added before it are tried.

func (*Context) BlockParent

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

BlockParent returns the current parent widget that block-level (non-inline) elements are added to, as for the widgets that an element handler makes.

func (*Context) HasRest added in v0.5.0

func (c *Context) HasRest() bool

HasRest returns whether ReadHTML has left elements to read with Context.ReadRest (see Context.SetStageSize).

func (*Context) ImageCache added in v0.5.0

func (c *Context) ImageCache() *ImageCache

ImageCache returns the cache of the decoded images, if any.

func (*Context) LinkButton added in v0.3.8

func (c *Context) LinkButton(bt *core.Button, url string) *core.Button

LinkButton makes the given button open the given URL when it is clicked, with the OpenURL function of the context, and sets its tooltip to the URL. The URL is also the "href" property of the button, so that the HTML generated for the app has a link for it.

func (*Context) LinkButtonUpdating added in v0.3.9

func (c *Context) LinkButtonUpdating(bt *core.Button, url func() string) *core.Button

LinkButtonUpdating is a version of Context.LinkButton for a URL that changes, from the given function.

func (*Context) Node

func (c *Context) Node() *html.Node

Node returns the node that is currently being read.

func (*Context) PageURL

func (c *Context) PageURL() string

PageURL returns the URL of the current page, which relative URLs are relative to, or "" if there is none.

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.

func (*Context) ReadRest added in v0.5.0

func (c *Context) ReadRest() (core.Widget, int, error)

ReadRest adds the widgets of the elements that ReadHTML left (see Context.SetStageSize), and returns the widget that they are added to and the index of the first child added, which need to be updated. The context must not have been used to read other HTML since.

func (*Context) SetDelayedImageLoad

func (c *Context) SetDelayedImageLoad(delayed bool) *Context

SetDelayedImageLoad sets whether images are loaded with a delay, rather than immediately.

func (*Context) SetGetURL

func (c *Context) SetGetURL(f func(url string) (*http.Response, error)) *Context

SetGetURL sets the function used to get resources from URLs, as images and style sheets, which defaults to http.Get.

func (*Context) SetImageCache

func (c *Context) SetImageCache(ic *ImageCache) *Context

SetImageCache sets the cache of the decoded images of img elements, by their URL, so that reading a page again does not decode them again (see NewImageCache). It must only be set when the GetURL function returns the same content for the same URL until the cache is reset.

func (*Context) SetNode

func (c *Context) SetNode(n *html.Node) *Context

SetNode sets the node that is currently being read, as for ExtractText of another node in an element handler.

func (*Context) SetOpenURL

func (c *Context) SetOpenURL(f func(url string, e events.Event)) *Context

SetOpenURL sets the function used to open URLs, as of links, which defaults to [system.App.OpenURL].

func (*Context) SetPageURL

func (c *Context) SetPageURL(url string) *Context

SetPageURL sets the URL of the current page (see Context.PageURL).

func (*Context) SetStageSize

func (c *Context) SetStageSize(size int) *Context

SetStageSize sets the size of the content of a page (in runes of text, with estimates for other elements) after which ReadHTML stops adding the widgets of its top-level elements, leaving the rest for Context.ReadRest, so that the first part of a long page is shown sooner. 0 reads all of it.

func (*Context) SetStyle

func (c *Context) SetStyle(style string) *Context

SetStyle sets CSS that is added to the style sheets of each page that is read, before those of the page, as for the margins of the pages of an app.

type ImageCache added in v0.5.0

type ImageCache = htmlengine.ImageCache

ImageCache caches the decoded images of img elements by their URL (see Context.SetImageCache).

func NewImageCache added in v0.5.0

func NewImageCache(maxBytes int) *ImageCache

NewImageCache returns a new ImageCache of at most the given number of bytes of decoded images.

type WikilinkHandler added in v0.3.8

type WikilinkHandler = htmlengine.WikilinkHandler

WikilinkHandler is a function that converts the text of a wikilink ([[text]]) to the URL and label of a link, or returns "", "" if it does not handle it.

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="cogentcore.org/core", the wikilink [[doc:core.Button]] links to https://pkg.go.dev/cogentcore.org/core/core#Button.

Directories

Path Synopsis
Package css has the parts of CSS that htmlcore uses to style the widgets of pages that do not depend on its widgets: parsing style sheets (Parse), compiling and indexing selectors (CompileSelector, SelectorIndex), the declarations of the cascade (Origin, ValidDeclaration), and the values of properties: lengths (LengthEnv), colors (ParseColor), filters, transitions, fonts, masks and others.
Package css has the parts of CSS that htmlcore uses to style the widgets of pages that do not depend on its widgets: parsing style sheets (Parse), compiling and indexing selectors (CompileSelector, SelectorIndex), the declarations of the cascade (Origin, ValidDeclaration), and the values of properties: lengths (LengthEnv), colors (ParseColor), filters, transitions, fonts, masks and others.
examples
html command
md command
Package htmlengine is the engine that reads HTML and Markdown into Cogent Core widget trees: parsing documents, the CSS cascade and the browser styles of elements, making and laying out the widgets of the elements, and reading and restyling the changed elements of documents (as by the scripts of pages) in place.
Package htmlengine is the engine that reads HTML and Markdown into Cogent Core widget trees: parsing documents, the CSS cascade and the browser styles of elements, making and laying out the widgets of the elements, and reading and restyling the changed elements of documents (as by the scripts of pages) in place.
Package tohtml converts Cogent Core widgets to HTML, which the core tool uses to pre-render apps for a preview while the app loads and for SEO purposes.
Package tohtml converts Cogent Core widgets to HTML, which the core tool uses to pre-render apps for a preview while the app loads and for SEO purposes.

Jump to

Keyboard shortcuts

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