preview

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: Apache-2.0 Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package preview manages persistent, revisioned compilation sessions for low-latency web previews. Sessions coalesce edits, cancel superseded work, retain LaTeX auxiliary files, and report only changed pages.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrProcessRunnerRequired = errors.New("preview: process runner is required")
	ErrRasterizerRequired    = errors.New("preview: page rasterizer is required")
	ErrPreviewPDFMissing     = errors.New("preview: compiler produced no PDF")
)
View Source
var (
	ErrClosed           = errors.New("preview session is closed")
	ErrSuperseded       = errors.New("preview revision was superseded")
	ErrCompilerRequired = errors.New("preview compiler is required")
	ErrResolverRequired = errors.New("preview asset resolver is required")
	ErrUnsafePath       = errors.New("unsafe preview workspace path")
)
View Source
var ErrWorkspaceRequired = errors.New("preview workspace factory returned no workspace")

Functions

This section is empty.

Types

type AssetResolver

type AssetResolver interface {
	Resolve(context.Context, latex.AssetBinding) ([]byte, error)
}

AssetResolver collects one inert asset at the workspace effect boundary.

type CompileOutput

type CompileOutput struct {
	PDF         []byte
	Pages       []Page
	Diagnostics []Diagnostic
	Timings     StageTimings
}

CompileOutput is returned by a target-specific compiler/rasterizer adapter.

type CompileRequest

type CompileRequest struct {
	Revision   uint64
	Workspace  string
	Main       string
	DirtyFiles []string
	SourceMap  map[string]latex.SourceLocation
}

CompileRequest identifies one revision within a persistent workspace.

type Compiler

type Compiler interface {
	Compile(context.Context, CompileRequest) (CompileOutput, error)
}

Compiler is the effect port around LaTeX execution and page rasterization.

type Diagnostic

type Diagnostic struct {
	ComponentID string `json:"componentId,omitempty"`
	Path        string `json:"path,omitempty"`
	Line        int    `json:"line,omitempty"`
	Severity    string `json:"severity,omitempty"`
	Message     string `json:"message"`
}

Diagnostic is a compiler message addressable to generated source.

type ExecProcessRunner

type ExecProcessRunner struct{}

ExecProcessRunner uses exec.CommandContext; cancellation terminates the child.

func (ExecProcessRunner) Run

type LaTeXCompiler

type LaTeXCompiler struct {
	// contains filtered or unexported fields
}

LaTeXCompiler retains per-workspace page fingerprints while TeX retains aux state in the workspace itself.

func NewLaTeXCompiler

func NewLaTeXCompiler(options LaTeXCompilerOptions) (*LaTeXCompiler, error)

NewLaTeXCompiler creates a concrete preview compiler. Defaults: xelatex, job name autopdf-preview, 144 DPI, and Poppler rasterization.

func (*LaTeXCompiler) Compile

func (compiler *LaTeXCompiler) Compile(ctx context.Context, request CompileRequest) (CompileOutput, error)

Compile reuses workspace aux files, compiles the current main source, then returns full page identities with bytes only for changed pages.

type LaTeXCompilerOptions

type LaTeXCompilerOptions struct {
	Engine     string
	JobName    string
	DPI        int
	Runner     ProcessRunner
	Rasterizer PageRasterizer
}

LaTeXCompilerOptions configures a warm compiler bound to session workspaces.

type Options

type Options struct {
	WorkspaceRoot    string
	IdleTTL          time.Duration
	KeepWorkspace    bool
	Resolver         AssetResolver
	WorkspaceFactory WorkspaceFactory
}

Options controls workspace placement and idle expiry.

type Page

type Page struct {
	Number    int    `json:"number"`
	MediaType string `json:"mediaType"`
	Data      []byte `json:"data"`
	Hash      string `json:"hash"`
}

Page is browser-ready output for one page. Hash may be omitted by compiler; the session fills it from MIME and Data.

type PageRasterizer

type PageRasterizer interface {
	Rasterize(context.Context, string, string, map[int]string) (RasterOutput, error)
}

PageRasterizer fingerprints pages and rasterizes only changed identities.

type PopplerRasterizer

type PopplerRasterizer struct {
	// contains filtered or unexported fields
}

PopplerRasterizer uses a cheap low-DPI visual pass for stable page identities and full-DPI pdftoppm only for pages whose identity changed.

func NewPopplerRasterizer

func NewPopplerRasterizer(runner ProcessRunner, dpi int) *PopplerRasterizer

func (*PopplerRasterizer) Rasterize

func (rasterizer *PopplerRasterizer) Rasterize(ctx context.Context, pdfPath, workspace string, previous map[int]string) (RasterOutput, error)

type Process

type Process struct {
	Executable string
	Args       []string
	Dir        string
}

Process is one cancellation-aware external command.

type ProcessOutput

type ProcessOutput struct {
	Stdout   string
	Stderr   string
	ExitCode int
}

ProcessOutput retains both output streams for diagnostic parsing.

type ProcessRunner

type ProcessRunner interface {
	Run(context.Context, Process) (ProcessOutput, error)
}

ProcessRunner is injectable so process lifecycle can be tested without TeX.

type RasterOutput

type RasterOutput struct {
	Pages       []Page
	Fingerprint time.Duration
	Raster      time.Duration
}

RasterOutput reports complete page identities; unchanged pages intentionally carry no Data, avoiding needless rasterization and transport.

type Result

type Result struct {
	Revision     uint64       `json:"revision"`
	Projection   string       `json:"projectionHash,omitempty"`
	PDF          []byte       `json:"pdf,omitempty"`
	ChangedPages []Page       `json:"changedPages,omitempty"`
	RemovedPages []int        `json:"removedPages,omitempty"`
	Diagnostics  []Diagnostic `json:"diagnostics,omitempty"`
	DirtyFiles   []string     `json:"dirtyFiles,omitempty"`
	Timings      StageTimings `json:"timings"`
	Err          error        `json:"-"`
}

Result contains only pages changed from the last accepted revision.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session serializes compiles within one persistent LaTeX workspace.

func NewSession

func NewSession(compiler Compiler, options Options) (*Session, error)

NewSession creates a private workspace retained across preview revisions.

func (*Session) Close

func (session *Session) Close() error

Close cancels work and removes only this session's private workspace.

func (*Session) Closed

func (session *Session) Closed() bool

Closed reports whether Close or idle expiry ended the session.

func (*Session) Done

func (session *Session) Done() <-chan struct{}

Done closes when manual close or idle expiry has released the workspace.

func (*Session) Submit

func (session *Session) Submit(ctx context.Context, projection latex.Projection) <-chan Result

Submit coalesces queued work, cancels an active older compile, and returns a one-shot result channel. Only the latest revision may publish output.

func (*Session) Workspace

func (session *Session) Workspace() string

Workspace returns the stable directory used for this session.

type StageTimings

type StageTimings struct {
	Materialize time.Duration `json:"materialize"`
	TeX         time.Duration `json:"tex"`
	Fingerprint time.Duration `json:"fingerprint"`
	Raster      time.Duration `json:"raster"`
	Total       time.Duration `json:"total"`
}

StageTimings separates deterministic orchestration from environment-sensitive TeX and raster work.

type Workspace

type Workspace interface {
	Path() string
	Close() error
}

Workspace is a private session directory lease. Close owns its cleanup.

type WorkspaceFactory

type WorkspaceFactory interface {
	Create() (Workspace, error)
}

WorkspaceFactory is the injectable lifecycle port for preview storage.

Jump to

Keyboard shortcuts

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