translate

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 39 Imported by: 0

Documentation

Overview

Package translate — flex layout dispatch and item-content construction. Quantization, weight computation, and slack distribution live in flex_layout.go.

Package translate converts a parsed HTML DOM into a slice of Paper rows.

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
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.

Functions

func Hamilton

func Hamilton(weights []float64, total int) []int

Hamilton distributes total integer units across items proportionally to their weights using the largest-remainder method (Hamilton's method). Returns []int{} for empty input; zero-weight inputs get equal split.

func NewPageBreakRow

func NewPageBreakRow() core.Row

NewPageBreakRow creates an exported pageBreakRow for use in tests and the translate pipeline.

func Translate

func Translate(ctx context.Context, doc *dom.Document, opts ...Option) ([]core.Row, error)

Translate walks the styled DOM and emits Paper rows. It observes ctx at cheap phase and recursive traversal boundaries.

Types

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
	FooterRows []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 TranslateDocument added in v0.2.0

func TranslateDocument(ctx context.Context, doc *dom.Document, opts ...Option) (*Document, error)

TranslateDocument walks the styled DOM and returns the full Document result: 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

type ImageResolver func(src string) (data []byte, ext string, err error)

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 Option

type Option func(*translator)

Option configures translator behaviour.

func WithContentWidth

func WithContentWidth(mm float64) Option

WithContentWidth sets the content width in mm, used for gap-to-col approximation.

func WithGridSize

func WithGridSize(n int) Option

WithGridSize overrides the default 12-column grid size used for flex quantization.

func WithImageBaseDir

func WithImageBaseDir(dir string) Option

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 WithStylesheetBaseDir

func WithStylesheetBaseDir(dir string) Option

WithStylesheetBaseDir scopes the default stylesheet resolver to a single directory. Local-file reads outside this directory are refused.

func WithUnsupportedHandler

func WithUnsupportedHandler(fn func(thing, value string)) Option

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 StylesheetResolver

type StylesheetResolver func(href string) ([]byte, error)

StylesheetResolver loads CSS text for a given <link href="…"> value. Returns the raw bytes (UTF-8 CSS text) and any error.

Jump to

Keyboard shortcuts

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