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.
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ # Loads LADL profiles written in the LutaML `.lml` instance syntax
5
+ # (MN 112 worked profile format) into typed Ladl profile models.
6
+ #
7
+ # Every instance is hydrated through the lutaml-model framework
8
+ # (Model.from_hash) against the hash mappings declared on the model
9
+ # classes; this class only groups instances by their base class and
10
+ # substitutes labeller template references with the referenced
11
+ # template instances.
12
+ class ProfileLoader
13
+ CLASS_FOR_BASE = {
14
+ "LabelStyle" => -> { Ladl::LabelStyle },
15
+ "LabelContext" => -> { Ladl::LabelContext },
16
+ "Labeller" => -> { Ladl::Labeller },
17
+ "LabelTemplate" => -> { Ladl::LabelTemplate },
18
+ "NumericSequence" => -> { Ladl::NumericSequence },
19
+ "CharacterSequence" => -> { Ladl::CharacterSequence },
20
+ "ContentElement" => -> { Ladl::ContentElement },
21
+ "Label" => -> { Ladl::Label },
22
+ }.freeze
23
+
24
+ class << self
25
+ def load_lml(text)
26
+ new.load_lml(text)
27
+ end
28
+
29
+ def load_file(path)
30
+ new.load_file(path)
31
+ end
32
+ end
33
+
34
+ def load_file(path)
35
+ load_lml(File.read(path, encoding: "UTF-8"))
36
+ end
37
+
38
+ def load_lml(text)
39
+ document = Lml::Parser.parse(text)
40
+ @index = {}
41
+ document.instances.each do |instance|
42
+ raise Ladl::ParseError,
43
+ "duplicate instance name in profile: #{instance.name}" if @index.key?(instance.name)
44
+
45
+ @index[instance.name] = instance
46
+ end
47
+ build_profile(document)
48
+ end
49
+
50
+ private
51
+
52
+ def build_profile(document)
53
+ built = {}
54
+ @index.each do |name, instance|
55
+ built[name] = hydrate(name, instance)
56
+ end
57
+ style = built.values.find { |m| m.is_a?(Ladl::LabelStyle) }
58
+ raise Ladl::ParseError, "profile defines no LabelStyle instance" unless style
59
+
60
+ Profile.new(
61
+ style: style,
62
+ labellers: select_models(built, Ladl::Labeller),
63
+ contexts: select_models(built, Ladl::LabelContext),
64
+ templates: select_models(built, Ladl::LabelTemplate),
65
+ sequences: select_models(built, Ladl::NumericSequence, Ladl::CharacterSequence),
66
+ elements: select_models(built, Ladl::ContentElement),
67
+ labels: select_models(built, Ladl::Label),
68
+ )
69
+ end
70
+
71
+ def select_models(built, *types)
72
+ built.select { |_, model| types.any? { |t| model.is_a?(t) } }
73
+ end
74
+
75
+ def hydrate(name, instance)
76
+ klass = class_for_base(name, instance)
77
+ attrs = prepared_attributes(name, instance)
78
+ klass.from_hash(attrs)
79
+ end
80
+
81
+ def class_for_base(name, instance)
82
+ unless instance.base
83
+ raise Ladl::ParseError,
84
+ "instance #{name.inspect} declares no base class"
85
+ end
86
+ unless (klass = CLASS_FOR_BASE[instance.base]&.call)
87
+ raise Ladl::ParseError,
88
+ "instance #{name.inspect} extends unknown base class " \
89
+ "#{instance.base.inspect}"
90
+ end
91
+ klass
92
+ end
93
+
94
+ # Labeller template references are substituted with the referenced
95
+ # template instances' attribute hashes so that from_hash hydrates
96
+ # nested Ladl::LabelTemplate objects (MN 112 labeller/template
97
+ # references are by instance name).
98
+ def prepared_attributes(_name, instance)
99
+ attrs = instance.attributes.dup
100
+ substitute_template_ref(attrs, "template")
101
+ substitute_template_ref(attrs, "global_template")
102
+ attrs
103
+ end
104
+
105
+ def substitute_template_ref(attrs, key)
106
+ return unless attrs.key?(key)
107
+
108
+ value = attrs[key]
109
+ attrs[key] = resolve_template_list(value)
110
+ end
111
+
112
+ def resolve_template_list(value)
113
+ refs = value.is_a?(Array) ? value : [value]
114
+ refs.map do |ref|
115
+ case ref
116
+ when String
117
+ template_instance = @index[ref]
118
+ unless template_instance
119
+ raise Ladl::ResolutionError,
120
+ "labeller references undefined template instance #{ref.inspect}"
121
+ end
122
+ template_instance.attributes.dup
123
+ when Hash
124
+ ref.dup
125
+ else
126
+ raise Ladl::ParseError,
127
+ "invalid template reference: #{ref.inspect}"
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,293 @@
1
+ // IETF Label Style Definition in LutaML
2
+ //
3
+ // Encodes the Internet-Draft / RFC labeling conventions verified against
4
+ // the reference toolchain (ietf-tools/xml2rfc 3.35.0 text writer and
5
+ // ietf-tools/idnits v3) for metanorma-ietf#355:
6
+ //
7
+ // * Sections: "1. Introduction", "1.1. Conventions" — arabic numbering
8
+ // with a trailing dot per component (xml2rfc render_section:
9
+ // num.title() + '.'); cross-references cite "Section 2" (text.py:4789).
10
+ // * Appendices: sections under <back> are first-class — top level
11
+ // "Appendix A. Title" (text.py:3343 prefixes 'Appendix '), deeper
12
+ // levels "A.1." without the prefix in headings, "Appendix A.1" in
13
+ // cross-references; idnits HEADING_NUMBER recognizes the shape.
14
+ // * Captions: "Table 1" / "Figure 1" label part (xml2rfc joins the
15
+ // caption name with ': ' — text.py:4275/1750).
16
+ // * Notes: RFC XML v3 has no numbered-note machinery; notes are bare
17
+ // and unnumbered ("Note"), with a counter only when repeated
18
+ // (metanorma-ietf termnote_anchor_names).
19
+ //
20
+ // Lists carry no labellers: xml2rfc list markers are a rendering concern
21
+ // of the <ol>/<ul> types, not numbering labels.
22
+
23
+ instance IETFLabelStyle < LabelStyle {
24
+ name = "IETF Label Style"
25
+ definition {
26
+ The IETF label style originates from and implements the requirements of:
27
+ * the RFC XML v3 vocabulary (RFC 7991) as rendered by xml2rfc
28
+ * the idnits submission checks.
29
+
30
+ This style defines the labeling scheme used by Internet-Drafts and RFCs.
31
+ }
32
+
33
+ // References to all labellers defined in this style
34
+ labellers = [
35
+ "id:clause_labeller",
36
+ "id:subclause_labeller",
37
+ "id:annex_labeller",
38
+ "id:annex_clause_labeller",
39
+ "id:annex_subclause_labeller",
40
+ "id:figure_labeller",
41
+ "id:annex_figure_labeller",
42
+ "id:table_labeller",
43
+ "id:annex_table_labeller",
44
+ "id:note_labeller"
45
+ ]
46
+
47
+ // References to all sequences used in this style
48
+ sequences = [
49
+ DecimalNumericSequence,
50
+ AlphaUpperNumericSequence
51
+ ]
52
+
53
+ // References to all templates used in this style
54
+ templates = [
55
+ ClauseLabelTemplate,
56
+ ClauseGlobalLabelTemplate,
57
+ SubclauseLabelTemplate,
58
+ SubclauseGlobalLabelTemplate,
59
+ AnnexLabelTemplate,
60
+ AnnexGlobalLabelTemplate,
61
+ AnnexClauseLabelTemplate,
62
+ AnnexClauseGlobalLabelTemplate,
63
+ AnnexSubclauseLabelTemplate,
64
+ AnnexSubclauseGlobalLabelTemplate,
65
+ FigureLabelTemplate,
66
+ AnnexFigureLabelTemplate,
67
+ TableLabelTemplate,
68
+ AnnexTableLabelTemplate,
69
+ NoteLabelTemplate
70
+ ]
71
+
72
+ // References to all contexts used in this style
73
+ contexts = [
74
+ DocumentContext,
75
+ ClauseContext,
76
+ SubclauseContext,
77
+ AnnexContext,
78
+ AnnexClauseContext
79
+ ]
80
+ }
81
+
82
+ // Contexts
83
+ instance DocumentContext < LabelContext {
84
+ name = "document"
85
+ definition "Top-level document context for Internet-Drafts"
86
+ }
87
+
88
+ instance ClauseContext < LabelContext {
89
+ name = "clause"
90
+ parent = DocumentContext
91
+ definition "Context for sections within the document"
92
+ }
93
+
94
+ instance SubclauseContext < LabelContext {
95
+ name = "subclause"
96
+ parent = ClauseContext
97
+ definition "Context for subsections within sections"
98
+ }
99
+
100
+ instance AnnexContext < LabelContext {
101
+ name = "annex"
102
+ parent = DocumentContext
103
+ definition "Context for appendices within the document"
104
+ }
105
+
106
+ instance AnnexClauseContext < LabelContext {
107
+ name = "annex_clause"
108
+ parent = AnnexContext
109
+ definition "Context for sections within appendices"
110
+ }
111
+
112
+ // Section templates: headings carry a trailing dot per component;
113
+ // cross-references cite "Section N" (xml2rfc xref labeling).
114
+ instance ClauseLabelTemplate < LabelTemplate {
115
+ pattern = "%n."
116
+ definition "Template for section heading labels"
117
+ }
118
+
119
+ instance ClauseGlobalLabelTemplate < LabelTemplate {
120
+ pattern = "Section %n"
121
+ definition "Template for section cross-references"
122
+ }
123
+
124
+ instance ClauseLabeller < Labeller {
125
+ id = "clause_labeller"
126
+ sequence = DecimalNumericSequence
127
+ context = DocumentContext
128
+ type = "clause"
129
+ template = ClauseLabelTemplate
130
+ global_template = ClauseGlobalLabelTemplate
131
+ definition "Labeller for sections in the document"
132
+ }
133
+
134
+ instance SubclauseLabelTemplate < LabelTemplate {
135
+ pattern = "%pn.%n."
136
+ definition "Template for subsection heading labels"
137
+ }
138
+
139
+ instance SubclauseGlobalLabelTemplate < LabelTemplate {
140
+ pattern = "Section %pn.%n"
141
+ definition "Template for subsection cross-references"
142
+ }
143
+
144
+ instance SubclauseLabeller < Labeller {
145
+ id = "subclause_labeller"
146
+ sequence = DecimalNumericSequence
147
+ context = ClauseContext
148
+ parent = ClauseLabeller
149
+ type = "subclause"
150
+ template = SubclauseLabelTemplate
151
+ global_template = SubclauseGlobalLabelTemplate
152
+ definition "Labeller for subsections within sections"
153
+ }
154
+
155
+ // Appendix templates: top-level back-matter sections are "Appendix A.";
156
+ // inner levels carry dotted numbers and cite "Appendix A.1".
157
+ instance AnnexLabelTemplate < LabelTemplate {
158
+ pattern = "Appendix %n."
159
+ definition "Template for appendix heading labels"
160
+ }
161
+
162
+ instance AnnexGlobalLabelTemplate < LabelTemplate {
163
+ pattern = "Appendix %n"
164
+ definition "Template for appendix cross-references"
165
+ }
166
+
167
+ instance AnnexLabeller < Labeller {
168
+ id = "annex_labeller"
169
+ sequence = AlphaUpperNumericSequence
170
+ context = DocumentContext
171
+ type = "annex"
172
+ template = AnnexLabelTemplate
173
+ global_template = AnnexGlobalLabelTemplate
174
+ definition "Labeller for appendices in the document"
175
+ }
176
+
177
+ instance AnnexClauseLabelTemplate < LabelTemplate {
178
+ pattern = "%pn.%n."
179
+ definition "Template for appendix section heading labels"
180
+ }
181
+
182
+ instance AnnexClauseGlobalLabelTemplate < LabelTemplate {
183
+ pattern = "Appendix %pn.%n"
184
+ definition "Template for appendix section cross-references"
185
+ }
186
+
187
+ instance AnnexClauseLabeller < Labeller {
188
+ id = "annex_clause_labeller"
189
+ sequence = DecimalNumericSequence
190
+ context = AnnexContext
191
+ parent = AnnexLabeller
192
+ type = "clause"
193
+ template = AnnexClauseLabelTemplate
194
+ global_template = AnnexClauseGlobalLabelTemplate
195
+ definition "Labeller for sections within appendices"
196
+ }
197
+
198
+ instance AnnexSubclauseLabelTemplate < LabelTemplate {
199
+ pattern = "%pn.%n."
200
+ definition "Template for appendix subsection heading labels"
201
+ }
202
+
203
+ instance AnnexSubclauseGlobalLabelTemplate < LabelTemplate {
204
+ pattern = "Appendix %pn.%n"
205
+ definition "Template for appendix subsection cross-references"
206
+ }
207
+
208
+ instance AnnexSubclauseLabeller < Labeller {
209
+ id = "annex_subclause_labeller"
210
+ sequence = DecimalNumericSequence
211
+ context = AnnexClauseContext
212
+ parent = AnnexClauseLabeller
213
+ type = "subclause"
214
+ template = AnnexSubclauseLabelTemplate
215
+ global_template = AnnexSubclauseGlobalLabelTemplate
216
+ definition "Labeller for subsections within appendix sections"
217
+ }
218
+
219
+ // Captions: xml2rfc renders "Table 1: Name" / "Figure 1: Name"; the
220
+ // label is the caption prefix, the name join is a rendering concern.
221
+ instance FigureLabelTemplate < LabelTemplate {
222
+ pattern = "Figure %n"
223
+ definition "Template for figure caption labels"
224
+ }
225
+
226
+ instance FigureLabeller < Labeller {
227
+ id = "figure_labeller"
228
+ sequence = DecimalNumericSequence
229
+ context = DocumentContext
230
+ type = "figure"
231
+ template = FigureLabelTemplate
232
+ definition "Labeller for figures in the document"
233
+ }
234
+
235
+ instance AnnexFigureLabelTemplate < LabelTemplate {
236
+ pattern = "Figure %pn.%n"
237
+ definition "Template for figure caption labels in appendices"
238
+ }
239
+
240
+ instance AnnexFigureLabeller < Labeller {
241
+ id = "annex_figure_labeller"
242
+ sequence = DecimalNumericSequence
243
+ context = AnnexContext
244
+ parent = AnnexLabeller
245
+ type = "figure"
246
+ template = AnnexFigureLabelTemplate
247
+ definition "Labeller for figures in appendices"
248
+ }
249
+
250
+ instance TableLabelTemplate < LabelTemplate {
251
+ pattern = "Table %n"
252
+ definition "Template for table caption labels"
253
+ }
254
+
255
+ instance TableLabeller < Labeller {
256
+ id = "table_labeller"
257
+ sequence = DecimalNumericSequence
258
+ context = DocumentContext
259
+ type = "table"
260
+ template = TableLabelTemplate
261
+ definition "Labeller for tables in the document"
262
+ }
263
+
264
+ instance AnnexTableLabelTemplate < LabelTemplate {
265
+ pattern = "Table %pn.%n"
266
+ definition "Template for table caption labels in appendices"
267
+ }
268
+
269
+ instance AnnexTableLabeller < Labeller {
270
+ id = "annex_table_labeller"
271
+ sequence = DecimalNumericSequence
272
+ context = AnnexContext
273
+ parent = AnnexLabeller
274
+ type = "table"
275
+ template = AnnexTableLabelTemplate
276
+ definition "Labeller for tables in appendices"
277
+ }
278
+
279
+ // Notes: RFC XML v3 numbers no notes (xml2rfc render_note emits the
280
+ // bare "Note" label); notes render bare regardless of repetition.
281
+ instance NoteLabelTemplate < LabelTemplate {
282
+ pattern = "Note"
283
+ definition "Template for notes"
284
+ }
285
+
286
+ instance NoteLabeller < Labeller {
287
+ id = "note_labeller"
288
+ sequence = DecimalNumericSequence
289
+ context = ClauseContext
290
+ type = "note"
291
+ template = NoteLabelTemplate
292
+ definition "Labeller for notes (bare, unnumbered)"
293
+ }