Module: Metanorma::Collection::Util
- Defined in:
- lib/metanorma/collection/util/util.rb,
lib/metanorma/collection/util/disambig_files.rb
Defined Under Namespace
Classes: DisambigFiles
Constant Summary collapse
- SVG_NS =
"http://www.w3.org/2000/svg".freeze
Class Method Summary collapse
-
.add_suffix_to_attrs(doc, suffix, tag, attr, isodoc) ⇒ Object
Skip SVGs, they are processed by Vectory::SvgDocument#suffix_ids.
- .anchor_id_attributes ⇒ Object
-
.asciidoc_dummy_header(docidentifier: nil) ⇒ Object
Dummy asciidoc header used by the prefatory-content parse so we can run Asciidoctor against arbitrary text.
- .gather_bibitemids(xml, presxml) ⇒ Object
- .gather_bibitems(xml) ⇒ Object
- .gather_citeases(xml, presxml) ⇒ Object
- .hash_key_detect(directives, key, variable) ⇒ Object
- .hide_refs(docxml) ⇒ Object
-
.isodoc_create(flavor, lang, script, xml, presxml: false) ⇒ Object
deprecated
Deprecated.
Call Metanorma::Core::Isodoc.create directly. Kept as a back-compat shim so existing callers keep working unchanged while the wrapper is retired.
- .key(ident) ⇒ Object
-
.load_isodoc(flavor, presxml: false) ⇒ Object
deprecated
Deprecated.
Call Metanorma::Core::Isodoc.resolve_converter directly.
- .mime_file_recognised?(filename) ⇒ Boolean
- .nokogiri_to_temp(xml, filename, suffix) ⇒ Object
- .rel_path_resolve(dir, path) ⇒ Object
-
.substitute_filename_pattern(pattern, options = {}) ⇒ Object
Substitute special strings in filename patterns.
- .taste2coverpage_pdf_portfolio(taste) ⇒ Object
- .taste2flavor(taste) ⇒ Object
- .taste2isodoc_attrs(taste, format) ⇒ Object
-
.update_ids_css(document, ids, suffix) ⇒ Object
replicates Vectory update_ids_css, but skips over svgs, which are processed separately by Vectory::SvgDocument#suffix_ids.
-
.update_ids_css_string(style, ids, suffix) ⇒ Object
Updates ID references in a CSS style string.
-
.url_in_css_styles(doc, ids, document_suffix) ⇒ Object
update relative URLs, url(#...), in CSS in @style attrs (incl. SVG), and in include SVG url(#..) attrs, not processed already by add_suffix_to_attrs.
Class Method Details
.add_suffix_to_attrs(doc, suffix, tag, attr, isodoc) ⇒ Object
Skip SVGs, they are processed by Vectory::SvgDocument#suffix_ids
48 49 50 51 52 53 54 55 56 |
# File 'lib/metanorma/collection/util/util.rb', line 48 def add_suffix_to_attrs(doc, suffix, tag, attr, isodoc) (suffix.nil? || suffix.empty?) and return doc.xpath(isodoc.ns("//#{tag}[@#{attr}][not(ancestor-or-self::*[local-name()='svg'])]")).each do |elem| # doc.xpath(isodoc.ns("//#{tag}[@#{attr}]")).each do |elem| a = elem.attributes[attr].value /_#{suffix}$/.match?(a) or elem.attributes[attr].value = "#{a}_#{suffix}" end end |
.anchor_id_attributes ⇒ Object
12 13 14 15 |
# File 'lib/metanorma/collection/util/util.rb', line 12 def anchor_id_attributes Metanorma::Utils::anchor_attributes(presxml: true) + [%w(* id), %w(* anchor), %w(link bibitemid), %w(fmt-link bibitemid)] end |
.asciidoc_dummy_header(docidentifier: nil) ⇒ Object
Dummy asciidoc header used by the prefatory-content parse so we
can run Asciidoctor against arbitrary text. If docidentifier
is supplied, it is injected as :docidentifier: so the flavor
processor's metadata_id has a real value to put into the
bibdata, instead of falling back to its (Liquid-templated)
docid_template. This prevents the prefatory parse from feeding
an unresolved Liquid template through Relaton/pubid in flavors
whose relaton-* gem eagerly parses the docidentifier in
content= (issue #558).
197 198 199 200 |
# File 'lib/metanorma/collection/util/util.rb', line 197 def asciidoc_dummy_header(docidentifier: nil) attrs = docidentifier ? ":docidentifier: #{docidentifier}\n" : "" "= X\nA\n#{attrs}\n" end |
.gather_bibitemids(xml, presxml) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/metanorma/collection/util/util.rb', line 30 def gather_bibitemids(xml, presxml) xml.xpath("//*[@bibitemid]").each_with_object({}) do |e, m| presxml && %w(xref eref link).include?(e.name) and next m[e["bibitemid"]] ||= [] m[e["bibitemid"]] << e end end |
.gather_bibitems(xml) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/metanorma/collection/util/util.rb', line 17 def gather_bibitems(xml) xml.xpath("//xmlns:bibitem[@id]").each_with_object({}) do |b, m| id = b["anchor"] || b["id"] if m[id] b.remove next # we can't update duplicate bibitem, processing updates wrong one else m[id] = b end end end |
.gather_citeases(xml, presxml) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/metanorma/collection/util/util.rb', line 38 def gather_citeases(xml, presxml) xml.xpath("//*[@citeas]").each_with_object({}) do |e, m| presxml && %w(xref eref link).include?(e.name) and next k = key(e["citeas"]) m[k] ||= [] m[k] << e end end |
.hash_key_detect(directives, key, variable) ⇒ Object
58 59 60 61 62 |
# File 'lib/metanorma/collection/util/util.rb', line 58 def hash_key_detect(directives, key, variable) c = directives.detect { |x| x.key == key } or return variable c.value end |
.hide_refs(docxml) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/metanorma/collection/util/util.rb', line 71 def hide_refs(docxml) p = "//xmlns:references[xmlns:bibitem]"\ "[not(./xmlns:bibitem[not(@hidden) or @hidden = 'false'])]" docxml.xpath(p).each do |f| f["hidden"] = "true" end end |
.isodoc_create(flavor, lang, script, xml, presxml: false) ⇒ Object
Call Metanorma::Core::Isodoc.create directly. Kept as a back-compat shim so existing callers keep working unchanged while the wrapper is retired.
183 184 185 186 |
# File 'lib/metanorma/collection/util/util.rb', line 183 def isodoc_create(flavor, lang, script, xml, presxml: false) Metanorma::Core::Isodoc.create(flavor, lang: lang, script: script, xml: xml, presxml: presxml) end |
.key(ident) ⇒ Object
79 80 81 82 |
# File 'lib/metanorma/collection/util/util.rb', line 79 def key(ident) @c ||= HTMLEntities.new @c.decode(ident).gsub(/(\p{Zs})+/, " ") end |
.load_isodoc(flavor, presxml: false) ⇒ Object
Call Metanorma::Core::Isodoc.resolve_converter directly.
176 177 178 |
# File 'lib/metanorma/collection/util/util.rb', line 176 def load_isodoc(flavor, presxml: false) Metanorma::Core::Isodoc.resolve_converter(flavor, presxml: presxml) end |
.mime_file_recognised?(filename) ⇒ Boolean
7 8 9 10 |
# File 'lib/metanorma/collection/util/util.rb', line 7 def mime_file_recognised?(filename) mime_type = Marcel::MimeType.for name: filename mime_type != "application/octet-stream" end |
.nokogiri_to_temp(xml, filename, suffix) ⇒ Object
202 203 204 205 206 207 |
# File 'lib/metanorma/collection/util/util.rb', line 202 def nokogiri_to_temp(xml, filename, suffix) file = Tempfile.new([filename, suffix]) file.write(xml.to_xml(indent: 0)) file.close [file, file.path] end |
.rel_path_resolve(dir, path) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/metanorma/collection/util/util.rb', line 64 def rel_path_resolve(dir, path) path.nil? and return path path.empty? and return path p = Pathname.new(path) p.absolute? ? path : File.join(dir, path) end |
.substitute_filename_pattern(pattern, options = {}) ⇒ Object
Substitute special strings in filename patterns. Single source for both FileLookup#substitute_filename_pattern and sectionsplit.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/metanorma/collection/util/util.rb', line 89 def substitute_filename_pattern(pattern, = {}) pattern or return pattern subs = { "{document-num}" => [:document_num], "{basename}" => [:basename], "{basename_legacy}" => [:basename_legacy], "{sectionsplit-num}" => [:sectionsplit_num] } subs.each_with_object(pattern.dup) do |(token, val), result| val and result.gsub!(token, val.to_s) end end |
.taste2coverpage_pdf_portfolio(taste) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/metanorma/collection/util/util.rb', line 112 def taste2coverpage_pdf_portfolio(taste) tastes = Metanorma::TasteRegister.instance.aliases tastes[taste.to_sym] or return nil taste = Metanorma::TasteRegister.instance.get(taste.to_sym) ret = taste.config.base_override&.filename_attributes &.coverpage_pdf_portfolio or return File.join(taste.directory, ret) end |
.taste2flavor(taste) ⇒ Object
100 101 102 103 104 |
# File 'lib/metanorma/collection/util/util.rb', line 100 def taste2flavor(taste) tastes = Metanorma::TasteRegister.instance.aliases tastes[taste.to_sym] and taste = tastes[taste.to_sym] taste end |
.taste2isodoc_attrs(taste, format) ⇒ Object
106 107 108 109 110 |
# File 'lib/metanorma/collection/util/util.rb', line 106 def taste2isodoc_attrs(taste, format) tastes = Metanorma::TasteRegister.instance.aliases tastes[taste.to_sym] or return {} Metanorma::TasteRegister.isodoc_attrs(taste.to_sym, format) end |
.update_ids_css(document, ids, suffix) ⇒ Object
replicates Vectory update_ids_css, but skips over svgs, which are processed separately by Vectory::SvgDocument#suffix_ids
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/metanorma/collection/util/util.rb', line 136 def update_ids_css(document, ids, suffix) suffix = suffix.is_a?(Integer) ? sprintf("%09d", suffix) : suffix document.xpath(".//m:style[not(ancestor::i:svg)]", "m" => SVG_NS, "i" => SVG_NS).each do |s| s.children = update_ids_css_string(s.children.to_xml, ids, suffix) end document.xpath(".//*[@style][not(ancestor::i:svg)]", "i" => SVG_NS).each do |s| s["style"] = update_ids_css_string(s["style"], ids, suffix) end end |
.update_ids_css_string(style, ids, suffix) ⇒ Object
Updates ID references in a CSS style string. Replicates Vectory update_ids_css_string
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/metanorma/collection/util/util.rb', line 150 def update_ids_css_string(style, ids, suffix) ids.each do |i| style = style.gsub(%r[##{i}\b], sprintf("#%<id>s_%<suffix>s", id: i, suffix: suffix)) .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]), sprintf("[id='%<id>s_%<suffix>s']", id: i, suffix: suffix)) end style end |
.url_in_css_styles(doc, ids, document_suffix) ⇒ Object
update relative URLs, url(#...), in CSS in @style attrs (incl. SVG), and in include SVG url(#..) attrs, not processed already by add_suffix_to_attrs
126 127 128 129 130 131 132 |
# File 'lib/metanorma/collection/util/util.rb', line 126 def url_in_css_styles(doc, ids, document_suffix) update_ids_css(doc.root, ids, document_suffix) doc.xpath("//i:svg", "i" => SVG_NS).each do |s| svg = Vectory::SvgDocument.new(s.to_xml) svg.suffix_ids(document_suffix) end end |