sections

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 20 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 (
	// Producer is the program that writes each structure output.
	Producer = "pdf-structure"
	// Version names PyMuPDF's, so a library upgrade writes its profile beside
	// the old one.
	Version = "1+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 = "1"
	// 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.

Variables

This section is empty.

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 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, 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. Where the outline is usable it is one row per entry, each with its kind as kindOf 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 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 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 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 title. A row that matches none names no item, so a later version renames no chapter and makes none.

Types

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.

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"`
	FontShares   FontShares `json:"font_shares"`
}

Profile is what pdf-structure finds in one PDF.

Jump to

Keyboard shortcuts

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