metanorma-plugin-lutaml 0.7.67 → 0.7.69

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff4a1fdfd30ff9ee4c26a25fcec80beb30ebec42596bda4ff9ce7f33a8fa08bc
4
- data.tar.gz: fbad62a8ac032cdb2a9b97e9e53425896cd66cf1d8229c7841f97989d72868c1
3
+ metadata.gz: 66af39979d425315f2ec0df8901ccf17f691a53cc44d9fce6b2c47c9427f7cc9
4
+ data.tar.gz: ff163a8fbaa6cd3ab5362ff6173e6e5715108ed7a56f682318a8ac71d4099bb5
5
5
  SHA512:
6
- metadata.gz: 5703d0bf6d33fb4a042e8e284ea337fa032d951d5d617e8829df8106576e01c666794bb9753beba5df61371a66c5be036363e94b6c73e34396c02b21d75a01ed
7
- data.tar.gz: bebf4a12e6bf8e5def96163eb827c1441348d97bdd2474b34b1a4f162771b443fb93704ecd720cc5de12bc8b7b71a3ab720c2fb7af171567ccb4add4c272c742
6
+ metadata.gz: fd8695ec9568498511ad984a9ae1ba0972f4bc8ba7f4cbe50b44edf7378b5e85d8f6cd25f00553c2a08de146a5a3ab70074500013ea848c5988ca45ba22391e0
7
+ data.tar.gz: fb753e85f753de969e1b8568c3a792a1c4f541a6df11900680a431e35d9dcdb964be7a779a1f92eeae86bc097249a97f4362bba89a7dc2671300f09c34bae125
@@ -225,3 +225,57 @@ Directories for Liquid `include` can be set with `options`:
225
225
  {% include 'heading' %}
226
226
  ----
227
227
  ------
228
+
229
+ === Data from a provider
230
+
231
+ A Ruby gem can expose data to documents as a provider. The block names the
232
+ provider and one of its queries instead of a definition file, and passes the
233
+ query's keyword arguments under `arguments` in `options`:
234
+
235
+ [source,asciidoc]
236
+ ------
237
+ [lutaml_data,provider:ituob.edition,edition,options={"arguments":{"register":"e212-mnc","as_of":1170}}]
238
+ ----
239
+ {{ edition.register }} as of issue {{ edition.as_of_issue }}
240
+ {% for row in edition.items %}
241
+ {{ row.mnc }} {{ row.network }}
242
+ {% endfor %}
243
+ ----
244
+ ------
245
+
246
+ The query's result reaches Liquid unconverted: lutaml-model objects answer
247
+ `to_liquid` themselves, so the template reads the provider's typed
248
+ attributes, in the order the provider returns them. The plugin never turns
249
+ them into hashes.
250
+
251
+ ==== Writing a provider
252
+
253
+ A provider gem ships
254
+ `lib/metanorma/plugin/lutaml/data_providers/<name>.rb`, which registers a
255
+ factory:
256
+
257
+ [source,ruby]
258
+ ----
259
+ Metanorma::Plugin::Lutaml::DataProviders.register("ituob") do
260
+ Ituob::Provider.new
261
+ end
262
+ ----
263
+
264
+ The plugin requires that file the first time a document names the provider,
265
+ so the provider gem only has to be in the document's bundle. Provider names
266
+ are lowercase letters, digits and underscores.
267
+
268
+ The provider object lists the queries a document may call:
269
+
270
+ [source,ruby]
271
+ ----
272
+ def lutaml_data_queries
273
+ %i[edition history]
274
+ end
275
+ ----
276
+
277
+ Any other method, public or not, cannot be called from a document. A query
278
+ takes keyword arguments (JSON keys become keywords, JSON numbers stay
279
+ numbers) and returns lutaml-model objects or arrays of them. An unknown
280
+ provider, an undeclared query or a wrong argument fails the block with an
281
+ error.
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metanorma
4
+ module Plugin
5
+ module Lutaml
6
+ # Registry of Ruby data providers that `lutaml_data` blocks can query.
7
+ #
8
+ # A provider gem registers a factory under a name:
9
+ #
10
+ # Metanorma::Plugin::Lutaml::DataProviders.register("ituob") do
11
+ # Ituob::Provider.new
12
+ # end
13
+ #
14
+ # and ships that call in
15
+ # `lib/metanorma/plugin/lutaml/data_providers/<name>.rb`, which is
16
+ # required on first use of an unregistered name.
17
+ #
18
+ # The provider object lists the queries a document may call in
19
+ # `lutaml_data_queries`; each query takes keyword arguments and
20
+ # returns lutaml-model objects, which reach Liquid unconverted.
21
+ module DataProviders
22
+ class Error < StandardError; end
23
+
24
+ NAME_PATTERN = /\A[a-z0-9_]+\z/
25
+ DISCOVERY_PATH = "metanorma/plugin/lutaml/data_providers"
26
+
27
+ @factories = {}
28
+ @providers = {}
29
+
30
+ class << self
31
+ def register(name, &factory)
32
+ name = validate_name(name)
33
+ unless factory
34
+ raise ArgumentError, "provider `#{name}` needs a factory"
35
+ end
36
+
37
+ @factories[name] = factory
38
+ @providers.delete(name)
39
+ end
40
+
41
+ def unregister(name)
42
+ @factories.delete(name.to_s)
43
+ @providers.delete(name.to_s)
44
+ end
45
+
46
+ def query(name, query_name, arguments = {})
47
+ provider = fetch(name)
48
+ query_name = query_name.to_s
49
+ unless allowed_queries(provider).include?(query_name)
50
+ raise Error, "provider `#{name}` has no query `#{query_name}`"
51
+ end
52
+
53
+ provider.public_send(query_name,
54
+ **arguments.transform_keys(&:to_sym))
55
+ end
56
+
57
+ def fetch(name)
58
+ name = validate_name(name)
59
+ discover(name) unless @factories.key?(name)
60
+ factory = @factories.fetch(name) do
61
+ raise Error, "no LutaML data provider named `#{name}`"
62
+ end
63
+ @providers[name] ||= factory.call
64
+ end
65
+
66
+ private
67
+
68
+ def validate_name(name)
69
+ name = name.to_s
70
+ return name if NAME_PATTERN.match?(name)
71
+
72
+ raise Error, "invalid LutaML data provider name `#{name}`"
73
+ end
74
+
75
+ def discover(name)
76
+ Gem.find_files("#{DISCOVERY_PATH}/#{name}.rb").each do |path|
77
+ require path
78
+ end
79
+ end
80
+
81
+ def allowed_queries(provider)
82
+ unless provider.respond_to?(:lutaml_data_queries)
83
+ raise Error, "provider #{provider.class} does not declare " \
84
+ "`lutaml_data_queries`"
85
+ end
86
+
87
+ provider.lutaml_data_queries.map(&:to_s)
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -79,6 +79,9 @@ module Metanorma
79
79
  # by the schema path, the same way an entity-tagged remark names
80
80
  # its target. The prose stays in Schema#remarks too; templates
81
81
  # that already render it there keep working unchanged.
82
+ #
83
+ # Since expressir 2.4.59 (expressir#603) the formatters print
84
+ # remark_items, so the synthetic item is hidden from them.
82
85
  def promote_schema_prose
83
86
  return unless @model.is_a?(Expressir::Model::Declarations::Schema)
84
87
  return if @model.remarks.to_a.empty?
@@ -91,6 +94,24 @@ module Metanorma
91
94
  remarks: @model.remarks.dup,
92
95
  )
93
96
  @model.remark_items = [prose] + items
97
+ hide_from_source_formats(prose)
98
+ end
99
+
100
+ # Each format is still computed lazily and memoized by expressir;
101
+ # it just runs without the promoted item in remark_items.
102
+ def hide_from_source_formats(prose)
103
+ %i[
104
+ formatted formatted_hyperlinked formatted_hyperlinked_adoc
105
+ ].each do |name|
106
+ format = @model.method(name)
107
+ @model.define_singleton_method(name) do
108
+ items = remark_items
109
+ self.remark_items = items - [prose]
110
+ format.call
111
+ ensure
112
+ self.remark_items = items
113
+ end
114
+ end
94
115
  end
95
116
 
96
117
  def decorate_own_remarks(options)
@@ -18,6 +18,22 @@ module Metanorma
18
18
  $ # End of line
19
19
  }x
20
20
 
21
+ MACRO_LINE_REGEXP = /^(link|image|video|audio|include)::/
22
+
23
+ # Asciidoctor's list-item continuation: attaches the next block
24
+ # only when the preceding line is a list item.
25
+ LONE_CONTINUATION_REGEXP = /^\+[[:space:]]*$/
26
+
27
+ # A `+` glued to a macro line's trailing `[]` (no whitespace): the
28
+ # form asciidoctor renders as literal text in every context.
29
+ GLUED_CONTINUATION_REGEXP = %r{
30
+ ^(?:link|image|video|audio|include):: .+ \[\][+][[:space:]]*$
31
+ }x
32
+ LIST_ITEM_REGEXP = %r{
33
+ ^(?:[-*]|\d+[.)]|[A-Za-z][.)])\s # bullets and enumerations
34
+ | \w[[:alnum:] _-]*::\s # definition lists
35
+ }x
36
+
21
37
  attr_reader :remark, :options
22
38
 
23
39
  def self.call(remark, options)
@@ -36,7 +52,8 @@ module Metanorma
36
52
  end
37
53
 
38
54
  def call
39
- result = remark
55
+ result = normalise_dangling_continuations(remark)
56
+ result = normalise_glued_continuations(result)
40
57
  if options["relative_path_prefix"]
41
58
  result = update_relative_paths(result,
42
59
  options["relative_path_prefix"])
@@ -46,17 +63,60 @@ module Metanorma
46
63
 
47
64
  private
48
65
 
66
+ # A `+` GLUED to a macro's closing bracket (`image::x.gif[]+`) is
67
+ # parsed by asciidoctor as literal text in EVERY context — list
68
+ # item or paragraph (TRThurman's trial, iso-10303#862: the
69
+ # kinematic_structure_schema `direction_vectors` continuation) —
70
+ # so it never performs the attachment the author wrote it for.
71
+ # Strip it: the macro renders as its block, and any intended
72
+ # continuation of the following text stays with the following
73
+ # text.
74
+ def normalise_glued_continuations(string)
75
+ strip = lambda do |line|
76
+ line.match?(GLUED_CONTINUATION_REGEXP) ? line[..-2] : line
77
+ end
78
+ string.split("\n").map(&strip).join("\n")
79
+ end
80
+
81
+ # A lone `+` is asciidoctor's list-item continuation: it attaches
82
+ # the next block only when the preceding line is a list item.
83
+ # Authored after plain paragraphs (the corpus's `term - definition`
84
+ # style), it folds the following macro line into a literal text
85
+ # paragraph — printing the macro source with the decorator's
86
+ # expanded path inside it, which leaks the build directory into
87
+ # published text (iso-10303#862: kinematic_structure_schema's
88
+ # figure continuations). After a paragraph there is nothing to
89
+ # attach to, so the `+` becomes a blank line: the macro renders
90
+ # as the standalone block the author intended, directly after the
91
+ # text. Continuations after genuine list items keep their `+`.
92
+ def normalise_dangling_continuations(string)
93
+ lines = string.split("\n")
94
+ lines.each_with_index
95
+ .map do |line, idx|
96
+ dangling_continuation?(lines, idx) ? "" : line
97
+ end
98
+ .join("\n")
99
+ end
100
+
101
+ def dangling_continuation?(lines, idx)
102
+ return false unless lines[idx].match?(LONE_CONTINUATION_REGEXP)
103
+ return false unless lines[idx + 1].to_s.match?(MACRO_LINE_REGEXP)
104
+
105
+ prev = idx.positive? && lines[idx - 1]
106
+ return false if prev&.match?(LIST_ITEM_REGEXP)
107
+
108
+ true
109
+ end
110
+
49
111
  def update_relative_paths(string, path_prefix)
50
- string
51
- .split("\n")
52
- .map do |line|
53
- if line.match?(RELATIVE_PREFIX_MACRO_REGEXP)
54
- prefix_relative_paths(line, path_prefix)
55
- else
56
- line
57
- end
112
+ expand_line = lambda do |line|
113
+ if line.match?(RELATIVE_PREFIX_MACRO_REGEXP)
114
+ prefix_relative_paths(line, path_prefix)
115
+ else
116
+ line
58
117
  end
59
- .join("\n")
118
+ end
119
+ string.split("\n").map(&expand_line).join("\n")
60
120
  end
61
121
 
62
122
  def prefix_relative_paths(line, path_prefix)
@@ -22,9 +22,18 @@ module Metanorma
22
22
  #
23
23
  # [lutaml_data,models.lml,store,options={"include_path":"templates"}]
24
24
  #
25
+ # Data from a registered provider (see DataProviders), with the
26
+ # query's keyword arguments under `arguments`:
27
+ #
28
+ # [lutaml_data,provider:ituob.edition,edition,
29
+ # options={"arguments":{"register":"e212-mnc","as_of":1170}}]
30
+ #
31
+ # (written on one line in a document)
32
+ #
25
33
  class LutamlDataPreprocessor < BasePreprocessor
26
34
  DATA_FILE_EXTENSIONS = %w[.lml .yaml .yml .json].freeze
27
35
  DEFAULT_CONTEXT_NAME = "lutaml_data"
36
+ PROVIDER_SOURCE = /\Aprovider:(?<name>[^.]+)\.(?<query>.+)\z/
28
37
 
29
38
  DATA_BLOCK_REGEX = %r{
30
39
  ^
@@ -62,7 +71,7 @@ module Metanorma
62
71
  end_mark = input_lines.next
63
72
  lines = extract_block_lines(input_lines, end_mark)
64
73
 
65
- render_data_template(
74
+ render_block(
66
75
  document: document,
67
76
  lines: lines,
68
77
  definition_file: definition_file,
@@ -130,6 +139,42 @@ module Metanorma
130
139
  raise e
131
140
  end
132
141
 
142
+ def render_block(document:, lines:, definition_file:, **rest)
143
+ provider = definition_file.match(PROVIDER_SOURCE)
144
+ unless provider
145
+ return render_data_template(document: document, lines: lines,
146
+ definition_file: definition_file,
147
+ **rest)
148
+ end
149
+
150
+ render_provider_template(document, lines, provider,
151
+ rest[:context_name], rest[:options])
152
+ end
153
+
154
+ def render_provider_template(document, lines, provider, # rubocop:disable Metrics/ParameterLists
155
+ context_name, options)
156
+ arguments, options = provider_arguments(options)
157
+ data = DataProviders.query(provider[:name], provider[:query],
158
+ arguments)
159
+ parsed_template = template(lines)
160
+ parsed_template.registers[:file_system] =
161
+ build_file_system(document, options)
162
+ [parsed_template.render(context_name => data)]
163
+ rescue StandardError => e
164
+ log_block_failure(e)
165
+ raise e
166
+ end
167
+
168
+ def provider_arguments(options)
169
+ arguments = options.fetch("arguments", {})
170
+ unless arguments.is_a?(Hash)
171
+ raise StandardError, "lutaml_data provider `arguments` must " \
172
+ "be a JSON object"
173
+ end
174
+
175
+ [arguments, options.except("arguments")]
176
+ end
177
+
133
178
  def log_block_failure(error)
134
179
  ::Metanorma::Util.log(
135
180
  "[#{self.class.name}] Failed to parse LutaML data block: " \
@@ -1,7 +1,7 @@
1
1
  module Metanorma
2
2
  module Plugin
3
3
  module Lutaml
4
- VERSION = "0.7.67".freeze
4
+ VERSION = "0.7.69".freeze
5
5
  end
6
6
  end
7
7
  end
@@ -18,6 +18,7 @@ module Metanorma
18
18
  autoload :CacheRegistry, "metanorma/plugin/lutaml/cache_registry"
19
19
  autoload :Config, "metanorma/plugin/lutaml/config"
20
20
  autoload :Content, "metanorma/plugin/lutaml/content"
21
+ autoload :DataProviders, "metanorma/plugin/lutaml/data_providers"
21
22
  autoload :Data2TextPreprocessor,
22
23
  "metanorma/plugin/lutaml/data2_text_preprocessor"
23
24
  autoload :ExpressRemarksDecorator,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metanorma-plugin-lutaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.67
4
+ version: 0.7.69
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-12 00:00:00.000000000 Z
11
+ date: 2026-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -462,6 +462,7 @@ files:
462
462
  - lib/metanorma/plugin/lutaml/config/root.rb
463
463
  - lib/metanorma/plugin/lutaml/content.rb
464
464
  - lib/metanorma/plugin/lutaml/data2_text_preprocessor.rb
465
+ - lib/metanorma/plugin/lutaml/data_providers.rb
465
466
  - lib/metanorma/plugin/lutaml/express_hotspot_anchor.rb
466
467
  - lib/metanorma/plugin/lutaml/express_remark_adapter.rb
467
468
  - lib/metanorma/plugin/lutaml/express_remarks_decorator.rb