Module: Metanorma::Collection::Util
- Defined in:
- lib/metanorma/collection/util/util.rb,
lib/metanorma/collection/util/disambig_files.rb
Defined Under Namespace
Classes: DisambigFiles, Dummy
Class Method Summary
collapse
-
.add_suffix_to_attrs(doc, suffix, tag_name, attr_name, isodoc) ⇒ Object
-
.anchor_id_attributes ⇒ Object
-
.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
-
.key(ident) ⇒ Object
-
.load_isodoc(flavor, presxml: false) ⇒ Object
-
.rel_path_resolve(dir, path) ⇒ Object
Class Method Details
.add_suffix_to_attrs(doc, suffix, tag_name, attr_name, isodoc) ⇒ Object
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/metanorma/collection/util/util.rb', line 39
def add_suffix_to_attrs(doc, suffix, tag_name, attr_name, isodoc)
(suffix.nil? || suffix.empty?) and return
doc.xpath(isodoc.ns("//#{tag_name}[@#{attr_name}]")).each do |elem|
a = elem.attributes[attr_name].value
/_#{suffix}$/.match?(a) or
elem.attributes[attr_name].value = "#{a}_#{suffix}"
end
end
|
.anchor_id_attributes ⇒ Object
5
6
7
8
|
# File 'lib/metanorma/collection/util/util.rb', line 5
def anchor_id_attributes
Metanorma::Utils::anchor_attributes(presxml: true) +
[%w(* id), %w(* anchor), %w(link bibitemid), %w(fmt-link bibitemid)]
end
|
.gather_bibitemids(xml, presxml) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/metanorma/collection/util/util.rb', line 22
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
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/metanorma/collection/util/util.rb', line 10
def gather_bibitems(xml)
xml.xpath("//xmlns:bibitem[@id]").each_with_object({}) do |b, m|
if m[b["id"]]
b.remove
next
else
m[b["id"]] = b
end
end
end
|
.gather_citeases(xml, presxml) ⇒ Object
30
31
32
33
34
35
36
37
|
# File 'lib/metanorma/collection/util/util.rb', line 30
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
50
51
52
53
54
|
# File 'lib/metanorma/collection/util/util.rb', line 50
def hash_key_detect(directives, key, variable)
c = directives.detect { |x| x.key == key } or
return variable
c.value
end
|
.hide_refs(docxml) ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/metanorma/collection/util/util.rb', line 63
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
89
90
91
92
93
94
95
96
|
# File 'lib/metanorma/collection/util/util.rb', line 89
def isodoc_create(flavor, lang, script, xml, presxml: false)
isodoc = Util::load_isodoc(flavor, presxml: presxml)
isodoc.i18n_init(lang, script, nil) isodoc.metadata_init(lang, script, nil, isodoc.i18n)
isodoc.info(xml, nil)
isodoc
end
|
.key(ident) ⇒ Object
71
72
73
74
|
# File 'lib/metanorma/collection/util/util.rb', line 71
def key(ident)
@c ||= HTMLEntities.new
@c.decode(ident).gsub(/(\p{Zs})+/, " ")
end
|
.load_isodoc(flavor, presxml: false) ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/metanorma/collection/util/util.rb', line 80
def load_isodoc(flavor, presxml: false)
x = Asciidoctor.load nil, backend: flavor.to_sym
if presxml
x.converter.presentation_xml_converter(Dummy.new)
else
x.converter.html_converter(Dummy.new) end
end
|
.rel_path_resolve(dir, path) ⇒ Object
56
57
58
59
60
61
|
# File 'lib/metanorma/collection/util/util.rb', line 56
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
|