Documentation
¶
Overview ¶
Package html converts HTML strings into Paper rows so they can be added to a Paper document. No browser, no external binary, no JavaScript.
Supported tags and CSS properties are documented in docs/html-support.md.
Index ¶
- Constants
- Variables
- func FromReader(ctx context.Context, r io.Reader, opts ...Option) ([]core.Row, error)
- func FromString(ctx context.Context, htmlStr string, opts ...Option) ([]core.Row, error)
- type AssetError
- type Document
- type LimitError
- type LimitKind
- type Limits
- type Option
- func WithContentWidth(mm float64) Option
- func WithFallbackFontPath(path string) Option
- func WithGridSize(n int) Option
- func WithHTTPClient(client *http.Client) Option
- func WithImageBaseDir(dir string) Option
- func WithLimits(l Limits) Option
- func WithMaxDepth(n int) Option
- func WithMaxElements(n int) Option
- func WithOutlineFromHeadings() Option
- func WithRemoteAssets() Option
- func WithStrictAssets() Option
- func WithStylesheetBaseDir(dir string) Option
- func WithURLPolicy(policy URLPolicy) Option
- func WithUnsafeNoLimits() Option
- func WithUnsupportedHandler(fn func(thing, value string)) Option
- type PageOptions
- type ParseError
- type URLPolicy
Constants ¶
const ( // LimitElements reports that the configured element/node budget was exceeded. LimitElements = translate.LimitElements // LimitDepth reports that the configured DOM nesting depth was exceeded. LimitDepth = translate.LimitDepth // LimitStyleRules reports that the configured stylesheet rule budget was exceeded. LimitStyleRules = translate.LimitStyleRules )
Variables ¶
var ( // ErrImageTooLarge is returned when an HTML image exceeds configured byte // or pixel limits. ErrImageTooLarge = htmllimits.ErrImageTooLarge // ErrDOMTooDeep is returned when the parsed DOM exceeds MaxDOMDepth. ErrDOMTooDeep = htmllimits.ErrDOMTooDeep // ErrDOMTooLarge is returned when the parsed DOM exceeds MaxDOMNodes. ErrDOMTooLarge = htmllimits.ErrDOMTooLarge // ErrSVGTooLarge is returned when SVG rasterization exceeds MaxSVGPixels. ErrSVGTooLarge = htmllimits.ErrSVGTooLarge // ErrStyleRulesTooLarge is returned when CSS parsing exceeds MaxStyleRules. ErrStyleRulesTooLarge = htmllimits.ErrStyleRulesTooLarge )
Functions ¶
func FromReader ¶
FromReader parses HTML from an io.Reader and returns the corresponding rows. It observes ctx before and after reading and during translation.
Types ¶
type AssetError ¶ added in v0.2.2
type AssetError = translate.AssetError
AssetError indicates a document-referenced asset failed to load while converting HTML with WithStrictAssets enabled.
type Document ¶ added in v0.2.0
Document is the full result of converting an HTML document: content rows plus document-level options parsed from `@page` rules.
func DocumentFromReader ¶ added in v0.2.0
DocumentFromReader parses HTML from an io.Reader and returns the full Document result. It observes ctx before and after reading and during translation.
func DocumentFromString ¶ added in v0.2.0
DocumentFromString parses an HTML string and returns the full Document result. Unlike FromString, it surfaces `@page` size/margin options so callers (notably paper.FromHTML) can configure the page before rendering. It observes ctx before and after DOM parsing and during translation.
type LimitError ¶ added in v0.2.2
type LimitError = translate.LimitError
LimitError indicates HTML conversion stopped because a configured resource ceiling was crossed. Its Kind field reports which one.
type LimitKind ¶ added in v0.2.2
LimitKind identifies which HTML conversion resource ceiling was exceeded.
type Limits ¶
type Limits = htmllimits.Limits
Limits caps resource use while translating untrusted HTML.
func DefaultLimits ¶
func DefaultLimits() Limits
DefaultLimits returns the safe resource limits used by FromString and FromReader unless overridden.
type Option ¶
type Option func(*config)
Option configures FromString / FromReader behaviour.
func WithContentWidth ¶
WithContentWidth sets the page content width in mm for accurate gap-to-col approximation. Default is 170mm (A4 with 20mm left+right margins).
func WithFallbackFontPath ¶ added in v0.2.2
WithFallbackFontPath registers a font (local path, data: URI, or http(s) URL with WithRemoteAssets) loaded on demand when the document contains text outside the WinAnsi (cp1252) repertoire.
func WithGridSize ¶
WithGridSize overrides the default 12-column grid for flex quantization. Use this when the paper document was built with config.WithMaxGridSize(n).
func WithHTTPClient ¶ added in v0.2.2
WithHTTPClient overrides http.DefaultClient for remote asset fetches.
func WithImageBaseDir ¶
WithImageBaseDir scopes <img src=...> local-file reads to a single directory. Paths that escape via ".." or absolute prefix are refused.
func WithLimits ¶
WithLimits overrides resource limits for untrusted HTML translation. Zero fields keep their safe default values.
func WithMaxDepth ¶ added in v0.2.2
WithMaxDepth caps the DOM nesting depth translated. Exceeding the cap returns a *LimitError with Kind LimitDepth. Other limits keep their safe default values.
func WithMaxElements ¶ added in v0.2.2
WithMaxElements caps the number of DOM nodes translated. Exceeding the cap returns a *LimitError with Kind LimitElements. Other limits keep their safe default values.
func WithOutlineFromHeadings ¶ added in v0.2.0
func WithOutlineFromHeadings() Option
WithOutlineFromHeadings adds h1-h6 headings to the PDF document outline: h1 becomes a level-0 entry, h2 a level-1 entry, and so on. Hidden headings are skipped.
func WithRemoteAssets ¶ added in v0.2.2
func WithRemoteAssets() Option
WithRemoteAssets enables http(s) fetching for document-referenced assets. Off by default so untrusted HTML cannot trigger network access.
func WithStrictAssets ¶ added in v0.2.2
func WithStrictAssets() Option
WithStrictAssets makes asset load failures (stylesheet links, @font-face sources, images, background images) surface as an error from FromString / FromReader. The partially-converted rows are still returned alongside the joined *AssetError values. Without this option failures warn-and-continue.
func WithStylesheetBaseDir ¶
WithStylesheetBaseDir scopes <link href=...> local-file reads to a single directory. Paths that escape via ".." or absolute prefix are refused.
func WithURLPolicy ¶ added in v0.2.2
WithURLPolicy registers a callback that can veto individual URLs before any remote fetch. Only consulted when WithRemoteAssets is enabled.
func WithUnsafeNoLimits ¶
func WithUnsafeNoLimits() Option
WithUnsafeNoLimits disables resource caps. Use only for trusted input.
func WithUnsupportedHandler ¶
WithUnsupportedHandler registers a callback invoked for unsupported HTML tags or CSS properties. Use it to log diagnostics during development.
type PageOptions ¶ added in v0.2.0
type PageOptions = translate.PageOptions
PageOptions captures the supported subset of the CSS `@page` rule.
type ParseError ¶ added in v0.2.2
type ParseError = translate.ParseError
ParseError indicates the input HTML could not be parsed into a document.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package css parses CSS declarations and resolves them into a ComputedStyle that the HTML translator can read.
|
Package css parses CSS declarations and resolves them into a ComputedStyle that the HTML translator can read. |
|
Package dom provides a Paper-friendly wrapper over golang.org/x/net/html.
|
Package dom provides a Paper-friendly wrapper over golang.org/x/net/html. |
|
Package translate converts a parsed HTML DOM into a slice of Paper rows.
|
Package translate converts a parsed HTML DOM into a slice of Paper rows. |