Documentation
¶
Overview ¶
Package crossref provides cross-reference and auto-numbering functionality. It supports numbering and referencing figures, tables, and sections, replacing placeholders in HTML with actual numbers.
Index ¶
- type Reference
- type Resolver
- func (r *Resolver) AddCaptions(html string) string
- func (r *Resolver) GetAllReferences() map[string]*Reference
- func (r *Resolver) ProcessHTML(html string) string
- func (r *Resolver) ProcessHTMLWithUnresolved(html string) (string, []string)
- func (r *Resolver) RegisterFigure(id, title string) int
- func (r *Resolver) RegisterFromHTML(htmlContent string)
- func (r *Resolver) RegisterSection(id, title string, level int)
- func (r *Resolver) RegisterTable(id, title string) int
- func (r *Resolver) Reset()
- func (r *Resolver) Resolve(id string) (*Reference, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Reference ¶
type Reference struct {
Type referenceType // Reference type (figure, table, or section)
ID string // Unique identifier
Number int // Auto-assigned number
Title string // Title or description
Level int // Heading level for sections; 0 for other types
NumberStr string // Hierarchical number string, e.g. "1.2.3" (sections only)
}
Reference represents a tracked reference object.
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver manages all cross-references and auto-numbering.
func NewResolver ¶
func NewResolver() *Resolver
NewResolver creates a new cross-reference resolver instance labeling references in English, the default book language.
func NewResolverForLanguage ¶ added in v0.8.0
NewResolverForLanguage creates a resolver whose labels and captions follow the book's language code (book.language), e.g. "Figure 1" or "图1".
func (*Resolver) AddCaptions ¶
AddCaptions adds numbered captions to figures and tables. Processes HTML like <figure id="fig_1"><img ...></figure> and adds <figcaption>图1: Title</figcaption>.
Example: Input: <figure id="fig_demo"><img src="demo.png"></figure> Output: <figure id="fig_demo"><img src="demo.png"><figcaption>图1: Demo</figcaption></figure>
func (*Resolver) GetAllReferences ¶
GetAllReferences returns all registered references (for debugging or building reference lists). Priority matches Resolve: figures > tables > sections. If the same ID exists in multiple categories, only the highest-priority entry is returned.
func (*Resolver) ProcessHTML ¶
ProcessHTML replaces {{ref:id}} placeholders with links to the referenced figure, table or section, labeled in the book's language ("Figure 1", "图1", "§1.2.3"). Placeholders whose ID is unknown are left as-is; use ProcessHTMLWithUnresolved to learn about them.
Example: Input: "As shown in {{ref:fig_demo}}, ..." Output: "As shown in <a href=\"#fig_demo\" class=\"ref-figure\">Figure 1</a>, ..."
func (*Resolver) ProcessHTMLWithUnresolved ¶ added in v0.8.0
ProcessHTMLWithUnresolved is ProcessHTML plus the IDs it could not resolve, in order of first appearance. A placeholder that stays in the text is printed verbatim to the reader, so callers report these rather than shipping "{{ref:fig_x}}" in the book.
func (*Resolver) RegisterFigure ¶
RegisterFigure registers a figure and returns its auto-assigned number. The id parameter is the figure's unique identifier (typically used for HTML anchors). The title parameter is the figure's caption or description.
func (*Resolver) RegisterFromHTML ¶ added in v0.8.0
RegisterFromHTML registers every identified <figure> and <table> found in the given HTML, in document order, so that {{ref:...}} placeholders and auto-captions work without the author numbering anything by hand. Callers run it over the whole book before resolving any placeholder.
Titles are taken from an existing <figcaption>/<caption>, falling back to the image's alt text, so a generated caption repeats what the author wrote instead of being blank.
func (*Resolver) RegisterSection ¶
RegisterSection registers a section. The id parameter is the section's unique identifier. The title parameter is the section heading text. The level parameter is the heading level (1-6), used for hierarchical numbering.
Hierarchical numbering example: Level 1: 1. 2. 3. ... Level 2: 1.1. 1.2. 2.1. ... Level 3: 1.1.1. 1.1.2. ...
func (*Resolver) RegisterTable ¶
RegisterTable registers a table and returns its auto-assigned number. The id parameter is the table's unique identifier. The title parameter is the table's caption or description.