metanorma 1.7.7 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -1
  3. data/lib/metanorma/array_monkeypatch.rb +9 -0
  4. data/lib/metanorma/asciidoctor_extensions/glob_include_processor.rb +13 -11
  5. data/lib/metanorma/collection/collection.rb +226 -0
  6. data/lib/metanorma/collection/config/bibdata.rb +12 -0
  7. data/lib/metanorma/collection/config/compile_options.rb +13 -0
  8. data/lib/metanorma/collection/config/config.rb +163 -0
  9. data/lib/metanorma/collection/config/converters.rb +30 -0
  10. data/lib/metanorma/collection/config/directive.rb +10 -0
  11. data/lib/metanorma/collection/config/manifest.rb +88 -0
  12. data/lib/metanorma/collection/document/document.rb +133 -0
  13. data/lib/metanorma/collection/filelookup/filelookup.rb +250 -0
  14. data/lib/metanorma/collection/filelookup/filelookup_sectionsplit.rb +87 -0
  15. data/lib/metanorma/collection/manifest/manifest.rb +237 -0
  16. data/lib/metanorma/collection/renderer/fileparse.rb +254 -0
  17. data/lib/metanorma/collection/renderer/fileprocess.rb +174 -0
  18. data/lib/metanorma/collection/renderer/navigation.rb +133 -0
  19. data/lib/metanorma/collection/renderer/render_word.rb +133 -0
  20. data/lib/metanorma/collection/renderer/renderer.rb +157 -0
  21. data/lib/metanorma/collection/renderer/utils.rb +211 -0
  22. data/lib/metanorma/collection/sectionsplit/sectionsplit.rb +219 -0
  23. data/lib/metanorma/collection/util/disambig_files.rb +37 -0
  24. data/lib/metanorma/collection/util/util.rb +72 -0
  25. data/lib/metanorma/collection/xrefprocess/xrefprocess.rb +222 -0
  26. data/lib/metanorma/{compile.rb → compile/compile.rb} +19 -12
  27. data/lib/metanorma/{compile_options.rb → compile/compile_options.rb} +9 -5
  28. data/lib/metanorma/{compile_validate.rb → compile/compile_validate.rb} +1 -1
  29. data/lib/metanorma/{extract.rb → compile/extract.rb} +2 -2
  30. data/lib/metanorma/{config.rb → config/config.rb} +1 -1
  31. data/lib/metanorma/input/asciidoc.rb +3 -3
  32. data/lib/metanorma/input/base.rb +1 -5
  33. data/lib/metanorma/processor/processor.rb +54 -0
  34. data/lib/metanorma/processor.rb +1 -49
  35. data/lib/metanorma/{registry.rb → registry/registry.rb} +0 -1
  36. data/lib/metanorma/shale_monkeypatch.rb +15 -0
  37. data/lib/metanorma/util/fontist_helper.rb +130 -0
  38. data/lib/metanorma/util/util.rb +45 -0
  39. data/lib/metanorma/util/worker_pool.rb +39 -0
  40. data/lib/metanorma/version.rb +1 -1
  41. data/lib/metanorma.rb +13 -8
  42. data/metanorma.gemspec +2 -1
  43. metadata +50 -24
  44. data/lib/metanorma/collection.rb +0 -244
  45. data/lib/metanorma/collection_fileparse.rb +0 -257
  46. data/lib/metanorma/collection_fileprocess.rb +0 -168
  47. data/lib/metanorma/collection_manifest.rb +0 -168
  48. data/lib/metanorma/collection_render_utils.rb +0 -169
  49. data/lib/metanorma/collection_render_word.rb +0 -131
  50. data/lib/metanorma/collection_renderer.rb +0 -237
  51. data/lib/metanorma/collection_xref_process.rb +0 -217
  52. data/lib/metanorma/document.rb +0 -133
  53. data/lib/metanorma/files_lookup.rb +0 -224
  54. data/lib/metanorma/files_lookup_sectionsplit.rb +0 -84
  55. data/lib/metanorma/fontist_utils.rb +0 -122
  56. data/lib/metanorma/sectionsplit.rb +0 -216
  57. data/lib/metanorma/util.rb +0 -127
  58. data/lib/metanorma/worker_pool.rb +0 -29
@@ -0,0 +1,45 @@
1
+ module Metanorma
2
+ module Util
3
+ class << self
4
+ def log(message, type = :info)
5
+ log_types = Metanorma.configuration.logs.map(&:to_s) || []
6
+
7
+ if log_types.include?(type.to_s)
8
+ puts(message)
9
+ end
10
+
11
+ if type == :fatal
12
+ exit(1)
13
+ end
14
+ end
15
+
16
+ # dependency ordering
17
+ def sort_extensions_execution_ord(ext)
18
+ case ext
19
+ when :xml then 0
20
+ when :rxl then 1
21
+ when :presentation then 2
22
+ else
23
+ 99
24
+ end
25
+ end
26
+
27
+ def sort_extensions_execution(ext)
28
+ ext.sort do |a, b|
29
+ sort_extensions_execution_ord(a) <=> sort_extensions_execution_ord(b)
30
+ end
31
+ end
32
+
33
+ def recursive_string_keys(hash)
34
+ case hash
35
+ when Hash then hash.map do |k, v|
36
+ [k.to_s, recursive_string_keys(v)]
37
+ end.to_h
38
+ when Enumerable then hash.map { |v| recursive_string_keys(v) }
39
+ else
40
+ hash
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,39 @@
1
+ module Metanorma
2
+ module Util
3
+ class WorkersPool
4
+ def initialize(workers)
5
+ init_vars(workers)
6
+ @threads = Array.new(@workers) do
7
+ init_thread
8
+ end
9
+ end
10
+
11
+ def init_vars(workers)
12
+ @workers = workers
13
+ @queue = SizedQueue.new(@workers)
14
+ end
15
+
16
+ def init_thread
17
+ Thread.new do
18
+ catch(:exit) do
19
+ loop do
20
+ job, args = @queue.pop
21
+ job.call *args
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def schedule(*args, &block)
28
+ @queue << [block, args]
29
+ end
30
+
31
+ def shutdown
32
+ @workers.times do
33
+ schedule { throw :exit }
34
+ end
35
+ @threads.map(&:join)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module Metanorma
2
- VERSION = "1.7.7".freeze
2
+ VERSION = "2.0.1".freeze
3
3
  end
data/lib/metanorma.rb CHANGED
@@ -1,17 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ class Array
4
+ alias orig_filter filter
5
+ end
6
+
3
7
  require "metanorma/version"
4
8
  require "asciidoctor"
5
- require "metanorma/util"
6
- require "metanorma/config"
9
+ require "metanorma/util/util"
10
+ require "metanorma/config/config"
7
11
  require "metanorma/input"
8
- require "metanorma/registry"
9
- require "metanorma/processor"
12
+ require "metanorma/registry/registry"
13
+ require "metanorma/processor/processor"
10
14
  require "metanorma/asciidoctor_extensions"
11
- require "metanorma/compile"
12
- require "metanorma/collection"
13
- require "metanorma/collection_renderer"
14
- require "metanorma/document"
15
+ require "metanorma/compile/compile"
16
+ require "metanorma/collection/collection"
17
+ require "metanorma/collection/manifest/manifest"
18
+ require "metanorma/collection/renderer/renderer"
19
+ require "metanorma/collection/document/document"
15
20
  require "vectory"
16
21
 
17
22
  # Metanorma module
data/metanorma.gemspec CHANGED
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.add_runtime_dependency "isodoc", ">= 2.6.3"
30
30
  spec.add_runtime_dependency "mn2pdf", "~> 1"
31
31
  spec.add_runtime_dependency "nokogiri"
32
- spec.add_runtime_dependency "pry"
32
+ spec.add_runtime_dependency "shale"
33
33
 
34
34
  # get relaton-cli to avoid circular reference with metanorma-standoc
35
35
  # spec.add_dependency "relaton-cli"
@@ -39,6 +39,7 @@ Gem::Specification.new do |spec|
39
39
  spec.add_development_dependency "equivalent-xml", "~> 0.6"
40
40
  spec.add_development_dependency "metanorma-iso"
41
41
  spec.add_development_dependency "mnconvert"
42
+ spec.add_development_dependency "pry"
42
43
  spec.add_development_dependency "rake", "~> 13.0"
43
44
  spec.add_development_dependency "rspec", "~> 3.0"
44
45
  spec.add_development_dependency "rspec-command", "~> 1.0"
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.7.7
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-06 00:00:00.000000000 Z
11
+ date: 2024-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: pry
98
+ name: shale
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: pry
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: rake
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -268,34 +282,46 @@ files:
268
282
  - LICENSE.txt
269
283
  - README.adoc
270
284
  - lib/metanorma.rb
285
+ - lib/metanorma/array_monkeypatch.rb
271
286
  - lib/metanorma/asciidoctor_extensions.rb
272
287
  - lib/metanorma/asciidoctor_extensions/glob_include_processor.rb
273
- - lib/metanorma/collection.rb
274
- - lib/metanorma/collection_fileparse.rb
275
- - lib/metanorma/collection_fileprocess.rb
276
- - lib/metanorma/collection_manifest.rb
277
- - lib/metanorma/collection_render_utils.rb
278
- - lib/metanorma/collection_render_word.rb
279
- - lib/metanorma/collection_renderer.rb
280
- - lib/metanorma/collection_xref_process.rb
281
- - lib/metanorma/compile.rb
282
- - lib/metanorma/compile_options.rb
283
- - lib/metanorma/compile_validate.rb
284
- - lib/metanorma/config.rb
285
- - lib/metanorma/document.rb
286
- - lib/metanorma/extract.rb
287
- - lib/metanorma/files_lookup.rb
288
- - lib/metanorma/files_lookup_sectionsplit.rb
289
- - lib/metanorma/fontist_utils.rb
288
+ - lib/metanorma/collection/collection.rb
289
+ - lib/metanorma/collection/config/bibdata.rb
290
+ - lib/metanorma/collection/config/compile_options.rb
291
+ - lib/metanorma/collection/config/config.rb
292
+ - lib/metanorma/collection/config/converters.rb
293
+ - lib/metanorma/collection/config/directive.rb
294
+ - lib/metanorma/collection/config/manifest.rb
295
+ - lib/metanorma/collection/document/document.rb
296
+ - lib/metanorma/collection/filelookup/filelookup.rb
297
+ - lib/metanorma/collection/filelookup/filelookup_sectionsplit.rb
298
+ - lib/metanorma/collection/manifest/manifest.rb
299
+ - lib/metanorma/collection/renderer/fileparse.rb
300
+ - lib/metanorma/collection/renderer/fileprocess.rb
301
+ - lib/metanorma/collection/renderer/navigation.rb
302
+ - lib/metanorma/collection/renderer/render_word.rb
303
+ - lib/metanorma/collection/renderer/renderer.rb
304
+ - lib/metanorma/collection/renderer/utils.rb
305
+ - lib/metanorma/collection/sectionsplit/sectionsplit.rb
306
+ - lib/metanorma/collection/util/disambig_files.rb
307
+ - lib/metanorma/collection/util/util.rb
308
+ - lib/metanorma/collection/xrefprocess/xrefprocess.rb
309
+ - lib/metanorma/compile/compile.rb
310
+ - lib/metanorma/compile/compile_options.rb
311
+ - lib/metanorma/compile/compile_validate.rb
312
+ - lib/metanorma/compile/extract.rb
313
+ - lib/metanorma/config/config.rb
290
314
  - lib/metanorma/input.rb
291
315
  - lib/metanorma/input/asciidoc.rb
292
316
  - lib/metanorma/input/base.rb
293
317
  - lib/metanorma/processor.rb
294
- - lib/metanorma/registry.rb
295
- - lib/metanorma/sectionsplit.rb
296
- - lib/metanorma/util.rb
318
+ - lib/metanorma/processor/processor.rb
319
+ - lib/metanorma/registry/registry.rb
320
+ - lib/metanorma/shale_monkeypatch.rb
321
+ - lib/metanorma/util/fontist_helper.rb
322
+ - lib/metanorma/util/util.rb
323
+ - lib/metanorma/util/worker_pool.rb
297
324
  - lib/metanorma/version.rb
298
- - lib/metanorma/worker_pool.rb
299
325
  - metanorma.gemspec
300
326
  homepage: https://github.com/metanorma/metanorma
301
327
  licenses:
@@ -1,244 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "relaton"
4
- require "relaton/cli"
5
- require "metanorma/collection_manifest"
6
- require "metanorma-utils"
7
- require_relative "util"
8
-
9
- module Metanorma
10
- # Metanorma collection of documents
11
- class Collection
12
- attr_reader :file
13
-
14
- # @return [Array<String>] documents-inline to inject the XML into
15
- # the collection manifest; documents-external to keeps them outside
16
- attr_accessor :directives, :documents, :bibdatas, :coverpage, :dirname
17
- attr_accessor :disambig, :manifest
18
-
19
- # @param file [String] path to source file
20
- # @param dirname [String] directory of source file
21
- # @param directives [Array<String>] documents-inline to inject the XML into
22
- # the collection manifest; documents-external to keeps them outside
23
- # @param bibdata [RelatonBib::BibliographicItem]
24
- # @param manifest [Metanorma::CollectionManifest]
25
- # @param documents [Hash<String, Metanorma::Document>]
26
- # @param prefatory [String]
27
- # @param coverpage [String]
28
- # @param final [String]
29
- # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
30
- def initialize(**args)
31
- @file = args[:file]
32
- @dirname = File.dirname(@file)
33
- @directives = args[:directives] || []
34
- @bibdata = args[:bibdata]
35
- @manifest = args[:manifest]
36
- @manifest.collection = self
37
- @coverpage = Util::hash_key_detect(@directives, "coverpage", @coverpage)
38
- @coverpage_style = Util::hash_key_detect(@directives, "coverpage-style",
39
- @coverpage_style)
40
- @documents = args[:documents] || {}
41
- @bibdatas = args[:documents] || {}
42
- if (@documents.any? || @manifest) &&
43
- (%w(documents-inline documents-external) & @directives).empty?
44
- @directives << "documents-inline"
45
- end
46
- @documents.merge! @manifest.documents(@dirname)
47
- @bibdatas.merge! @manifest.documents(@dirname)
48
- @documents.transform_keys { |k| Util::key(k) }
49
- @bibdatas.transform_keys { |k| Util::key(k) }
50
- @prefatory = args[:prefatory]
51
- @final = args[:final]
52
- @compile = Metanorma::Compile.new
53
- @log = Metanorma::Utils::Log.new
54
- @disambig = Util::DisambigFiles.new
55
- end
56
-
57
- # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
58
- def clean_exit
59
- @log.write(File.join(@dirname,
60
- "#{File.basename(@file, '.*')}.err.html"))
61
- end
62
-
63
- # @return [String] XML
64
- def to_xml
65
- b = Nokogiri::XML::Builder.new do |xml|
66
- xml.send(:"metanorma-collection",
67
- "xmlns" => "http://metanorma.org") do |mc|
68
- collection_body(mc)
69
- end
70
- end
71
- b.to_xml
72
- end
73
-
74
- def collection_body(coll)
75
- coll << @bibdata.to_xml(bibdata: true, date_format: :full)
76
- @directives.each do |d|
77
- coll << "<directives>#{obj_to_xml(d)}</directives>"
78
- end
79
- @manifest.to_xml coll
80
- content_to_xml "prefatory", coll
81
- doccontainer coll
82
- content_to_xml "final", coll
83
- end
84
-
85
- def obj_to_xml(elem)
86
- case elem
87
- when ::Array
88
- elem.each_with_object([]) do |v, m|
89
- m << "<value>#{obj_to_xml(v)}</value>"
90
- end.join
91
- when ::Hash
92
- elem.each_with_object([]) do |(k, v), m|
93
- m << "<#{k}>#{obj_to_xml(v)}</#{k}>"
94
- end.join
95
- else elem end
96
- end
97
-
98
- def render(opts)
99
- CollectionRenderer.render self, opts.merge(log: @log)
100
- clean_exit
101
- end
102
-
103
- class << self
104
- # @param file [String]
105
- # @return [RelatonBib::BibliographicItem,
106
- # RelatonIso::IsoBibliographicItem]
107
- def parse(file)
108
- case file
109
- when /\.xml$/ then parse_xml(file)
110
- when /.ya?ml$/ then parse_yaml(file)
111
- end
112
- end
113
-
114
- private
115
-
116
- def parse_xml(file)
117
- xml = Nokogiri::XML(File.read(file, encoding: "UTF-8"), &:huge)
118
- (b = xml.at("/xmlns:metanorma-collection/xmlns:bibdata")) and
119
- bd = Relaton::Cli.parse_xml(b)
120
- mnf_xml = xml.at("/xmlns:metanorma-collection/xmlns:manifest")
121
- mnf = CollectionManifest.from_xml mnf_xml
122
- pref = pref_final_content xml.at("//xmlns:prefatory-content")
123
- fnl = pref_final_content xml.at("//xmlns:final-content")
124
- cov = pref_final_content xml.at("//xmlns:coverpage")
125
- new(file: file, bibdata: bd, manifest: mnf,
126
- directives: directives_from_xml(xml.xpath("//xmlns:directives")),
127
- documents: docs_from_xml(xml, mnf),
128
- bibdatas: docs_from_xml(xml, mnf),
129
- prefatory: pref, final: fnl, coverpage: cov)
130
- end
131
-
132
- # TODO refine
133
- def directives_from_xml(dir)
134
- dir.each_with_object([]) do |d, m|
135
- m << if d.at("./xmlns:value")
136
- x.xpath("./xmlns:value").map(&:text)
137
- elsif d.at("./*")
138
- d.elements.each_with_object({}) do |e, ret|
139
- ret[e.name] = e.children.to_xml
140
- end
141
- else d.children.to_xml
142
- end
143
- end
144
- end
145
-
146
- def parse_yaml(file)
147
- yaml = YAML.load_file file
148
- if yaml["bibdata"]
149
- bd = Relaton::Cli::YAMLConvertor.convert_single_file yaml["bibdata"]
150
- end
151
- mnf = CollectionManifest.from_yaml yaml["manifest"]
152
- dirs = yaml["directives"]
153
- pref = yaml["prefatory-content"]
154
- fnl = yaml["final-content"]
155
- new(file: file, directives: dirs, bibdata: bd, manifest: mnf,
156
- prefatory: pref, final: fnl)
157
- end
158
-
159
- # @param xml [Nokogiri::XML::Document]
160
- # @parma mnf [Metanorma::CollectionManifest]
161
- # @return [Hash{String=>Metanorma::Document}]
162
- def docs_from_xml(xml, mnf)
163
- xml.xpath("//xmlns:doc-container//xmlns:bibdata")
164
- .each_with_object({}) do |b, m|
165
- bd = Relaton::Cli.parse_xml b
166
- docref = mnf.docref_by_id bd.docidentifier.first.id
167
- m[docref["identifier"]] = Document.new bd, docref["fileref"]
168
- m
169
- end
170
- end
171
-
172
- # @param xml [Nokogiri::XML::Element, nil]
173
- # @return [String, nil]
174
- def pref_final_content(xml)
175
- xml or return
176
- <<~CONT
177
-
178
- == #{xml.at('title')&.text}
179
- #{xml.at('p')&.text}
180
- CONT
181
- end
182
- end
183
-
184
- private
185
-
186
- # @return [String, nil]
187
- attr_reader :prefatory, :final
188
-
189
- # @return [String]
190
- def dummy_header
191
- <<~DUMMY
192
- = X
193
- A
194
-
195
- DUMMY
196
- end
197
-
198
- # @param elm [String] 'prefatory' or 'final'
199
- # @param builder [Nokogiri::XML::Builder]
200
- def content_to_xml(elm, builder)
201
- return unless (cnt = send(elm))
202
-
203
- @compile.load_flavor(doctype)
204
- out = sections(dummy_header + cnt.strip)
205
- builder.send("#{elm}-content") { |b| b << out }
206
- end
207
-
208
- # @param cnt [String] prefatory/final content
209
- # @return [String] XML
210
- def sections(cnt)
211
- c = Asciidoctor.convert(cnt, backend: doctype.to_sym, header_footer: true)
212
- Nokogiri::XML(c, &:huge).at("//xmlns:sections").children.to_xml
213
- end
214
-
215
- # @param builder [Nokogiri::XML::Builder]
216
- def doccontainer(builder)
217
- Array(@directives).include? "documents-inline" or return
218
- documents.each_with_index do |(_, d), i|
219
- doccontainer1(builder, d, i)
220
- end
221
- end
222
-
223
- def doccontainer1(builder, doc, idx)
224
- id = format("doc%<index>09d", index: idx)
225
- builder.send(:"doc-container", id: id) do |b|
226
- if doc.attachment
227
- doc.bibitem and b << doc.bibitem.root.to_xml
228
- b.attachment Vectory::Utils::datauri(doc.file)
229
- else doc.to_xml b
230
- end
231
- end
232
- end
233
-
234
- def doctype
235
- @doctype ||= fetch_doctype || "standoc"
236
- end
237
-
238
- def fetch_doctype
239
- docid = @bibdata.docidentifier.first
240
- docid or return
241
- docid.type&.downcase || docid.id&.sub(/\s.*$/, "")&.downcase
242
- end
243
- end
244
- end