ladl 0.1.0
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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.adoc +280 -0
- data/lib/ladl/adapters/metanorma_document.rb +213 -0
- data/lib/ladl/adapters.rb +7 -0
- data/lib/ladl/character_sequence.rb +35 -0
- data/lib/ladl/content_element.rb +41 -0
- data/lib/ladl/errors.rb +16 -0
- data/lib/ladl/label.rb +30 -0
- data/lib/ladl/label_context.rb +24 -0
- data/lib/ladl/label_context_condition.rb +39 -0
- data/lib/ladl/label_style.rb +28 -0
- data/lib/ladl/label_template.rb +27 -0
- data/lib/ladl/labeller.rb +44 -0
- data/lib/ladl/lml/document.rb +30 -0
- data/lib/ladl/lml/instance.rb +19 -0
- data/lib/ladl/lml/parser.rb +345 -0
- data/lib/ladl/lml.rb +11 -0
- data/lib/ladl/numeric_sequence.rb +43 -0
- data/lib/ladl/profile.rb +66 -0
- data/lib/ladl/profile_loader.rb +132 -0
- data/lib/ladl/profiles/ietf_label_style.lml +293 -0
- data/lib/ladl/profiles/iso_iec_label_style.lml +647 -0
- data/lib/ladl/runtime/engine.rb +282 -0
- data/lib/ladl/runtime/renderer.rb +49 -0
- data/lib/ladl/runtime/resolver.rb +175 -0
- data/lib/ladl/runtime/result.rb +61 -0
- data/lib/ladl/runtime.rb +10 -0
- data/lib/ladl/sequences/formatters.rb +104 -0
- data/lib/ladl/sequences/registry.rb +61 -0
- data/lib/ladl/sequences.rb +100 -0
- data/lib/ladl/version.rb +5 -0
- data/lib/ladl.rb +78 -0
- metadata +123 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 268ac6f82b374387c7667088da1b9ea980a33cc16e35d579eabb8f3a8e783789
|
|
4
|
+
data.tar.gz: 37dbf51ade3f190309245c8f431c1fd9d9e4d739a417767f654ae50037f7c0ed
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a9b38987a0405bd597b560db847298aa5728fe55198da8addc4bfce0d205aa4ef0f4a7af41bae1f56818e1113db39f3094b239161f2a34558d34f6ee30b1af11
|
|
7
|
+
data.tar.gz: 788eab5c1e9c9e94d1cd5a8ff8905fb61963227c74d7078d2475a39b37b522a407598f7a4c13dbd30cf9b7d138a2089a50d3b2d926bf82d138e188bffd385bcf
|
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,280 @@
|
|
|
1
|
+
= ladl: 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
|
+
bundled profiles are `lib/ladl/profiles/iso_iec_label_style.lml` (the
|
|
13
|
+
MN 112-A worked profile instance encoding the ISO/IEC Directives Part 2
|
|
14
|
+
labeling conventions) and `lib/ladl/profiles/ietf_label_style.lml` (the
|
|
15
|
+
Internet-Draft/RFC conventions, verified against the xml2rfc text
|
|
16
|
+
writer and idnits).
|
|
17
|
+
|
|
18
|
+
== Installation
|
|
19
|
+
|
|
20
|
+
gem install ladl
|
|
21
|
+
|
|
22
|
+
== Usage
|
|
23
|
+
|
|
24
|
+
[source,ruby]
|
|
25
|
+
----
|
|
26
|
+
require "ladl"
|
|
27
|
+
|
|
28
|
+
profile = Ladl.iso_iec_profile
|
|
29
|
+
tree = Ladl::ContentElement.new(id: "doc", type: "document",
|
|
30
|
+
children: [
|
|
31
|
+
Ladl::ContentElement.new(id: "c1", type: "clause"),
|
|
32
|
+
Ladl::ContentElement.new(id: "ax1", type: "annex", children: [
|
|
33
|
+
Ladl::ContentElement.new(id: "ax1s1", type: "clause"),
|
|
34
|
+
]),
|
|
35
|
+
])
|
|
36
|
+
|
|
37
|
+
result = Ladl::Runtime::Engine.new(profile).label(tree)
|
|
38
|
+
result.local_for(tree.children.first) # => "Clause 1"
|
|
39
|
+
result.global_for(tree.children.first) # => "Clause 1"
|
|
40
|
+
result.local_for(tree.children.last.children.first) # => "A.1"
|
|
41
|
+
----
|
|
42
|
+
|
|
43
|
+
Load a custom profile written in the MN 112 `.lml` instance syntax:
|
|
44
|
+
|
|
45
|
+
[source,ruby]
|
|
46
|
+
----
|
|
47
|
+
profile = Ladl.load_profile("my_flavor.lml")
|
|
48
|
+
----
|
|
49
|
+
|
|
50
|
+
Adapt a metanorma-document presentation tree:
|
|
51
|
+
|
|
52
|
+
[source,ruby]
|
|
53
|
+
----
|
|
54
|
+
content = Ladl::Adapters::MetanormaDocument.wrap(mn_document_root)
|
|
55
|
+
result = Ladl::Runtime::Engine.new(profile).label(content)
|
|
56
|
+
----
|
|
57
|
+
|
|
58
|
+
== Architecture
|
|
59
|
+
|
|
60
|
+
LADL models labeling over three object trees (MN 112 "Architecture"):
|
|
61
|
+
|
|
62
|
+
[cols="1,2,3"]
|
|
63
|
+
|===
|
|
64
|
+
| Tree | Model | Role
|
|
65
|
+
|
|
66
|
+
| Content tree
|
|
67
|
+
| `Ladl::ContentElement`
|
|
68
|
+
| The elements being labeled. Provided by the host (a metanorma-document
|
|
69
|
+
presentation tree wrapped by the adapter, or any duck-typed tree).
|
|
70
|
+
|
|
71
|
+
| Context tree
|
|
72
|
+
| `Ladl::LabelContext`
|
|
73
|
+
| Label contexts that scope labeller counters (document, clause, annex,
|
|
74
|
+
list), linked by `parent`.
|
|
75
|
+
|
|
76
|
+
| Labeller tree
|
|
77
|
+
| `Ladl::Labeller`
|
|
78
|
+
| Hierarchical labellers bound to contexts, linked by `parent`; each
|
|
79
|
+
carries a sequence and local/global label templates.
|
|
80
|
+
|===
|
|
81
|
+
|
|
82
|
+
.Profile loading
|
|
83
|
+
[cols="1,3"]
|
|
84
|
+
|===
|
|
85
|
+
| `Ladl::Lml::Parser`
|
|
86
|
+
| Parses the `.lml` instance syntax (comments, attribute assignments,
|
|
87
|
+
lists, nested `{ }` maps, `definition` free-text blocks).
|
|
88
|
+
|
|
89
|
+
| `Ladl::ProfileLoader`
|
|
90
|
+
| Groups parsed instances by base class and hydrates them into the typed
|
|
91
|
+
profile models through lutaml-model mappings (`Model.from_hash`); no
|
|
92
|
+
hand-written serialization anywhere.
|
|
93
|
+
|
|
94
|
+
| `Ladl::Profile`
|
|
95
|
+
| The loaded style plus instance tables (labellers, contexts, templates,
|
|
96
|
+
sequences).
|
|
97
|
+
|===
|
|
98
|
+
|
|
99
|
+
.Runtime
|
|
100
|
+
[cols="1,3"]
|
|
101
|
+
|===
|
|
102
|
+
| `Ladl::Runtime::Resolver`
|
|
103
|
+
| Wires profile references into indexes: labellers by context instance
|
|
104
|
+
and element type, context instances by name and parent, sequence
|
|
105
|
+
resolution against the registry and profile-defined sequences, and
|
|
106
|
+
labeller-tree ancestry for `%pn`.
|
|
107
|
+
|
|
108
|
+
| `Ladl::Runtime::Engine`
|
|
109
|
+
| The labeling run: walks the content tree depth-first, instantiates
|
|
110
|
+
label context instances, resolves each element's labeller, increments
|
|
111
|
+
counters scoped by context instance and parent element, selects label
|
|
112
|
+
templates by condition, and renders patterns. The walk plans and counts
|
|
113
|
+
first, then renders, so conditional templates see final sibling counts.
|
|
114
|
+
|
|
115
|
+
| `Ladl::Runtime::Renderer`
|
|
116
|
+
| String interpolation of `%n`, `%pn`, `%parent` and `%self`.
|
|
117
|
+
|
|
118
|
+
| `Ladl::Runtime::Result`
|
|
119
|
+
| The output: a `Ladl::Label` per labeled element, lookup by element
|
|
120
|
+
identity, with local and global label strings, hierarchical paths,
|
|
121
|
+
sequence values and positions.
|
|
122
|
+
|
|
123
|
+
| `Ladl::Sequences`
|
|
124
|
+
| The default sequences MN 112 requires of a compliant runtime (decimal,
|
|
125
|
+
Roman, alphabetic, Cyrillic, Greek, Chinese, Japanese, Korean, Arabic,
|
|
126
|
+
Hebrew) plus the expansion rules and arithmetic for profile-defined
|
|
127
|
+
sequence instances.
|
|
128
|
+
|===
|
|
129
|
+
|
|
130
|
+
== MN 112 concept map
|
|
131
|
+
|
|
132
|
+
[cols="2,3"]
|
|
133
|
+
|===
|
|
134
|
+
| MN 112 concept | Class / module
|
|
135
|
+
|
|
136
|
+
| Label | `Ladl::Label`
|
|
137
|
+
| Labeller | `Ladl::Labeller`
|
|
138
|
+
| Sequence | `Ladl::NumericSequence`, `Ladl::CharacterSequence`
|
|
139
|
+
| Labeling format | `Ladl::Sequences::Formatters`
|
|
140
|
+
| Label context | `Ladl::LabelContext`
|
|
141
|
+
| Label context condition | `Ladl::LabelContextCondition`
|
|
142
|
+
| Label template | `Ladl::LabelTemplate`
|
|
143
|
+
| Label style | `Ladl::LabelStyle` (loaded into `Ladl::Profile`)
|
|
144
|
+
| ContentElement | `Ladl::ContentElement`
|
|
145
|
+
| String interpolation | `Ladl::Runtime::Renderer`
|
|
146
|
+
| Runtime requirements (MN 112 "Runtime") | `Ladl::Runtime::Engine`
|
|
147
|
+
| Default sequences | `Ladl::Sequences::Registry`
|
|
148
|
+
| Value for position | `Ladl::Sequences.value_for`,
|
|
149
|
+
`Sequence#value_for_position`
|
|
150
|
+
| Algorithm to resolve the label | `Ladl::Runtime::Engine`
|
|
151
|
+
| Instance model examples (MN 112-A) | `lib/ladl/profiles/iso_iec_label_style.lml`
|
|
152
|
+
|===
|
|
153
|
+
|
|
154
|
+
== Engine behavior notes
|
|
155
|
+
|
|
156
|
+
* *Counter scope*. A labeller's counters live in the context instance
|
|
157
|
+
its `context` binds it to: notes bound to `ClauseContext` count per
|
|
158
|
+
clause (across subclauses); annex figures bound to `AnnexContext` count
|
|
159
|
+
per annex; list items bound to `ListContext` count per list.
|
|
160
|
+
|
|
161
|
+
* *Parent scoping*. A labeller with a `parent` labeller additionally
|
|
162
|
+
scopes its counter per enclosing parent element: subfigure letters
|
|
163
|
+
restart per figure; nested ordered-list items restart per containing
|
|
164
|
+
item.
|
|
165
|
+
|
|
166
|
+
* *`%pn`*. The parent value is the hierarchical path of the nearest
|
|
167
|
+
enclosing element labeled by the current labeller's ancestor chain
|
|
168
|
+
(subclause 1.2 for 1.2.1; the annex for Figure A.1), falling back to the
|
|
169
|
+
nearest enclosing element of the parent labeller's type (an enclosing
|
|
170
|
+
figure labeled by the annex figure labeller for a subfigure).
|
|
171
|
+
|
|
172
|
+
* *`%parent` / `%self`*. The rendered global label of the context
|
|
173
|
+
element anchoring the labeller's counter frame, and the element's own
|
|
174
|
+
local label (used inside global templates).
|
|
175
|
+
|
|
176
|
+
* *Conditional templates*. The first template whose condition evaluates
|
|
177
|
+
to true wins; the first unconditional template is the fallback (MN 112
|
|
178
|
+
algorithm).
|
|
179
|
+
|
|
180
|
+
* *Direction*. `char_direction`/`direction` are carried on models;
|
|
181
|
+
rendering emits logical order and relies on Unicode bidi for display.
|
|
182
|
+
|
|
183
|
+
== Spec ambiguities and deviations
|
|
184
|
+
|
|
185
|
+
MN 112 is a draft; the following points are ambiguous in the text and
|
|
186
|
+
were resolved as documented. Each is testable in the suite and is a
|
|
187
|
+
candidate for a spec ruling.
|
|
188
|
+
|
|
189
|
+
1. *`ArabicNumericSequence`*. RESOLVED 2026-11-26 by engine evidence:
|
|
190
|
+
ISO/IEC deliverables label clauses with Western digits, so the bundled
|
|
191
|
+
ISO/IEC and IETF profiles now bind clause numbering to
|
|
192
|
+
`DecimalNumericSequence`, and the registry restores
|
|
193
|
+
`ArabicNumericSequence` to its MN 112 definition (Arabic-script
|
|
194
|
+
numerals ١, ٢, ٣). `ArabicIndicNumericSequence` (the RTL example's
|
|
195
|
+
name in MN 112) remains as the explicit RTL-flavored spelling.
|
|
196
|
+
|
|
197
|
+
2. *`%parent` in the Japanese example*. MN 112's Japanese clause
|
|
198
|
+
example (`%parentの%n`, expected 二の一) uses `%parent` where only
|
|
199
|
+
`%pn` semantics (parent sequence value) produce the shown output; by
|
|
200
|
+
the `%parent` definition (rendered global label) the output is
|
|
201
|
+
第二章の一. The engine follows the definition; the example is flagged.
|
|
202
|
+
|
|
203
|
+
3. *SimpleLabelStyle template*. MN 112's SimpleLabelStyle example wires
|
|
204
|
+
the subclause template as `%parent.%n` but states the desired output
|
|
205
|
+
"1.1", which requires `%pn.%n`. The engine follows the stated outputs.
|
|
206
|
+
|
|
207
|
+
4. *`%pn` beyond one level*. RESOLVED by the spec's own example: the
|
|
208
|
+
MN 112 subfigure template (`Figure %pn %n)` → "Figure A.1 a)") is only
|
|
209
|
+
satisfiable when `%pn` carries the two-level parent path (A.1), so the
|
|
210
|
+
engine computes `%pn` as the hierarchical path (parent chain values
|
|
211
|
+
joined with `.`).
|
|
212
|
+
|
|
213
|
+
5. *Instantiation of annex clauses*. The profile distinguishes annex
|
|
214
|
+
clauses only by their enclosing annex; no context is name-matched by
|
|
215
|
+
the clause type inside annexes. The engine instantiates the child
|
|
216
|
+
context of the element's labeller context (`AnnexClauseContext` for
|
|
217
|
+
annex clauses) and falls back to name matching.
|
|
218
|
+
|
|
219
|
+
6. *MN 112-A profile gaps*. RESOLVED 2026-11-26 by engine evidence: the
|
|
220
|
+
current ISO engine (isodoc `annex_names` recursion) labels annex
|
|
221
|
+
subclauses hierarchically (A.1.1, A.1.1.1) and numbers annex
|
|
222
|
+
notes/examples, so the profile now carries `AnnexSubclauseLabeller`,
|
|
223
|
+
`AnnexNoteLabeller` and `AnnexExampleLabeller` (bound to
|
|
224
|
+
`AnnexClauseContext`). The suite covers A.1.1 and annex
|
|
225
|
+
notes/examples end to end.
|
|
226
|
+
|
|
227
|
+
7. *`cycle_length`*. RESOLVED by engine evidence: the ISO annex
|
|
228
|
+
expansion (Annex Z → Annex AA) that isodoc implements is bijective
|
|
229
|
+
(spreadsheet-style) decomposition, which is what the engine applies
|
|
230
|
+
when `cycle_length` is present.
|
|
231
|
+
|
|
232
|
+
8. *`append_suffix` expansion*. The expansion result after the
|
|
233
|
+
character set is exhausted is underdefined; the engine appends cycling
|
|
234
|
+
suffix characters symmetrically to `prepend_prefix`.
|
|
235
|
+
|
|
236
|
+
9. *NumericSequence `format` attribute*. RESOLVED: profile-defined
|
|
237
|
+
sequence instances are supported by the profile syntax — an `.lml`
|
|
238
|
+
profile can declare `instance X < NumericSequence { format = "decimal"
|
|
239
|
+
... }` (or derive from a registry sequence via `base_sequence`), and
|
|
240
|
+
the bundled registry defaults (decimal, Roman, alphabetic, ...) are
|
|
241
|
+
`NumericSequence` instances selected by the `format` attribute.
|
|
242
|
+
|
|
243
|
+
10. *`char_direction` vs `direction`*. MN 112 declares both on the
|
|
244
|
+
sequence models; the engine carries both and treats `direction` as
|
|
245
|
+
taking precedence.
|
|
246
|
+
|
|
247
|
+
11. *Profile syntax vs lutaml-lml*. MN 112 profiles use unquoted
|
|
248
|
+
instance names with `<` inheritance (`instance Foo < Bar`); the
|
|
249
|
+
released LutaML grammar (lutaml-lml, RS 3001) spells inheritance as a
|
|
250
|
+
body statement (`isa Bar;`) with quoted instance names. The parser now
|
|
251
|
+
ACCEPTS BOTH SPELLINGS (2026-11-26), so a profile written in either
|
|
252
|
+
syntax loads and labels identically; the bundled profiles keep the MN
|
|
253
|
+
112 spelling. Full grammar unification — extending the RS 3001 grammar
|
|
254
|
+
with `<` inheritance, or amending MN 112 to `isa` — remains a
|
|
255
|
+
language-level decision on the lutaml-lml side.
|
|
256
|
+
|
|
257
|
+
12. *Counter restart for notes in subclauses*. RESOLVED 2026-11-26 by
|
|
258
|
+
engine evidence: isodoc's `note_anchor_names`/`example_anchor_names`
|
|
259
|
+
restart the counter for every clause and subclause (`Counter.new` per
|
|
260
|
+
section; `increment_label` returns an empty label for a singleton), so
|
|
261
|
+
ISO/IEC note numbering IS per section. The profile now carries
|
|
262
|
+
`SubclauseNoteLabeller`/`SubclauseExampleLabeller` bound to
|
|
263
|
+
`SubclauseContext`: lone notes render "NOTE", notes continue
|
|
264
|
+
"NOTE 1, NOTE 2" within one subclause and restart in the next, and
|
|
265
|
+
global refs anchor at the enclosing section ("1.2, NOTE").
|
|
266
|
+
|
|
267
|
+
== Development
|
|
268
|
+
|
|
269
|
+
bundle install
|
|
270
|
+
bundle exec rspec
|
|
271
|
+
|
|
272
|
+
Run the full test suite with `bundle exec rspec`. The suite covers the
|
|
273
|
+
`.lml` parser, the profile loader against the bundled ISO/IEC profile,
|
|
274
|
+
every default sequence, the core models, the resolver, the renderer,
|
|
275
|
+
the engine algorithm (counter scoping, `%pn` resolution, conditional
|
|
276
|
+
templates) and the metanorma-document adapter.
|
|
277
|
+
|
|
278
|
+
== License
|
|
279
|
+
|
|
280
|
+
The gem is available as open source under the terms of the MIT license.
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ladl
|
|
4
|
+
module Adapters
|
|
5
|
+
# Thin duck-typed adapter mapping metanorma-document presentation
|
|
6
|
+
# models onto the LADL content tree (Ladl::ContentElement).
|
|
7
|
+
#
|
|
8
|
+
# The gem does not depend on metanorma-document: elements are
|
|
9
|
+
# recognized by their class name suffix (e.g. "NoteBlock",
|
|
10
|
+
# "HierarchicalSection"), and the adapter accepts any object tree
|
|
11
|
+
# that carries the attributes used here. Flavor models that evolve
|
|
12
|
+
# (e.g. a dedicated Annex section class) are picked up by the name
|
|
13
|
+
# patterns below or by an explicit `annex?`/`ladl_type` method on
|
|
14
|
+
# the element.
|
|
15
|
+
#
|
|
16
|
+
# Element types emitted (matching the ISO/IEC label style profile):
|
|
17
|
+
# "document", "clause", "subclause", "annex", "figure", "table",
|
|
18
|
+
# "note", "example", "formula", "list", "list_item_unordered",
|
|
19
|
+
# "list_item_ordered_level1".."list_item_ordered_level4". Unlabeled
|
|
20
|
+
# kinds (paragraphs, titles, sourcecode blocks, ...) map to their
|
|
21
|
+
# underscored class name and are traversed but never labeled, since
|
|
22
|
+
# no labeller in a profile binds their type.
|
|
23
|
+
class MetanormaDocument
|
|
24
|
+
BLOCK_TYPES = {
|
|
25
|
+
"NoteBlock" => "note",
|
|
26
|
+
"ExampleBlock" => "example",
|
|
27
|
+
"FormulaBlock" => "formula",
|
|
28
|
+
"FigureBlock" => "figure",
|
|
29
|
+
"TableBlock" => "table",
|
|
30
|
+
}.freeze
|
|
31
|
+
|
|
32
|
+
SECTION_CLASSES = %w[HierarchicalSection BasicSection ContentSection
|
|
33
|
+
ReferencesSection].freeze
|
|
34
|
+
LIST_CLASSES = %w[OrderedList UnorderedList].freeze
|
|
35
|
+
LIST_ITEM_CLASSES = %w[ListItem].freeze
|
|
36
|
+
LIST_ITEM_CHILD_METHODS = %i[ordered_lists unordered_lists figure
|
|
37
|
+
formula note example table sourcecode].freeze
|
|
38
|
+
FIGURE_CHILD_METHODS = %i[figure].freeze
|
|
39
|
+
ROOT_CHILD_METHODS = %i[sections annexes preface].freeze
|
|
40
|
+
|
|
41
|
+
class << self
|
|
42
|
+
# Builds a Ladl::ContentElement tree from a metanorma-document
|
|
43
|
+
# presentation root.
|
|
44
|
+
def wrap(root)
|
|
45
|
+
new.build(root)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def build(element, section_depth: 0, list_depth: 0, annex: false,
|
|
50
|
+
ordered: false, figure_child: false)
|
|
51
|
+
type = element_type(element, section_depth, annex, ordered, figure_child)
|
|
52
|
+
content = Ladl::ContentElement.new(id: element_id(element), type: type)
|
|
53
|
+
child_params = next_params(type, element, section_depth, list_depth, annex)
|
|
54
|
+
children_of(element).each do |child|
|
|
55
|
+
content.add_child(build(child, **child_params))
|
|
56
|
+
end
|
|
57
|
+
content
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def element_type(element, section_depth, annex, ordered, figure_child)
|
|
63
|
+
explicit = element.respond_to?(:ladl_type) ? element.ladl_type : nil
|
|
64
|
+
return explicit if explicit
|
|
65
|
+
|
|
66
|
+
name = class_name(element)
|
|
67
|
+
case name
|
|
68
|
+
when "Root", "DocumentRoot"
|
|
69
|
+
"document"
|
|
70
|
+
when *LIST_CLASSES
|
|
71
|
+
"list"
|
|
72
|
+
when *LIST_ITEM_CLASSES
|
|
73
|
+
list_item_type(ordered)
|
|
74
|
+
when *SECTION_CLASSES
|
|
75
|
+
section_type(section_depth)
|
|
76
|
+
when "FigureBlock"
|
|
77
|
+
figure_child ? "subfigure" : "figure"
|
|
78
|
+
when *BLOCK_TYPES.keys
|
|
79
|
+
BLOCK_TYPES.fetch(name)
|
|
80
|
+
else
|
|
81
|
+
if annex_section?(element)
|
|
82
|
+
"annex"
|
|
83
|
+
else
|
|
84
|
+
underscore(name)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# depth 0 within the current scope: a main-document top-level
|
|
90
|
+
# section is a "clause", deeper sections are "subclauses"; inside
|
|
91
|
+
# an annex, the depth restarts, so annex clauses are "clause"
|
|
92
|
+
# again (the AnnexClauseLabeller picks them up through the annex
|
|
93
|
+
# context).
|
|
94
|
+
def section_type(depth)
|
|
95
|
+
depth.zero? ? "clause" : "subclause"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def list_item_type(ordered)
|
|
99
|
+
if ordered.to_i.positive?
|
|
100
|
+
"list_item_ordered_level#{level_of(ordered)}"
|
|
101
|
+
else
|
|
102
|
+
"list_item_unordered"
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# The ordered-list nesting depth is passed down as `ordered` (an
|
|
107
|
+
# Integer, 0 for unordered); levels wrap every four levels,
|
|
108
|
+
# repeating the first level's scheme (MN 112-A ordered list items:
|
|
109
|
+
# a) 1) i) a) ...).
|
|
110
|
+
def level_of(ordered)
|
|
111
|
+
((ordered - 1) % 4) + 1
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def annex_section?(element)
|
|
115
|
+
return true if element.respond_to?(:annex?) && element.annex?
|
|
116
|
+
|
|
117
|
+
class_name(element) =~ /Annex/ ? true : false
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def class_name(element)
|
|
121
|
+
element.class.name&.split("::")&.last.to_s
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def element_id(element)
|
|
125
|
+
if element.respond_to?(:id) && element.id
|
|
126
|
+
element.id
|
|
127
|
+
elsif element.respond_to?(:anchor) && element.anchor
|
|
128
|
+
element.anchor
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def underscore(name)
|
|
133
|
+
name.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
|
134
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
|
135
|
+
.downcase
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Returns the params hash for the recursive child build.
|
|
139
|
+
def next_params(type, element, section_depth, list_depth, annex)
|
|
140
|
+
case type
|
|
141
|
+
when "document"
|
|
142
|
+
{ section_depth: 0, list_depth: 0, annex: false, ordered: false,
|
|
143
|
+
figure_child: false }
|
|
144
|
+
when "clause", "subclause"
|
|
145
|
+
{ section_depth: section_depth + 1, list_depth: 0, annex: annex,
|
|
146
|
+
ordered: false, figure_child: false }
|
|
147
|
+
when "annex"
|
|
148
|
+
{ section_depth: 0, list_depth: 0, annex: true, ordered: false,
|
|
149
|
+
figure_child: false }
|
|
150
|
+
when "list"
|
|
151
|
+
{ section_depth: section_depth, list_depth: list_depth + 1,
|
|
152
|
+
annex: annex,
|
|
153
|
+
ordered: class_name(element) == "OrderedList" ? list_depth + 1 : 0,
|
|
154
|
+
figure_child: false }
|
|
155
|
+
when "figure"
|
|
156
|
+
{ section_depth: section_depth, list_depth: list_depth,
|
|
157
|
+
annex: annex, ordered: false, figure_child: true }
|
|
158
|
+
else
|
|
159
|
+
{ section_depth: section_depth, list_depth: list_depth,
|
|
160
|
+
annex: annex, ordered: false, figure_child: false }
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Child accessor per element kind; metanorma-document keeps
|
|
165
|
+
# children in kind-specific collections, visited in document
|
|
166
|
+
# order: blocks (with their nested notes), section notes, then
|
|
167
|
+
# subsections.
|
|
168
|
+
def children_of(element)
|
|
169
|
+
return Array(element.children) if element.respond_to?(:children)
|
|
170
|
+
|
|
171
|
+
name = class_name(element)
|
|
172
|
+
if SECTION_CLASSES.include?(name) || name =~ /Annex/
|
|
173
|
+
section_children(element)
|
|
174
|
+
elsif LIST_CLASSES.include?(name)
|
|
175
|
+
Array(element.listitem)
|
|
176
|
+
elsif LIST_ITEM_CLASSES.include?(name)
|
|
177
|
+
via_methods(element, LIST_ITEM_CHILD_METHODS)
|
|
178
|
+
elsif name == "FigureBlock"
|
|
179
|
+
via_methods(element, FIGURE_CHILD_METHODS)
|
|
180
|
+
elsif %w[Root DocumentRoot Document].include?(name)
|
|
181
|
+
via_methods(element, ROOT_CHILD_METHODS)
|
|
182
|
+
else
|
|
183
|
+
via_methods(element, %i[notes])
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def section_children(element)
|
|
188
|
+
children = []
|
|
189
|
+
Array(element.blocks).each do |block|
|
|
190
|
+
children << block
|
|
191
|
+
Array(block.notes).each { |n| children << n } if block.respond_to?(:notes)
|
|
192
|
+
end
|
|
193
|
+
if element.respond_to?(:notes)
|
|
194
|
+
Array(element.notes).each { |n| children << n }
|
|
195
|
+
end
|
|
196
|
+
if element.respond_to?(:subsections)
|
|
197
|
+
Array(element.subsections).each { |s| children << s }
|
|
198
|
+
end
|
|
199
|
+
children
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def via_methods(element, methods)
|
|
203
|
+
children = []
|
|
204
|
+
methods.each do |method|
|
|
205
|
+
if element.respond_to?(method)
|
|
206
|
+
Array(element.public_send(method)).each { |c| children << c }
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
children
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ladl
|
|
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
|
+
# Ladl::Sequences.value_for.
|
|
31
|
+
def value_for_position(position, lookup: nil)
|
|
32
|
+
Ladl::Sequences.value_for(self, position, lookup: lookup)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ladl
|
|
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
|
+
class ContentElement < Lutaml::Model::Serializable
|
|
11
|
+
attribute :id, :string
|
|
12
|
+
attribute :type, :string
|
|
13
|
+
attribute :children, Ladl::ContentElement, collection: true
|
|
14
|
+
attribute :definition, :string
|
|
15
|
+
|
|
16
|
+
hsh do
|
|
17
|
+
map "id", to: :id
|
|
18
|
+
map "type", to: :type
|
|
19
|
+
map "children", to: :children
|
|
20
|
+
map "definition", to: :definition
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def initialize(*args, **kwargs, &block)
|
|
24
|
+
super
|
|
25
|
+
@children ||= []
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def add_child(child)
|
|
29
|
+
@children << child
|
|
30
|
+
child
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Depth-first enumeration of the subtree rooted at this element.
|
|
34
|
+
def each(&block)
|
|
35
|
+
return enum_for(:each) unless block
|
|
36
|
+
|
|
37
|
+
yield self
|
|
38
|
+
Array(children).each { |c| c.each(&block) }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
data/lib/ladl/errors.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ladl
|
|
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
|
data/lib/ladl/label.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ladl
|
|
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 Ladl
|
|
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
|