Documentation
¶
Overview ¶
Package translate converts a parsed HTML DOM into a slice of Paper rows.
Index ¶
- Variables
- func NewBlankPageRow(control core.PageControl) core.Row
- func NewPageBreakRow() core.Row
- func NewPageControlRow(control core.PageControl) core.Row
- func Translate(ctx context.Context, doc *dom.Document, opts ...Option) ([]core.Row, error)
- type AssetError
- type Document
- type ImageResolver
- type LimitError
- type LimitKind
- 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 WithImageResolver(fn ImageResolver) Option
- func WithLimits(l htmllimits.Limits) Option
- func WithOutlineFromHeadings() Option
- func WithRemoteAssets() Option
- func WithStrictAssets() Option
- func WithStylesheetBaseDir(dir string) Option
- func WithURLPolicy(policy URLPolicy) Option
- func WithUnsupportedHandler(fn func(thing, value string)) Option
- type PageOptions
- type ParseError
- type StylesheetResolver
- type URLPolicy
Constants ¶
This section is empty.
Variables ¶
var ErrImageResolverRefused = errors.New(
"html: default resolver refuses local file reads; configure html.WithImageBaseDir or translate.WithImageResolver",
)
ErrImageResolverRefused is returned by the default resolver when asked to load a non-data: URI without an explicit html.WithImageBaseDir or the lower-level translate.WithImageResolver.
var ErrStylesheetResolverRefused = errors.New("html: default stylesheet resolver refuses local file reads; configure WithStylesheetBaseDir")
ErrStylesheetResolverRefused is returned by the default resolver when asked to load a non-data: URI without an explicit WithStylesheetBaseDir.
var ErrURLPolicyDenied = errors.New("html: URL fetch blocked by URLPolicy")
ErrURLPolicyDenied wraps a URLPolicy rejection so callers can distinguish intentional policy blocks from network or filesystem failures.
Functions ¶
func NewBlankPageRow ¶ added in v0.2.2
func NewBlankPageRow(control core.PageControl) core.Row
NewBlankPageRow creates a zero-content row that inserts one physical blank page. The supplied control determines whether that blank page receives repeated footer/page-number decorations.
func NewPageBreakRow ¶
NewPageBreakRow creates an exported pageBreakRow for use in tests and the translate pipeline.
func NewPageControlRow ¶ added in v0.2.2
func NewPageControlRow(control core.PageControl) core.Row
NewPageControlRow creates a zero-content row that changes how the current physical page is committed, for example suppressing footer/page-number reservation on a title or interstitial page.
Types ¶
type AssetError ¶ added in v0.2.2
AssetError indicates a document-referenced asset failed to load while translating HTML with strict asset checking enabled.
func NewAssetError ¶ added in v0.2.2
func NewAssetError(category, ref string, err error) *AssetError
NewAssetError builds an AssetError for a reference that could not be loaded. category names the kind of asset, and ref is the value that failed.
func (*AssetError) Error ¶ added in v0.2.2
func (e *AssetError) Error() string
func (*AssetError) Unwrap ¶ added in v0.2.2
func (e *AssetError) Unwrap() error
type Document ¶ added in v0.2.0
type Document struct {
Rows []core.Row
// HeaderRows and FooterRows hold the translated content of the FIRST
// top-level <header> / <footer> element (direct children of <body>).
// They are meant for Paper's RegisterHeader/RegisterFooter so the bands
// repeat on every page. Subsequent top-level header/footer elements and
// nested ones render inline as part of Rows.
HeaderRows []core.Row
// Page holds size/margin options from a plain `@page { ... }` rule, or
// nil when the document has none (or none that parsed to a usable value).
Page *PageOptions
}
Document is the full result of translating an HTML document: the content rows plus document-level options parsed from `@page` rules.
func FromDOM ¶ added in v0.2.2
FromDOM walks the styled DOM and returns the full Document: content rows plus @page options. It observes ctx at cheap phase and recursive traversal boundaries. Unlike Translate, the first top-level <header>/<footer> elements are extracted into HeaderRows/FooterRows instead of rendering inline.
type ImageResolver ¶
ImageResolver loads image bytes for a given <img src="…"> value. It returns the raw bytes and a hint extension ("png", "jpg", "svg", etc.). When err is non-nil the caller falls back to the <img>'s alt text.
type LimitError ¶ added in v0.2.2
LimitError indicates HTML translation stopped because a configured resource ceiling was crossed.
func (*LimitError) Error ¶ added in v0.2.2
func (e *LimitError) Error() string
func (*LimitError) Unwrap ¶ added in v0.2.2
func (e *LimitError) Unwrap() error
type LimitKind ¶ added in v0.2.2
type LimitKind int
LimitKind identifies which HTML translation resource ceiling was exceeded.
const ( // LimitElements reports that the configured element/node budget was exceeded. LimitElements LimitKind = iota // LimitDepth reports that the configured DOM nesting depth was exceeded. LimitDepth // LimitStyleRules reports that the configured stylesheet rule budget was exceeded. LimitStyleRules )
type Option ¶
type Option func(*translator)
Option configures translator behaviour.
func WithContentWidth ¶
WithContentWidth sets the content width in mm, used for gap-to-col approximation.
func WithFallbackFontPath ¶ added in v0.2.2
WithFallbackFontPath registers a font (local path, data: URI, or http(s) URL with WithRemoteAssets) that is loaded on demand when the document contains text outside the WinAnsi (cp1252) repertoire, so such text renders instead of degrading.
func WithGridSize ¶
WithGridSize overrides the default 12-column grid size used for flex quantization.
func WithHTTPClient ¶ added in v0.2.2
WithHTTPClient overrides http.DefaultClient for remote asset fetches.
func WithImageBaseDir ¶
WithImageBaseDir scopes the default resolver to a single directory. Local file reads outside this directory are refused (path-traversal safe).
func WithImageResolver ¶
func WithImageResolver(fn ImageResolver) Option
WithImageResolver lets callers plug in a custom <img src=…> loader.
func WithLimits ¶
func WithLimits(l htmllimits.Limits) Option
WithLimits configures resource limits for untrusted HTML translation.
func WithOutlineFromHeadings ¶ added in v0.2.0
func WithOutlineFromHeadings() Option
WithOutlineFromHeadings marks h1-h6 headings with a props.Outline so they appear in the PDF document outline (h1 = level 0 ... h6 = level 5).
func WithRemoteAssets ¶ added in v0.2.2
func WithRemoteAssets() Option
WithRemoteAssets enables http(s) fetching for document-referenced assets (stylesheet links and the fallback font). Off by default: remote URLs are refused 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 errors from Translate. The partially-translated rows are still returned alongside the joined asset errors. Without this option failures warn-and-continue.
func WithStylesheetBaseDir ¶
WithStylesheetBaseDir scopes the default stylesheet resolver to a single directory. Local-file reads outside this directory 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 remote fetching is enabled.
func WithUnsupportedHandler ¶
WithUnsupportedHandler registers a callback for unsupported tags/props.
type PageOptions ¶ added in v0.2.0
type PageOptions struct {
// PageSize is a normalized lowercase named size ("a4", "letter", ...);
// empty when explicit Width/Height are set or only orientation was given.
PageSize string
// Width and Height are explicit page dimensions in mm; 0 when unset.
Width float64
Height float64
// Landscape reports the `landscape` keyword.
Landscape bool
// Margins in mm; -1 means unset (keep the document default).
MarginLeft float64
MarginTop float64
MarginRight float64
MarginBottom float64
}
PageOptions captures the supported subset of the CSS `@page` rule: `size` (named size, explicit dimensions, landscape/portrait keyword) and `margin` (shorthand or per-side).
type ParseError ¶ added in v0.2.2
type ParseError struct {
Err error
}
ParseError indicates the input HTML could not be parsed into a document.
func (*ParseError) Error ¶ added in v0.2.2
func (e *ParseError) Error() string
func (*ParseError) Unwrap ¶ added in v0.2.2
func (e *ParseError) Unwrap() error
type StylesheetResolver ¶
StylesheetResolver loads CSS text for a given <link href="…"> value. Returns the raw bytes (UTF-8 CSS text) and any error.
Source Files
¶
- anchor.go
- basedir.go
- block_margins.go
- constants.go
- container.go
- counter.go
- cssparse.go
- cssvalue.go
- definition_list.go
- details.go
- errors.go
- fallback_font.go
- flex.go
- flex_align.go
- flex_cell.go
- flex_inline_axis.go
- flex_layout.go
- fontface.go
- forms.go
- grid.go
- image.go
- inline.go
- link.go
- list.go
- multicol.go
- multicol_balanced.go
- multicol_rule.go
- nested_table.go
- options.go
- pagebreak.go
- pagerule.go
- paragraph.go
- paragraph_split.go
- position.go
- quote.go
- remote_assets.go
- selector.go
- selector_expand.go
- strict_assets.go
- style.go
- stylesheet.go
- stylesheet_resolver.go
- table.go
- table_split.go
- table_split_multi.go
- transform.go
- translate.go