sectionize

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 11 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 sectionize reconstructs a document's clause hierarchy.

The input is a doc.Document and, when the file has one, its structure tree. The output is a doc.Outline: a real tree of sections, each with a title, a level, its own content, and its children.

The package rests on one measured fact about how real specifications declare hierarchy. ISO 32000-2 contains 7 Sect elements against 981 headings, and a single Part holds 13,442 direct children in a flat H1 P P P … stream — 966 of those 981 headings have no element children at all. So a clause's body is not its heading's subtree; it is the heading's *following siblings*, up to the next heading of equal or higher rank. Collecting container elements would emit 7 sections from a 1,023-page standard, and would look correct on any document that happened to nest properly.

That makes the hierarchy a level stack over a linear sequence of headings rather than a subtree extraction, which is also why the same builder will serve the untagged path: a sequence of (level, title, content) triples is all it needs, and font-size clustering can produce those where a structure tree cannot.

This is also where roles are assigned. extract deliberately marks every block RoleParagraph, because heading rank, list nesting and cell membership are declared by the tree and inferring them from geometry as well would mean two packages guessing at the same thing from less evidence. The declared role arrives here.

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{MaxTitle: 200}

DefaultOptions is reconstruction as the CLI runs it.

Functions

This section is empty.

Types

type Options

type Options struct {
	// MaxTitle bounds a resolved title's length in bytes. A heading is short, so a
	// title in the hundreds of characters means the join picked up something that is
	// not the heading — a producer that put a whole paragraph in one marked-content
	// sequence, most often. Truncating keeps the clause, where dropping the title
	// would leave a section nothing can name. Zero means the default.
	MaxTitle int
}

Options configures reconstruction.

type Stats

type Stats struct {
	// Sections is the total at every level. docs/DESIGN.md §8 puts ISO 32000-2 at
	// roughly 981.
	Sections int

	// Titled is the sections whose title resolved to something.
	Titled int

	// Numbered is the sections whose title began with a clause number.
	Numbered int

	// MaxLevel is the deepest heading level reached.
	MaxLevel int

	// Blocks is the content blocks attributed to a section or to the preamble.
	Blocks int

	// UnplacedBlocks and UnplacedChars are the content that reached
	// doc.Outline.Unplaced: text the extractor produced that no structure element
	// claimed. A tagged document should have little, and what it has is a property of
	// the file rather than of this join — ISO 32000-2 draws clause 1 outside any
	// marked content — but a large number means the join is losing content, so it is
	// reported rather than left to be noticed.
	//
	// Measured: 0 on ISO/TS 32005, 0.23% of characters on ISO 32000-2.
	UnplacedBlocks int
	UnplacedChars  int
}

Stats reports what a reconstruction did, and exists because the failure modes here are quiet ones. A run that emits 7 sections from a standard has silently reverted to container-driven segmentation; a run that leaves half the text unclaimed has silently dropped it. Neither is an error and both are visible in these numbers.

func Tagged

func Tagged(d *doc.Document, tr *tag.Tree, opt Options) (*doc.Outline, Stats)

Tagged reconstructs the outline from a structure tree.

The tree supplies heading rank and reading order — both declared, which is the expensive part to infer — and the document supplies the text. They are joined on (page, MCID), so tag.Tree.ResolvePages must have run first; without it no element knows its page and every title and body comes back empty.

A nil tree yields an outline with no sections and the whole document as preamble. That is the honest result for an untagged file rather than an error: it means this path does not apply.

func Untagged added in v1.0.0

func Untagged(d *doc.Document, opt Options) (*doc.Outline, Stats)

Untagged reconstructs the outline from blocks whose roles are already assigned.

The package comment states that the hierarchy is a level stack over a linear sequence of headings rather than a subtree extraction, and that the same stack would serve a document with no structure tree. This is that second producer. Tagged reads (level, title, content) out of H1..H6 elements; this reads it out of doc.RoleHeading blocks and their Level, and both drive the identical builder.open and builder.place.

It reads declared roles and infers nothing

No geometry, no typography, no font-size clustering — those belong to package layout, which has the measurements behind them and the negative results that bound them. A block arrives here already marked, by layout.Headings, which promotes a numbered heading in an untagged file, or by any other producer that sets a role. Reading the role rather than re-deriving it is what lets one function serve them all, and it is the same precedence the tagged path keeps — a declaration outranks a guess, wherever the declaration came from.

The consequence worth naming is that this is only as good as its input. layout promotes a heading only where the document numbers it, so an unnumbered "Foreword" stays a paragraph and lands in the preamble rather than opening a clause. That is layout's measured limit (its package comment, and DESIGN.md §10), not one this adds: a levelled sequence with no levels in it correctly yields no sections.

No Unplaced, and that is not a gap

Tagged joins page text to structure elements on (page, MCID) and reports what the join failed to claim, because on a tagged file unclaimed text means content silently lost. There is no join here. Every non-empty block in the document is either a heading or is placed under the open section, so Unplaced is always empty and the accounting is exact rather than merely reported.

A document whose blocks hold no heading yields an outline with no sections and the whole document as preamble — the same shape Tagged returns for a nil tree, and for the same reason: it is the honest statement that there was no hierarchy to find, not an error. A caller that needs clauses checks Outline.Sections.

Jump to

Keyboard shortcuts

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