translate

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 40 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(doc *dom.Document, opts ...Option) ([]core.Row, error)

Translate walks the styled DOM and emits Paper rows.

func TranslateCtx

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

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

Types

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 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 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