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 ¶
- Variables
- func SourceMapFromLaTeX(source map[string]latex.SourceLocation) map[string]SourceLocation
- type AssetResolver
- type CompileOutput
- type CompileRequest
- type Compiler
- type Diagnostic
- type ExecProcessRunner
- type LaTeXCompiler
- type LaTeXCompilerOptions
- type Options
- type Page
- type PageRasterizer
- type PopplerRasterizer
- type Process
- type ProcessOutput
- type ProcessRunner
- type RasterOutput
- type Result
- type Session
- type SourceLocation
- type StageTimings
- type TypstCompiler
- type TypstCompilerOptions
- type Workspace
- type WorkspaceFactory
Constants ¶
This section is empty.
Variables ¶
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") )
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") )
var ErrTypstPagesMissing = errors.New("preview: typst produced no page images")
ErrTypstPagesMissing reports a PNG run that produced no page images.
var ErrWorkspaceRequired = errors.New("preview workspace factory returned no workspace")
Functions ¶
func SourceMapFromLaTeX ¶ added in v2.4.0
func SourceMapFromLaTeX(source map[string]latex.SourceLocation) map[string]SourceLocation
SourceMapFromLaTeX converts a LaTeX projection's source map.
Types ¶
type AssetResolver ¶
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]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 backend execution and page rasterization.
type Diagnostic ¶
type Diagnostic struct {
ComponentID string `json:"componentId,omitempty"`
Path string `json:"path,omitempty"`
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
Severity string `json:"severity,omitempty"`
Message string `json:"message"`
Hints []string `json:"hints,omitempty"`
}
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 ¶
func (ExecProcessRunner) Run(ctx context.Context, process Process) (ProcessOutput, error)
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 ProcessOutput ¶
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 ¶
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 ¶
NewSession creates a private workspace retained across preview revisions.
func (*Session) Done ¶
func (session *Session) Done() <-chan struct{}
Done closes when manual close or idle expiry has released the workspace.
type SourceLocation ¶ added in v2.4.0
SourceLocation maps a component to the generated source file that holds it, whatever the backend. latex.SourceLocation converts to it directly.
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 TypstCompiler ¶ added in v2.4.0
type TypstCompiler struct {
// contains filtered or unexported fields
}
TypstCompiler compiles a workspace's main .typ file to a PDF and rasterizes its pages with typst itself, so no external rasterizer is involved. The workspace is the typst --root: the document can read every file in it and nothing outside it.
func NewTypstCompiler ¶ added in v2.4.0
func NewTypstCompiler(options TypstCompilerOptions) (*TypstCompiler, error)
NewTypstCompiler creates a Typst preview compiler.
func (*TypstCompiler) Compile ¶ added in v2.4.0
func (compiler *TypstCompiler) Compile(ctx context.Context, request CompileRequest) (CompileOutput, error)
Compile runs typst twice in the workspace: once to PDF, once to one PNG per page. Every page carries its bytes and a content hash; the session reports only the pages whose hash changed.
type TypstCompilerOptions ¶ added in v2.4.0
type TypstCompilerOptions struct {
// Binary is the typst executable; default "typst".
Binary string
// PPI is the pixel density of the page images; default 144.
PPI int
// FontPaths are extra font directories passed as --font-path.
FontPaths []string
// Inputs are passed as --input key=value (sys.inputs in the document).
Inputs map[string]string
// Runner executes typst; default ExecProcessRunner.
Runner ProcessRunner
}
TypstCompilerOptions configures a preview compiler for Typst workspaces.
type WorkspaceFactory ¶
WorkspaceFactory is the injectable lifecycle port for preview storage.