Documentation
¶
Overview ¶
Package html parses and lays out the HTML-like labels of Graphviz (D38), and parses HTML documents (D49).
A label of the DOT language, label=<...>, holds lines of text or one table. The caller cuts the text between the outer < and > and gives it to Parse:
l, issues, err := html.Parse(`<table><tr><td port="id">id: integer</td></tr></table>`)
if err != nil {
return err
}
for _, issue := range issues {
log.Print(issue)
}
lay, err := l.Layout(html.WithTextStyle(html.TextStyle{Size: 11}))
The layout gives the boxes, the rules and the runs of text in points, with y down and the origin at the top left of the label. The package draws nothing: the caller draws the layout, for example with the root package okage, or as SVG data.
The parser and the layout follow Graphviz 16.1.0, and the test module compares the layout with the output of dot.
ParseDocument parses a web page or a chapter of an ebook with the parsing algorithm of the HTML standard. It is a port of tinyhtml5, the parser of WeasyPrint. It finds the encoding, recovers from each error as a browser does, and returns a tree of nodes:
doc, err := html.ParseDocument(r)
if err != nil {
return err
}
for n := range doc.DocumentElement().Children() {
fmt.Println(n.Type(), n.Name())
}
The options WithMaxDepth, WithMaxNodes and WithMaxIssues bound the work that a hostile file can cause.
Index ¶
- Constants
- func Parse(src string) (*Label, []Issue, error)
- type Align
- type Attribute
- type Box
- type Cell
- type CharsetReader
- type Document
- type DocumentOption
- type Font
- type Issue
- type Label
- type Layout
- type Line
- type LineStyle
- type Measure
- type Metrics
- type Node
- func (n *Node) Attr(name string) (string, bool)
- func (n *Node) AttrNS(namespace, name string) (string, bool)
- func (n *Node) Attributes() []Attribute
- func (n *Node) Children() iter.Seq[*Node]
- func (n *Node) Data() string
- func (n *Node) FirstChild() *Node
- func (n *Node) LastChild() *Node
- func (n *Node) Name() string
- func (n *Node) Namespace() string
- func (n *Node) NextSibling() *Node
- func (n *Node) Parent() *Node
- func (n *Node) PrevSibling() *Node
- func (n *Node) PublicID() string
- func (n *Node) SystemID() string
- func (n *Node) Type() NodeType
- type NodeType
- type Option
- type Pos
- type QuirksMode
- type Rect
- type Row
- type Rule
- type Run
- type Span
- type Style
- type SyntaxError
- type Table
- type TextStyle
- type VAlign
Constants ¶
const ( // DefaultMaxDepth is the default of WithMaxDepth. DefaultMaxDepth = 512 // DefaultMaxNodes is the default of WithMaxNodes. DefaultMaxNodes = 1 << 20 // DefaultMaxIssues is the default of WithMaxIssues. DefaultMaxIssues = 1000 )
The default limits of ParseDocument and ParseFragment.
const ( NamespaceHTML = "http://www.w3.org/1999/xhtml" NamespaceMathML = "http://www.w3.org/1998/Math/MathML" NamespaceSVG = "http://www.w3.org/2000/svg" NamespaceXLink = "http://www.w3.org/1999/xlink" NamespaceXML = "http://www.w3.org/XML/1998/namespace" NamespaceXMLNS = "http://www.w3.org/2000/xmlns/" )
The namespaces of an element or an attribute, as the namespaces of tinyhtml5 constants.py name them. An element of HTML has NamespaceHTML.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Align ¶
type Align uint8
Align is a horizontal place: an align, a balign, or the align of a <br/>.
type Attribute ¶ added in v0.10.0
type Attribute struct {
// Namespace is empty for most attributes. An attribute of an element of
// SVG or MathML, such as xlink:href, has NamespaceXLink, NamespaceXML or
// NamespaceXMLNS.
Namespace string
// Name is the local name, in lower case for an element of HTML. An
// attribute of SVG or MathML keeps the case of the standard, such as
// viewBox.
Name string
// Value is the value, with its character references decoded.
Value string
}
Attribute is an attribute of an element.
type Box ¶
type Box struct {
Rect
// Fill is the raw fill, or empty for no fill.
Fill string
// Stroke is the raw color of the border, or empty for the default
// color. StrokeWidth is the width of the border, or 0 for no border.
Stroke string
StrokeWidth float64
// Style rounds the corners and sets the line of the border.
Style Style
// Table is the table of the box, or nil for a cell.
Table *Table
// Cell is the cell of the box, or nil for a table.
Cell *Cell
}
Box is the box of a table or a cell. Fill it, then draw its border inside it: the path of the border is the box inset by half of StrokeWidth.
type Cell ¶
type Cell struct {
// Lines are the lines of the text of the cell.
Lines []Line
// Table is the table of the cell, or nil.
Table *Table
// Align places the content across the cell, and BAlign places each line
// that has no align of its own. VAlign places the content down the
// cell.
Align Align
BAlign Align
VAlign VAlign
// Border is the width of the border, or -1 for the cellborder of the
// table. CellPadding is the space inside the border, or -1 for the
// cellpadding of the table.
Border int
CellPadding int
// Width and Height are the least size of the cell, or 0. With
// FixedSize, they are its size.
Width int
Height int
FixedSize bool
// ColSpan and RowSpan are the number of columns and rows of the cell,
// 1 by default.
ColSpan int
RowSpan int
// BgColor is the raw fill, and Color the raw color of the border. An
// empty Color takes the color of the table.
BgColor string
Color string
// Port names the box of the cell.
Port string
// Sides lists the sides of the border, as some of "L", "T", "R" and "B"
// in that order. Empty is all four.
Sides string
Style Style
// Rule draws a rule after the cell, as a <vr/> after it does.
Rule bool
}
Cell is a <td>. It holds lines of text, one table, or nothing.
type CharsetReader ¶ added in v0.10.0
CharsetReader decodes an input in an encoding that html does not decode itself. It returns a reader of the text in UTF-8. charset is the name of the encoding in the WHATWG Encoding Standard, such as "shift_jis".
type Document ¶ added in v0.10.0
type Document struct {
// contains filtered or unexported fields
}
Document is a parsed HTML document, or a parsed fragment.
func ParseDocument ¶ added in v0.10.0
func ParseDocument(r io.Reader, opts ...DocumentOption) (*Document, error)
ParseDocument reads and parses an HTML document with the parsing algorithm of the HTML standard, as a port of tinyhtml5. It finds the encoding from a byte order mark, WithCharset, a meta element or WithDefaultCharset, in that order. It never fails for HTML that is not valid: it recovers as a browser does, and it lists each parse error as an issue of the document. It returns an error only when r fails.
func ParseFragment ¶ added in v0.10.0
ParseFragment reads and parses a fragment of HTML, as the content of an element with the name container, such as "div". The root of the returned document holds the nodes of the fragment. It returns an error only when r fails.
func (*Document) DocumentElement ¶ added in v0.10.0
DocumentElement returns the html element of a document, or nil for a fragment.
func (*Document) Encoding ¶ added in v0.10.0
Encoding returns the name of the encoding that decoded the input, such as "utf-8" or "windows-1252", as the WHATWG Encoding Standard names it.
func (*Document) Issues ¶ added in v0.10.0
Issues returns a copy of the parse errors of the document, in order. HTML recovers from each one, so an issue is not an error. The list stops at the limit of WithMaxIssues.
func (*Document) Mode ¶ added in v0.10.0
func (d *Document) Mode() QuirksMode
Mode returns the quirks mode of the document.
type DocumentOption ¶ added in v0.10.0
type DocumentOption func(*documentOptions)
DocumentOption is an option of ParseDocument and ParseFragment.
func WithCharset ¶ added in v0.10.0
func WithCharset(label string) DocumentOption
WithCharset gives the encoding of the input, such as "utf-8", which wins over a meta element. Only a byte order mark wins over it. An unknown label has no effect.
func WithCharsetReader ¶ added in v0.10.0
func WithCharsetReader(r CharsetReader) DocumentOption
WithCharsetReader gives the decoder of the encodings that html does not decode itself. html decodes UTF-8, UTF-16LE, UTF-16BE, windows-1252 and x-user-defined. Without a reader, or when the reader fails, html decodes another encoding as windows-1252, and adds an issue.
func WithDefaultCharset ¶ added in v0.10.0
func WithDefaultCharset(label string) DocumentOption
WithDefaultCharset gives the encoding of an input with no byte order mark and no meta element that declares one. Its default is windows-1252.
func WithMaxDepth ¶ added in v0.10.0
func WithMaxDepth(n int) DocumentOption
WithMaxDepth sets the largest number of open elements. The tree is about as deep as the largest number of open elements. When the input opens more, the parse stops there, as at the end of the input, and adds an issue. A value below 1 keeps the default, DefaultMaxDepth.
func WithMaxIssues ¶ added in v0.10.0
func WithMaxIssues(n int) DocumentOption
WithMaxIssues sets the largest number of issues that a document keeps. A value below 1 keeps the default, DefaultMaxIssues.
func WithMaxNodes ¶ added in v0.10.0
func WithMaxNodes(n int) DocumentOption
WithMaxNodes sets the largest number of nodes in the tree. When the input makes more, the parse stops there, as at the end of the input, and adds an issue. A value below 1 keeps the default, DefaultMaxNodes.
type Font ¶
type Font struct {
// Face is the face of the innermost font tag with a face, or empty for
// the default family.
Face string
// Color is the raw color of the innermost font tag with a color, or
// empty for the default color.
Color string
// Size is the point-size of the innermost font tag with a point-size, or
// 0 for the default size.
Size float64
Bold bool
Italic bool
Underline bool
Overline bool
Strike bool
Sub bool
Sup bool
}
Font is the font that the tags b, i, u, o, s, sub, sup and font set.
type Issue ¶
type Issue struct {
// Pos is the position of the part in the text of the label.
Pos Pos
// Msg says what html does not read.
Msg string
}
Issue is a part of a label that html does not read, or reads with its default: an unknown attribute or tag, an invalid value, or a port name that repeats.
type Label ¶
type Label struct {
// Lines are the lines of a label of text. They are empty when Table is
// set.
Lines []Line
// Table is the table of the label, or nil for a label of text.
Table *Table
}
Label is a parsed HTML-like label. It holds lines of text or one table.
type Layout ¶
type Layout struct {
// Width and Height are the size of the label.
Width, Height float64
// Boxes are the boxes of the tables and the cells, in the order that
// they draw.
Boxes []Box
// Rules are the rules between rows and cells, and the sides of the
// cells with a sides attribute.
Rules []Rule
// Runs are the runs of text.
Runs []Run
// Ports maps each port name to the box of its table or cell.
Ports map[string]Rect
}
Layout is a label laid out, in points from the top left of the label, with y down. Draw the boxes in order, then the rules, then the runs.
type Line ¶
type Line struct {
// Spans are the runs of text of the line, each in one font. An empty
// line has no span.
Spans []Span
// Align is the align of the <br/> that ends the line, or AlignDefault.
Align Align
}
Line is a line of text, with the runs of text in it.
type Metrics ¶
type Metrics struct {
// Width is the advance width of the run.
Width float64
// Height is the height of a line of the run.
Height float64
// Ascent is the distance from the top of the line to the baseline.
Ascent float64
}
Metrics is the size of a run of text, in points.
type Node ¶ added in v0.10.0
type Node struct {
// contains filtered or unexported fields
}
Node is a node of a parsed document. A node does not change after the parse.
func (*Node) Attr ¶ added in v0.10.0
Attr returns the value of the attribute of an element with a name and no namespace, and true if the element has it.
func (*Node) AttrNS ¶ added in v0.10.0
AttrNS returns the value of the attribute of an element with a namespace and a local name, and true if the element has it.
func (*Node) Attributes ¶ added in v0.10.0
Attributes returns a copy of the attributes of an element, in the order of the source.
func (*Node) FirstChild ¶ added in v0.10.0
FirstChild returns the first child of the node, or nil.
func (*Node) Name ¶ added in v0.10.0
Name returns the local name of an element, or the name of a doctype. The name of an element of HTML is in lower case. An element of SVG keeps the case of the standard, such as foreignObject.
func (*Node) Namespace ¶ added in v0.10.0
Namespace returns the namespace of an element: NamespaceHTML, NamespaceSVG or NamespaceMathML.
func (*Node) NextSibling ¶ added in v0.10.0
NextSibling returns the node after this one in its parent, or nil.
func (*Node) PrevSibling ¶ added in v0.10.0
PrevSibling returns the node before this one in its parent, or nil.
func (*Node) PublicID ¶ added in v0.10.0
PublicID returns the public identifier of a doctype, or an empty string.
type NodeType ¶ added in v0.10.0
type NodeType uint8
NodeType is the type of a Node.
type Option ¶
type Option func(*options)
Option is an option of Label.Layout.
func WithMeasure ¶
WithMeasure sets the measure of text. It overrides WithRenderer.
func WithRenderer ¶
WithRenderer sets the renderer that the default measure shapes text with, with its fonts and its generic families (D36).
func WithTextStyle ¶
WithTextStyle sets the font of text with no <font> around it. An empty family is the default family of the measure, and a size of 0 is 14 points, as in Graphviz. Bold and Italic are ignored: the tags b and i set them.
type Pos ¶
Pos is a position in the text of a label: a line and a column, from 1, with a column for each character.
type QuirksMode ¶ added in v0.10.0
type QuirksMode uint8
QuirksMode is the mode of a document, which its doctype sets. A renderer lays out a document in quirks mode with the rules of old browsers.
const ( NoQuirks QuirksMode = iota Quirks LimitedQuirks )
The values of QuirksMode.
func (QuirksMode) String ¶ added in v0.10.0
func (m QuirksMode) String() string
String returns the name of the mode, as tinyhtml5 names it.
type Rect ¶
type Rect struct {
X, Y, Width, Height float64
}
Rect is a box in points, from its top left, with y down.
type Row ¶
type Row struct {
// Cells are the cells of the row, in order.
Cells []*Cell
// Rule draws a rule under the row, as an <hr/> after it does.
Rule bool
}
Row is a <tr>.
type Rule ¶
type Rule struct {
X1, Y1, X2, Y2 float64
// Stroke is the raw color of the line, or empty for the default color.
Stroke string
// Width is the width of the line.
Width float64
// Line is the line style.
Line LineStyle
// Cell is the cell of a side, or nil for a rule between rows or cells.
Cell *Cell
}
Rule is a line: a rule between rows or cells, or a side of a cell.
type Run ¶
type Run struct {
// X is the left end of the run, and Y its baseline.
X, Y float64
// Width is the advance width of the run, as the measure gives it.
Width float64
// Top and Height are the top and the height of the line of the run. The
// middle of the line is Top + Height/2.
Top, Height float64
// Text is the text of the run.
Text string
// Style is the font of the run, with the default family and size of the
// options where the label gives none. The size of a run in sub or sup is
// already smaller.
Style TextStyle
// Color is the raw color of the text, or empty for the default color.
Color string
Underline bool
Overline bool
Strike bool
Sub bool
Sup bool
// Shift is the distance that sub or sup moves the baseline, up for a
// positive value. Y already holds it.
Shift float64
// Under is the raw fill of the nearest box under the run, or empty.
Under string
}
Run is a run of text in one font.
type Span ¶
type Span struct {
// Text is the text of the run, with its spaces and its entities decoded.
Text string
// Font is the font of the run, from the tags around it.
Font Font
}
Span is a run of text in one font.
type Style ¶
type Style struct {
// Rounded rounds the corners of the border.
Rounded bool
// Line is the line of the border.
Line LineStyle
}
Style is the style attribute of a table or a cell.
type SyntaxError ¶
type SyntaxError struct {
// Pos is the position of the error in the text of the label.
Pos Pos
// Msg says what is wrong.
Msg string
// contains filtered or unexported fields
}
SyntaxError is the error of Parse for a label that is not valid. It wraps okage.ErrHTMLSyntax, and the error of roxmltree when the label is not valid XML.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
Error returns the message, with the position.
func (*SyntaxError) Unwrap ¶
func (e *SyntaxError) Unwrap() []error
Unwrap returns okage.ErrHTMLSyntax, and the error of the XML parser when there is one.
type Table ¶
type Table struct {
// Rows are the rows of the table, in order.
Rows []*Row
// Font is the font around the table. Its cells take it.
Font Font
// Border is the width of the border of the table. Its default is 1.
Border int
// CellBorder is the width of the border of each cell, or -1 for the
// width of Border.
CellBorder int
// CellSpacing is the space between the cells, and between the cells and
// the border. Its default is 2.
CellSpacing int
// CellPadding is the space inside each cell, around its content. Its
// default is 2.
CellPadding int
// Width and Height are the least size of the table, or 0. With
// FixedSize, they are its size.
Width int
Height int
FixedSize bool
// BgColor is the raw fill, and Color the raw color of the border. An
// empty Color takes the color of the cell around the table.
BgColor string
Color string
// Port names the box of the table.
Port string
// Align and VAlign place a table with FixedSize in its cell.
Align Align
VAlign VAlign
Style Style
// RowRules and ColumnRules draw a rule between each two rows and each
// two columns, as rows="*" and columns="*" do.
RowRules bool
ColumnRules bool
}
Table is a <table>.