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
- Variables
- func AfterStructure(ctx context.Context, tx *store.Tx, task store.Task) error
- func CreationTime(creationDate *string) (time.Time, bool)
- func FromEntries(entries []Entry, book Book, producer, version string) []store.Section
- func NewHandler(path string, client *worker.Client, log *slog.Logger) worker.Handler
- func Offset(units []store.Unit) (int, bool)
- func Outline(p Profile, units []store.Unit, title, version string) []store.Section
- func OutlineVersion(structure, parse store.Output) string
- func PageLabels(units []store.Unit) []string
- func QueueAfterParse(ctx context.Context, tx *store.Tx, task store.Task) error
- func Replayable() server.Replayable
- func Squashed(s string) string
- func TitleKey(title string) string
- func Write(ctx context.Context, tx *store.Tx, book store.Item, rows []store.Section) error
- type Book
- type Entry
- type FontShares
- type OutlineEntry
- type PageLabel
- type Profile
Constants ¶
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 )
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 )
const ( PlacedByOutline = "outline" PlacedByWholeFile = "whole-file" )
How a section's start page was found.
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 ¶
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 ¶
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 ¶
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
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 ¶
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 ¶
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 ¶
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 ¶
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
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 ¶
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 Squashed ¶ added in v0.21.0
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
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 ¶
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 {
}
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"`
// Title is the metadata's title, and nil where it holds none.
Title *string `json:"title"`
}
Profile is what pdf-structure finds in one PDF.