Project

ladl

0.0
The project is in a healthy, maintained state
Declarative auto-numbering and labeling engine implementing MN 112 (Label Auto-assignment Definition Language, LADL). Parses labeling style profiles written in the LutaML instance syntax and evaluates labels, numbers and sequences against a content tree over the three LADL model trees: the content tree, the context tree and the labeller tree.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 13
~> 3

Runtime

 Project Readme

ladl: the Label Auto-assignment Definition Language engine

LADL — the Label Auto-assignment Definition Language — is a declarative modeling language for automatic label assignment (numbering, figure and table captions, note and example counting, ordered-list schemes) across technical documentation and publishing systems. This gem implements the LADL specification MN 112 as a runtime: it parses labeling style profiles, evaluates them against a content tree, and produces the labels.

The engine is shared; flavor behavior comes entirely from profiles. The engine ships NO bundled profiles: each flavor profile lives in its flavor gem and each taste profile in metanorma-taste (per the release plan, one LADL profile extraction per flavor gem / per taste), loaded through Ladl.load_profile. The MN 112-A worked profile instance from the specification is kept as the conformance fixture (spec/fixtures/iso_iec_label_style.lml).

Installation

gem install ladl

Usage

require "ladl"

profile = Ladl.iso_iec_profile
tree = Ladl::ContentElement.new(id: "doc", type: "document",
  children: [
    Ladl::ContentElement.new(id: "c1", type: "clause"),
    Ladl::ContentElement.new(id: "ax1", type: "annex", children: [
      Ladl::ContentElement.new(id: "ax1s1", type: "clause"),
    ]),
  ])

result = Ladl::Runtime::Engine.new(profile).label(tree)
result.local_for(tree.children.first)   # => "Clause 1"
result.global_for(tree.children.first)  # => "Clause 1"
result.local_for(tree.children.last.children.first) # => "A.1"

Load a custom profile written in the MN 112 .lml instance syntax:

profile = Ladl.load_profile("my_flavor.lml")

Adapt a metanorma-document presentation tree:

content = Ladl::Adapters::MetanormaDocument.wrap(mn_document_root)
result = Ladl::Runtime::Engine.new(profile).label(content)

Architecture

LADL models labeling over three object trees (MN 112 "Architecture"):

Tree Model Role

Content tree

Ladl::ContentElement

The elements being labeled. Provided by the host (a metanorma-document presentation tree wrapped by the adapter, or any duck-typed tree).

Context tree

Ladl::LabelContext

Label contexts that scope labeller counters (document, clause, annex, list), linked by parent.

Labeller tree

Ladl::Labeller

Hierarchical labellers bound to contexts, linked by parent; each carries a sequence and local/global label templates.

Table 1. Profile loading

Ladl::Lml::Parser

Parses the .lml instance syntax (comments, attribute assignments, lists, nested { } maps, definition free-text blocks).

Ladl::ProfileLoader

Groups parsed instances by base class and hydrates them into the typed profile models through lutaml-model mappings (Model.from_hash); no hand-written serialization anywhere.

Ladl::Profile

The loaded style plus instance tables (labellers, contexts, templates, sequences).

Table 2. Runtime

Ladl::Runtime::Resolver

Wires profile references into indexes: labellers by context instance and element type, context instances by name and parent, sequence resolution against the registry and profile-defined sequences, and labeller-tree ancestry for %pn.

Ladl::Runtime::Engine

The labeling run: walks the content tree depth-first, instantiates label context instances, resolves each element’s labeller, increments counters scoped by context instance and parent element, selects label templates by condition, and renders patterns. The walk plans and counts first, then renders, so conditional templates see final sibling counts.

Ladl::Runtime::Renderer

String interpolation of %n, %pn, %parent and %self.

Ladl::Runtime::Result

The output: a Ladl::Label per labeled element, lookup by element identity, with local and global label strings, hierarchical paths, sequence values and positions.

Ladl::Sequences

The default sequences MN 112 requires of a compliant runtime (decimal, Roman, alphabetic, Cyrillic, Greek, Chinese, Japanese, Korean, Arabic, Hebrew) plus the expansion rules and arithmetic for profile-defined sequence instances.

MN 112 concept map

MN 112 concept Class / module

Label

Ladl::Label

Labeller

Ladl::Labeller

Sequence

Ladl::NumericSequence, Ladl::CharacterSequence

Labeling format

Ladl::Sequences::Formatters

Label context

Ladl::LabelContext

Label context condition

Ladl::LabelContextCondition

Label template

Ladl::LabelTemplate

Label style

Ladl::LabelStyle (loaded into Ladl::Profile)

ContentElement

Ladl::ContentElement

String interpolation

Ladl::Runtime::Renderer

Runtime requirements (MN 112 "Runtime")

Ladl::Runtime::Engine

Default sequences

Ladl::Sequences::Registry

Value for position

Ladl::Sequences.value_for, Sequence#value_for_position

Algorithm to resolve the label

Ladl::Runtime::Engine

Instance model examples (MN 112-A)

lib/ladl/profiles/iso_iec_label_style.lml

Engine behavior notes

  • Counter scope. A labeller’s counters live in the context instance its context binds it to: notes bound to ClauseContext count per clause (across subclauses); annex figures bound to AnnexContext count per annex; list items bound to ListContext count per list.

  • Parent scoping. A labeller with a parent labeller additionally scopes its counter per enclosing parent element: subfigure letters restart per figure; nested ordered-list items restart per containing item.

  • %pn. The parent value is the hierarchical path of the nearest enclosing element labeled by the current labeller’s ancestor chain (subclause 1.2 for 1.2.1; the annex for Figure A.1), falling back to the nearest enclosing element of the parent labeller’s type (an enclosing figure labeled by the annex figure labeller for a subfigure).

  • %parent / %self. The rendered global label of the context element anchoring the labeller’s counter frame, and the element’s own local label (used inside global templates).

  • Conditional templates. The first template whose condition evaluates to true wins; the first unconditional template is the fallback (MN 112 algorithm).

  • Direction. char_direction/direction are carried on models; rendering emits logical order and relies on Unicode bidi for display.

Spec ambiguities and deviations

MN 112 is a draft; the following points are ambiguous in the text and were resolved as documented. Each is testable in the suite and is a candidate for a spec ruling.

  1. ArabicNumericSequence. RESOLVED 2026-11-26 by engine evidence: ISO/IEC deliverables label clauses with Western digits, so the bundled ISO/IEC and IETF profiles now bind clause numbering to DecimalNumericSequence, and the registry restores ArabicNumericSequence to its MN 112 definition (Arabic-script numerals ١, ٢, ٣). ArabicIndicNumericSequence (the RTL example’s name in MN 112) remains as the explicit RTL-flavored spelling.

  2. %parent in the Japanese example. MN 112’s Japanese clause example (%parentの%n, expected 二の一) uses %parent where only %pn semantics (parent sequence value) produce the shown output; by the %parent definition (rendered global label) the output is 第二章の一. The engine follows the definition; the example is flagged.

  3. SimpleLabelStyle template. MN 112’s SimpleLabelStyle example wires the subclause template as %parent.%n but states the desired output "1.1", which requires %pn.%n. The engine follows the stated outputs.

  4. %pn beyond one level. RESOLVED by the spec’s own example: the MN 112 subfigure template (Figure %pn %n) → "Figure A.1 a)") is only satisfiable when %pn carries the two-level parent path (A.1), so the engine computes %pn as the hierarchical path (parent chain values joined with .).

  5. Instantiation of annex clauses. The profile distinguishes annex clauses only by their enclosing annex; no context is name-matched by the clause type inside annexes. The engine instantiates the child context of the element’s labeller context (AnnexClauseContext for annex clauses) and falls back to name matching.

  6. MN 112-A profile gaps. RESOLVED 2026-11-26 by engine evidence: the current ISO engine (isodoc annex_names recursion) labels annex subclauses hierarchically (A.1.1, A.1.1.1) and numbers annex notes/examples, so the profile now carries AnnexSubclauseLabeller, AnnexNoteLabeller and AnnexExampleLabeller (bound to AnnexClauseContext). The suite covers A.1.1 and annex notes/examples end to end.

  7. cycle_length. RESOLVED by engine evidence: the ISO annex expansion (Annex Z → Annex AA) that isodoc implements is bijective (spreadsheet-style) decomposition, which is what the engine applies when cycle_length is present.

  8. append_suffix expansion. The expansion result after the character set is exhausted is underdefined; the engine appends cycling suffix characters symmetrically to prepend_prefix.

  9. NumericSequence format attribute. RESOLVED: profile-defined sequence instances are supported by the profile syntax — an .lml profile can declare `instance X < NumericSequence { format = "decimal"

    1. }` (or derive from a registry sequence via base_sequence), and the bundled registry defaults (decimal, Roman, alphabetic, …​) are NumericSequence instances selected by the format attribute.

  10. char_direction vs direction. MN 112 declares both on the sequence models; the engine carries both and treats direction as taking precedence.

  11. Profile syntax vs lutaml-lml. MN 112 profiles use unquoted instance names with < inheritance (instance Foo < Bar); the released LutaML grammar (lutaml-lml, RS 3001) spells inheritance as a body statement (isa Bar;) with quoted instance names. The parser now ACCEPTS BOTH SPELLINGS (2026-11-26), so a profile written in either syntax loads and labels identically; the bundled profiles keep the MN 112 spelling. Full grammar unification — extending the RS 3001 grammar with < inheritance, or amending MN 112 to isa — remains a language-level decision on the lutaml-lml side.

  12. Counter restart for notes in subclauses. RESOLVED 2026-11-26 by engine evidence: isodoc’s note_anchor_names/example_anchor_names restart the counter for every clause and subclause (Counter.new per section; increment_label returns an empty label for a singleton), so ISO/IEC note numbering IS per section. The profile now carries SubclauseNoteLabeller/SubclauseExampleLabeller bound to SubclauseContext: lone notes render "NOTE", notes continue "NOTE 1, NOTE 2" within one subclause and restart in the next, and global refs anchor at the enclosing section ("1.2, NOTE").

Development

bundle install
bundle exec rspec

Run the full test suite with bundle exec rspec. The suite covers the .lml parser, the profile loader against the bundled ISO/IEC profile, every default sequence, the core models, the resolver, the renderer, the engine algorithm (counter scoping, %pn resolution, conditional templates) and the metanorma-document adapter.

License

The gem is available as open source under the terms of the MIT license.