kaleidl 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a99527045eb7b80c05683b6c7843e38f75e8964ad948c1004547ec0a79d99f69
4
+ data.tar.gz: 95691042e2b4da861c25c017cec4bdceb67e44117a215ddcf0acb84509d06fda
5
+ SHA512:
6
+ metadata.gz: 37a34c6d037f03f078fb8b12c3c7ec8a4f42a6ca2b5643c34fb199853a94c1cc62bd658a94d63c6819c7ec14c32a2e1c37a253efe04bd7027b41a94a2262b75b
7
+ data.tar.gz: 3b34882c8e9ab6091b45f0e4199b2698d21ad14df672619fea8e02160f1ef1bb102d04572f1df0fdd3d38eaaa4633a94af7754baec231559edd1144184774dc6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ribose, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.adoc ADDED
@@ -0,0 +1,284 @@
1
+ = kaleidl: the Label Auto-assignment Definition Language engine
2
+
3
+ LADL — the Label Auto-assignment Definition Language — is a declarative
4
+ modeling language for automatic label assignment (numbering, figure and
5
+ table captions, note and example counting, ordered-list schemes) across
6
+ technical documentation and publishing systems. This gem implements the
7
+ LADL specification MN 112 as a runtime: it parses labeling style
8
+ profiles, evaluates them against a content tree, and produces the
9
+ labels.
10
+
11
+ The engine is shared; flavor behavior comes entirely from profiles. The
12
+ engine ships NO bundled profiles: each flavor profile lives in its
13
+ flavor gem and each taste profile in metanorma-taste (per the release
14
+ plan, one LADL profile extraction per flavor gem / per taste), loaded
15
+ through `Kaleidl.load_profile`. The MN 112-A worked profile instance from
16
+ the specification is kept as the conformance fixture
17
+ (`spec/fixtures/iso_iec_label_style.lml`).
18
+
19
+ == Installation
20
+
21
+ gem install kaleidl
22
+
23
+ == Usage
24
+
25
+ [source,ruby]
26
+ ----
27
+ require "kaleidl"
28
+
29
+ profile = Kaleidl.iso_iec_profile
30
+ tree = Kaleidl::ContentElement.new(id: "doc", type: "document",
31
+ children: [
32
+ Kaleidl::ContentElement.new(id: "c1", type: "clause"),
33
+ Kaleidl::ContentElement.new(id: "ax1", type: "annex", children: [
34
+ Kaleidl::ContentElement.new(id: "ax1s1", type: "clause"),
35
+ ]),
36
+ ])
37
+
38
+ result = Kaleidl::Runtime::Engine.new(profile).label(tree)
39
+ result.local_for(tree.children.first) # => "Clause 1"
40
+ result.global_for(tree.children.first) # => "Clause 1"
41
+ result.local_for(tree.children.last.children.first) # => "A.1"
42
+ ----
43
+
44
+ Load a custom profile written in the MN 112 `.lml` instance syntax:
45
+
46
+ [source,ruby]
47
+ ----
48
+ profile = Kaleidl.load_profile("my_flavor.lml")
49
+ ----
50
+
51
+ Adapt a metanorma-document presentation tree:
52
+
53
+ [source,ruby]
54
+ ----
55
+ content = Kaleidl::Adapters::MetanormaDocument.wrap(mn_document_root)
56
+ result = Kaleidl::Runtime::Engine.new(profile).label(content)
57
+ ----
58
+
59
+ == Architecture
60
+
61
+ LADL models labeling over three object trees (MN 112 "Architecture"):
62
+
63
+ [cols="1,2,3"]
64
+ |===
65
+ | Tree | Model | Role
66
+
67
+ | Content tree
68
+ | `Kaleidl::ContentElement`
69
+ | The elements being labeled. Provided by the host (a metanorma-document
70
+ presentation tree wrapped by the adapter, or any duck-typed tree).
71
+
72
+ | Context tree
73
+ | `Kaleidl::LabelContext`
74
+ | Label contexts that scope labeller counters (document, clause, annex,
75
+ list), linked by `parent`.
76
+
77
+ | Labeller tree
78
+ | `Kaleidl::Labeller`
79
+ | Hierarchical labellers bound to contexts, linked by `parent`; each
80
+ carries a sequence and local/global label templates.
81
+ |===
82
+
83
+ .Profile loading
84
+ [cols="1,3"]
85
+ |===
86
+ | `Kaleidl::Lml::Parser`
87
+ | Parses the `.lml` instance syntax (comments, attribute assignments,
88
+ lists, nested `{ }` maps, `definition` free-text blocks).
89
+
90
+ | `Kaleidl::ProfileLoader`
91
+ | Groups parsed instances by base class and hydrates them into the typed
92
+ profile models through lutaml-model mappings (`Model.from_hash`); no
93
+ hand-written serialization anywhere.
94
+
95
+ | `Kaleidl::Profile`
96
+ | The loaded style plus instance tables (labellers, contexts, templates,
97
+ sequences).
98
+ |===
99
+
100
+ .Runtime
101
+ [cols="1,3"]
102
+ |===
103
+ | `Kaleidl::Runtime::Resolver`
104
+ | Wires profile references into indexes: labellers by context instance
105
+ and element type, context instances by name and parent, sequence
106
+ resolution against the registry and profile-defined sequences, and
107
+ labeller-tree ancestry for `%pn`.
108
+
109
+ | `Kaleidl::Runtime::Engine`
110
+ | The labeling run: walks the content tree depth-first, instantiates
111
+ label context instances, resolves each element's labeller, increments
112
+ counters scoped by context instance and parent element, selects label
113
+ templates by condition, and renders patterns. The walk plans and counts
114
+ first, then renders, so conditional templates see final sibling counts.
115
+
116
+ | `Kaleidl::Runtime::Renderer`
117
+ | String interpolation of `%n`, `%pn`, `%parent` and `%self`.
118
+
119
+ | `Kaleidl::Runtime::Result`
120
+ | The output: a `Kaleidl::Label` per labeled element, lookup by element
121
+ identity, with local and global label strings, hierarchical paths,
122
+ sequence values and positions.
123
+
124
+ | `Kaleidl::Sequences`
125
+ | The default sequences MN 112 requires of a compliant runtime (decimal,
126
+ Roman, alphabetic, Cyrillic, Greek, Chinese, Japanese, Korean, Arabic,
127
+ Hebrew) plus the expansion rules and arithmetic for profile-defined
128
+ sequence instances.
129
+ |===
130
+
131
+ == MN 112 concept map
132
+
133
+ [cols="2,3"]
134
+ |===
135
+ | MN 112 concept | Class / module
136
+
137
+ | Label | `Kaleidl::Label`
138
+ | Labeller | `Kaleidl::Labeller`
139
+ | Sequence | `Kaleidl::NumericSequence`, `Kaleidl::CharacterSequence`
140
+ | Labeling format | `Kaleidl::Sequences::Formatters`
141
+ | Label context | `Kaleidl::LabelContext`
142
+ | Label context condition | `Kaleidl::LabelContextCondition`
143
+ | Label template | `Kaleidl::LabelTemplate`
144
+ | Label style | `Kaleidl::LabelStyle` (loaded into `Kaleidl::Profile`)
145
+ | ContentElement | `Kaleidl::ContentElement`
146
+ | String interpolation | `Kaleidl::Runtime::Renderer`
147
+ | Runtime requirements (MN 112 "Runtime") | `Kaleidl::Runtime::Engine`
148
+ | Default sequences | `Kaleidl::Sequences::Registry`
149
+ | Value for position | `Kaleidl::Sequences.value_for`,
150
+ `Sequence#value_for_position`
151
+ | Algorithm to resolve the label | `Kaleidl::Runtime::Engine`
152
+ | Instance model examples (MN 112-A) | `lib/kaleidl/profiles/iso_iec_label_style.lml`
153
+ |===
154
+
155
+ == Engine behavior notes
156
+
157
+ * *Counter scope*. A labeller's counters live in the context instance
158
+ its `context` binds it to: notes bound to `ClauseContext` count per
159
+ clause (across subclauses); annex figures bound to `AnnexContext` count
160
+ per annex; list items bound to `ListContext` count per list.
161
+
162
+ * *Parent scoping*. A labeller with a `parent` labeller additionally
163
+ scopes its counter per enclosing parent element: subfigure letters
164
+ restart per figure; nested ordered-list items restart per containing
165
+ item.
166
+
167
+ * *`%pn`*. The parent value is the hierarchical path of the nearest
168
+ enclosing element labeled by the current labeller's ancestor chain
169
+ (subclause 1.2 for 1.2.1; the annex for Figure A.1), falling back to the
170
+ nearest enclosing element of the parent labeller's type (an enclosing
171
+ figure labeled by the annex figure labeller for a subfigure).
172
+
173
+ * *`%parent` / `%self`*. The rendered global label of the context
174
+ element anchoring the labeller's counter frame, and the element's own
175
+ local label (used inside global templates).
176
+
177
+ * *Conditional templates*. The first template whose condition evaluates
178
+ to true wins; the first unconditional template is the fallback (MN 112
179
+ algorithm).
180
+
181
+ * *Direction*. `char_direction`/`direction` are carried on models;
182
+ rendering emits logical order and relies on Unicode bidi for display.
183
+
184
+ == Spec ambiguities and deviations
185
+
186
+ MN 112 is a draft; the following points are ambiguous in the text and
187
+ were resolved as documented. Each is testable in the suite and is a
188
+ candidate for a spec ruling.
189
+
190
+ 1. *`ArabicNumericSequence`*. RESOLVED 2026-11-26 by engine evidence:
191
+ ISO/IEC deliverables label clauses with Western digits, so the bundled
192
+ ISO/IEC and IETF profiles now bind clause numbering to
193
+ `DecimalNumericSequence`, and the registry restores
194
+ `ArabicNumericSequence` to its MN 112 definition (Arabic-script
195
+ numerals ١, ٢, ٣). `ArabicIndicNumericSequence` (the RTL example's
196
+ name in MN 112) remains as the explicit RTL-flavored spelling.
197
+
198
+ 2. *`%parent` in the Japanese example*. RESOLVED 2026-11-27: the ISO/IEC
199
+ style templates in the bundled fixtures carried invented term words
200
+ (`Clause %n`), which made `%parent` render `Clause 1` instead of the
201
+ pure value. MN 112 is self-consistent: the Japanese chapter template is
202
+ `%n` (rendering 二), so `%parentの%n` → 二の一 reproduces exactly, and
203
+ `%parent.%n` → 1.1 likewise. The fixture templates now match MN 112
204
+ (term words appear only where MN 112 writes them, as in the subfigure
205
+ `Figure %pn %n`).
206
+
207
+ 3. *SimpleLabelStyle template*. RESOLVED 2026-11-27 together with 2:
208
+ with the pure templates (`%n`, `%parent.%n`), the stated output "1.1"
209
+ reproduces exactly.
210
+
211
+ 4. *`%pn` beyond one level*. RESOLVED by the spec's own example: the
212
+ MN 112 subfigure template (`Figure %pn %n)` → "Figure A.1 a)") is only
213
+ satisfiable when `%pn` carries the two-level parent path (A.1), so the
214
+ engine computes `%pn` as the hierarchical path (parent chain values
215
+ joined with `.`).
216
+
217
+ 5. *Instantiation of annex clauses*. The profile distinguishes annex
218
+ clauses only by their enclosing annex; no context is name-matched by
219
+ the clause type inside annexes. The engine instantiates the child
220
+ context of the element's labeller context (`AnnexClauseContext` for
221
+ annex clauses) and falls back to name matching.
222
+
223
+ 6. *MN 112-A profile gaps*. RESOLVED 2026-11-26 by engine evidence: the
224
+ current ISO engine (isodoc `annex_names` recursion) labels annex
225
+ subclauses hierarchically (A.1.1, A.1.1.1) and numbers annex
226
+ notes/examples, so the profile now carries `AnnexSubclauseLabeller`,
227
+ `AnnexNoteLabeller` and `AnnexExampleLabeller` (bound to
228
+ `AnnexClauseContext`). The suite covers A.1.1 and annex
229
+ notes/examples end to end.
230
+
231
+ 7. *`cycle_length`*. RESOLVED by engine evidence: the ISO annex
232
+ expansion (Annex Z → Annex AA) that isodoc implements is bijective
233
+ (spreadsheet-style) decomposition, which is what the engine applies
234
+ when `cycle_length` is present.
235
+
236
+ 8. *`append_suffix` expansion*. The expansion result after the
237
+ character set is exhausted is underdefined; the engine appends cycling
238
+ suffix characters symmetrically to `prepend_prefix`.
239
+
240
+ 9. *NumericSequence `format` attribute*. RESOLVED: profile-defined
241
+ sequence instances are supported by the profile syntax — an `.lml`
242
+ profile can declare `instance X < NumericSequence { format = "decimal"
243
+ ... }` (or derive from a registry sequence via `base_sequence`), and
244
+ the bundled registry defaults (decimal, Roman, alphabetic, ...) are
245
+ `NumericSequence` instances selected by the `format` attribute.
246
+
247
+ 10. *`char_direction` vs `direction`*. MN 112 declares both on the
248
+ sequence models; the engine carries both and treats `direction` as
249
+ taking precedence.
250
+
251
+ 11. *Profile syntax vs lutaml-lml*. MN 112 profiles use unquoted
252
+ instance names with `<` inheritance (`instance Foo < Bar`); the
253
+ released LutaML grammar (lutaml-lml, RS 3001) spells inheritance as a
254
+ body statement (`isa Bar;`) with quoted instance names. The parser now
255
+ ACCEPTS BOTH SPELLINGS (2026-11-26), so a profile written in either
256
+ syntax loads and labels identically; the bundled profiles keep the MN
257
+ 112 spelling. Full grammar unification — extending the RS 3001 grammar
258
+ with `<` inheritance, or amending MN 112 to `isa` — remains a
259
+ language-level decision on the lutaml-lml side.
260
+
261
+ 12. *Counter restart for notes in subclauses*. RESOLVED 2026-11-26 by
262
+ engine evidence: isodoc's `note_anchor_names`/`example_anchor_names`
263
+ restart the counter for every clause and subclause (`Counter.new` per
264
+ section; `increment_label` returns an empty label for a singleton), so
265
+ ISO/IEC note numbering IS per section. The profile now carries
266
+ `SubclauseNoteLabeller`/`SubclauseExampleLabeller` bound to
267
+ `SubclauseContext`: lone notes render "NOTE", notes continue
268
+ "NOTE 1, NOTE 2" within one subclause and restart in the next, and
269
+ global refs anchor at the enclosing section ("1.2, NOTE").
270
+
271
+ == Development
272
+
273
+ bundle install
274
+ bundle exec rspec
275
+
276
+ Run the full test suite with `bundle exec rspec`. The suite covers the
277
+ `.lml` parser, the profile loader against the bundled ISO/IEC profile,
278
+ every default sequence, the core models, the resolver, the renderer,
279
+ the engine algorithm (counter scoping, `%pn` resolution, conditional
280
+ templates) and the metanorma-document adapter.
281
+
282
+ == License
283
+
284
+ The gem is available as open source under the terms of the MIT license.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ # MN 112 "CharacterSequence" — defines a fixed sequence of characters.
5
+ #
6
+ # MN 112 declares both a `char_direction` (on the Sequence base class)
7
+ # and a `direction` (on CharacterSequence); this class carries both and
8
+ # treats `direction` as taking precedence. Expansion rules:,
9
+ # `repeat_label`, `prepend_prefix`, `double_character`, `append_suffix`,
10
+ # `none`.
11
+ class CharacterSequence < Lutaml::Model::Serializable
12
+ attribute :char_direction, :string, default: "ltr"
13
+ attribute :characters, :string, collection: true
14
+ attribute :expansion_rule, :string
15
+ attribute :expansion_prefix, :string, collection: true
16
+ attribute :direction, :string, default: "ltr"
17
+ attribute :definition, :string
18
+
19
+ hsh do
20
+ map "char_direction", to: :char_direction
21
+ map "characters", to: :characters
22
+ map "expansion_rule", to: :expansion_rule
23
+ map "expansion_prefix", to: :expansion_prefix
24
+ map "direction", to: :direction
25
+ map "definition", to: :definition
26
+ end
27
+
28
+ # MN 112 Sequence#value_for_position — provides the value for a given
29
+ # order in the sequence. Delegates the computation to
30
+ # Kaleidl::Sequences.value_for.
31
+ def value_for_position(position, lookup: nil)
32
+ Kaleidl::Sequences.value_for(self, position, lookup: lookup)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ # MN 112 "ContentElement" — represents a content element in an
5
+ # information container that can potentially hold a LabelContext object.
6
+ #
7
+ # ContentElements form the content tree: one of the three model trees
8
+ # of MN 112. The `type` attribute is used to match whether a Labeller
9
+ # can label the element.
10
+ #
11
+ # Per MN 112, "a ContentElement must be associated with a LabelContext
12
+ # object if it has children to be labeled": the `label_context`
13
+ # attribute names the LabelContext that scopes the element's
14
+ # descendants. The label resolution algorithm navigates the content
15
+ # tree through `parent` to the closest element carrying an
16
+ # association, so the scoping structure is DECLARED by the host as
17
+ # part of the content tree — the engine never infers it.
18
+ class ContentElement < Lutaml::Model::Serializable
19
+ attribute :id, :string
20
+ attribute :type, :string
21
+ attribute :label_context, :string
22
+ attribute :children, Kaleidl::ContentElement, collection: true
23
+ attribute :definition, :string
24
+
25
+ hsh do
26
+ map "id", to: :id
27
+ map "type", to: :type
28
+ map "label_context", to: :label_context
29
+ map "children", to: :children
30
+ map "definition", to: :definition
31
+ end
32
+
33
+ def initialize(*args, **kwargs, &block)
34
+ super
35
+ @children ||= []
36
+ end
37
+
38
+ def add_child(child)
39
+ @children << child
40
+ child
41
+ end
42
+
43
+ # Depth-first enumeration of the subtree rooted at this element.
44
+ def each(&block)
45
+ return enum_for(:each) unless block
46
+
47
+ yield self
48
+ Array(children).each { |c| c.each(&block) }
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ # Base error class for all LADL failures.
5
+ class Error < StandardError; end
6
+
7
+ # Raised when a `.lml` profile document cannot be parsed.
8
+ class ParseError < Error; end
9
+
10
+ # Raised when a profile references an instance that does not exist.
11
+ class ResolutionError < Error; end
12
+
13
+ # Raised when a sequence is asked for a position it cannot produce
14
+ # (e.g. a CharacterSequence with expansion rule "none" past its end).
15
+ class SequenceExhaustedError < Error; end
16
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ # MN 112 "Label" — a machine-readable label assigned to a content
5
+ # element, carrying the rendered local and global label strings plus
6
+ # the intermediate values used to build them.
7
+ class Label < Lutaml::Model::Serializable
8
+ attribute :value, :string
9
+ attribute :global_value, :string
10
+ attribute :hierarchical, :string
11
+ attribute :sequence_value, :string
12
+ attribute :position, :integer
13
+ attribute :labeller_id, :string
14
+ attribute :content_element, :string
15
+
16
+ hsh do
17
+ map "value", to: :value
18
+ map "global_value", to: :global_value
19
+ map "hierarchical", to: :hierarchical
20
+ map "sequence_value", to: :sequence_value
21
+ map "position", to: :position
22
+ map "labeller_id", to: :labeller_id
23
+ map "content_element", to: :content_element
24
+ end
25
+
26
+ # The runtime element this label is attached to. Not serialized: the
27
+ # element belongs to the host content tree, not to the profile.
28
+ attr_accessor :element
29
+ end
30
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ # MN 112 "Label context" — defines the scope of labeling (and a label
5
+ # hierarchy) within an information container.
6
+ #
7
+ # Instances of LabelContext form the context tree: one of the three
8
+ # model trees of MN 112, linked via the `parent` attribute.
9
+ class LabelContext < Lutaml::Model::Serializable
10
+ attribute :name, :string
11
+ attribute :parent, :string
12
+ attribute :element, :string
13
+ attribute :labellers, :string, collection: true
14
+ attribute :definition, :string
15
+
16
+ hsh do
17
+ map "name", to: :name
18
+ map "parent", to: :parent
19
+ map "element", to: :element
20
+ map "labellers", to: :labellers
21
+ map "definition", to: :definition
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ # MN 112 "Label context condition" — a context condition for a labeller
5
+ # to determine which label template to use, based on the number of
6
+ # similar elements within the same label context.
7
+ #
8
+ # Condition types: `element_count_gt`, `element_count_eq`,
9
+ # `element_count_lt`.
10
+ class LabelContextCondition < Lutaml::Model::Serializable
11
+ attribute :type, :string
12
+ attribute :threshold, :integer
13
+
14
+ hsh do
15
+ map "type", to: :type
16
+ map "threshold", to: :threshold
17
+ end
18
+
19
+ # Evaluates the condition against the element count of similar
20
+ # elements within the same label context.
21
+ #
22
+ # @param count [Integer] the total number of elements labeled by the
23
+ # same labeller within the same context instance
24
+ # @return [Boolean]
25
+ # @raise [Kaleidl::Error] if the condition type is unknown
26
+ def evaluate(count)
27
+ case type
28
+ when "element_count_gt"
29
+ count > threshold
30
+ when "element_count_eq"
31
+ count == threshold
32
+ when "element_count_lt"
33
+ count < threshold
34
+ else
35
+ raise Kaleidl::Error, "unknown label context condition type: #{type.inspect}"
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ # MN 112 "Label style" — a full set of label models sufficient to
5
+ # implement a coherent labeling system for a publisher.
6
+ #
7
+ # The collection attributes hold references to the instances bound to
8
+ # this style, exactly as written in the `.lml` profile: labellers are
9
+ # referenced by id (`"id:clause_labeller"`), the other collections by
10
+ # instance name (`"ClauseLabelTemplate"`).
11
+ class LabelStyle < Lutaml::Model::Serializable
12
+ attribute :name, :string
13
+ attribute :definition, :string
14
+ attribute :labellers, :string, collection: true
15
+ attribute :sequences, :string, collection: true
16
+ attribute :templates, :string, collection: true
17
+ attribute :contexts, :string, collection: true
18
+
19
+ hsh do
20
+ map "name", to: :name
21
+ map "definition", to: :definition
22
+ map "labellers", to: :labellers
23
+ map "sequences", to: :sequences
24
+ map "templates", to: :templates
25
+ map "contexts", to: :contexts
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ # MN 112 "Label template" — defines how string labels are constructed
5
+ # from labeller values and fixed text.
6
+ #
7
+ # Supported placeholders: `%n` (labeller value), `%pn` (parent labeller
8
+ # value), `%parent` (rendered global label of the parent context
9
+ # element), `%self` (rendered local label of the element itself).
10
+ class LabelTemplate < Lutaml::Model::Serializable
11
+ attribute :pattern, :string
12
+ attribute :direction, :string, default: "ltr"
13
+ attribute :condition, Kaleidl::LabelContextCondition
14
+ attribute :definition, :string
15
+
16
+ hsh do
17
+ map "pattern", to: :pattern
18
+ map "direction", to: :direction
19
+ map "condition", to: :condition
20
+ map "definition", to: :definition
21
+ end
22
+
23
+ def conditional?
24
+ !condition.nil?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ # MN 112 "Labeller" — assigns labels to content elements based on their
5
+ # position in the document structure and labeling context, and generates
6
+ # a human-readable label string from a label object.
7
+ #
8
+ # Labellers form the labeller tree: one of the three model trees of
9
+ # MN 112, linked via the `parent` attribute.
10
+ #
11
+ # The `sequence`, `context` and `parent` attributes hold references to
12
+ # profile instances (by instance name) as written in the `.lml`
13
+ # profile; the runtime resolver (Kaleidl::Runtime::Resolver) wires them to
14
+ # the referenced models. The `template` and `global_template`
15
+ # collections are hydrated into nested Kaleidl::LabelTemplate objects by
16
+ # the profile loader.
17
+ class Labeller < Lutaml::Model::Serializable
18
+ attribute :id, :string
19
+ attribute :sequence, :string
20
+ attribute :context, :string
21
+ attribute :type, :string
22
+ attribute :parent, :string
23
+ attribute :template, Kaleidl::LabelTemplate, collection: true
24
+ attribute :global_template, Kaleidl::LabelTemplate, collection: true
25
+ attribute :definition, :string
26
+
27
+ hsh do
28
+ map "id", to: :id
29
+ map "sequence", to: :sequence
30
+ map "context", to: :context
31
+ map "type", to: :type
32
+ map "parent", to: :parent
33
+ map "template", to: :template
34
+ map "global_template", to: :global_template
35
+ map "definition", to: :definition
36
+ end
37
+
38
+ # First template of the local template collection; used as the
39
+ # fallback when no template condition matches.
40
+ def first_template
41
+ Array(template).first
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ module Lml
5
+ # The parse result of a `.lml` document: the ordered list of its
6
+ # instance declarations.
7
+ class Document
8
+ attr_reader :instances
9
+
10
+ def initialize(instances: [])
11
+ @instances = instances
12
+ end
13
+
14
+ def add_instance(instance)
15
+ @instances << instance
16
+ instance
17
+ end
18
+
19
+ # The first instance with the given name, if any.
20
+ def instance_named(name)
21
+ @instances.find { |i| i.name == name }
22
+ end
23
+
24
+ # All instances whose base class name matches `base`.
25
+ def instances_of_base(base)
26
+ @instances.select { |i| i.base == base }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ module Lml
5
+ # A parsed `instance` declaration from a `.lml` document: its name,
6
+ # its base class (after `<`), and its attribute map. Attribute values
7
+ # are plain Ruby data: String, Integer, true/false, nil, Array, or a
8
+ # nested Hash (from `{ ... }` blocks). `references` lists the value
9
+ # positions that were references (bare identifiers or `ref:(Name)`
10
+ # forms) so the loader can verify they resolve.
11
+ class Instance
12
+ attr_reader :name, :base, :attributes, :references
13
+
14
+ def initialize(name:, base: nil, attributes: {}, references: [])
15
+ @name = name
16
+ @base = base
17
+ @attributes = attributes
18
+ @references = references
19
+ end
20
+ end
21
+ end
22
+ end