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 ¶
- func ExtractText(ctx *Context) string
- func GetAttr(n *html.Node, attr string) string
- func GetLanguage(class 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 MDToHTML(ctx *Context, md []byte) []byte
- func NodeOf(w core.Widget) *html.Node
- func ParseHTML(ctx *Context, r io.Reader) (*html.Node, error)
- func ReadDocument(ctx *Context, parent core.Widget, n *html.Node) error
- 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
- func TagTexts(w core.Widget)
- func TextTag(tx *core.Text) string
- type Context
- func (c *Context) AddAttributeHandler(tag string, ...) *Context
- func (c *Context) AddElementHandler(tag string, f func(ctx *Context) bool) *Context
- func (c *Context) AddWidgetHandler(f func(w core.Widget)) *Context
- func (c *Context) AddWikilinkHandler(h WikilinkHandler) *Context
- func (c *Context) BlockParent() core.Widget
- func (c *Context) HasRest() bool
- func (c *Context) ImageCache() *ImageCache
- func (c *Context) LinkButton(bt *core.Button, url string) *core.Button
- func (c *Context) LinkButtonUpdating(bt *core.Button, url func() string) *core.Button
- func (c *Context) Node() *html.Node
- func (c *Context) PageURL() string
- func (c *Context) Parent() core.Widget
- func (c *Context) ReadRest() (core.Widget, int, error)
- func (c *Context) SetDelayedImageLoad(delayed bool) *Context
- func (c *Context) SetGetURL(f func(url string) (*http.Response, error)) *Context
- func (c *Context) SetImageCache(ic *ImageCache) *Context
- func (c *Context) SetNode(n *html.Node) *Context
- func (c *Context) SetOpenURL(f func(url string, e events.Event)) *Context
- func (c *Context) SetPageURL(url string) *Context
- func (c *Context) SetStageSize(size int) *Context
- func (c *Context) SetStyle(style string) *Context
- type ImageCache
- type WikilinkHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractText ¶
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 ¶
GetAttr returns the value of the given attribute of the given node, or "" if it has none.
func GetLanguage ¶ added in v0.3.13
GetLanguage returns the 'x' in a `language-x` class from the given string of class(es).
func GetURLFromFS ¶ added in v0.3.8
GetURLFromFS returns the response for the given URL from the given filesystem, as for a GetURL function of the files of an app.
func MDGetAttr ¶ added in v0.3.11
MDGetAttr returns the value of the given attribute of the given Markdown node, or "" if it has none.
func MDToHTML ¶ added in v0.3.13
MDToHTML converts the given Markdown to HTML, with the attribute and wikilink handlers of the context.
func NodeOf ¶ added in v0.5.28
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
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
ReadDocument adds the widgets of the elements of the given parsed document (see ParseHTML) to the given parent widget.
func ReadHTML ¶
ReadHTML reads HTML from the given reader and adds the widgets of its elements to the given parent widget.
func ReadHTMLString ¶
ReadHTMLString reads HTML from the given string and adds the widgets of its elements to the given parent widget.
func ReadMD ¶
ReadMD reads Markdown from the given bytes and adds the widgets of its elements to the given parent widget.
func ReadMDString ¶
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
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).
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 (*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 ¶
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
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 ¶
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
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
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
LinkButtonUpdating is a version of Context.LinkButton for a URL that changes, from the given function.
func (*Context) PageURL ¶
PageURL returns the URL of the current page, which relative URLs are relative to, or "" if there is none.
func (*Context) Parent ¶
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
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 ¶
SetDelayedImageLoad sets whether images are loaded with a delay, rather than immediately.
func (*Context) SetGetURL ¶
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 ¶
SetNode sets the node that is currently being read, as for ExtractText of another node in an element handler.
func (*Context) SetOpenURL ¶
SetOpenURL sets the function used to open URLs, as of links, which defaults to [system.App.OpenURL].
func (*Context) SetPageURL ¶
SetPageURL sets the URL of the current page (see Context.PageURL).
func (*Context) SetStageSize ¶
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.
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 ¶ added in v0.3.8
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. |