metanorma-plugin-lutaml 0.7.68 → 0.7.70
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 +4 -4
- data/lib/metanorma/plugin/lutaml/express_remark_adapter.rb +120 -70
- data/lib/metanorma/plugin/lutaml/express_schema_listing.rb +101 -0
- data/lib/metanorma/plugin/lutaml/lutaml_preprocessor.rb +4 -0
- data/lib/metanorma/plugin/lutaml/version.rb +1 -1
- data/lib/metanorma-plugin-lutaml.rb +2 -2
- metadata +3 -3
- data/lib/metanorma/plugin/lutaml/express_remarks_decorator.rb +0 -128
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e0d3eeed259899046e507228c9896e3956c85441ba7737bc23ba7fec74b5a361
|
|
4
|
+
data.tar.gz: 835d53f863ffd1b13e1e1acbb33d0275e9700f0537d87367b90a55644de90b23
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9c180340bb5cffc26abfac8d900aa38e170a3d72ae14f0d0cdc33c311f305662d3ef02ab6f7ca31890a86991392834be54598761598e213b2b44945eb3985a9f
|
|
7
|
+
data.tar.gz: '0549ff2f14b27184bee416da749d8949442d63eb3644ec9cded55934d9fc16aaa947fd6a0a8ca6c070f06fa18d25718ee4f23c77b45615defdcad46e37406725'
|
|
@@ -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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
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.
|
|
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,58 +127,69 @@ 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
|
-
|
|
104
|
-
|
|
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
|
-
|
|
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
|
|
135
|
-
@model.
|
|
136
|
-
|
|
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
|
-
|
|
139
|
-
end
|
|
142
|
+
@model.define_singleton_method(name) { splicer.call }
|
|
140
143
|
end
|
|
141
144
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
|
167
|
+
apply_document_imagesdir(options["document"])
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
private
|
|
171
|
+
|
|
172
|
+
# A per-schema document (one schema rendered per document —
|
|
173
|
+
# suma's SchemaTemplate::Document) takes a document-level
|
|
174
|
+
# imagesdir: every schema in the repository resolves its images
|
|
175
|
+
# against the same directory, so templates that print the raw
|
|
176
|
+
# formatted_hyperlinked_adoc keep working without path
|
|
177
|
+
# rewriting. An imagesdir the author (or an earlier block) set
|
|
178
|
+
# is respected; a multi-directory repository cannot take one
|
|
179
|
+
# document-level value — its schemas carry the attribute entries
|
|
180
|
+
# in their listing output instead.
|
|
181
|
+
def apply_document_imagesdir(document)
|
|
182
|
+
return unless document
|
|
183
|
+
|
|
184
|
+
dirs = @model.schemas.filter_map do |schema|
|
|
185
|
+
next unless schema.respond_to?(:listing_imagesdir)
|
|
186
|
+
|
|
187
|
+
schema.listing_imagesdir
|
|
188
|
+
end.uniq
|
|
189
|
+
return unless dirs.one?
|
|
190
|
+
return if document.attributes["imagesdir"]
|
|
191
|
+
|
|
192
|
+
document.attributes["imagesdir"] = dirs.first
|
|
155
193
|
end
|
|
156
194
|
end
|
|
157
195
|
|
|
@@ -174,6 +212,18 @@ module Metanorma
|
|
|
174
212
|
|
|
175
213
|
def decorate_remarks(_options); end
|
|
176
214
|
end
|
|
215
|
+
|
|
216
|
+
SOURCE_FORMATS = %i[
|
|
217
|
+
formatted formatted_hyperlinked formatted_hyperlinked_adoc
|
|
218
|
+
].freeze
|
|
219
|
+
|
|
220
|
+
# The formatter family behind each source format, so the spliced
|
|
221
|
+
# prose prints the way its format prints references.
|
|
222
|
+
SOURCE_FORMATTERS = {
|
|
223
|
+
formatted: Expressir::Express::Formatter,
|
|
224
|
+
formatted_hyperlinked: Expressir::Express::SourceFormatter,
|
|
225
|
+
formatted_hyperlinked_adoc: Expressir::Express::AdocSourceFormatter,
|
|
226
|
+
}.freeze
|
|
177
227
|
end
|
|
178
228
|
end
|
|
179
229
|
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
|
|
@@ -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.
|
|
4
|
+
version: 0.7.70
|
|
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-
|
|
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,128 +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
|
-
LIST_ITEM_REGEXP = %r{
|
|
28
|
-
^(?:[-*]|\d+[.)]|[A-Za-z][.)])\s # bullets and enumerations
|
|
29
|
-
| \w[[:alnum:] _-]*::\s # definition lists
|
|
30
|
-
}x
|
|
31
|
-
|
|
32
|
-
attr_reader :remark, :options
|
|
33
|
-
|
|
34
|
-
def self.call(remark, options)
|
|
35
|
-
new(remark, options).call
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def self.decorate_array(remarks, options)
|
|
39
|
-
return [] unless remarks
|
|
40
|
-
|
|
41
|
-
remarks.map { |remark| call(remark, options) }
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def initialize(remark, options)
|
|
45
|
-
@remark = remark
|
|
46
|
-
@options = options
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def call
|
|
50
|
-
result = normalise_dangling_continuations(remark)
|
|
51
|
-
if options["relative_path_prefix"]
|
|
52
|
-
result = update_relative_paths(result,
|
|
53
|
-
options["relative_path_prefix"])
|
|
54
|
-
end
|
|
55
|
-
result
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
private
|
|
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
|
-
|
|
90
|
-
def update_relative_paths(string, path_prefix)
|
|
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
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
string.split("\n").map(&expand_line).join("\n")
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
def prefix_relative_paths(line, path_prefix)
|
|
102
|
-
# Decorated remarks live on models shared across documents and
|
|
103
|
-
# preprocess passes: a second pass over an already-decorated line
|
|
104
|
-
# would stack the prefix again (paths like
|
|
105
|
-
# ../schemas/schemas/schemas/... rendered unusable). Idempotent:
|
|
106
|
-
# a line that carries this prefix is left alone.
|
|
107
|
-
return line if path_prefix && line.include?(path_prefix)
|
|
108
|
-
|
|
109
|
-
line.gsub(RELATIVE_PREFIX_MACRO_REGEXP) do |_match|
|
|
110
|
-
# Expand against the docfile directory (passed by the adapter
|
|
111
|
-
# as relative_path_base), NOT the process CWD: a CWD expansion
|
|
112
|
-
# produced absolute image paths under whatever directory the
|
|
113
|
-
# build was launched from (e.g. <repo>/../../schemas/...).
|
|
114
|
-
# The absolute result is deliberate: relative targets are
|
|
115
|
-
# re-anchored by asciidoctor's imagesdir handling
|
|
116
|
-
# (images/../../schemas collapses a level), which made every
|
|
117
|
-
# decorated diagram unresolvable at render time.
|
|
118
|
-
full_path = File.expand_path(
|
|
119
|
-
File.join(path_prefix, $3.strip),
|
|
120
|
-
options["relative_path_base"] || Dir.pwd,
|
|
121
|
-
)
|
|
122
|
-
"#{$1}#{$2}#{full_path}#{$4}"
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
end
|