crossref

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 8 Imported by: 0

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

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

func NewResolverForLanguage(lang string) *Resolver

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

func (r *Resolver) AddCaptions(html string) string

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

func (r *Resolver) GetAllReferences() map[string]*Reference

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

func (r *Resolver) ProcessHTML(html string) string

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

func (r *Resolver) ProcessHTMLWithUnresolved(html string) (string, []string)

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

func (r *Resolver) RegisterFigure(id, title string) int

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

func (r *Resolver) RegisterFromHTML(htmlContent string)

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

func (r *Resolver) RegisterSection(id, title string, level int)

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

func (r *Resolver) RegisterTable(id, title string) int

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.

func (*Resolver) Reset

func (r *Resolver) Reset()

Reset clears all references and reinitializes the resolver. Used when processing multiple independent documents.

func (*Resolver) Resolve

func (r *Resolver) Resolve(id string) (*Reference, error)

Resolve looks up reference information by ID. Returns the found Reference pointer, or an error if not found.

Jump to

Keyboard shortcuts

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