metanorma 1.2.6pre → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/rake.yml +10 -17
- data/.gitignore +2 -0
- data/.rubocop.yml +9 -1
- data/lib/metanorma/collection.rb +1 -0
- data/lib/metanorma/collection_fileparse.rb +213 -0
- data/lib/metanorma/collection_fileprocess.rb +42 -191
- data/lib/metanorma/collection_manifest.rb +3 -1
- data/lib/metanorma/collection_renderer.rb +20 -4
- data/lib/metanorma/compile.rb +34 -117
- data/lib/metanorma/compile_validate.rb +45 -0
- data/lib/metanorma/document.rb +5 -3
- data/lib/metanorma/fontist_utils.rb +108 -0
- data/lib/metanorma/input/asciidoc.rb +21 -51
- data/lib/metanorma/version.rb +1 -1
- data/metanorma.gemspec +22 -22
- metadata +54 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e823c389c083ead32851781abb9fa3e28d7744d7fcd3ebacb60b051c118157ed
|
4
|
+
data.tar.gz: 6a3ef75f087f322c0fcf317f72cf5d91c52576b3f322c0935c368e9c74fb7e6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b942d2fadf478c4aa4e8f27437a29a75c37d9515541bc1c13aec1fece10c8d81b07ad8d60cdce455341fc8f65b35d1a53b3d20bf6005281753774e21e1130354
|
7
|
+
data.tar.gz: f744d5316edcd92e46fcfff79128a20353849b8a5d7037cc808afbc39aeb5c1feecb503d3e47fffad4f5fd175129f152be83c5208dc3f8ca771d748d4deb5736
|
data/.github/workflows/rake.yml
CHANGED
@@ -16,35 +16,28 @@ jobs:
|
|
16
16
|
strategy:
|
17
17
|
fail-fast: false
|
18
18
|
matrix:
|
19
|
-
ruby: [ '2.6', '2.5', '2.4' ]
|
19
|
+
ruby: [ '2.7', '2.6', '2.5', '2.4' ]
|
20
20
|
os: [ ubuntu-latest, windows-latest, macos-latest ]
|
21
21
|
experimental: [ false ]
|
22
22
|
include:
|
23
|
-
- ruby: '
|
23
|
+
- ruby: '3.0'
|
24
24
|
os: 'ubuntu-latest'
|
25
25
|
experimental: true
|
26
|
-
- ruby: '
|
26
|
+
- ruby: '3.0'
|
27
27
|
os: 'windows-latest'
|
28
28
|
experimental: true
|
29
|
-
- ruby: '
|
29
|
+
- ruby: '3.0'
|
30
30
|
os: 'macos-latest'
|
31
31
|
experimental: true
|
32
32
|
steps:
|
33
|
-
- uses: actions/checkout@
|
33
|
+
- uses: actions/checkout@v2
|
34
|
+
with:
|
35
|
+
submodules: true
|
34
36
|
|
35
37
|
- uses: ruby/setup-ruby@v1
|
36
38
|
with:
|
37
39
|
ruby-version: ${{ matrix.ruby }}
|
38
|
-
|
39
|
-
- uses: actions/cache@v2
|
40
|
-
with:
|
41
|
-
path: vendor/bundle
|
42
|
-
key: bundle-${{ matrix.os }}-${{ matrix.ruby }}-${{ hashFiles('**/*.gemspec') }}
|
43
|
-
restore-keys: bundle-${{ matrix.os }}-${{ matrix.ruby }}
|
44
|
-
|
45
|
-
- run: bundle config set path 'vendor/bundle'
|
46
|
-
|
47
|
-
- run: bundle install --jobs 4 --retry 3
|
40
|
+
bundler-cache: true
|
48
41
|
|
49
42
|
- run: bundle exec rake
|
50
43
|
|
@@ -54,7 +47,7 @@ jobs:
|
|
54
47
|
steps:
|
55
48
|
- uses: peter-evans/repository-dispatch@v1
|
56
49
|
with:
|
57
|
-
token: ${{ secrets.GITHUB_TOKEN }}
|
50
|
+
token: ${{ secrets.METANORMA_CI_PAT_TOKEN || secrets.GITHUB_TOKEN }}
|
58
51
|
repository: ${{ github.repository }}
|
59
52
|
event-type: notify
|
60
|
-
client-payload: '{"ref": "${{ github.ref }}"}'
|
53
|
+
client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
# This project follows the Ribose OSS style guide.
|
2
2
|
# https://github.com/riboseinc/oss-guides
|
3
3
|
# All project-specific additions and overrides should be specified in this file.
|
4
|
-
|
5
4
|
inherit_from:
|
6
5
|
- https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
|
6
|
+
|
7
|
+
# local repo-specific modifications
|
8
|
+
|
9
|
+
AllCops:
|
10
|
+
DisplayCopNames: false
|
11
|
+
StyleGuideCopsOnly: false
|
12
|
+
TargetRubyVersion: 2.4
|
13
|
+
Rails:
|
14
|
+
Enabled: true
|
data/lib/metanorma/collection.rb
CHANGED
@@ -168,6 +168,7 @@ module Metanorma
|
|
168
168
|
return unless Array(@directives).include? "documents-inline"
|
169
169
|
|
170
170
|
documents.each_with_index do |(_, d), i|
|
171
|
+
next if d.attachment
|
171
172
|
id = format("doc%<index>09d", index: i)
|
172
173
|
builder.send("doc-container", id: id) { |b| d.to_xml b }
|
173
174
|
end
|
@@ -0,0 +1,213 @@
|
|
1
|
+
module Metanorma
|
2
|
+
# XML collection renderer
|
3
|
+
class CollectionRenderer
|
4
|
+
# map locality type and label (e.g. "clause" "1") to id = anchor for
|
5
|
+
# a document
|
6
|
+
# Note: will only key clauses, which have unambiguous reference label in
|
7
|
+
# locality. Notes, examples etc with containers are just plunked against
|
8
|
+
# UUIDs, so that their IDs can at least be registered to be tracked
|
9
|
+
# as existing.
|
10
|
+
def read_anchors(xml)
|
11
|
+
ret = {}
|
12
|
+
xrefs = @isodoc.xref_init(@lang, @script, @isodoc, @isodoc.i18n, {})
|
13
|
+
xrefs.parse xml
|
14
|
+
xrefs.get.each do |k, v|
|
15
|
+
ret[v[:type]] ||= {}
|
16
|
+
index = v[:container] || v[:label].nil? || v[:label].empty? ?
|
17
|
+
UUIDTools::UUID.random_create.to_s : v[:label]
|
18
|
+
ret[v[:type]][index] = k
|
19
|
+
end
|
20
|
+
ret
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param id [String]
|
24
|
+
# @param read [Boolean]
|
25
|
+
# @return [Array<String, nil>]
|
26
|
+
def xml_file(id, read)
|
27
|
+
file = @xml.at(ns("//doc-container[@id = '#{id}']")).to_xml if read
|
28
|
+
filename = "#{id}.html"
|
29
|
+
[file, filename]
|
30
|
+
end
|
31
|
+
|
32
|
+
# @param bib [Nokogiri::XML::Element]
|
33
|
+
# @param identifier [String]
|
34
|
+
def update_bibitem(bib, identifier) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
35
|
+
docid = bib&.at(ns("./docidentifier"))&.text
|
36
|
+
unless @files[docid]
|
37
|
+
error = "[metanorma] Cannot find crossreference to document #{docid} "\
|
38
|
+
"in document #{identifier}."
|
39
|
+
@log.add("Cross-References", nil, error)
|
40
|
+
Util.log(error, :warning)
|
41
|
+
return
|
42
|
+
end
|
43
|
+
id = bib["id"]
|
44
|
+
newbib = bib.replace(@files[docid][:bibdata])
|
45
|
+
newbib.name = "bibitem"
|
46
|
+
newbib["id"] = id
|
47
|
+
newbib["hidden"] = "true"
|
48
|
+
newbib&.at(ns("./ext"))&.remove
|
49
|
+
_file, url = targetfile(@files[docid], false, !@files[docid][:attachment])
|
50
|
+
uri_node = Nokogiri::XML::Node.new "uri", newbib
|
51
|
+
uri_node[:type] = "citation"
|
52
|
+
uri_node.content = url
|
53
|
+
newbib.at(ns("./docidentifier")).previous = uri_node
|
54
|
+
end
|
55
|
+
|
56
|
+
# Resolves direct links to other files in collection
|
57
|
+
# (repo(current-metanorma-collection/x),
|
58
|
+
# and indirect links to other files in collection
|
59
|
+
# (bibitem[@type = 'internal'] pointing to a file anchor
|
60
|
+
# in another file in the collection)
|
61
|
+
# @param file [String] XML content
|
62
|
+
# @param identifier [String] docid
|
63
|
+
# @param internal_refs [Hash{String=>Hash{String=>String}] schema name to anchor to filename
|
64
|
+
# @return [String] XML content
|
65
|
+
def update_xrefs(file, identifier, internal_refs)
|
66
|
+
docxml = Nokogiri::XML(file)
|
67
|
+
update_indirect_refs_to_docs(docxml, internal_refs)
|
68
|
+
add_document_suffix(identifier, docxml)
|
69
|
+
update_direct_refs_to_docs(docxml, identifier)
|
70
|
+
svgmap_resolve(datauri_encode(docxml))
|
71
|
+
docxml.xpath(ns("//references[not(./bibitem[not(@hidden) or @hidden = 'false'])]")).each do |f|
|
72
|
+
f["hidden"] = "true"
|
73
|
+
end
|
74
|
+
docxml.to_xml
|
75
|
+
end
|
76
|
+
|
77
|
+
def datauri_encode(docxml)
|
78
|
+
docxml.xpath(ns("//image")).each do |i|
|
79
|
+
i["src"] = Metanorma::Utils::datauri(i["src"])
|
80
|
+
end
|
81
|
+
docxml
|
82
|
+
end
|
83
|
+
|
84
|
+
def svgmap_resolve(docxml)
|
85
|
+
isodoc = IsoDoc::Convert.new({})
|
86
|
+
docxml.xpath(ns("//svgmap//eref")).each do |e|
|
87
|
+
href = isodoc.eref_target(e)
|
88
|
+
next if href == "##{e['bibitemid']}"
|
89
|
+
|
90
|
+
if href.match(/^#/)
|
91
|
+
next unless docxml.at("//*[@id = '#{href.sub(/^#/, '')}']")
|
92
|
+
end
|
93
|
+
e["target"] = href.strip
|
94
|
+
e.name = "link"
|
95
|
+
e&.elements&.remove
|
96
|
+
end
|
97
|
+
Metanorma::Utils::svgmap_rewrite(docxml, "")
|
98
|
+
end
|
99
|
+
|
100
|
+
# repo(current-metanorma-collection/ISO 17301-1:2016)
|
101
|
+
# replaced by bibdata of "ISO 17301-1:2016" in situ as bibitem.
|
102
|
+
# Any erefs to that bibitem id are replaced with relative URL
|
103
|
+
# Preferably with anchor, and is a job to realise dynamic lookup
|
104
|
+
# of localities.
|
105
|
+
def update_direct_refs_to_docs(docxml, identifier)
|
106
|
+
docxml.xpath(ns("//bibitem[not(ancestor::bibitem)]")).each do |b|
|
107
|
+
docid = b&.at(ns("./docidentifier[@type = 'repository']"))&.text
|
108
|
+
next unless docid && %r{^current-metanorma-collection/}.match(docid)
|
109
|
+
update_bibitem(b, identifier)
|
110
|
+
update_anchors(b, docxml, docid)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Resolve erefs to a container of ids in another doc,
|
115
|
+
# to an anchor eref (direct link)
|
116
|
+
def update_indirect_refs_to_docs(docxml, internal_refs)
|
117
|
+
internal_refs.each do |schema, ids|
|
118
|
+
ids.each do |id, file|
|
119
|
+
update_indirect_refs_to_docs1(docxml, schema, id, file)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def update_indirect_refs_to_docs1(docxml, schema, id, file)
|
125
|
+
docxml.xpath(ns("//eref[@bibitemid = '#{schema}_#{id}']")).each do |e|
|
126
|
+
e["citeas"] = file
|
127
|
+
end
|
128
|
+
docid = docxml.at(ns("//bibitem[@id = '#{schema}_#{id}']/"\
|
129
|
+
"docidentifier[@type = 'repository']")) or return
|
130
|
+
docid.children = "current-metanorma-collection/#{file}"
|
131
|
+
docid.previous = "<docidentifier type='X'>#{file}</docidentifier>"
|
132
|
+
end
|
133
|
+
|
134
|
+
# update crossrefences to other documents, to include disambiguating document suffix on id
|
135
|
+
def update_anchors(bib, docxml, _id) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
136
|
+
docid = bib&.at(ns("./docidentifier"))&.text
|
137
|
+
docxml.xpath("//xmlns:eref[@citeas = '#{docid}']").each do |e|
|
138
|
+
if @files[docid]
|
139
|
+
update_anchor_loc(bib, e, docid)
|
140
|
+
else
|
141
|
+
e << "<strong>** Unresolved reference to document #{docid}, id #{e['bibitemid']}</strong>"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def update_anchor_loc(bib, e, docid)
|
147
|
+
loc = e.at(ns(".//locality[@type = 'anchor']")) or
|
148
|
+
return update_anchor_create_loc(bib, e, docid)
|
149
|
+
document_suffix = Metanorma::Utils::to_ncname(docid)
|
150
|
+
ref = loc.at(ns("./referenceFrom")) || return
|
151
|
+
anchor = "#{ref.text}_#{document_suffix}"
|
152
|
+
return unless @files[docid][:anchors].inject([]) do |m, (_, x)|
|
153
|
+
m+= x.values
|
154
|
+
end.include?(anchor)
|
155
|
+
ref.content = anchor
|
156
|
+
end
|
157
|
+
|
158
|
+
# if there is a crossref to another document, with no anchor, retrieve the
|
159
|
+
# anchor given the locality, and insert it into the crossref
|
160
|
+
def update_anchor_create_loc(bib, e, docid)
|
161
|
+
ins = e.at(ns("./localityStack")) || return
|
162
|
+
type = ins&.at(ns("./locality/@type"))&.text
|
163
|
+
ref = ins&.at(ns("./locality/referenceFrom"))&.text
|
164
|
+
(anchor = @files[docid][:anchors][type][ref]) || return
|
165
|
+
ref_from = Nokogiri::XML::Node.new "referenceFrom", bib
|
166
|
+
ref_from.content = anchor.sub(/^_/, "")
|
167
|
+
locality = Nokogiri::XML::Node.new "locality", bib
|
168
|
+
locality[:type] = "anchor"
|
169
|
+
locality.add_child ref_from
|
170
|
+
ins << locality
|
171
|
+
end
|
172
|
+
|
173
|
+
# gather internal bibitem references
|
174
|
+
def gather_internal_refs
|
175
|
+
@files.each_with_object({}) do |(_, x), refs|
|
176
|
+
next if x[:attachment]
|
177
|
+
file, _ = targetfile(x, true)
|
178
|
+
Nokogiri::XML(file)
|
179
|
+
.xpath(ns("//bibitem[@type = 'internal']/"\
|
180
|
+
"docidentifier[@type = 'repository']")).each do |d|
|
181
|
+
a = d.text.split(%r{/}, 2)
|
182
|
+
a.size > 1 or next
|
183
|
+
refs[a[0]] ||= {}
|
184
|
+
refs[a[0]][a[1]] = true
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
# resolve file location for the target of each internal reference
|
190
|
+
def locate_internal_refs
|
191
|
+
refs = gather_internal_refs
|
192
|
+
@files.each do |identifier, x|
|
193
|
+
next if x[:attachment]
|
194
|
+
|
195
|
+
file, _filename = targetfile(x, true)
|
196
|
+
docxml = Nokogiri::XML(file)
|
197
|
+
refs.each do |schema, ids|
|
198
|
+
ids.each_key do |id|
|
199
|
+
n = docxml.at("//*[@id = '#{id}']") and
|
200
|
+
n.at("./ancestor-or-self::*[@type = '#{schema}']") and
|
201
|
+
refs[schema][id] = identifier
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
refs.each do |schema, ids|
|
206
|
+
ids.each do |id, key|
|
207
|
+
key == true and refs[schema][id] = "Missing:#{schema}:#{id}"
|
208
|
+
end
|
209
|
+
end
|
210
|
+
refs
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "isodoc"
|
4
|
+
require "metanorma-utils"
|
5
|
+
require_relative "collection_fileparse"
|
4
6
|
|
5
7
|
module Metanorma
|
6
8
|
# XML collection renderer
|
@@ -14,11 +16,9 @@ module Metanorma
|
|
14
16
|
files = {}
|
15
17
|
@xml.xpath(ns("//docref")).each do |d|
|
16
18
|
identifier = d.at(ns("./identifier")).text
|
17
|
-
files[identifier] =
|
18
|
-
|
19
|
-
|
20
|
-
else { type: "id", ref: d["id"] }
|
21
|
-
end
|
19
|
+
files[identifier] = file_entry(d, path)
|
20
|
+
next if files[identifier][:attachment]
|
21
|
+
|
22
22
|
file, _filename = targetfile(files[identifier], true)
|
23
23
|
xml = Nokogiri::XML(file)
|
24
24
|
add_document_suffix(identifier, xml)
|
@@ -28,6 +28,17 @@ module Metanorma
|
|
28
28
|
files
|
29
29
|
end
|
30
30
|
|
31
|
+
def file_entry(docref, path)
|
32
|
+
ret = if docref["fileref"]
|
33
|
+
{ type: "fileref", ref: File.join(path, docref["fileref"]),
|
34
|
+
rel_path: docref["fileref"] }
|
35
|
+
else
|
36
|
+
{ type: "id", ref: docref["id"] }
|
37
|
+
end
|
38
|
+
ret[:attachment] = docref["attachment"] if docref["attachment"]
|
39
|
+
ret
|
40
|
+
end
|
41
|
+
|
31
42
|
def add_suffix_to_attributes(doc, suffix, tag_name, attribute_name)
|
32
43
|
doc.xpath(ns("//#{tag_name}[@#{attribute_name}]")).each do |elem|
|
33
44
|
elem.attributes[attribute_name].value =
|
@@ -37,178 +48,42 @@ module Metanorma
|
|
37
48
|
|
38
49
|
def add_document_suffix(identifier, doc)
|
39
50
|
document_suffix = Metanorma::Utils::to_ncname(identifier)
|
40
|
-
[%w[* id],
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
%w[index to],
|
45
|
-
%w[xref target],
|
46
|
-
%w[callout target]]
|
47
|
-
.each do |(tag_name, attribute_name)|
|
51
|
+
[%w[* id], %w[* bibitemid], %w[review from],
|
52
|
+
%w[review to], %w[index to], %w[xref target],
|
53
|
+
%w[callout target]]
|
54
|
+
.each do |(tag_name, attribute_name)|
|
48
55
|
add_suffix_to_attributes(doc, document_suffix, tag_name, attribute_name)
|
49
56
|
end
|
50
57
|
end
|
51
58
|
|
52
|
-
# map locality type and label (e.g. "clause" "1") to id = anchor for
|
53
|
-
# a document
|
54
|
-
def read_anchors(xml)
|
55
|
-
ret = {}
|
56
|
-
xrefs = @isodoc.xref_init(@lang, @script, @isodoc, @isodoc.i18n, {})
|
57
|
-
xrefs.parse xml
|
58
|
-
xrefs.get.each do |k, v|
|
59
|
-
ret[v[:type]] ||= {}
|
60
|
-
index = v[:container] || v[:label].nil? || v[:label].empty? ?
|
61
|
-
UUIDTools::UUID.random_create.to_s : v[:label]
|
62
|
-
# Note: will only key clauses, which have unambiguous reference label in locality.
|
63
|
-
# Notes, examples etc with containers are just plunked agaisnt UUIDs, so that their
|
64
|
-
# IDs can at least be registered to be tracked as existing.
|
65
|
-
ret[v[:type]][index] = k
|
66
|
-
end
|
67
|
-
ret
|
68
|
-
end
|
69
|
-
|
70
59
|
# return file contents + output filename for each file in the collection,
|
71
60
|
# given a docref entry
|
72
61
|
# @param data [Hash]
|
73
62
|
# @param read [Boolean]
|
74
63
|
# @return [Array<String, nil>]
|
75
|
-
def targetfile(data, read = false)
|
76
|
-
if data[:type] == "fileref" then ref_file data[:ref], read
|
64
|
+
def targetfile(data, read = false, doc = true)
|
65
|
+
if data[:type] == "fileref" then ref_file data[:ref], read, doc
|
77
66
|
else xml_file data[:id], read
|
78
67
|
end
|
79
68
|
end
|
80
69
|
|
81
70
|
# @param ref [String]
|
82
71
|
# @param read [Boolean]
|
72
|
+
# @param doc [Boolean]
|
83
73
|
# @return [Array<String, nil>]
|
84
|
-
def ref_file(ref, read)
|
74
|
+
def ref_file(ref, read, doc)
|
85
75
|
file = File.read(ref, encoding: "utf-8") if read
|
86
|
-
filename = ref.
|
87
|
-
|
88
|
-
end
|
89
|
-
|
90
|
-
# @param id [String]
|
91
|
-
# @param read [Boolean]
|
92
|
-
# @return [Array<String, nil>]
|
93
|
-
def xml_file(id, read)
|
94
|
-
file = @xml.at(ns("//doc-container[@id = '#{id}']")).to_xml if read
|
95
|
-
filename = id + ".html"
|
76
|
+
filename = ref.dup
|
77
|
+
filename.sub!(/\.xml$/, ".html") if doc
|
96
78
|
[file, filename]
|
97
79
|
end
|
98
80
|
|
99
|
-
# @param bib [Nokogiri::XML::Element]
|
100
|
-
# @param identifier [String]
|
101
|
-
def update_bibitem(bib, identifier) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
102
|
-
docid = bib&.at(ns("./docidentifier"))&.text
|
103
|
-
unless @files[docid]
|
104
|
-
error = "[metanorma] Cannot find crossreference to document #{docid} in document #{identifier}."
|
105
|
-
@log.add("Cross-References", nil, error)
|
106
|
-
Util.log(error, :warning)
|
107
|
-
return
|
108
|
-
end
|
109
|
-
id = bib["id"]
|
110
|
-
newbib = bib.replace(@files[docid][:bibdata])
|
111
|
-
newbib.name = "bibitem"
|
112
|
-
newbib["id"] = id
|
113
|
-
newbib["hidden"] = "true"
|
114
|
-
newbib&.at(ns("./ext"))&.remove
|
115
|
-
_file, url = targetfile(@files[docid], false)
|
116
|
-
uri_node = Nokogiri::XML::Node.new "uri", newbib
|
117
|
-
uri_node[:type] = "citation"
|
118
|
-
uri_node.content = url
|
119
|
-
newbib.at(ns("./docidentifier")).previous = uri_node
|
120
|
-
end
|
121
|
-
|
122
|
-
# Resolves direct links to other files in collection (repo(current-metanorma-collection/x),
|
123
|
-
# and indirect links to other files in collection (bibitem[@type = 'internal'] pointing to a file anchor
|
124
|
-
# in another file in the collection)
|
125
|
-
# @param file [String] XML content
|
126
|
-
# @param identifier [String] docid
|
127
|
-
# @param internal_refs [Hash{String=>Hash{String=>String}] schema name to anchor to filename
|
128
|
-
# @return [String] XML content
|
129
|
-
def update_xrefs(file, identifier, internal_refs)
|
130
|
-
docxml = Nokogiri::XML(file)
|
131
|
-
update_indirect_refs_to_docs(docxml, internal_refs)
|
132
|
-
add_document_suffix(identifier, docxml)
|
133
|
-
update_direct_refs_to_docs(docxml, identifier)
|
134
|
-
docxml.xpath(ns("//references[not(./bibitem[not(@hidden) or @hidden = 'false'])]")).each do |f|
|
135
|
-
f["hidden"] = "true"
|
136
|
-
end
|
137
|
-
docxml.to_xml
|
138
|
-
end
|
139
|
-
|
140
|
-
# repo(current-metanorma-collection/ISO 17301-1:2016)
|
141
|
-
# replaced by bibdata of "ISO 17301-1:2016" in situ as bibitem.
|
142
|
-
# Any erefs to that bibitem id are replaced with relative URL
|
143
|
-
# Preferably with anchor, and is a job to realise dynamic lookup of localities.
|
144
|
-
def update_direct_refs_to_docs(docxml, identifier)
|
145
|
-
docxml.xpath(ns("//bibitem[not(ancestor::bibitem)]")).each do |b|
|
146
|
-
docid = b&.at(ns("./docidentifier[@type = 'repository']"))&.text
|
147
|
-
next unless docid && %r{^current-metanorma-collection/}.match(docid)
|
148
|
-
update_bibitem(b, identifier)
|
149
|
-
update_anchors(b, docxml, docid)
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
# Resolve erefs to a container of ids in another doc, to an anchor eref (direct link)
|
154
|
-
def update_indirect_refs_to_docs(docxml, internal_refs)
|
155
|
-
internal_refs.each do |schema, ids|
|
156
|
-
ids.each do |id, file|
|
157
|
-
update_indirect_refs_to_docs1(docxml, schema, id, file)
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
def update_indirect_refs_to_docs1(docxml, schema, id, file)
|
163
|
-
docxml.xpath(ns("//eref[@bibitemid = '#{schema}_#{id}']")).each do |e|
|
164
|
-
e["citeas"] = file
|
165
|
-
end
|
166
|
-
docid = docxml.at(ns("//bibitem[@id = '#{schema}_#{id}']/docidentifier[@type = 'repository']")) or return
|
167
|
-
docid.children = "current-metanorma-collection/#{file}"
|
168
|
-
docid.previous = "<docidentifier type='X'>#{file}</docidentifier>"
|
169
|
-
end
|
170
|
-
|
171
|
-
# update crossrefences to other documents, to include disambiguating document suffix on id
|
172
|
-
def update_anchors(bib, docxml, _id) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
173
|
-
docid = bib&.at(ns("./docidentifier"))&.text
|
174
|
-
docxml.xpath("//xmlns:eref[@citeas = '#{docid}']").each do |e|
|
175
|
-
if @files[docid]
|
176
|
-
update_anchor_loc(bib, e, docid)
|
177
|
-
else
|
178
|
-
e << "<strong>** Unresolved reference to document #{docid}, id #{e['bibitemid']}</strong>"
|
179
|
-
end
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
def update_anchor_loc(bib, e, docid)
|
184
|
-
loc = e.at(ns(".//locality[@type = 'anchor']")) or return update_anchor_create_loc(bib, e, docid)
|
185
|
-
document_suffix = Metanorma::Utils::to_ncname(docid)
|
186
|
-
ref = loc.at(ns("./referenceFrom")) || return
|
187
|
-
anchor = "#{ref.text}_#{document_suffix}"
|
188
|
-
return unless @files[docid][:anchors].inject([]) { |m, (_, x)| m+= x.values }.include?(anchor)
|
189
|
-
ref.content = anchor
|
190
|
-
end
|
191
|
-
|
192
|
-
# if there is a crossref to another document, with no anchor, retrieve the
|
193
|
-
# anchor given the locality, and insert it into the crossref
|
194
|
-
def update_anchor_create_loc(bib, e, docid)
|
195
|
-
ins = e.at(ns("./localityStack")) || return
|
196
|
-
type = ins&.at(ns("./locality/@type"))&.text
|
197
|
-
ref = ins&.at(ns("./locality/referenceFrom"))&.text
|
198
|
-
(anchor = @files[docid][:anchors][type][ref]) || return
|
199
|
-
ref_from = Nokogiri::XML::Node.new "referenceFrom", bib
|
200
|
-
ref_from.content = anchor.sub(/^_/, "")
|
201
|
-
locality = Nokogiri::XML::Node.new "locality", bib
|
202
|
-
locality[:type] = "anchor"
|
203
|
-
locality.add_child ref_from
|
204
|
-
ins << locality
|
205
|
-
end
|
206
|
-
|
207
81
|
# compile and output individual file in collection
|
208
82
|
def file_compile(f, filename, identifier)
|
209
83
|
# warn "metanorma compile -x html #{f.path}"
|
210
84
|
c = Compile.new
|
211
|
-
c.compile f.path, { format: :asciidoc,
|
85
|
+
c.compile f.path, { format: :asciidoc,
|
86
|
+
extension_keys: @format }.merge(@compile_options)
|
212
87
|
@files[identifier][:outputs] = {}
|
213
88
|
@format.each do |e|
|
214
89
|
ext = c.processor.output_formats[e]
|
@@ -218,38 +93,11 @@ module Metanorma
|
|
218
93
|
end
|
219
94
|
end
|
220
95
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
a = d.text.split(%r{/}, 2)
|
227
|
-
a.size > 1 or next
|
228
|
-
refs[a[0]] ||= {}
|
229
|
-
refs[a[0]][a[1]] = true
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
# resolve file location for the target of each internal reference
|
235
|
-
def locate_internal_refs
|
236
|
-
refs = gather_internal_refs
|
237
|
-
@files.each do |identifier, x|
|
238
|
-
file, filename = targetfile(x, true)
|
239
|
-
docxml = Nokogiri::XML(file)
|
240
|
-
refs.each do |schema, ids|
|
241
|
-
ids.keys.each do |id|
|
242
|
-
docxml.at(ns("//*[@id = '#{id}'][@type = '#{schema}']")) and
|
243
|
-
refs[schema][id] = identifier
|
244
|
-
end
|
245
|
-
end
|
246
|
-
end
|
247
|
-
refs.each do |schema, ids|
|
248
|
-
ids.each do |id, key|
|
249
|
-
key == true and refs[schema][id] = "Missing:#{schema}:#{id}"
|
250
|
-
end
|
251
|
-
end
|
252
|
-
refs
|
96
|
+
def copy_file_to_dest(fileref)
|
97
|
+
_file, filename = targetfile(fileref, true, false)
|
98
|
+
dest = File.join(@outdir, fileref[:rel_path])
|
99
|
+
FileUtils.mkdir_p(File.dirname(dest))
|
100
|
+
FileUtils.cp filename, dest
|
253
101
|
end
|
254
102
|
|
255
103
|
# process each file in the collection
|
@@ -257,12 +105,15 @@ module Metanorma
|
|
257
105
|
def files # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
258
106
|
internal_refs = locate_internal_refs
|
259
107
|
@files.each do |identifier, x|
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
f
|
265
|
-
|
108
|
+
if x[:attachment] then copy_file_to_dest(x)
|
109
|
+
else
|
110
|
+
file, filename = targetfile(x, true)
|
111
|
+
file = update_xrefs(file, identifier, internal_refs)
|
112
|
+
Tempfile.open(["collection", ".xml"], encoding: "utf-8") do |f|
|
113
|
+
f.write(file)
|
114
|
+
f.close
|
115
|
+
file_compile(f, filename, identifier)
|
116
|
+
end
|
266
117
|
end
|
267
118
|
end
|
268
119
|
end
|