metanorma 1.7.6 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) 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 +225 -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 +247 -0
  17. data/lib/metanorma/collection/renderer/fileprocess.rb +173 -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 +183 -0
  22. data/lib/metanorma/collection/sectionsplit/sectionsplit.rb +218 -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} +17 -11
  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 +51 -26
  44. data/Gemfile.devel +0 -2
  45. data/lib/metanorma/collection.rb +0 -243
  46. data/lib/metanorma/collection_fileparse.rb +0 -254
  47. data/lib/metanorma/collection_fileprocess.rb +0 -157
  48. data/lib/metanorma/collection_manifest.rb +0 -139
  49. data/lib/metanorma/collection_render_utils.rb +0 -169
  50. data/lib/metanorma/collection_render_word.rb +0 -131
  51. data/lib/metanorma/collection_renderer.rb +0 -230
  52. data/lib/metanorma/document.rb +0 -133
  53. data/lib/metanorma/files_lookup.rb +0 -208
  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/sectionsplit_links.rb +0 -189
  58. data/lib/metanorma/util.rb +0 -127
  59. data/lib/metanorma/worker_pool.rb +0 -29
@@ -1,189 +0,0 @@
1
- module Metanorma
2
- class Sectionsplit
3
- def xref_preprocess(xml, _fileslookup, _identifier)
4
- key = (0...8).map { rand(65..90).chr }.join # random string
5
- xml.root["type"] = key # to force recognition of internal refs
6
- # bookmarks etc as new id elements introduced in Presentation XML:
7
- # add doc suffix
8
- Metanorma::Utils::anchor_attributes.each do |(tag_name, attribute_name)|
9
- Util::add_suffix_to_attributes(xml, xml.root["document_suffix"],
10
- tag_name, attribute_name, @isodoc)
11
- end
12
- key
13
- end
14
-
15
- def xref_process(section, xml, key)
16
- svg_preprocess(section, Metanorma::Utils::to_ncname(@ident))
17
- refs = eref_to_internal_eref(section, xml, key)
18
- refs += xref_to_internal_eref(section, xml, key)
19
- ins = new_hidden_ref(section)
20
- copied_refs = copy_repo_items_biblio(ins, section, xml)
21
- insert_indirect_biblio(ins, refs - copied_refs, key, xml)
22
- end
23
-
24
- def svg_preprocess(xml, doc_suffix)
25
- suffix = doc_suffix.nil? || doc_suffix.blank? ? "" : "_#{doc_suffix}"
26
- xml.xpath("//m:svg", "m" => "http://www.w3.org/2000/svg").each do |s|
27
- m = svgmap_wrap(s)
28
- svg_xrefs(s, m, suffix)
29
- end
30
- xml
31
- end
32
-
33
- def svgmap_wrap(svg)
34
- ret = svg.at("./ancestor::xmlns:svgmap") and return ret
35
- ret = svg.at("./ancestor::xmlns:figure")
36
- ret.wrap("<svgmap/>")
37
- svg.at("./ancestor::xmlns:svgmap")
38
- end
39
-
40
- def svg_xrefs(svg, svgmap, suffix)
41
- svg.xpath(".//m:a", "m" => "http://www.w3.org/2000/svg").each do |a|
42
- /^#/.match? a["href"] or next
43
- a["href"] = a["href"].sub(/^#/, "")
44
- svgmap << "<target href='#{a['href']}'>" \
45
- "<xref target='#{a['href']}#{suffix}'/></target>"
46
- end
47
- end
48
-
49
- def make_anchor(anchor)
50
- "<localityStack><locality type='anchor'><referenceFrom>" \
51
- "#{anchor}</referenceFrom></locality></localityStack>"
52
- end
53
-
54
- def xref_to_internal_eref(section, xml, key)
55
- bibitems, indirect = xref_to_internal_eref_prep(section, xml)
56
- section.xpath(ns("//xref")).each_with_object({}) do |x, m|
57
- xref_prefix_key(x, key, indirect)
58
- x["bibitemid"] = x["target"]
59
- m[x["bibitemid"]] = true
60
- xref_to_internal_eref_anchor(x, key, bibitems, xml.root["document_suffix"])
61
- end.keys
62
- end
63
-
64
- def xref_to_internal_eref_prep(section, xml)
65
- bibitems = Util::gather_bibitems(section)
66
- indirect_bibitems = Util::gather_bibitems(xml)
67
- .select { |_, v| indirect_bib?(v) }
68
- [bibitems, indirect_bibitems]
69
- end
70
-
71
- def xref_to_internal_eref_anchor(xref, key, bibitems, document_suffix)
72
- t = xref["target"]
73
- if d = bibitems[t]&.at(ns("./docidentifier[@type = 'repository']"))
74
- m = %r{^([^/]+)}.match(d.text) and
75
- t.sub!(%r(#{m[0]}_), "")
76
- end
77
- t.sub!(%r{^#{key}_}, "")
78
- xref << make_anchor(t.sub(%r(_#{document_suffix}$), ""))
79
- xref.delete("target")
80
- xref.name = "eref"
81
- end
82
-
83
- def xref_prefix_key(xref, key, indirect)
84
- if b = indirect[xref["target"]]
85
- t = b.at(ns("./docidentifier[@type = 'repository']"))
86
- xref["type"] = t.text.sub(%r{/.*$}, "")
87
- else
88
- xref["target"] = "#{key}_#{xref['target']}"
89
- xref["type"] = key
90
- end
91
- end
92
-
93
- def eref_to_internal_eref(section, xml, key)
94
- bibitems, bibitemids = eref_to_internal_eref_prep(section, xml)
95
- eref_to_internal_eref_select(section, xml, bibitems)
96
- .each_with_object([]) do |x, m|
97
- url = bibitems[x]&.at(ns("./uri[@type = 'citation']"))
98
- bibitemids[x]&.each do |e|
99
- id = eref_to_internal_eref1(e, key, url) and m << id
100
- end
101
- end
102
- end
103
-
104
- def eref_to_internal_eref_prep(section, xml)
105
- bibitems = Util::gather_bibitems(xml)
106
- .delete_if { |_, v| internal_bib?(v) }
107
- bibitemids = Util::gather_bibitemids(section)
108
- [bibitems, bibitemids]
109
- end
110
-
111
- def eref_to_internal_eref1(elem, key, url)
112
- if url
113
- elem.name = "link"
114
- elem["target"] = url
115
- nil
116
- else
117
- elem["bibitemid"] = "#{key}_#{elem['bibitemid']}"
118
- elem << make_anchor(elem["bibitemid"])
119
- elem["type"] = key
120
- elem["bibitemid"]
121
- end
122
- end
123
-
124
- def eref_to_internal_eref_select(section, _xml, bibitems)
125
- refs = Util::gather_bibitemids(section).keys
126
- refs.uniq.reject do |x|
127
- b = bibitems[x] and ( indirect_bib?(b) || internal_bib?(b) )
128
- end
129
- end
130
-
131
- def internal_bib?(bibitem)
132
- bibitem["type"] == "internal" ||
133
- bibitem.at(ns("./docidentifier[@type = 'repository']"))
134
- end
135
-
136
- def indirect_bib?(bibitem)
137
- a = bibitem.at(ns("./docidentifier[@type = 'repository']")) or
138
- return false
139
- %r{^current-metanorma-collection/}.match?(a.text) and return false
140
- a.text.include?("/")
141
- end
142
-
143
- # from standoc
144
- def new_hidden_ref(xmldoc)
145
- ins = xmldoc.at("bibliography") or
146
- xmldoc.root << "<bibliography/>" and ins = xmldoc.at("bibliography")
147
- ins.add_child("<references hidden='true' normative='false'/>").first
148
- end
149
-
150
- def copy_repo_items_biblio(ins, section, xml)
151
- bibitems = Util::gather_bibitems(section)
152
- xml.xpath(ns("//references/bibitem[docidentifier/@type = 'repository']"))
153
- .each_with_object([]) do |b, m|
154
- bibitems[b["id"]] or next
155
- # section.at("//*[@bibitemid = '#{b['id']}']") or next
156
- ins << b.dup
157
- m << b["id"]
158
- end
159
- end
160
-
161
- def insert_indirect_biblio(ins, refs, key, xml)
162
- refs.empty? and return
163
- internal_bibitems, external_bibitems = insert_indirect_biblio_prep(xml)
164
- refs.compact.reject do |x|
165
- #external_bibitems[x.sub(/^#{key}_/, "")]
166
- end.each do |x|
167
- ins << if b = internal_bibitems[x.sub(/^#{key}_/, "")]
168
- b.dup.tap { |m| m["id"] = x }
169
- else new_indirect_bibitem(x, key)
170
- end
171
- end
172
- end
173
-
174
- def insert_indirect_biblio_prep(xml)
175
- bibitems = Util::gather_bibitems(xml)
176
- internal_bibitems = bibitems.select { |_, v| internal_bib?(v) }
177
- external_bibitems = bibitems.reject { |_, v| internal_bib?(v) }
178
- [internal_bibitems, external_bibitems]
179
- end
180
-
181
- def new_indirect_bibitem(ident, prefix)
182
- <<~BIBENTRY
183
- <bibitem id="#{ident}" type="internal">
184
- <docidentifier type="repository">#{ident.sub(/^#{prefix}_/, "#{prefix}/")}</docidentifier>
185
- </bibitem>
186
- BIBENTRY
187
- end
188
- end
189
- end
@@ -1,127 +0,0 @@
1
- module Metanorma
2
- module Util
3
- def self.log(message, type = :info)
4
- log_types = Metanorma.configuration.logs.map(&:to_s) || []
5
-
6
- if log_types.include?(type.to_s)
7
- puts(message)
8
- end
9
-
10
- if type == :fatal
11
- exit(1)
12
- end
13
- end
14
-
15
- # dependency ordering
16
- def self.sort_extensions_execution_ord(ext)
17
- case ext
18
- when :xml then 0
19
- when :rxl then 1
20
- when :presentation then 2
21
- else
22
- 99
23
- end
24
- end
25
-
26
- def self.sort_extensions_execution(ext)
27
- ext.sort do |a, b|
28
- sort_extensions_execution_ord(a) <=> sort_extensions_execution_ord(b)
29
- end
30
- end
31
-
32
- def self.recursive_string_keys(hash)
33
- case hash
34
- when Hash then hash.map { |k, v| [k.to_s, recursive_string_keys(v)] }.to_h
35
- when Enumerable then hash.map { |v| recursive_string_keys(v) }
36
- else
37
- hash
38
- end
39
- end
40
-
41
- def self.gather_bibitems(xml)
42
- xml.xpath("//xmlns:bibitem[@id]").each_with_object({}) do |b, m|
43
- if m[b["id"]]
44
- b.remove
45
- next # we can't update duplicate bibitem, processing updates wrong one
46
- else
47
- m[b["id"]] = b
48
- end
49
- end
50
- end
51
-
52
- def self.gather_bibitemids(xml)
53
- xml.xpath("//*[@bibitemid]").each_with_object({}) do |e, m|
54
- /^semantic__/.match?(e.name) and next
55
- m[e["bibitemid"]] ||= []
56
- m[e["bibitemid"]] << e
57
- end
58
- end
59
-
60
- def self.gather_citeases(xml)
61
- xml.xpath("//*[@citeas]").each_with_object({}) do |e, m|
62
- /^semantic__/.match?(e.name) and next
63
- m[e["citeas"]] ||= []
64
- m[e["citeas"]] << e
65
- end
66
- end
67
-
68
- def self.add_suffix_to_attributes(doc, suffix, tag_name, attr_name, isodoc)
69
- (suffix.nil? || suffix.empty?) and return
70
- doc.xpath(isodoc.ns("//#{tag_name}[@#{attr_name}]")).each do |elem|
71
- a = elem.attributes[attr_name].value
72
- /_#{suffix}$/.match?(a) or
73
- elem.attributes[attr_name].value = "#{a}_#{suffix}"
74
- end
75
- end
76
-
77
- def self.hash_key_detect(directives, key, variable)
78
- c = directives.detect { |x| x.is_a?(Hash) && x.has_key?(key) } or
79
- return variable
80
- c[key]
81
- end
82
-
83
- def self.rel_path_resolve(dir, path)
84
- path.nil? and return path
85
- path.empty? and return path
86
- p = Pathname.new(path)
87
- p.absolute? ? path : File.join(dir, path)
88
- end
89
-
90
- def self.key(ident)
91
- @c ||= HTMLEntities.new
92
- @c.decode(ident).gsub(/(\p{Zs})+/, " ")
93
- end
94
-
95
- class DisambigFiles
96
- def initialize
97
- @seen_filenames = []
98
- end
99
-
100
- def strip_root(name)
101
- name.sub(%r{^(\./)?(\.\./)+}, "")
102
- end
103
-
104
- def source2dest_filename(name, disambig = true)
105
- n = strip_root(name)
106
- dir = File.dirname(n)
107
- base = File.basename(n)
108
- if disambig && @seen_filenames.include?(base)
109
- base = disambiguate_filename(base)
110
- end
111
- @seen_filenames << base
112
- dir == "." ? base : File.join(dir, base)
113
- end
114
-
115
- def disambiguate_filename(base)
116
- m = /^(?<start>.+\.)(?!0)(?<num>\d+)\.(?<suff>[^.]*)$/.match(base) ||
117
- /^(?<start>.+\.)(?<suff>[^.]*)/.match(base) ||
118
- /^(?<start>.+)$/.match(base)
119
- i = m.names.include?("num") ? m["num"].to_i + 1 : 1
120
- while @seen_filenames.include? base = "#{m['start']}#{i}.#{m['suff']}"
121
- i += 1
122
- end
123
- base
124
- end
125
- end
126
- end
127
- end
@@ -1,29 +0,0 @@
1
- module Metanorma
2
- class WorkersPool
3
- def initialize(workers)
4
- @workers = workers
5
- @queue = SizedQueue.new(@workers)
6
- @threads = Array.new(@workers) do
7
- Thread.new do
8
- catch(:exit) do
9
- loop do
10
- job, args = @queue.pop
11
- job.call *args
12
- end
13
- end
14
- end
15
- end
16
- end
17
-
18
- def schedule(*args, &block)
19
- @queue << [block, args]
20
- end
21
-
22
- def shutdown
23
- @workers.times do
24
- schedule { throw :exit }
25
- end
26
- @threads.map(&:join)
27
- end
28
- end
29
- end