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 ¶
- Variables
- func Hamilton(weights []float64, total int) []int
- func NewPageBreakRow() core.Row
- func Translate(ctx context.Context, doc *dom.Document, opts ...Option) ([]core.Row, error)
- type Document
- type ImageResolver
- type Option
- func WithContentWidth(mm float64) Option
- func WithGridSize(n int) Option
- func WithImageBaseDir(dir string) Option
- func WithImageResolver(fn ImageResolver) Option
- func WithLimits(l htmllimits.Limits) Option
- func WithOutlineFromHeadings() Option
- func WithStylesheetBaseDir(dir string) Option
- func WithUnsupportedHandler(fn func(thing, value string)) Option
- type PageOptions
- type StylesheetResolver
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.
Functions ¶
func Hamilton ¶
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 ¶
NewPageBreakRow creates an exported pageBreakRow for use in tests and the translate pipeline.
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
// 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
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 ¶
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 ¶
WithContentWidth sets the content width in mm, used for gap-to-col approximation.
func WithGridSize ¶
WithGridSize overrides the default 12-column grid size used for flex quantization.
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 WithStylesheetBaseDir ¶
WithStylesheetBaseDir scopes the default stylesheet resolver to a single directory. Local-file reads outside this directory are refused.
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 StylesheetResolver ¶
StylesheetResolver loads CSS text for a given <link href="…"> value. Returns the raw bytes (UTF-8 CSS text) and any error.