sections

package
v0.51.2 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 24 Imported by: 1 module, 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 sections is the structure stage and the sections that serve writes from it. A worker runs pdf-structure over a PDF's bytes, through isolatedprocess, and reports the Profile it writes as the structure output's answer. serve then applies the outline rule in the write that ends the structure task. It writes one sections row per outline entry, a child item per body chapter, and the book's source time from its creation date.

Index

Constants

View Source
const (
	DoclingMonospaceLeast = 0.20
	DoclingTeXOrMathUnder = 0.10
)

The font shares that select a book for extract.PDFDocling. Code is set in monospace, and Docling fences it where PyMuPDF's page text loses its layout. Formulas are set in TeX and math fonts, and Docling drops them, so a book with both stays pdf-text's alone.

View Source
const (
	// Producer is the program that writes each structure output.
	Producer = "pdf-structure"
	// Version names the program's number and PyMuPDF's, so a changed program
	// or a library upgrade writes its profile beside the old one.
	Version = "2+pymupdf-" + extract.PyMuPDFVersion
	// Capability is the capability of every structure task.
	Capability = store.StageStructure + ":" + Producer
	// Program is where the worker image installs pdf-structure.
	Program = extract.ProgramDir + "/" + Producer
	// Timeout bounds one run of pdf-structure, and a run past it fails its
	// task. It sits far above any book's run, so it stops only a program
	// that hangs.
	Timeout = 10 * time.Minute
)
View Source
const (
	// RuleVersion names the outline rule and the offset, so a changed rule
	// writes its sections beside the old.
	RuleVersion = "2"
	// WholeFilePages is the page count under which a PDF with no usable
	// outline takes one section over the whole file. A longer one gets no
	// sections from the outline rule.
	WholeFilePages = 50
)
View Source
const (
	PlacedByOutline   = "outline"
	PlacedByWholeFile = "whole-file"
)

How a section's start page was found.

View Source
const (
	// TitleLeast is the fewest letters and digits of a title's key that a
	// search for the title looks for. A title whose words after its
	// numbering hold fewer is keyed whole.
	TitleLeast = 4
)

Variables

View Source
var ParsedBy = extract.PDFText

ParsedBy is the extractor whose items pass the structure stage. Its parse's end queues the stage, a replay selects its media type, and the outline rule and the contents placement read its parse.

Functions

func AfterStructure

func AfterStructure(ctx context.Context, tx *store.Tx, task store.Task) error

AfterStructure runs in the write that ends a PDF's structure task done. It reads the newest done profile and the newest done pdf-text parse, and writes the sections that Outline finds from them through Write. Where the item holds no source time, it takes the PDF's creation date.

func CreationTime

func CreationTime(creationDate *string) (time.Time, bool)

CreationTime is the instant that a PDF's creationDate names, and false where it names none or is not a date. A date with no zone, or with Z, is read as UTC.

func FromEntries added in v0.21.0

func FromEntries(entries []Entry, book Book, producer, version string) []store.Section

FromEntries is the sections that producer writes from entries under version: one row per entry, each with its kind as kindOf decides it, and nil where no level of them makes a usable outline of book. An entry on PDF page 1 whose title names the book is front matter, as a cover or title page is.

func NewHandler

func NewHandler(path string, client *worker.Client, log *slog.Logger) worker.Handler

NewHandler is the worker handler that profiles each structure task's PDF with the program at path, reading its bytes through client. It is nil where the program is not installed, so the worker claims no structure task.

func Offset

func Offset(units []store.Unit) (int, bool)

Offset is the most common difference between a page's PDF page and a number standing alone on one of its first or last edgeLines lines, and true where that difference holds on at least offsetCover of the pages. A tie goes to the difference nearer 0.

func Outline

func Outline(p Profile, units []store.Unit, title, version string) []store.Section

Outline is the sections that serve writes from p and the units of the PDF's positional parse, under producer version version. title is the book's title as its item holds it. Where the outline is usable it is one row per entry, each with its kind as FromEntries decides it. Where the outline is not usable and the PDF is under WholeFilePages pages, it is one section over the whole file. Otherwise it is none.

func OutlineVersion

func OutlineVersion(structure, parse store.Output) string

OutlineVersion is the producer version of the sections that the outline rule writes from structure and parse, each of which is part of it. So a new profile or a new parse writes its sections beside the old.

func PageLabels added in v0.21.0

func PageLabels(units []store.Unit) []string

PageLabels is each page's label in units, trimmed, unit n's at index n-1, and empty where it has none. Every one is empty where the labels are their pages' PDF pages on identityShare of the pages, because such labels say nothing of the numbers that the book prints.

func QueueAfterParse

func QueueAfterParse(ctx context.Context, tx *store.Tx, task store.Task) error

QueueAfterParse queues the structure task of a PDF in the write that ends its pdf-text parse done, so every parsed PDF is profiled.

func QueueDoclingParse added in v0.22.0

func QueueDoclingParse(ctx context.Context, tx *store.Tx, task store.Task) error

QueueDoclingParse runs in the write that ends a PDF's structure task done. Where the newest profile is CodeHeavyMathLight, it queues a parse by extract.PDFDocling, unless one is queued or leased, or a done one at its version is held. So a structure replay queues each selected book that Docling has not read at its version, and a book whose Docling parse failed is read again.

func Replayable

func Replayable() server.Replayable

Replayable is the structure stage as a replay runs it again, such as with a new pdf-structure: a task for each PDF. Its end writes the sections again, beside the old.

func Squashed added in v0.21.0

func Squashed(s string) string

Squashed is s folded by NFKC and lowercased, with every rune but its letters and digits left out, so a ligature such as fi reads as fi.

func TitleKey added in v0.21.0

func TitleKey(title string) string

TitleKey is a title as a search for it on a page, and a later version's chapter, match it: Squashed after any numbering before its words, up to titleRunes runes. So 1 Start and Chapter 1: Start share a key.

func Write

func Write(ctx context.Context, tx *store.Tx, book store.Item, rows []store.Section) error

Write writes one producer version's sections of book, once, and the chapter items they name. Where the book holds no chapter yet, each chapter row becomes a child item, numbered from 1, which takes the book's zone, source time, side and read decisions. Where it holds chapters, a chapter row names the item that an earlier version made for a chapter with the same start page and TitleKey. So where a model words a title anew, the chapter keeps its item. A row that matches none names no item, so a later version renames no chapter and makes none.

Types

type Book added in v0.21.0

type Book struct {
	Pages int
	// Titles are the names the book goes by, such as its item's title and
	// its PDF's own, any of them empty.
	Titles []string
	// Units are the PDF's positional parse, unit n being PDF page n.
	Units []store.Unit
}

Book is what the rule reads of a book beside its entries.

type Entry added in v0.21.0

type Entry struct {
	// Level is the entry's depth, from 1 at the top.
	Level int
	Title string
	// Page is the PDF page that the entry opens, from 1.
	Page int
	// PlacedBy says how Page was found, such as outline.
	PlacedBy string
}

Entry is one entry that opens a page of a book, as its outline or its printed contents names it, in the book's order.

type FontShares

type FontShares struct {
	SampledPages int     `json:"sampled_pages"`
	Monospace    float64 `json:"monospace"`
	TeXOrMath    float64 `json:"tex_or_math"`
}

FontShares are the shares of the sampled pages, every fifth from the first, whose fonts include a monospace one or a TeX or math one. Code is set in monospace, and formulas in TeX and math fonts.

func (FontShares) CodeHeavyMathLight added in v0.22.0

func (s FontShares) CodeHeavyMathLight() bool

CodeHeavyMathLight says whether monospace fonts reach DoclingMonospaceLeast of the sampled pages while TeX and math fonts stay under DoclingTeXOrMathUnder.

type OutlineEntry

type OutlineEntry struct {
	// Level is the entry's depth, from 1 at the top.
	Level int    `json:"level"`
	Title string `json:"title"`
	// Page is the entry's target, as a PDF page from 1, and nil where the
	// target lies outside the document.
	Page *int `json:"page"`
}

OutlineEntry is one entry of a PDF's outline.

type PageLabel

type PageLabel struct {
	// StartPage is the first PDF page that the rule labels, from 1.
	StartPage int    `json:"start_page"`
	Prefix    string `json:"prefix"`
	// Style is D for decimal, r and R for roman, a and A for letters, and
	// empty for the prefix alone.
	Style       string `json:"style"`
	FirstNumber int    `json:"first_number"`
}

PageLabel is one of a PDF's page-label rules. It labels each page from StartPage until the next rule's start.

type Profile

type Profile struct {
	Pages int `json:"pages"`
	// Outline is every outline entry, in the outline's order.
	Outline    []OutlineEntry `json:"outline"`
	PageLabels []PageLabel    `json:"page_labels"`
	// CreationDate is the metadata's creationDate as the PDF writes it, such
	// as D:20190415093021+02'00', and nil where it holds none.
	CreationDate *string `json:"creation_date"`
	// Title is the metadata's title, and nil where it holds none.
	Title      *string    `json:"title"`
	FontShares FontShares `json:"font_shares"`
}

Profile is what pdf-structure finds in one PDF.

func NewestProfile added in v0.21.0

func NewestProfile(outputs []store.Output) (Profile, bool, error)

NewestProfile is the profile of the newest done structure output among outputs, and false where none is done.

Jump to

Keyboard shortcuts

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