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.
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ module Runtime
5
+ # The English label vocabulary backing `{key}` tokens in label
6
+ # template patterns — the isodoc i18n key set, so a host document's
7
+ # i18n-<lang>.yaml table slots in directly. Profiles carry the
8
+ # labeling scheme; the vocabulary is language data supplied by the
9
+ # host (Kaleidl::Runtime::Engine labels:).
10
+ module Defaults
11
+ LABEL_VOCABULARY = {
12
+ "clause" => "Clause",
13
+ "subclause" => "Clause",
14
+ "annex" => "Annex",
15
+ "appendix" => "Appendix",
16
+ "section" => "Section",
17
+ "figure" => "Figure",
18
+ "table" => "Table",
19
+ "formula" => "Formula",
20
+ "inequality" => "Inequality",
21
+ "note" => "NOTE",
22
+ "note_xref" => "NOTE",
23
+ "note_to_entry" => "Note { $n } to entry",
24
+ "note_to_entry_xref" => "{ $parent }, Note { $n } to entry",
25
+ "example" => "EXAMPLE",
26
+ "example_xref" => "EXAMPLE",
27
+ "termexample" => "EXAMPLE",
28
+ "list" => "List",
29
+ "introduction" => "Introduction",
30
+ "norm_annex" => "normative",
31
+ "inform_annex" => "informative",
32
+ }.freeze
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,301 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ module Runtime
5
+ # The LADL labeling evaluator (MN 112 "Algorithm to resolve the
6
+ # label for a content element").
7
+ #
8
+ # The evaluator is a thin kernel over the three model trees:
9
+ #
10
+ # * The CONTENT TREE arrives from the host: ContentElements carrying
11
+ # `type`, and — for elements with children to be labeled — the
12
+ # DECLARED `label_context` association. The scoping structure is
13
+ # the host's declaration; the evaluator never infers it.
14
+ # * The CONTEXT TREE and LABELLER TREE come from the profile.
15
+ #
16
+ # Resolution, per MN 112:
17
+ #
18
+ # 1. From the ContentElement, find its applicable LabelContext: the
19
+ # element's own association, else the closest one navigated
20
+ # through `parent`.
21
+ # 2. From that context, find the Labeller applicable to the
22
+ # element's type.
23
+ # 3. The Labeller generates the label: the first LabelTemplate whose
24
+ # condition holds (the element count of similar elements in the
25
+ # same context), the Sequence value at the element's position in
26
+ # that context, and the parent labeller's value where the
27
+ # template requires it.
28
+ #
29
+ # Counters are scoped by the context instance — the element carrying
30
+ # the association — so like-labeled elements under different
31
+ # carriers number independently.
32
+ class Engine
33
+ # A labeled element planned during the walk.
34
+ Entry = Struct.new(:element, :type, :labeller, :labeller_name,
35
+ :context_name, :carrier_object_id, :carrier_entry, :position,
36
+ :raw_value, :parent_hier, :parent_entry, :hierarchical, :local,
37
+ :global, keyword_init: true)
38
+
39
+ attr_reader :result, :resolved
40
+
41
+ # @param resolved [Runtime::Resolver, Kaleidl::Profile] a resolved
42
+ # label style, or a profile to resolve
43
+ # @param labels [Hash{String => String}] the label vocabulary for
44
+ # `{key}` tokens in template patterns — the host document's i18n
45
+ # table (e.g. annex: "Annexe"); keys absent from the dictionary
46
+ # fall back to the English default vocabulary
47
+ def initialize(resolved, labels: {})
48
+ @labels = labels || {}
49
+ @resolved = case resolved
50
+ when Runtime::Resolver
51
+ resolved
52
+ when Kaleidl::Profile
53
+ Runtime::Resolver.new(resolved)
54
+ else
55
+ raise Kaleidl::Error,
56
+ "engine requires a Kaleidl::Profile or Runtime::Resolver, " \
57
+ "got #{resolved.class}"
58
+ end
59
+ @plan = []
60
+ @totals = Hash.new(0)
61
+ @result = Result.new
62
+ end
63
+
64
+ # Labels the content tree rooted at `root` and returns the
65
+ # Kaleidl::Runtime::Result.
66
+ def label(root)
67
+ @result = Result.new
68
+ @plan = []
69
+ @totals = Hash.new(0)
70
+ walk(root, [])
71
+ render
72
+ @result
73
+ end
74
+ alias label_document label
75
+
76
+ private
77
+
78
+ # MN 112 step 1 has two independent resolutions:
79
+ #
80
+ # * The element's own LABEL: the first context in the association
81
+ # chain (self, then ancestors nearest-first) whose labeller
82
+ # binds the element's type. An association whose context has no
83
+ # labeller for the type is skipped — it scopes but does not
84
+ # label (a sub-clause context scopes its notes yet carries no
85
+ # sub-clause labeller).
86
+ # * The element's SCOPE CARRIER for its descendants and its
87
+ # `%parent` value: the nearest association (self, then
88
+ # ancestors), whose context may bind nothing itself.
89
+ def walk(element, ancestors)
90
+ labeller, label_context_name, label_carrier =
91
+ resolve_own_label(element, ancestors)
92
+ association = nearest_association(element, ancestors)
93
+ # The frame carries only the element's OWN declared association
94
+ # — inherited scopes do not masquerade as associations, so the
95
+ # carrier of `%parent` stays the element that declared it.
96
+ frame = { element: element, declared: element.label_context,
97
+ entry: nil }
98
+ frame[:entry] = if labeller
99
+ plan_element(element, labeller, label_context_name,
100
+ label_carrier, association, ancestors)
101
+ end
102
+ ancestors.push(frame)
103
+ children_of(element).each { |child| walk(child, ancestors) }
104
+ ancestors.pop
105
+ end
106
+
107
+ # The element's own label: the first context in the association
108
+ # chain (self, then ancestors nearest-first) whose labeller binds
109
+ # the element's type. Associations whose context binds no labeller
110
+ # for the type are skipped — they scope descendants but do not
111
+ # label this element (a sub-clause context scopes its notes yet
112
+ # carries no sub-clause labeller).
113
+ # Returns [labeller, context name, the frame whose context
114
+ # supplied the label].
115
+ def resolve_own_label(element, ancestors)
116
+ if element.label_context
117
+ labeller = resolve_labeller(element.label_context, element.type.to_s)
118
+ return [labeller, element.label_context,
119
+ { element: element, context_name: element.label_context,
120
+ entry: nil }] if labeller
121
+ end
122
+ ancestors.reverse_each do |frame|
123
+ next unless frame[:declared]
124
+
125
+ labeller = resolve_labeller(frame[:declared], element.type.to_s)
126
+ return [labeller, frame[:declared], frame] if labeller
127
+ end
128
+ [nil, nil, nil]
129
+ end
130
+
131
+ # The nearest association frame (self, then ancestors nearest
132
+ # first).
133
+ def nearest_association(element, ancestors)
134
+ if element.label_context
135
+ return { element: element, context_name: element.label_context,
136
+ entry: nil }
137
+ end
138
+
139
+ ancestors.reverse_each do |frame|
140
+ return frame if frame[:declared]
141
+ end
142
+ nil
143
+ end
144
+
145
+ # MN 112 step 2: the Labeller applicable to the element's type in
146
+ # the resolved context.
147
+ def resolve_labeller(context_name, type)
148
+ @resolved.labeller_for_context(context_name, type)
149
+ end
150
+
151
+ def children_of(element)
152
+ Array(element.children)
153
+ end
154
+
155
+ def plan_element(element, labeller, context_name, label_carrier,
156
+ scope_association, ancestors)
157
+ parent = find_parent_entry(labeller, ancestors)
158
+ parent_hier = parent&.hierarchical
159
+ position = increment_counter(label_carrier, labeller, parent_hier)
160
+ raw = sequence_raw(labeller, position)
161
+ entry = Entry.new(
162
+ element: element,
163
+ type: element.type.to_s,
164
+ labeller: labeller,
165
+ labeller_name: @resolved.instance_name_of(labeller),
166
+ context_name: context_name,
167
+ carrier_object_id: label_carrier[:element].object_id,
168
+ carrier_entry: scope_association && scope_association[:entry],
169
+ position: position,
170
+ raw_value: raw,
171
+ parent_hier: parent_hier,
172
+ parent_entry: parent,
173
+ hierarchical: join_hierarchical(parent_hier, raw),
174
+ )
175
+ # A self-associated element is its own scope instance: `%parent`
176
+ # resolves to its own label.
177
+ if scope_association && scope_association[:element].equal?(element)
178
+ scope_association[:entry] = entry
179
+ entry.carrier_entry = entry
180
+ end
181
+ @plan << entry
182
+ entry
183
+ end
184
+
185
+ # The nearest ancestor element labeled by this labeller's parent
186
+ # labeller: it supplies the `%pn` value (its hierarchical label)
187
+ # and anchors the hierarchical counter scope. Falls back to the
188
+ # nearest ancestor entry of the parent labeller's element type —
189
+ # an enclosing figure labeled by the annex figure labeller still
190
+ # supplies a subfigure's `%pn` across flavor bindings.
191
+ def find_parent_entry(labeller, ancestors)
192
+ parent_name = labeller.parent
193
+ return nil if parent_name.nil? || parent_name.empty?
194
+
195
+ parent_labeller = @resolved.profile.labeller(parent_name)
196
+ unless parent_labeller
197
+ raise Kaleidl::ResolutionError,
198
+ "labeller #{@resolved.instance_name_of(labeller).inspect} " \
199
+ "references undefined parent labeller #{parent_name.inspect}"
200
+ end
201
+ chain = @resolved.ancestor_chain(@resolved.instance_name_of(labeller))
202
+ ancestors.reverse_each do |frame|
203
+ entry = frame[:entry]
204
+ return entry if entry && chain.include?(entry.labeller_name)
205
+ end
206
+ ancestors.reverse_each do |frame|
207
+ entry = frame[:entry]
208
+ return entry if entry && entry.type == parent_labeller.type
209
+ end
210
+ nil
211
+ end
212
+
213
+ # The position of the element in its context: the count of like-
214
+ # labeled elements under the same carrier, restarted per
215
+ # hierarchical scope.
216
+ def increment_counter(carrier, labeller, parent_hier)
217
+ key = counter_key(carrier[:element].object_id,
218
+ @resolved.instance_name_of(labeller), parent_hier)
219
+ @totals[key] += 1
220
+ @totals[key]
221
+ end
222
+
223
+ def counter_key(carrier_id, labeller_name, parent_hier)
224
+ [carrier_id, labeller_name, parent_hier.to_s]
225
+ end
226
+
227
+ def sequence_raw(labeller, position)
228
+ return if labeller.sequence.nil? || labeller.sequence.empty?
229
+
230
+ @resolved.sequence_value(labeller.sequence, position)
231
+ end
232
+
233
+ def join_hierarchical(parent_hier, raw)
234
+ return parent_hier if parent_hier && raw.nil?
235
+ return raw if parent_hier.nil?
236
+
237
+ "#{parent_hier}.#{raw}"
238
+ end
239
+
240
+ # MN 112 step 3: template selection (the first condition that
241
+ # holds, else the first unconditional, else the first), then
242
+ # rendering with the sequence value, the parent-labeller value and
243
+ # the carrier element's label.
244
+ def render
245
+ @plan.each do |entry|
246
+ labeller = entry.labeller
247
+ total = @totals.fetch(counter_key(entry.carrier_object_id,
248
+ entry.labeller_name, entry.parent_hier), 1)
249
+ local_template = select_template(Array(labeller.template), total)
250
+ raise Kaleidl::ResolutionError,
251
+ "labeller #{entry.labeller_name.inspect} has no label template" unless local_template
252
+
253
+ bindings = base_bindings(entry)
254
+ entry.local = Renderer.render(
255
+ local_template.pattern, bindings.merge(pattern: local_template.pattern)
256
+ )
257
+ globals = Array(labeller.global_template)
258
+ entry.global = if globals.empty?
259
+ entry.local
260
+ else
261
+ global_template = select_template(globals, total)
262
+ Renderer.render(
263
+ global_template.pattern,
264
+ bindings.merge(pattern: global_template.pattern, self: entry.local),
265
+ )
266
+ end
267
+ @result.add(build_label(entry, labeller))
268
+ end
269
+ end
270
+
271
+ def select_template(templates, total)
272
+ templates.find { |template| template.condition&.evaluate(total) } ||
273
+ templates.find { |template| template.condition.nil? } ||
274
+ templates.first
275
+ end
276
+
277
+ def base_bindings(entry)
278
+ {
279
+ n: entry.raw_value,
280
+ pn: entry.parent_hier,
281
+ parent: entry.carrier_entry&.global,
282
+ labels: @labels,
283
+ }
284
+ end
285
+
286
+ def build_label(entry, labeller)
287
+ label = Kaleidl::Label.new(
288
+ value: entry.local,
289
+ global_value: entry.global,
290
+ hierarchical: entry.hierarchical,
291
+ sequence_value: entry.raw_value,
292
+ position: entry.position,
293
+ labeller_id: labeller.id,
294
+ content_element: entry.element.id,
295
+ )
296
+ label.element = entry.element
297
+ label
298
+ end
299
+ end
300
+ end
301
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rhoella"
4
+
5
+ module Kaleidl
6
+ module Runtime
7
+ # MN 112 "string interpolation" — substitutes variable placeholders
8
+ # in a label template pattern with actual values to construct a
9
+ # rendered label.
10
+ #
11
+ # Supported placeholders:
12
+ #
13
+ # * `%n` — the labeller value (sequence value at this position)
14
+ # * `%pn` — the parent labeller value (hierarchical path of the
15
+ # nearest enclosing parent-labeled element)
16
+ # * `%parent` — the rendered global label of the parent context
17
+ # element
18
+ # * `%self` — the rendered local label of the element itself
19
+ # * `{key}` — a label vocabulary key, resolved through the labels
20
+ # dictionary supplied with the profile (the host
21
+ # document's i18n table, e.g. annex: "Annexe" for
22
+ # French). Profiles carry the labeling SCHEME; the
23
+ # vocabulary is language data and comes from the host.
24
+ # Keys absent from the dictionary fall back to the
25
+ # English default vocabulary; keys absent from both
26
+ # are profile errors.
27
+ module Renderer
28
+ PLACEHOLDER = /%pn|%parent|%self|%n|\{([A-Za-z][A-Za-z0-9_]*)\}/.freeze
29
+
30
+ module_function
31
+
32
+ # Renders `pattern` with the given bindings. A `%pn` placeholder in
33
+ # the pattern with no parent value bound is a profile error.
34
+ def render(pattern, bindings)
35
+ pattern.to_s.gsub(PLACEHOLDER) do |placeholder|
36
+ case placeholder
37
+ when "%n"
38
+ text(bindings[:n], placeholder, bindings)
39
+ when "%pn"
40
+ text(bindings[:pn], placeholder, bindings)
41
+ when "%parent"
42
+ text(bindings[:parent], placeholder, bindings)
43
+ when "%self"
44
+ text(bindings[:self], placeholder, bindings)
45
+ when /\A\{([A-Za-z][A-Za-z0-9_]*)\}\z/
46
+ label_key(Regexp.last_match(1), bindings)
47
+ end
48
+ end
49
+ end
50
+
51
+ # The catalog value is a Fluent message pattern (Metanorma's
52
+ # l10n layer is the rhoella engine): term words are plain
53
+ # messages, designations that need engine values carry
54
+ # `{ $n }`, `{ $pn }`, `{ $parent }` and `{ $self }` placeables
55
+ # bound from the labeller bindings.
56
+ def label_key(key, bindings)
57
+ value = catalog_value(key, bindings)
58
+ # A label vocabulary is the host's own contract: the engine
59
+ # renders unknown references softly (per the upstream Fluent
60
+ # fixtures), but a catalog value referencing a message absent
61
+ # from the vocabulary is a profile error, not a "{nowhere}"
62
+ # string leaking into a published document.
63
+ missing = Rhoella::Parser.new(value).references - message_table(bindings).keys
64
+ unless missing.empty?
65
+ raise Rhoella::ResolutionError,
66
+ "references unknown message(s) #{missing.join(', ')}"
67
+ end
68
+
69
+ Rhoella.format(value, {
70
+ "n" => bindings[:n].to_s,
71
+ "pn" => bindings[:pn].to_s,
72
+ "parent" => bindings[:parent].to_s,
73
+ "self" => bindings[:self].to_s,
74
+ }, message_table(bindings))
75
+ rescue Rhoella::Error => e
76
+ raise Kaleidl::ResolutionError,
77
+ "template #{bindings[:pattern].inspect} resolves " \
78
+ "{#{key}} to the message #{value.inspect}, which failed " \
79
+ "to render: #{e.message}"
80
+ end
81
+
82
+ # The host catalog plus the English default vocabulary form the
83
+ # message catalog that references inside catalog messages resolve
84
+ # against.
85
+ def message_table(bindings)
86
+ table = Defaults::LABEL_VOCABULARY.merge(bindings[:labels] || {})
87
+ table.transform_values(&:to_s)
88
+ end
89
+
90
+ def catalog_value(key, bindings)
91
+ labels = bindings[:labels] || {}
92
+ return labels[key].to_s if labels.key?(key)
93
+
94
+ Defaults::LABEL_VOCABULARY.fetch(key) do
95
+ raise Kaleidl::ResolutionError,
96
+ "template #{bindings[:pattern].inspect} uses {#{key}} " \
97
+ "but the key is in neither the labels dictionary nor the " \
98
+ "default vocabulary"
99
+ end
100
+ end
101
+
102
+ def text(value, placeholder, bindings)
103
+ return value.to_s unless value.nil?
104
+ return "" if placeholder == "%parent" || placeholder == "%self"
105
+
106
+ raise Kaleidl::ResolutionError,
107
+ "template #{bindings[:pattern].inspect} uses #{placeholder} " \
108
+ "but the labeller has no parent labeller bound for this element"
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,175 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ module Runtime
5
+ # Wires a loaded Kaleidl::Profile into the indexes the engine needs:
6
+ # labellers by context instance and element type, context instances
7
+ # by name attribute and parent relation, sequence resolution, and
8
+ # the labeller-tree descendant index used for `%pn` resolution.
9
+ class Resolver
10
+ attr_reader :profile
11
+
12
+ def initialize(profile)
13
+ @profile = profile
14
+ validate_style_references
15
+ @labeller_names = {}
16
+ @profile.labeller_names.each do |name|
17
+ @labeller_names[@profile.labeller(name)] = name
18
+ end
19
+ @labellers_by_context = build_labeller_index
20
+ @descendants = build_descendants
21
+ @context_names = {}
22
+ @profile.context_names.each do |name|
23
+ context = @profile.context(name)
24
+ @context_names[name] = context.name if context.name
25
+ end
26
+ end
27
+
28
+ # The labeller bound to `context_instance_name` (a context
29
+ # instance name, e.g. "ClauseContext") that labels elements of
30
+ # `type`, or nil.
31
+ def labeller_for_context(context_instance_name, type)
32
+ @labellers_by_context.dig(context_instance_name, type)
33
+ end
34
+
35
+ # The `name` attribute of the context instance with the given
36
+ # instance name (e.g. "ClauseContext" -> "clause").
37
+ def context_name(instance_name)
38
+ @context_names[instance_name]
39
+ end
40
+
41
+ # Instance names of contexts whose `parent` is the given context
42
+ # instance name.
43
+ def contexts_with_parent(instance_name)
44
+ @profile.context_names.select do |name|
45
+ @profile.context(name).parent == instance_name
46
+ end
47
+ end
48
+
49
+ # Instance names of contexts whose `name` attribute equals the
50
+ # given element type (e.g. "clause" -> ["ClauseContext"]).
51
+ def contexts_named(type)
52
+ @profile.context_names.select do |name|
53
+ @context_names[name] == type
54
+ end
55
+ end
56
+
57
+ # Sequence value for the given sequence reference (instance name)
58
+ # at the given 1-based position; nil when the reference is blank.
59
+ def sequence_value(sequence_name, position)
60
+ return if sequence_name.nil? || sequence_name.empty?
61
+
62
+ sequence = @profile.sequence(sequence_name) || Sequences::Registry.lookup(sequence_name)
63
+ unless sequence
64
+ raise Kaleidl::ResolutionError,
65
+ "undefined sequence: #{sequence_name.inspect}"
66
+ end
67
+ Sequences.value_for(sequence, position, lookup: method(:lookup_sequence))
68
+ end
69
+
70
+ # True when `labeller_name` is a strict descendant of
71
+ # `ancestor_name` in the labeller tree.
72
+ def descendant_labeller?(ancestor_name, labeller_name)
73
+ @descendants[ancestor_name].include?(labeller_name)
74
+ end
75
+
76
+ # The self-inclusive ancestor chain of the given labeller instance
77
+ # name: the labeller itself followed by its parent, grandparent,
78
+ # ... in the labeller tree.
79
+ def ancestor_chain(labeller_name)
80
+ chain = [labeller_name]
81
+ seen = { labeller_name => true }
82
+ current = labeller_name
83
+ while (parent = @profile.labeller(current)&.parent)
84
+ break if seen.key?(parent)
85
+
86
+ chain << parent
87
+ seen[parent] = true
88
+ current = parent
89
+ end
90
+ chain
91
+ end
92
+
93
+ # The instance name of a labeller model within the profile.
94
+ def instance_name_of(labeller)
95
+ @labeller_names[labeller]
96
+ end
97
+
98
+ # The parent labeller instance name of the given labeller, or nil.
99
+ def parent_labeller_name(labeller)
100
+ labeller.parent
101
+ end
102
+
103
+ def lookup_sequence(name)
104
+ @profile.sequence(name) || Sequences::Registry.lookup(name)
105
+ end
106
+
107
+ private
108
+
109
+ def validate_style_references
110
+ missing = []
111
+ Array(@profile.style.labellers).each do |ref|
112
+ id = ref.start_with?("id:") ? ref[3..] : ref
113
+ missing << ref unless @profile.labeller_by_id(id) || @profile.labeller(ref)
114
+ end
115
+ Array(@profile.style.contexts).each do |ref|
116
+ missing << ref unless @profile.context(ref)
117
+ end
118
+ Array(@profile.style.templates).each do |ref|
119
+ missing << ref unless @profile.template(ref)
120
+ end
121
+ Array(@profile.style.sequences).each do |ref|
122
+ next if @profile.sequence(ref) || Sequences::Registry.lookup(ref)
123
+
124
+ missing << ref
125
+ end
126
+ unless missing.empty?
127
+ raise Kaleidl::ResolutionError,
128
+ "label style #{@profile.style.name.inspect} references " \
129
+ "undefined instances: #{missing.join(", ")}"
130
+ end
131
+ end
132
+
133
+ def build_labeller_index
134
+ index = Hash.new { |h, k| h[k] = {} }
135
+ @profile.labeller_names.each do |name|
136
+ labeller = @profile.labeller(name)
137
+ context = labeller.context
138
+ unless context
139
+ raise Kaleidl::ResolutionError,
140
+ "labeller #{name.inspect} declares no context"
141
+ end
142
+ unless @profile.context(context)
143
+ raise Kaleidl::ResolutionError,
144
+ "labeller #{name.inspect} references undefined context " \
145
+ "#{context.inspect}"
146
+ end
147
+ index[context][labeller.type] = labeller
148
+ end
149
+ index
150
+ end
151
+
152
+ def build_descendants
153
+ children = Hash.new { |h, k| h[k] = [] }
154
+ @profile.labeller_names.each do |name|
155
+ labeller = @profile.labeller(name)
156
+ children[labeller.parent] << name if labeller.parent
157
+ end
158
+ descendants = Hash.new { |h, k| h[k] = [] }
159
+ @profile.labeller_names.each do |name|
160
+ collect_descendants(name, children, descendants[name])
161
+ end
162
+ descendants
163
+ end
164
+
165
+ def collect_descendants(name, children, into)
166
+ children[name].each do |child|
167
+ into << child
168
+ collect_descendants(child, children, into)
169
+ end
170
+ into.uniq!
171
+ into
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ module Runtime
5
+ # The output of a labeling run: the Kaleidl::Label assigned to each
6
+ # content element, in document order, with lookup by element
7
+ # identity.
8
+ class Result
9
+ include Enumerable
10
+
11
+ def initialize
12
+ @labels = []
13
+ @by_element = {}.compare_by_identity
14
+ end
15
+
16
+ def add(label)
17
+ @labels << label
18
+ @by_element[label.element] = label if label.element
19
+ label
20
+ end
21
+
22
+ def each(&block)
23
+ @labels.each(&block)
24
+ end
25
+
26
+ def size
27
+ @labels.size
28
+ end
29
+ alias length size
30
+
31
+ def empty?
32
+ @labels.empty?
33
+ end
34
+
35
+ # The label for the given content element, or nil.
36
+ def label_for(element)
37
+ @by_element[element]
38
+ end
39
+
40
+ # The rendered local label for the given content element, or nil.
41
+ def local_for(element)
42
+ label = @by_element[element]
43
+ label&.value
44
+ end
45
+
46
+ # The rendered global label for the given content element, or nil.
47
+ def global_for(element)
48
+ label = @by_element[element]
49
+ label&.global_value
50
+ end
51
+
52
+ # Labels keyed by the content element id (elements without an id
53
+ # are omitted).
54
+ def labels_by_element_id
55
+ @labels.each_with_object({}) do |label, hash|
56
+ hash[label.element.id] = label if label.element && label.element.id
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kaleidl
4
+ module Runtime
5
+ autoload :Resolver, "kaleidl/runtime/resolver"
6
+ autoload :Engine, "kaleidl/runtime/engine"
7
+ autoload :Renderer, "kaleidl/runtime/renderer"
8
+ autoload :Result, "kaleidl/runtime/result"
9
+ autoload :Defaults, "kaleidl/runtime/defaults"
10
+ end
11
+ end