metanorma-plugin-lutaml 0.7.67 → 0.7.68

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: e7ef65c3a2ef0dabf7b9cec376a1f2c5ee207cb9ca710665462ee99e508495f0
4
+ data.tar.gz: b523aded0b2d72a504d1afbd149e9d93b77f6a931fded9f9cc1d1a9f547c8f9f
5
5
  SHA512:
6
- metadata.gz: 5703d0bf6d33fb4a042e8e284ea337fa032d951d5d617e8829df8106576e01c666794bb9753beba5df61371a66c5be036363e94b6c73e34396c02b21d75a01ed
7
- data.tar.gz: bebf4a12e6bf8e5def96163eb827c1441348d97bdd2474b34b1a4f162771b443fb93704ecd720cc5de12bc8b7b71a3ab720c2fb7af171567ccb4add4c272c742
6
+ metadata.gz: a30614c2017d73cc7d4580924ec472138d4c1fef77b9cf1a8b900816002530ed82b3dcf9cbf5b44623731aa2dd20ad5c635e1f39ef9a0a47586ede1b2519f4e5
7
+ data.tar.gz: 5bc2b634b31449303ae41e4ed4aad464d9940688371fd682e21fa5f58820b5ec1cae11b7fc52ae86993a1655f4f9e72ffd24d14db51ec082141247aac58fed3d
@@ -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,17 @@ 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
+ LIST_ITEM_REGEXP = %r{
28
+ ^(?:[-*]|\d+[.)]|[A-Za-z][.)])\s # bullets and enumerations
29
+ | \w[[:alnum:] _-]*::\s # definition lists
30
+ }x
31
+
21
32
  attr_reader :remark, :options
22
33
 
23
34
  def self.call(remark, options)
@@ -36,7 +47,7 @@ module Metanorma
36
47
  end
37
48
 
38
49
  def call
39
- result = remark
50
+ result = normalise_dangling_continuations(remark)
40
51
  if options["relative_path_prefix"]
41
52
  result = update_relative_paths(result,
42
53
  options["relative_path_prefix"])
@@ -46,17 +57,45 @@ module Metanorma
46
57
 
47
58
  private
48
59
 
60
+ # A lone `+` is asciidoctor's list-item continuation: it attaches
61
+ # the next block only when the preceding line is a list item.
62
+ # Authored after plain paragraphs (the corpus's `term - definition`
63
+ # style), it folds the following macro line into a literal text
64
+ # paragraph — printing the macro source with the decorator's
65
+ # expanded path inside it, which leaks the build directory into
66
+ # published text (iso-10303#862: kinematic_structure_schema's
67
+ # figure continuations). After a paragraph there is nothing to
68
+ # attach to, so the `+` becomes a blank line: the macro renders
69
+ # as the standalone block the author intended, directly after the
70
+ # text. Continuations after genuine list items keep their `+`.
71
+ def normalise_dangling_continuations(string)
72
+ lines = string.split("\n")
73
+ lines.each_with_index
74
+ .map do |line, idx|
75
+ dangling_continuation?(lines, idx) ? "" : line
76
+ end
77
+ .join("\n")
78
+ end
79
+
80
+ def dangling_continuation?(lines, idx)
81
+ return false unless lines[idx].match?(LONE_CONTINUATION_REGEXP)
82
+ return false unless lines[idx + 1].to_s.match?(MACRO_LINE_REGEXP)
83
+
84
+ prev = idx.positive? && lines[idx - 1]
85
+ return false if prev&.match?(LIST_ITEM_REGEXP)
86
+
87
+ true
88
+ end
89
+
49
90
  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
91
+ expand_line = lambda do |line|
92
+ if line.match?(RELATIVE_PREFIX_MACRO_REGEXP)
93
+ prefix_relative_paths(line, path_prefix)
94
+ else
95
+ line
58
96
  end
59
- .join("\n")
97
+ end
98
+ string.split("\n").map(&expand_line).join("\n")
60
99
  end
61
100
 
62
101
  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.68".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.68
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