metanorma 1.2.2 → 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9718bdc06f44f31d38c70b61597971da40b59aec92c34803fd90dee5a9737ef3
4
- data.tar.gz: b4441a0010cc32bcea52b79533649bdde985f3a18054827f039346c1db0ba296
3
+ metadata.gz: e4552898da027404dc0a7556a0c3b97d8c55c014c8ef397dff1d421f495a748d
4
+ data.tar.gz: a9a7f45f7e35f5a06bdc05c367a8ff3f25369251f5de1993fdbd166604a9349e
5
5
  SHA512:
6
- metadata.gz: ed6c60df3dc293db574f7484e918deda0de754d23696e95834d30856bbc8c6a0917aebf15d0fad56e13bc7bfc5ef133b9554e92a679f949ac6495171bcc77025
7
- data.tar.gz: 5c65c98e7194b3a366977a356d9f44fdbc3f2b2ebf4d871ee76b8f040f7ef824d540eccc82850c1e06908058210ed2cf2f58740388eee29fd63c977b6a80fa78
6
+ metadata.gz: 9be3cea69411bf8f1b48dde44a432353f9b93741ff13ce388312386817e6a8a90a2290dafa222013b7397838cd948b40228882e329bb2d89270924c1b0a08469
7
+ data.tar.gz: 1eff396a38c708948620c489664f8d9713ac0b76e7df72d1631e60e31f1bda0d2aea93ac7015a6a6f7863b9ed466eac731db6ec90fd42ca831d6ffead3f0ecfc
@@ -3,16 +3,16 @@
3
3
  name: notify
4
4
 
5
5
  on:
6
- push:
7
- branches: [ master ]
8
- tags:
9
- - '*'
6
+ repository_dispatch:
7
+ types: [ notify ]
10
8
 
11
9
  jobs:
12
10
  notify:
13
11
  name: Notify dependent repos
14
12
  runs-on: ubuntu-latest
15
13
  steps:
14
+ - uses: actions/checkout@v2
15
+
16
16
  - name: Trigger repositories
17
17
  env:
18
18
  GH_USERNAME: metanorma-ci
@@ -21,7 +21,7 @@ jobs:
21
21
  curl -LO --retry 3 https://raw.githubusercontent.com/metanorma/metanorma-build-scripts/master/trigger-gh-actions.sh
22
22
  [[ -f ".github/workflows/dependent_repos.env" ]] && source .github/workflows/dependent_repos.env
23
23
  CLIENT_PAYLOAD=$(cat <<EOF
24
- "{ "ref": "${GITHUB_REF}", "repo": "${GITHUB_REPOSITORY}" }"
24
+ "{ "ref": "${{ github.event.client_payload.ref }}", "repo": "${GITHUB_REPOSITORY}" }"
25
25
  EOF
26
26
  )
27
27
  for repo in $TEMPLATE_REPOS" $SAMPLES_REPOS"
@@ -30,7 +30,7 @@ jobs:
30
30
  done
31
31
 
32
32
  - name: Trigger release repositories
33
- if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')
33
+ if: github.event.client_payload.ref == 'refs/heads/master' || startsWith(github.event.client_payload.ref, 'refs/tags/v')
34
34
  env:
35
35
  GH_USERNAME: metanorma-ci
36
36
  GH_ACCESS_TOKEN: ${{ secrets.METANORMA_CI_PAT_TOKEN }}
@@ -38,7 +38,7 @@ jobs:
38
38
  curl -LO --retry 3 https://raw.githubusercontent.com/metanorma/metanorma-build-scripts/master/trigger-gh-actions.sh
39
39
  [[ -f ".github/workflows/dependent_repos.env" ]] && source .github/workflows/dependent_repos.env
40
40
  CLIENT_PAYLOAD=$(cat <<EOF
41
- "{ "ref": "${GITHUB_REF}", "repo": "${GITHUB_REPOSITORY}" }"
41
+ "{ "ref": "${{ github.event.client_payload.ref }}", "repo": "${GITHUB_REPOSITORY}" }"
42
42
  EOF
43
43
  )
44
44
  for repo in $DEPENDENT_REPOS
@@ -4,7 +4,7 @@ name: rake
4
4
 
5
5
  on:
6
6
  push:
7
- branches: [ master ]
7
+ branches: [ master, main ]
8
8
  tags: [ v* ]
9
9
  pull_request:
10
10
 
@@ -32,28 +32,29 @@ jobs:
32
32
  steps:
33
33
  - uses: actions/checkout@master
34
34
 
35
- - name: Use Ruby
36
- uses: ruby/setup-ruby@v1
35
+ - uses: ruby/setup-ruby@v1
37
36
  with:
38
37
  ruby-version: ${{ matrix.ruby }}
39
- bundler-cache: true
40
38
 
41
- - name: Update gems
42
- run: bundle install --jobs 4 --retry 3
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
43
48
 
44
- - name: Run specs
45
- run: bundle exec rake
49
+ - run: bundle exec rake
46
50
 
47
- notify:
48
- name: Trigger notify workflow
51
+ tests-passed:
49
52
  needs: rake
50
53
  runs-on: ubuntu-latest
51
54
  steps:
52
- - name: Trigger notify workflow
53
- uses: Sibz/github-status-action@v1
55
+ - uses: peter-evans/repository-dispatch@v1
54
56
  with:
55
- authToken: ${{ secrets.GITHUB_TOKEN }}
56
- context: 'tests-passed-successfully'
57
- description: 'Tests passed successfully'
58
- state: 'success'
59
- sha: ${{ github.event.pull_request.head.sha || github.sha }}
57
+ token: ${{ secrets.GITHUB_TOKEN }}
58
+ repository: ${{ github.repository }}
59
+ event-type: notify
60
+ client-payload: '{"ref": "${{ github.ref }}"}'
@@ -3,6 +3,7 @@
3
3
  require "relaton"
4
4
  require "relaton/cli"
5
5
  require "metanorma/collection_manifest"
6
+ require "metanorma-utils"
6
7
 
7
8
  module Metanorma
8
9
  # Metanorma collection of documents
@@ -39,8 +40,13 @@ module Metanorma
39
40
  @documents.merge! @manifest.documents(File.dirname(@file))
40
41
  @prefatory = args[:prefatory]
41
42
  @final = args[:final]
43
+ @log = Metanorma::Utils::Log.new
42
44
  end
45
+
43
46
  # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
47
+ def clean_exit
48
+ @log.write(File.join(File.dirname(@file), File.basename(@file, ".*") + ".err"))
49
+ end
44
50
 
45
51
  # @return [String] XML
46
52
  def to_xml
@@ -57,7 +63,8 @@ module Metanorma
57
63
  end
58
64
 
59
65
  def render(opts)
60
- CollectionRenderer.render self, opts
66
+ CollectionRenderer.render self, opts.merge(log: @log)
67
+ clean_exit
61
68
  end
62
69
 
63
70
  class << self
@@ -0,0 +1,292 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "isodoc"
4
+ require "metanorma-utils"
5
+
6
+ module Metanorma
7
+ # XML collection renderer
8
+ class CollectionRenderer
9
+ # hash for each document in collection of document identifier to:
10
+ # document reference (fileref or id), type of document reference,
11
+ # and bibdata entry for that file
12
+ # @param path [String] path to collection
13
+ # @return [Hash{String=>Hash}]
14
+ def read_files(path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
15
+ files = {}
16
+ @xml.xpath(ns("//docref")).each do |d|
17
+ identifier = d.at(ns("./identifier")).text
18
+ files[identifier] = if d["fileref"]
19
+ { type: "fileref",
20
+ ref: File.join(path, d["fileref"]) }
21
+ else { type: "id", ref: d["id"] }
22
+ end
23
+ file, _filename = targetfile(files[identifier], true)
24
+ xml = Nokogiri::XML(file)
25
+ add_document_suffix(identifier, xml)
26
+ files[identifier][:anchors] = read_anchors(xml)
27
+ files[identifier][:bibdata] = xml.at(ns("//bibdata"))
28
+ end
29
+ files
30
+ end
31
+
32
+ def add_suffix_to_attributes(doc, suffix, tag_name, attribute_name)
33
+ doc.xpath(ns("//#{tag_name}[@#{attribute_name}]")).each do |elem|
34
+ elem.attributes[attribute_name].value =
35
+ "#{elem.attributes[attribute_name].value}_#{suffix}"
36
+ end
37
+ end
38
+
39
+ def add_document_suffix(identifier, doc)
40
+ document_suffix = Metanorma::Utils::to_ncname(identifier)
41
+ [%w[* id],
42
+ %w[* bibitemid],
43
+ %w[review from],
44
+ %w[review to],
45
+ %w[index to],
46
+ %w[xref target],
47
+ %w[callout target]]
48
+ .each do |(tag_name, attribute_name)|
49
+ add_suffix_to_attributes(doc, document_suffix, tag_name, attribute_name)
50
+ end
51
+ end
52
+
53
+ # map locality type and label (e.g. "clause" "1") to id = anchor for
54
+ # a document
55
+ def read_anchors(xml)
56
+ ret = {}
57
+ xrefs = @isodoc.xref_init(@lang, @script, @isodoc, @isodoc.i18n, {})
58
+ xrefs.parse xml
59
+ xrefs.get.each do |k, v|
60
+ ret[v[:type]] ||= {}
61
+ index = v[:container] || v[:label].nil? || v[:label].empty? ?
62
+ UUIDTools::UUID.random_create.to_s : v[:label]
63
+ # Note: will only key clauses, which have unambiguous reference label in locality.
64
+ # Notes, examples etc with containers are just plunked agaisnt UUIDs, so that their
65
+ # IDs can at least be registered to be tracked as existing.
66
+ ret[v[:type]][index] = k
67
+ end
68
+ ret
69
+ end
70
+
71
+ # return file contents + output filename for each file in the collection,
72
+ # given a docref entry
73
+ # @param data [Hash]
74
+ # @param read [Boolean]
75
+ # @return [Array<String, nil>]
76
+ def targetfile(data, read = false)
77
+ if data[:type] == "fileref" then ref_file data[:ref], read
78
+ else xml_file data[:id], read
79
+ end
80
+ end
81
+
82
+ # @param ref [String]
83
+ # @param read [Boolean]
84
+ # @return [Array<String, nil>]
85
+ def ref_file(ref, read)
86
+ file = File.read(ref, encoding: "utf-8") if read
87
+ filename = ref.sub(/\.xml$/, ".html")
88
+ [file, filename]
89
+ end
90
+
91
+ # @param id [String]
92
+ # @param read [Boolean]
93
+ # @return [Array<String, nil>]
94
+ def xml_file(id, read)
95
+ file = @xml.at(ns("//doc-container[@id = '#{id}']")).to_xml if read
96
+ filename = id + ".html"
97
+ [file, filename]
98
+ end
99
+
100
+ # @param bib [Nokogiri::XML::Element]
101
+ # @param identifier [String]
102
+ def update_bibitem(bib, identifier) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
103
+ docid = bib&.at(ns("./docidentifier"))&.text
104
+ unless @files[docid]
105
+ error = "[metanorma] Cannot find crossreference to document #{docid} in document #{identifier}."
106
+ @log.add("Cross-References", nil, error)
107
+ Util.log(error, :warning)
108
+ return
109
+ end
110
+ id = bib["id"]
111
+ newbib = bib.replace(@files[docid][:bibdata])
112
+ newbib.name = "bibitem"
113
+ newbib["id"] = id
114
+ newbib["hidden"] = "true"
115
+ newbib&.at(ns("./ext"))&.remove
116
+ _file, url = targetfile(@files[docid], false)
117
+ uri_node = Nokogiri::XML::Node.new "uri", newbib
118
+ uri_node[:type] = "citation"
119
+ uri_node.content = url
120
+ newbib.at(ns("./docidentifier")).previous = uri_node
121
+ end
122
+
123
+ # Resolves direct links to other files in collection (repo(current-metanorma-collection/x),
124
+ # and indirect links to other files in collection (bibitem[@type = 'internal'] pointing to a file anchor
125
+ # in another file in the collection)
126
+ # @param file [String] XML content
127
+ # @param identifier [String] docid
128
+ # @param internal_refs [Hash{String=>Hash{String=>String}] schema name to anchor to filename
129
+ # @return [String] XML content
130
+ def update_xrefs(file, identifier, internal_refs)
131
+ docxml = Nokogiri::XML(file)
132
+ update_indirect_refs_to_docs(docxml, internal_refs)
133
+ add_document_suffix(identifier, docxml)
134
+ update_direct_refs_to_docs(docxml, identifier)
135
+ svgmap_resolve(datauri_encode(docxml))
136
+ docxml.xpath(ns("//references[not(./bibitem[not(@hidden) or @hidden = 'false'])]")).each do |f|
137
+ f["hidden"] = "true"
138
+ end
139
+ docxml.to_xml
140
+ end
141
+
142
+ def datauri_encode(docxml)
143
+ docxml.xpath(ns("//image")).each { |i| i["src"] = Metanorma::Utils::datauri(i["src"]) }
144
+ docxml
145
+ end
146
+
147
+ def svgmap_resolve(docxml)
148
+ isodoc = IsoDoc::Convert.new({})
149
+ docxml.xpath(ns("//svgmap//eref")).each do |e|
150
+ href = isodoc.eref_target(e)
151
+ next if href == "#" + e["bibitemid"]
152
+ if href.match(/^#/)
153
+ next unless docxml.at("//*[@id = '#{href.sub(/^#/, '')}']")
154
+ end
155
+ e["target"] = href.strip
156
+ e.name = "link"
157
+ e&.elements&.remove
158
+ end
159
+ Metanorma::Utils::svgmap_rewrite(docxml, "")
160
+ end
161
+
162
+ # repo(current-metanorma-collection/ISO 17301-1:2016)
163
+ # replaced by bibdata of "ISO 17301-1:2016" in situ as bibitem.
164
+ # Any erefs to that bibitem id are replaced with relative URL
165
+ # Preferably with anchor, and is a job to realise dynamic lookup of localities.
166
+ def update_direct_refs_to_docs(docxml, identifier)
167
+ docxml.xpath(ns("//bibitem[not(ancestor::bibitem)]")).each do |b|
168
+ docid = b&.at(ns("./docidentifier[@type = 'repository']"))&.text
169
+ next unless docid && %r{^current-metanorma-collection/}.match(docid)
170
+ update_bibitem(b, identifier)
171
+ update_anchors(b, docxml, docid)
172
+ end
173
+ end
174
+
175
+ # Resolve erefs to a container of ids in another doc, to an anchor eref (direct link)
176
+ def update_indirect_refs_to_docs(docxml, internal_refs)
177
+ internal_refs.each do |schema, ids|
178
+ ids.each do |id, file|
179
+ update_indirect_refs_to_docs1(docxml, schema, id, file)
180
+ end
181
+ end
182
+ end
183
+
184
+ def update_indirect_refs_to_docs1(docxml, schema, id, file)
185
+ docxml.xpath(ns("//eref[@bibitemid = '#{schema}_#{id}']")).each do |e|
186
+ e["citeas"] = file
187
+ end
188
+ docid = docxml.at(ns("//bibitem[@id = '#{schema}_#{id}']/docidentifier[@type = 'repository']")) or return
189
+ docid.children = "current-metanorma-collection/#{file}"
190
+ docid.previous = "<docidentifier type='X'>#{file}</docidentifier>"
191
+ end
192
+
193
+ # update crossrefences to other documents, to include disambiguating document suffix on id
194
+ def update_anchors(bib, docxml, _id) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
195
+ docid = bib&.at(ns("./docidentifier"))&.text
196
+ docxml.xpath("//xmlns:eref[@citeas = '#{docid}']").each do |e|
197
+ if @files[docid]
198
+ update_anchor_loc(bib, e, docid)
199
+ else
200
+ e << "<strong>** Unresolved reference to document #{docid}, id #{e['bibitemid']}</strong>"
201
+ end
202
+ end
203
+ end
204
+
205
+ def update_anchor_loc(bib, e, docid)
206
+ loc = e.at(ns(".//locality[@type = 'anchor']")) or return update_anchor_create_loc(bib, e, docid)
207
+ document_suffix = Metanorma::Utils::to_ncname(docid)
208
+ ref = loc.at(ns("./referenceFrom")) || return
209
+ anchor = "#{ref.text}_#{document_suffix}"
210
+ return unless @files[docid][:anchors].inject([]) { |m, (_, x)| m+= x.values }.include?(anchor)
211
+ ref.content = anchor
212
+ end
213
+
214
+ # if there is a crossref to another document, with no anchor, retrieve the
215
+ # anchor given the locality, and insert it into the crossref
216
+ def update_anchor_create_loc(bib, e, docid)
217
+ ins = e.at(ns("./localityStack")) || return
218
+ type = ins&.at(ns("./locality/@type"))&.text
219
+ ref = ins&.at(ns("./locality/referenceFrom"))&.text
220
+ (anchor = @files[docid][:anchors][type][ref]) || return
221
+ ref_from = Nokogiri::XML::Node.new "referenceFrom", bib
222
+ ref_from.content = anchor.sub(/^_/, "")
223
+ locality = Nokogiri::XML::Node.new "locality", bib
224
+ locality[:type] = "anchor"
225
+ locality.add_child ref_from
226
+ ins << locality
227
+ end
228
+
229
+ # compile and output individual file in collection
230
+ def file_compile(f, filename, identifier)
231
+ # warn "metanorma compile -x html #{f.path}"
232
+ c = Compile.new
233
+ c.compile f.path, { format: :asciidoc, extension_keys: @format }.merge(@compile_options)
234
+ @files[identifier][:outputs] = {}
235
+ @format.each do |e|
236
+ ext = c.processor.output_formats[e]
237
+ fn = File.basename(filename).sub(/(?<=\.)[^\.]+$/, ext.to_s)
238
+ FileUtils.mv f.path.sub(/\.xml$/, ".#{ext}"), File.join(@outdir, fn)
239
+ @files[identifier][:outputs][e] = File.join(@outdir, fn)
240
+ end
241
+ end
242
+
243
+ # gather internal bibitem references
244
+ def gather_internal_refs
245
+ @files.each_with_object({}) do |(identifier, x), refs|
246
+ file, _ = targetfile(x, true)
247
+ Nokogiri::XML(file).xpath(ns("//bibitem[@type = 'internal']/docidentifier[@type = 'repository']")).each do |d|
248
+ a = d.text.split(%r{/}, 2)
249
+ a.size > 1 or next
250
+ refs[a[0]] ||= {}
251
+ refs[a[0]][a[1]] = true
252
+ end
253
+ end
254
+ end
255
+
256
+ # resolve file location for the target of each internal reference
257
+ def locate_internal_refs
258
+ refs = gather_internal_refs
259
+ @files.each do |identifier, x|
260
+ file, filename = targetfile(x, true)
261
+ docxml = Nokogiri::XML(file)
262
+ refs.each do |schema, ids|
263
+ ids.keys.each do |id|
264
+ docxml.at(ns("//*[@id = '#{id}'][@type = '#{schema}']")) and
265
+ refs[schema][id] = identifier
266
+ end
267
+ end
268
+ end
269
+ refs.each do |schema, ids|
270
+ ids.each do |id, key|
271
+ key == true and refs[schema][id] = "Missing:#{schema}:#{id}"
272
+ end
273
+ end
274
+ refs
275
+ end
276
+
277
+ # process each file in the collection
278
+ # files are held in memory, and altered as postprocessing
279
+ def files # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
280
+ internal_refs = locate_internal_refs
281
+ @files.each do |identifier, x|
282
+ file, filename = targetfile(x, true)
283
+ file = update_xrefs(file, identifier, internal_refs)
284
+ Tempfile.open(["collection", ".xml"], encoding: "utf-8") do |f|
285
+ f.write(file)
286
+ f.close
287
+ file_compile(f, filename, identifier)
288
+ end
289
+ end
290
+ end
291
+ end
292
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "isodoc"
4
+ require_relative "./collection_fileprocess"
4
5
 
5
6
  module Metanorma
6
7
  # XML collection renderer
@@ -33,6 +34,8 @@ module Metanorma
33
34
  @outdir = options[:output_folder]
34
35
  @coverpage = options[:coverpage]
35
36
  @format = options[:format]
37
+ @compile_options = options[:compile] || {}
38
+ @log = options[:log]
36
39
 
37
40
  # list of files in the collection
38
41
  @files = read_files folder
@@ -113,64 +116,6 @@ module Metanorma
113
116
  IsoDoc::Convert.new({}).ns(xpath)
114
117
  end
115
118
 
116
- # hash for each document in collection of document identifier to:
117
- # document reference (fileref or id), type of document reference,
118
- # and bibdata entry for that file
119
- # @param path [String] path to collection
120
- # @return [Hash{String=>Hash}]
121
- def read_files(path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
122
- files = {}
123
- @xml.xpath(ns("//docref")).each do |d|
124
- identifier = d.at(ns("./identifier")).text
125
- files[identifier] = if d["fileref"]
126
- { type: "fileref",
127
- ref: File.join(path, d["fileref"]) }
128
- else { type: "id", ref: d["id"] }
129
- end
130
- file, _filename = targetfile(files[identifier], true)
131
- xml = Nokogiri::XML(file)
132
- add_document_suffix(identifier, xml)
133
- files[identifier][:anchors] = read_anchors(xml)
134
- files[identifier][:bibdata] = xml.at(ns("//bibdata"))
135
- end
136
- files
137
- end
138
-
139
- def add_suffix_to_attributes(doc, suffix, tag_name, attribute_name)
140
- doc.xpath(ns("//#{tag_name}[@#{attribute_name}]")).each do |elem|
141
- elem.attributes[attribute_name].value =
142
- "#{elem.attributes[attribute_name].value}_#{suffix}"
143
- end
144
- end
145
-
146
- def add_document_suffix(identifier, doc)
147
- document_suffix = Asciidoctor::Standoc::Cleanup.to_ncname(identifier)
148
- [%w[* id],
149
- %w[* bibitemid],
150
- %w[review from],
151
- %w[review to],
152
- %w[index to],
153
- %w[xref target],
154
- %w[callout target]]
155
- .each do |(tag_name, attribute_name)|
156
- add_suffix_to_attributes(doc, document_suffix, tag_name, attribute_name)
157
- end
158
- end
159
-
160
- # map locality type and label (e.g. "clause" "1") to id = anchor for
161
- # a document
162
- def read_anchors(xml)
163
- ret = {}
164
- xrefs = @isodoc.xref_init(@lang, @script, @isodoc, @isodoc.i18n, {})
165
- xrefs.parse xml
166
- xrefs.get.each do |k, v|
167
- v[:label] && v[:type] || next
168
- ret[v[:type]] ||= {}
169
- ret[v[:type]][v[:label]] = k
170
- end
171
- ret
172
- end
173
-
174
119
  # populate liquid template of ARGV[1] with metadata extracted from
175
120
  # collection manifest
176
121
  def coverpage
@@ -230,125 +175,6 @@ module Metanorma
230
175
  end.doc.root.to_html
231
176
  end
232
177
 
233
- # return file contents + output filename for each file in the collection,
234
- # given a docref entry
235
- # @param data [Hash]
236
- # @param read [Boolean]
237
- # @return [Array<String, nil>]
238
- def targetfile(data, read = false)
239
- if data[:type] == "fileref" then ref_file data[:ref], read
240
- else xml_file data[:id], read
241
- end
242
- end
243
-
244
- # @param ref [String]
245
- # @param read [Boolean]
246
- # @return [Array<String, nil>]
247
- def ref_file(ref, read)
248
- file = File.read(ref, encoding: "utf-8") if read
249
- filename = ref.sub(/\.xml$/, ".html")
250
- [file, filename]
251
- end
252
-
253
- # @param id [String]
254
- # @param read [Boolean]
255
- # @return [Array<String, nil>]
256
- def xml_file(id, read)
257
- file = @xml.at(ns("//doc-container[@id = '#{id}']")).to_xml if read
258
- filename = id + ".html"
259
- [file, filename]
260
- end
261
-
262
- # @param bib [Nokogiri::XML::Element]
263
- # @param identifier [String]
264
- def update_bibitem(bib, identifier) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
265
- docid = bib&.at(ns("./docidentifier"))&.text
266
- unless @files[docid]
267
- warn "Cannot find crossreference to document #{docid} in document "\
268
- "#{identifier}!"
269
- abort
270
- end
271
- id = bib["id"]
272
- newbib = bib.replace(@files[docid][:bibdata])
273
- newbib.name = "bibitem"
274
- newbib["id"] = id
275
- newbib["hidden"] = "true"
276
- newbib&.at(ns("./ext"))&.remove
277
- _file, url = targetfile(@files[docid], false)
278
- uri_node = Nokogiri::XML::Node.new "uri", newbib
279
- uri_node[:type] = "citation"
280
- uri_node.content = url
281
- newbib.at(ns("./docidentifier")).previous = uri_node
282
- end
283
-
284
- # TODO: update crossreferences to other files in the selection
285
- # repo(current-metanorma-collection/ISO 17301-1:2016)
286
- # replaced by
287
- # bibdata of "ISO 17301-1:2016" in situ as bibitem
288
- # Any erefs to that bibitem id are replaced with relative URL
289
- # Preferably with anchor, and is a job to realise dynamic lookup of
290
- # localities
291
- # @param file [String] XML content
292
- # @param identifier [String] docid
293
- # @return [String] XML content
294
- def update_xrefs(file, identifier)
295
- docxml = Nokogiri::XML(file)
296
- add_document_suffix(identifier, docxml)
297
- docxml.xpath(ns("//bibitem[not(ancestor::bibitem)]")).each do |b|
298
- docid = b&.at(ns("./docidentifier[@type = 'repository']"))&.text
299
- next unless docid && %r{^current-metanorma-collection/}.match(docid)
300
-
301
- update_bibitem(b, identifier)
302
- update_anchors(b, docxml, docid)
303
- end
304
- docxml.xpath(ns("//references[not(./bibitem[not(@hidden) or @hidden = 'false'])]")).each do |f|
305
- f["hidden"] = "true"
306
- end
307
- docxml.to_xml
308
- end
309
-
310
- # if there is a crossref to another document, with no anchor, retrieve the
311
- # anchor given the locality, and insert it into the crossref
312
- def update_anchors(bib, docxml, _id) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
313
- docid = bib&.at(ns("./docidentifier"))&.text
314
- docxml.xpath("//xmlns:eref[@citeas = '#{docid}']").each do |e|
315
- e.at(ns(".//locality[@type = 'anchor']")).nil? || next
316
- ins = e.at(ns("./localityStack")) || next
317
- type = ins&.at(ns("./locality/@type"))&.text
318
- ref = ins&.at(ns("./locality/referenceFrom"))&.text
319
- (anchor = @files[docid][:anchors][type][ref]) || next
320
- ref_from = Nokogiri::XML::Node.new "referenceFrom", bib
321
- ref_from.content = anchor.sub(/^_/, "")
322
- locality = Nokogiri::XML::Node.new "locality", bib
323
- locality[:type] = "anchor"
324
- locality.add_child ref_from
325
- ins << locality
326
- end
327
- end
328
-
329
- # process each file in the collection
330
- # files are held in memory, and altered as postprocessing
331
- def files # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
332
- @files.each do |identifier, x|
333
- file, filename = targetfile(x, true)
334
- file = update_xrefs(file, identifier)
335
- Tempfile.open(["collection", ".xml"], encoding: "utf-8") do |f|
336
- f.write(file)
337
- f.close
338
- # warn "metanorma compile -x html #{f.path}"
339
- c = Compile.new
340
- c.compile f.path, format: :asciidoc, extension_keys: @format
341
- @files[identifier][:outputs] = {}
342
- @format.each do |e|
343
- ext = c.processor.output_formats[e]
344
- fn = File.basename(filename).sub(/(?<=\.)[^\.]+$/, ext.to_s)
345
- FileUtils.mv f.path.sub(/\.xml$/, ".#{ext}"), File.join(@outdir, fn)
346
- @files[identifier][:outputs][e] = File.join(@outdir, fn)
347
- end
348
- end
349
- end
350
- end
351
-
352
178
  private
353
179
 
354
180
  # @param options [Hash]
@@ -358,7 +184,6 @@ module Metanorma
358
184
  raise ArgumentError, "Need to specify formats (xml,html,pdf,doc)"
359
185
  end
360
186
  return if !options[:format].include?(:html) || options[:coverpage]
361
-
362
187
  raise ArgumentError, "Need to specify a coverpage to render HTML"
363
188
  end
364
189
  end
@@ -2,6 +2,9 @@ require "fileutils"
2
2
  require "nokogiri"
3
3
  require "htmlentities"
4
4
 
5
+ require "fontist"
6
+ require "fontist/manifest/install"
7
+
5
8
  module Metanorma
6
9
  class Compile
7
10
  # @return [Array<String>]
@@ -21,6 +24,7 @@ module Metanorma
21
24
  (file, isodoc = process_input(filename, options)) or return nil
22
25
  relaton_export(isodoc, options)
23
26
  extract(isodoc, options[:extract], options[:extract_type])
27
+ install_fonts(options)
24
28
  process_extensions(extensions, file, isodoc, options)
25
29
  end
26
30
 
@@ -63,21 +67,32 @@ module Metanorma
63
67
  Util.log("[metanorma] Error: Please specify a standard type: #{@registry.supported_backends}.", :error)
64
68
  return nil
65
69
  end
70
+
66
71
  stdtype = options[:type].to_sym
72
+ metanorma_flavor = "metanorma-#{stdtype}"
73
+
67
74
  unless @registry.supported_backends.include? stdtype
68
- Util.log("[metanorma] Info: Loading `metanorma-#{stdtype}` gem for standard type `#{stdtype}`.", :info)
75
+ Util.log("[metanorma] Info: Loading `#{metanorma_flavor}` gem for standard type `#{stdtype}`.", :info)
69
76
  end
77
+
70
78
  begin
71
79
  require "metanorma-#{stdtype}"
72
- Util.log("[metanorma] Info: gem `metanorma-#{stdtype}` loaded.", :info)
80
+ Util.log("[metanorma] Info: gem `#{metanorma_flavor}` loaded.", :info)
81
+
82
+ rescue Gem::ConflictError
83
+ Util.log("[metanorma] Error: Couldn't resolve dependencies for `metanorma-#{stdtype}`, Please add it to your Gemfile and run bundle install first", :error)
84
+ return false
85
+
73
86
  rescue LoadError
74
- Util.log("[metanorma] Error: loading gem `metanorma-#{stdtype}` failed. Exiting.", :error)
87
+ Util.log("[metanorma] Error: loading gem `#{metanorma_flavor}` failed. Exiting.", :error)
75
88
  return false
76
89
  end
90
+
77
91
  unless @registry.supported_backends.include? stdtype
78
- Util.log("[metanorma] Error: The `metanorma-#{stdtype}` gem still doesn't support `#{stdtype}`. Exiting.", :error)
92
+ Util.log("[metanorma] Error: The `#{metanorma_flavor}` gem still doesn't support `#{stdtype}`. Exiting.", :error)
79
93
  return false
80
94
  end
95
+
81
96
  true
82
97
  end
83
98
 
@@ -246,19 +261,70 @@ module Metanorma
246
261
  @processor.output(isodoc, xml_name, outfilename, ext, isodoc_options)
247
262
  rescue StandardError => e
248
263
  puts e.message
264
+ puts e.backtrace.join("\n")
249
265
  end
250
266
  end
251
267
  wrap_html(options, file_extension, outfilename)
252
268
  end
253
269
  end
254
270
 
271
+ def install_fonts(options)
272
+ if options[:no_install_fonts]
273
+ Util.log("[fontist] Skip font installation because" \
274
+ " --no-install-fonts argument passed", :debug)
275
+ return
276
+ end
277
+
278
+ if !@processor.respond_to?(:fonts_manifest) || @processor.fonts_manifest.nil?
279
+ Util.log("[fontist] Skip font installation because font_manifest is missing", :debug)
280
+ return
281
+ end
282
+
283
+ @updated_formulas_repo = false
284
+
285
+ manifest = @processor.fonts_manifest
286
+ agree_to_terms = options[:agree_to_terms] || false
287
+ continue_without_fonts = options[:continue_without_fonts] || false
288
+
289
+ install_fonts_safe(manifest, agree_to_terms, continue_without_fonts)
290
+ end
291
+
255
292
  private
256
293
 
294
+ def install_fonts_safe(manifest, agree, continue)
295
+ fontist_install(manifest, agree)
296
+ rescue Fontist::Errors::LicensingError
297
+ if continue
298
+ Util.log("[fontist] Processing will continue without fonts installed", :debug)
299
+ else
300
+ Util.log("[fontist] Aborting without proper fonts installed," \
301
+ " make sure that you have set option --agree-to-terms", :fatal)
302
+ end
303
+ rescue Fontist::Errors::FontError => e
304
+ log_level = continue ? :warning : :fatal
305
+ Util.log("[fontist] '#{e.font}' font is not supported. " \
306
+ "Please report this issue at github.com/metanorma/metanorma-#{@processor.short}/issues" \
307
+ " to report this issue.", log_level)
308
+ rescue Fontist::Errors::FormulaIndexNotFoundError
309
+ Util.log("[fontist] Bug: formula index not found after 'fontist update'", :fatal) if @updated_formulas_repo
310
+ Util.log("[fontist] Missing formula index. Fetching it...", :debug)
311
+ Fontist::Formula.update_formulas_repo
312
+ @updated_formulas_repo = true
313
+ install_fonts_safe(manifest, agree, continue)
314
+ end
315
+
316
+ def fontist_install(manifest, agree)
317
+ Fontist::Manifest::Install.from_hash(
318
+ manifest,
319
+ confirmation: agree ? "yes" : "no"
320
+ )
321
+ end
322
+
257
323
  # @param options [Hash]
258
324
  # @return [String]
259
325
  def change_output_dir(options)
260
- if options[:"output-dir"]
261
- File.join options[:"output-dir"], File.basename(options[:filename])
326
+ if options[:output_dir]
327
+ File.join options[:output_dir], File.basename(options[:filename])
262
328
  else options[:filename]
263
329
  end
264
330
  end
@@ -14,7 +14,7 @@ module Metanorma
14
14
  header_footer: true,
15
15
  attributes: [
16
16
  "nodoc", "stem", "xrefstyle=short", "docfile=#{filename}",
17
- "output_dir=#{options[:"output-dir"]}"
17
+ "output_dir=#{options[:output_dir]}"
18
18
  ]
19
19
  }
20
20
  unless asciidoctor_validate(file, filename, out_opts)
@@ -1,3 +1,3 @@
1
1
  module Metanorma
2
- VERSION = "1.2.2"
2
+ VERSION = "1.2.6"
3
3
  end
data/metanorma.gemspec CHANGED
@@ -27,17 +27,19 @@ Gem::Specification.new do |spec|
27
27
  spec.add_runtime_dependency 'htmlentities'
28
28
  spec.add_runtime_dependency 'nokogiri'
29
29
  spec.add_runtime_dependency 'mn2pdf', "~> 1"
30
+ spec.add_runtime_dependency 'metanorma-utils', "~> 1.0.3"
30
31
  spec.add_runtime_dependency 'pry'
31
- # get relaton-cli to avoic circular reference with metanorma-standoc
32
+ spec.add_runtime_dependency 'fontist', '~> 1.8'
33
+
34
+ # get relaton-cli to avoid circular reference with metanorma-standoc
32
35
  #spec.add_dependency "relaton-cli"
33
- #spec.add_dependency "metanorma-standoc", "~> 1.5.3"
36
+ #spec.add_dependency "metanorma-standoc"
34
37
 
35
38
  spec.add_development_dependency "rake", "~> 12.0"
36
39
  spec.add_development_dependency "rspec", "~> 3.0"
37
40
  spec.add_development_dependency "byebug", "~> 10.0"
38
41
  spec.add_development_dependency "rspec-command", "~> 1.0"
39
42
  spec.add_development_dependency "equivalent-xml", "~> 0.6"
40
- spec.add_development_dependency "metanorma-iso", "~> 1.6.0"
43
+ spec.add_development_dependency "metanorma-iso", "~> 1.7.0"
41
44
  spec.add_development_dependency "sassc", "~> 2.4.0"
42
- #spec.add_development_dependency "isodoc", "~> 1.2.1"
43
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metanorma
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-28 00:00:00.000000000 Z
11
+ date: 2021-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: metanorma-utils
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.3
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.3
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: pry
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: fontist
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.8'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.8'
83
111
  - !ruby/object:Gem::Dependency
84
112
  name: rake
85
113
  requirement: !ruby/object:Gem::Requirement
@@ -156,14 +184,14 @@ dependencies:
156
184
  requirements:
157
185
  - - "~>"
158
186
  - !ruby/object:Gem::Version
159
- version: 1.6.0
187
+ version: 1.7.0
160
188
  type: :development
161
189
  prerelease: false
162
190
  version_requirements: !ruby/object:Gem::Requirement
163
191
  requirements:
164
192
  - - "~>"
165
193
  - !ruby/object:Gem::Version
166
- version: 1.6.0
194
+ version: 1.7.0
167
195
  - !ruby/object:Gem::Dependency
168
196
  name: sassc
169
197
  requirement: !ruby/object:Gem::Requirement
@@ -210,6 +238,7 @@ files:
210
238
  - lib/metanorma/asciidoctor_extensions.rb
211
239
  - lib/metanorma/asciidoctor_extensions/glob_include_processor.rb
212
240
  - lib/metanorma/collection.rb
241
+ - lib/metanorma/collection_fileprocess.rb
213
242
  - lib/metanorma/collection_manifest.rb
214
243
  - lib/metanorma/collection_renderer.rb
215
244
  - lib/metanorma/compile.rb
@@ -247,7 +276,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
276
  - !ruby/object:Gem::Version
248
277
  version: '0'
249
278
  requirements: []
250
- rubygems_version: 3.0.3
279
+ rubygems_version: 3.1.4
251
280
  signing_key:
252
281
  specification_version: 4
253
282
  summary: Metanorma is the standard of standards; the metanorma gem allows you to create