metanorma-plugin-lutaml 0.7.69 → 0.7.71

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: 66af39979d425315f2ec0df8901ccf17f691a53cc44d9fce6b2c47c9427f7cc9
4
- data.tar.gz: ff163a8fbaa6cd3ab5362ff6173e6e5715108ed7a56f682318a8ac71d4099bb5
3
+ metadata.gz: 6603da3f34c5507740381e3b0a8bddba42aca769f703135ee2c399463a856dac
4
+ data.tar.gz: 541459302ba0085e21cb4b24fa5d513f1005d1ce74a288d06f83f3cbe2cc421c
5
5
  SHA512:
6
- metadata.gz: fd8695ec9568498511ad984a9ae1ba0972f4bc8ba7f4cbe50b44edf7378b5e85d8f6cd25f00553c2a08de146a5a3ab70074500013ea848c5988ca45ba22391e0
7
- data.tar.gz: fb753e85f753de969e1b8568c3a792a1c4f541a6df11900680a431e35d9dcdb964be7a779a1f92eeae86bc097249a97f4362bba89a7dc2671300f09c34bae125
6
+ metadata.gz: e3eb2153ce12811e8df70e9ace1a60019c17e4f576d4a6a5e12aebe3418411b4f7ba9eabb663d93f4da69e87cd85af8ee0310b2a42d2c03f5791153ce8a4a3a2
7
+ data.tar.gz: b607a363a06fc9f8ba980bc56ef4490431ea65d09966faeb77d7cddb1ad6c750959b55349d54c0b958a9852c56b366e5c0d1978b56e13a34e9206690aa27a15b
@@ -1,39 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "expressir"
4
- require "pathname"
5
4
 
6
5
  module Metanorma
7
6
  module Plugin
8
7
  module Lutaml
9
8
  module ExpressRemarkAdapter
10
- def self.relative_path_prefix(options, model)
11
- return if options.nil? || options["document"].nil?
12
-
13
- document = options["document"]
14
- file_path = File.dirname(model.file)
15
- docfile_directory = File.dirname(
16
- document.attributes["docfile"] || ".",
9
+ # Resolves the directory of a schema's source file the way the
10
+ # document resolves any referenced path (the document's
11
+ # PathResolver#system_path against the docfile directory). The
12
+ # result is the schema's imagesdir: image macros in the schema's
13
+ # remarks then resolve as written and no build path enters the
14
+ # remark text (TRThurman's source-fidelity requirement: the
15
+ # 0.7.x path rewriting leaked /home/runner/... into 639 image
16
+ # macros and 3,470 remark headers across the SRL corpus).
17
+ def self.schema_imagesdir(options, model)
18
+ document = options && options["document"]
19
+ return unless document
20
+ return unless model.respond_to?(:file) && model.file
21
+
22
+ document.path_resolver.system_path(
23
+ File.dirname(model.file),
24
+ File.dirname(document.attributes["docfile"] || "."),
17
25
  )
18
- resolved = document.path_resolver
19
- .system_path(file_path, docfile_directory)
20
- # The decorator expands emitted paths against this base: relative
21
- # prefixes alone get re-anchored by asciidoctor's imagesdir
22
- # handling (images/../../schemas collapses a level), so the
23
- # decoration must produce paths that resolve from the docfile.
24
- options["relative_path_base"] =
25
- File.expand_path(docfile_directory)
26
- # The prefix lands inside emitted image macros, so it must not
27
- # carry the absolute build path: two builds of the same tree in
28
- # two directories produced different image/@src values
29
- # (metanorma/metanorma-plugin-lutaml#325). A path relative to the
30
- # docfile resolves to the same schema file at render time and is
31
- # byte-identical across build directories.
32
- # The docfile directory may be relative ("."): relative_path_from
33
- # refuses mixed prefixes, so both sides expand first
34
- Pathname.new(File.expand_path(resolved))
35
- .relative_path_from(Pathname.new(File.expand_path(docfile_directory)))
36
- .to_s
37
26
  end
38
27
 
39
28
  def self.for(model)
@@ -54,6 +43,44 @@ module Metanorma
54
43
  end
55
44
  end
56
45
 
46
+ # Prints one source format of a decorated schema: the format runs
47
+ # without the promoted prose item (so expressir >= 2.4.59, which
48
+ # formats remark_items, does not double-print it), and the prose
49
+ # block is then spliced into the printed source right after the
50
+ # SCHEMA line — its standard position. When the format's own print
51
+ # already carries the block (the print of remark_items depends on
52
+ # the expressir version), the output stands as printed: the prose
53
+ # shows exactly once either way.
54
+ class ProseSplicer
55
+ def initialize(schema, format, prose, block)
56
+ @schema = schema
57
+ @format = format
58
+ @prose = prose
59
+ @block = block
60
+ @schema_head = %r{
61
+ \ASCHEMA\s+#{Regexp.escape(schema.id.to_s)}[^\n;]*;[^\n]*
62
+ }x
63
+ end
64
+
65
+ def call
66
+ printed = format_without_prose
67
+ return printed if printed.include?(@block)
68
+ return printed unless printed.match?(@schema_head)
69
+
70
+ printed.sub(@schema_head) { |line| "#{line}\n\n#{@block}" }
71
+ end
72
+
73
+ private
74
+
75
+ def format_without_prose
76
+ items = @schema.remark_items
77
+ @schema.remark_items = items - [@prose]
78
+ @format.call.to_s
79
+ ensure
80
+ @schema.remark_items = items
81
+ end
82
+ end
83
+
57
84
  class Base
58
85
  def initialize(model)
59
86
  @model = model
@@ -65,9 +92,7 @@ module Metanorma
65
92
 
66
93
  def decorate_remarks(options)
67
94
  promote_schema_prose
68
- decorate_own_remarks(options)
69
- decorate_child_remark_items(options)
70
- traverse_children(options)
95
+ stash_listing_imagesdir(options)
71
96
  end
72
97
 
73
98
  private
@@ -77,11 +102,13 @@ module Metanorma
77
102
  # renders that walk remark_items (every clause-4 template) drop
78
103
  # it (iso-10303#720). Promote it to a leading RemarkItem keyed
79
104
  # by the schema path, the same way an entity-tagged remark names
80
- # its target. The prose stays in Schema#remarks too; templates
81
- # that already render it there keep working unchanged.
105
+ # its target.
82
106
  #
83
107
  # Since expressir 2.4.59 (expressir#603) the formatters print
84
- # remark_items, so the synthetic item is hidden from them.
108
+ # remark_items, so the synthetic item is hidden from them — and
109
+ # then spliced back into the printed source in its standard
110
+ # position (right after the SCHEMA line), so the source listing
111
+ # shows the prose exactly once, as written.
85
112
  def promote_schema_prose
86
113
  return unless @model.is_a?(Expressir::Model::Declarations::Schema)
87
114
  return if @model.remarks.to_a.empty?
@@ -100,59 +127,56 @@ module Metanorma
100
127
  # Each format is still computed lazily and memoized by expressir;
101
128
  # it just runs without the promoted item in remark_items.
102
129
  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
130
+ blocks = SOURCE_FORMATS.to_h do |name|
131
+ [name, SOURCE_FORMATTERS[name].format(prose)]
114
132
  end
115
- end
116
-
117
- def decorate_own_remarks(options)
118
-
119
- @model.remarks = ExpressRemarksDecorator.decorate_array(
120
- @model.remarks, options
121
- )
122
- end
123
-
124
- def decorate_child_remark_items(options)
125
- return unless @model.is_a?(Expressir::Model::HasRemarkItems)
126
-
127
- @model.remark_items&.each do |ri|
128
- ri.remarks = ExpressRemarksDecorator.decorate_array(
129
- ri.remarks, options
130
- )
133
+ SOURCE_FORMATS.each do |name|
134
+ splice_prose_into_format(name, prose, blocks[name])
131
135
  end
132
136
  end
133
137
 
134
- def traverse_children(options)
135
- @model.children&.each do |child|
136
- next unless traversable?(child)
138
+ def splice_prose_into_format(name, prose, block)
139
+ format = @model.method(name)
140
+ splicer = ProseSplicer.new(@model, format, prose, block)
137
141
 
138
- ExpressRemarkAdapter.for(child).decorate_remarks(options)
139
- end
142
+ @model.define_singleton_method(name) { splicer.call }
140
143
  end
141
144
 
142
- def traversable?(child)
143
- child.is_a?(Expressir::Model::ModelElement) &&
144
- !child.is_a?(Expressir::Model::Declarations::RemarkItem)
145
+ # The per-schema imagesdir for the listing's attribute entries
146
+ # (formatted_hyperlinked_adoc_listing), plus the document's
147
+ # imagesdir at decoration time so the listing can restore it
148
+ # after the schema's content. The values ride on the schema as
149
+ # singleton readers: the liquid drop has no access to the
150
+ # asciidoctor document.
151
+ def stash_listing_imagesdir(options)
152
+ document = options["document"]
153
+ dir = ExpressRemarkAdapter.schema_imagesdir(options, @model)
154
+ previous = document && document.attributes["imagesdir"]
155
+ @model.define_singleton_method(:listing_imagesdir) { dir }
156
+ @model.define_singleton_method(:listing_imagesdir_restore) do
157
+ previous
158
+ end
145
159
  end
146
160
  end
147
161
 
148
162
  class RepoAdapter < Base
149
163
  def decorate_remarks(options)
150
164
  @model.schemas.each do |schema|
151
- options["relative_path_prefix"] =
152
- ExpressRemarkAdapter.relative_path_prefix(options, schema)
153
165
  ExpressRemarkAdapter.for(schema).decorate_remarks(options)
154
166
  end
155
167
  end
168
+
169
+ private
170
+
171
+ # NO document-level imagesdir here: metanorma's figure
172
+ # absolutization joins document.attributes["imagesdir"] with
173
+ # svgmap-synthesized targets that are ALREADY docfile-relative
174
+ # (the express-g assembly writes `../../schemas/...`), doubling
175
+ # the directory segments and failing the image-existence check
176
+ # (suma's dummy-collection repro). The remark content is served
177
+ # by the listing-scoped attribute entries emitted around each
178
+ # source block, which only affect asciidoctor resolution inside
179
+ # the listing.
156
180
  end
157
181
 
158
182
  class CachedRepoAdapter < Base
@@ -174,6 +198,18 @@ module Metanorma
174
198
 
175
199
  def decorate_remarks(_options); end
176
200
  end
201
+
202
+ SOURCE_FORMATS = %i[
203
+ formatted formatted_hyperlinked formatted_hyperlinked_adoc
204
+ ].freeze
205
+
206
+ # The formatter family behind each source format, so the spliced
207
+ # prose prints the way its format prints references.
208
+ SOURCE_FORMATTERS = {
209
+ formatted: Expressir::Express::Formatter,
210
+ formatted_hyperlinked: Expressir::Express::SourceFormatter,
211
+ formatted_hyperlinked_adoc: Expressir::Express::AdocSourceFormatter,
212
+ }.freeze
177
213
  end
178
214
  end
179
215
  end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metanorma
4
+ module Plugin
5
+ module Lutaml
6
+ # The liquid-facing source listing for a schema: a complete
7
+ # asciidoctor source block around Schema#formatted_hyperlinked_adoc
8
+ # (including the schema's own remark, spliced in by the adapter),
9
+ # whose fence is computed from the content so that no dash run
10
+ # inside remark prose can close the listing early — 43 of the 125
11
+ # SRL schema listings close early under the templates' hardcoded
12
+ # two-dash fence — plus the imagesdir attribute entries that let
13
+ # the schema's relative image macros resolve as written, with no
14
+ # build path in the remark text (TRThurman's source-fidelity
15
+ # requirements).
16
+ module ExpressSchemaListing
17
+ SOURCE_BLOCK_HEADING = '[source%unnumbered,subs="+macros"]'
18
+ # A dash run of two or more at line start: only such a run can
19
+ # close a listing fence (asciidoctor closes a listing only on a
20
+ # line of dashes of the SAME length as its opening fence).
21
+ DASH_RUN_REGEXP = /\A(-{2,})/
22
+ # Asciidoctor's shortest valid listing fence: a three-dash line
23
+ # is not a block delimiter at all.
24
+ MIN_FENCE_LENGTH = 4
25
+
26
+ class << self
27
+ # Makes formatted_hyperlinked_adoc_listing available to liquid
28
+ # templates. lutaml-model materializes the schema's drop class
29
+ # from its `liquid` mappings, which cannot be extended
30
+ # declaratively from outside, so the drop gains the method here.
31
+ # Applied before the first template render: update_repo
32
+ # decorates every loaded repository, and Liquid memoizes a
33
+ # drop class's invokable methods on first render.
34
+ def apply
35
+ schema = schema_class
36
+ return unless schema
37
+
38
+ schema.ensure_liquid_registered!
39
+ drop_class = schema.base_drop_class
40
+ return unless drop_class
41
+ return if drop_class.include?(DropMethods)
42
+
43
+ drop_class.include(DropMethods)
44
+ end
45
+
46
+ def fence_length(content)
47
+ longest = content.each_line
48
+ .filter_map { |line| line[DASH_RUN_REGEXP, 1]&.length }
49
+ .max || 2
50
+ [longest + 1, MIN_FENCE_LENGTH].max
51
+ end
52
+
53
+ def fence(content)
54
+ "-" * fence_length(content)
55
+ end
56
+
57
+ private
58
+
59
+ def schema_class
60
+ return unless defined?(::Expressir::Model::Declarations::Schema)
61
+
62
+ ::Expressir::Model::Declarations::Schema
63
+ end
64
+ end
65
+
66
+ # Methods mixed into the schema's liquid drop class. The
67
+ # decorated schema carries its imagesdir values as singleton
68
+ # readers (the drop has no access to the asciidoctor document);
69
+ # attribute entries are emitted immediately before and after the
70
+ # block, since attribute entries take effect sequentially and
71
+ # cannot be changed inside the listing itself.
72
+ module DropMethods
73
+ def formatted_hyperlinked_adoc_listing
74
+ content = @object.formatted_hyperlinked_adoc.to_s
75
+ fence = ExpressSchemaListing.fence(content)
76
+ listing = [
77
+ SOURCE_BLOCK_HEADING,
78
+ fence,
79
+ content,
80
+ fence,
81
+ ].join("\n")
82
+ with_imagesdir_entries(listing)
83
+ end
84
+
85
+ private
86
+
87
+ def with_imagesdir_entries(listing)
88
+ return listing unless @object.respond_to?(:listing_imagesdir)
89
+
90
+ dir = @object.listing_imagesdir
91
+ return listing unless dir
92
+
93
+ restore = @object.listing_imagesdir_restore
94
+ closing = restore ? ":imagesdir: #{restore}" : ":!imagesdir:"
95
+ ":imagesdir: #{dir}\n\n#{listing}\n\n#{closing}"
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -56,6 +56,10 @@ module Metanorma
56
56
  unwrapped = adapter.unwrap
57
57
  return unwrapped unless unwrapped
58
58
 
59
+ # Applied here, before the first template render of this
60
+ # repository: Liquid memoizes a drop class's invokable methods
61
+ # on first render, so the listing method must exist by then.
62
+ ExpressSchemaListing.apply
59
63
  adapter.decorate_remarks(options)
60
64
  unwrapped
61
65
  end
@@ -1,7 +1,7 @@
1
1
  module Metanorma
2
2
  module Plugin
3
3
  module Lutaml
4
- VERSION = "0.7.69".freeze
4
+ VERSION = "0.7.71".freeze
5
5
  end
6
6
  end
7
7
  end
@@ -21,12 +21,12 @@ module Metanorma
21
21
  autoload :DataProviders, "metanorma/plugin/lutaml/data_providers"
22
22
  autoload :Data2TextPreprocessor,
23
23
  "metanorma/plugin/lutaml/data2_text_preprocessor"
24
- autoload :ExpressRemarksDecorator,
25
- "metanorma/plugin/lutaml/express_remarks_decorator"
26
24
  autoload :ExpressRemarkAdapter,
27
25
  "metanorma/plugin/lutaml/express_remark_adapter"
28
26
  autoload :ExpressRepositoryCache,
29
27
  "metanorma/plugin/lutaml/express_repository_cache"
28
+ autoload :ExpressSchemaListing,
29
+ "metanorma/plugin/lutaml/express_schema_listing"
30
30
  autoload :FileNotFoundError,
31
31
  "metanorma/plugin/lutaml/file_not_found_error"
32
32
  autoload :Json2TextPreprocessor,
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.69
4
+ version: 0.7.71
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-15 00:00:00.000000000 Z
11
+ date: 2026-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -465,8 +465,8 @@ files:
465
465
  - lib/metanorma/plugin/lutaml/data_providers.rb
466
466
  - lib/metanorma/plugin/lutaml/express_hotspot_anchor.rb
467
467
  - lib/metanorma/plugin/lutaml/express_remark_adapter.rb
468
- - lib/metanorma/plugin/lutaml/express_remarks_decorator.rb
469
468
  - lib/metanorma/plugin/lutaml/express_repository_cache.rb
469
+ - lib/metanorma/plugin/lutaml/express_schema_listing.rb
470
470
  - lib/metanorma/plugin/lutaml/file_not_found_error.rb
471
471
  - lib/metanorma/plugin/lutaml/gc_budget.rb
472
472
  - lib/metanorma/plugin/lutaml/json2_text_preprocessor.rb
@@ -1,149 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Metanorma
4
- module Plugin
5
- module Lutaml
6
- class ExpressRemarksDecorator
7
- RELATIVE_PREFIX_MACRO_REGEXP = %r{
8
- ^ # Start of line
9
- (link|image|video|audio|include) # Capture group 1: content type
10
- (:+)? # Capture group 2: optional colons
11
- (?! # Negative lookahead
12
- [^/:]+://| # Don't match URLs (http://, etc.)
13
- [A-Z]:/| # Don't match Windows paths
14
- / # Don't match absolute paths
15
- ) # End negative lookahead
16
- ([^:\[]+) # Capture group 3: the path/name
17
- (\[.*\])? # Capture group 4: optional attribute
18
- $ # End of line
19
- }x
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
-
37
- attr_reader :remark, :options
38
-
39
- def self.call(remark, options)
40
- new(remark, options).call
41
- end
42
-
43
- def self.decorate_array(remarks, options)
44
- return [] unless remarks
45
-
46
- remarks.map { |remark| call(remark, options) }
47
- end
48
-
49
- def initialize(remark, options)
50
- @remark = remark
51
- @options = options
52
- end
53
-
54
- def call
55
- result = normalise_dangling_continuations(remark)
56
- result = normalise_glued_continuations(result)
57
- if options["relative_path_prefix"]
58
- result = update_relative_paths(result,
59
- options["relative_path_prefix"])
60
- end
61
- result
62
- end
63
-
64
- private
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
-
111
- def update_relative_paths(string, path_prefix)
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
117
- end
118
- end
119
- string.split("\n").map(&expand_line).join("\n")
120
- end
121
-
122
- def prefix_relative_paths(line, path_prefix)
123
- # Decorated remarks live on models shared across documents and
124
- # preprocess passes: a second pass over an already-decorated line
125
- # would stack the prefix again (paths like
126
- # ../schemas/schemas/schemas/... rendered unusable). Idempotent:
127
- # a line that carries this prefix is left alone.
128
- return line if path_prefix && line.include?(path_prefix)
129
-
130
- line.gsub(RELATIVE_PREFIX_MACRO_REGEXP) do |_match|
131
- # Expand against the docfile directory (passed by the adapter
132
- # as relative_path_base), NOT the process CWD: a CWD expansion
133
- # produced absolute image paths under whatever directory the
134
- # build was launched from (e.g. <repo>/../../schemas/...).
135
- # The absolute result is deliberate: relative targets are
136
- # re-anchored by asciidoctor's imagesdir handling
137
- # (images/../../schemas collapses a level), which made every
138
- # decorated diagram unresolvable at render time.
139
- full_path = File.expand_path(
140
- File.join(path_prefix, $3.strip),
141
- options["relative_path_base"] || Dir.pwd,
142
- )
143
- "#{$1}#{$2}#{full_path}#{$4}"
144
- end
145
- end
146
- end
147
- end
148
- end
149
- end