Module: Metanorma::Standoc::Bibdata
- Included in:
- Cleanup
- Defined in:
- lib/metanorma/cleanup/bibdata.rb
Instance Method Summary collapse
- #bibdata_anchor_cleanup(xmldoc) ⇒ Object
- #bibdata_cleanup(xmldoc) ⇒ Object
- #bibdata_docidentifier_cleanup(xmldoc) ⇒ Object
- #bibdata_embed_hdr_cleanup(xmldoc) ⇒ Object
- #bibdata_embed_id_cleanup(xmldoc) ⇒ Object
- #bibdata_empty_contribs(xmldoc) ⇒ Object
- #bibdata_published(xmldoc) ⇒ Object
- #biblio_indirect_erefs(xmldoc, prefixes) ⇒ Object
-
#bpart_cleanup(xmldoc) ⇒ Object
allows us to deal with doc relation localities, temporarily stashed to “bpart”.
- #coverpage_images(xmldoc) ⇒ Object
-
#docidentifier_cleanup(xmldoc) ⇒ Object
(e.g. in ISO, reformatting identifier).
- #embed_recurse(bibitem, node) ⇒ Object
- #ext_contributor_cleanup(xmldoc) ⇒ Object
- #ext_contributors_process(yaml, ins) ⇒ Object
- #gather_indirect_erefs(xmldoc, prefix) ⇒ Object
- #hdr2bibitem(hdr) ⇒ Object
- #hdr2bibitem_type(hdr) ⇒ Object
- #indirect_eref_to_xref(eref, ident, id_map = nil) ⇒ Object
- #insert_indirect_biblio(xmldoc, refs, prefix) ⇒ Object
- #published?(stage, _xmldoc) ⇒ Boolean
- #published_base?(stage, _xmldoc) ⇒ Boolean
- #resolve_local_indirect_erefs(xmldoc, refs, prefix) ⇒ Object
- #resolve_local_indirect_erefs_prep(xmldoc) ⇒ Object
- #yaml2relaton(yaml, amend = nil) ⇒ Object
Instance Method Details
#bibdata_anchor_cleanup(xmldoc) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 26 def bibdata_anchor_cleanup(xmldoc) xmldoc.xpath("//bibdata//bibitem | //bibdata//note").each do |b| b.delete("id") b.delete("anchor") end end |
#bibdata_cleanup(xmldoc) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 4 def bibdata_cleanup(xmldoc) bibdata_anchor_cleanup(xmldoc) bibdata_docidentifier_cleanup(xmldoc) (xmldoc) # feeds bibdata_embed_id_cleanup (xmldoc) biblio_indirect_erefs(xmldoc, @internal_eref_namespaces&.uniq) coverpage_images(xmldoc) bibdata_empty_contribs(xmldoc) end |
#bibdata_docidentifier_cleanup(xmldoc) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 33 def bibdata_docidentifier_cleanup(xmldoc) ins = xmldoc.at("//bibdata/docidentifier") xmldoc.xpath("//bibdata/docidentifier").each_with_index do |b, i| i.zero? and next ins.next = b.remove ins = ins.next end end |
#bibdata_embed_hdr_cleanup(xmldoc) ⇒ Object
116 117 118 119 120 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 116 def (xmldoc) (@embed_hdr.nil? || @embed_hdr.empty?) and return xmldoc.at("//bibdata") << "<relation type='derivedFrom'>" \ "#{hdr2bibitem(@embed_hdr.first)}</relation>" end |
#bibdata_embed_id_cleanup(xmldoc) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 148 def (xmldoc) @embed_id.nil? and return bibdata = xmldoc.at("//bibdata") @embed_id.each do |d| bibdata = bibdata.at("./relation[@type = 'derivedFrom']/bibitem") ident = bibdata.at("./docidentifier[@primary = 'true']") || bibdata.at("./docidentifier") xmldoc.xpath("//xref[@target = '#{d}'][normalize-space(.//text()) = '']") .each do |x| x << ident.text end end end |
#bibdata_empty_contribs(xmldoc) ⇒ Object
221 222 223 224 225 226 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 221 def bibdata_empty_contribs(xmldoc) xmldoc.xpath("//bibdata//contributor").each do |c| p = c.at("./person | ./organization") p.nil? || p.text.strip.empty? and c.remove end end |
#bibdata_published(xmldoc) ⇒ Object
191 192 193 194 195 196 197 198 199 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 191 def bibdata_published(xmldoc) ins = add_misc_container(xmldoc) ins ||= add_misc_container(xmldoc) ins.at("./semantic-metadata/stage-published") and return p = published_base?(xmldoc.at("bibdata/status/stage")&.text, xmldoc) ins << <<~XML <semantic-metadata><stage-published>#{p}</stage-published></semantic-metadata> XML end |
#biblio_indirect_erefs(xmldoc, prefixes) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 107 def biblio_indirect_erefs(xmldoc, prefixes) prefixes&.each do |prefix| refs = gather_indirect_erefs(xmldoc, prefix) refs = resolve_local_indirect_erefs(xmldoc, refs, prefix) refs.empty? and next insert_indirect_biblio(xmldoc, refs, prefix) end end |
#bpart_cleanup(xmldoc) ⇒ Object
allows us to deal with doc relation localities, temporarily stashed to “bpart”
214 215 216 217 218 219 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 214 def bpart_cleanup(xmldoc) xmldoc.xpath("//relation/bpart").each do |x| extract_localities(x) x.replace(x.children) end end |
#coverpage_images(xmldoc) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 14 def coverpage_images(xmldoc) %w(coverpage-image innercoverpage-image tocside-image backpage-image).each do |n| xmldoc.xpath("//bibdata/ext/#{n}").each do |x| ins = add_misc_container(xmldoc) ins << "<presentation-metadata><#{n}>" \ "#{x.remove.children.to_xml}</#{n}>" \ "</presentation-metadata>" end end end |
#docidentifier_cleanup(xmldoc) ⇒ Object
(e.g. in ISO, reformatting identifier)
229 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 229 def docidentifier_cleanup(xmldoc); end |
#embed_recurse(bibitem, node) ⇒ Object
142 143 144 145 146 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 142 def (bibitem, node) node[:child].map { |x| hdr2bibitem(x) }.each do |x| bibitem << "<relation type='derivedFrom'>#{x}</relation>" end end |
#ext_contributor_cleanup(xmldoc) ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 162 def ext_contributor_cleanup(xmldoc) t = xmldoc.xpath("//metanorma-extension/clause/title").detect do |x| x.text.strip.casecmp("contributor metadata").zero? end or return a = t.at("../sourcecode") or return ins = xmldoc.at("//bibdata/contributor[last()]") yaml = yaml_deep_stringify_dates(YAML.safe_load(a.text, permitted_classes: [Date])) ext_contributors_process(yaml, ins) end |
#ext_contributors_process(yaml, ins) ⇒ Object
181 182 183 184 185 186 187 188 189 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 181 def ext_contributors_process(yaml, ins) yaml.is_a?(Hash) && !yaml["contributor"] and yaml = [yaml] yaml.is_a?(Array) and yaml = { "contributor" => yaml } yaml["contributor"].each { |c| c["role"] ||= { "type" => "author" } } r = yaml2relaton(yaml) Nokogiri::XML(r).xpath("//contributor").reverse_each do |c| ins.next = c end end |
#gather_indirect_erefs(xmldoc, prefix) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 42 def gather_indirect_erefs(xmldoc, prefix) xmldoc.xpath("//eref[@type = '#{prefix}']") .each_with_object({}) do |e, m| e.delete("type") m[e["bibitemid"]] = true end.keys end |
#hdr2bibitem(hdr) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 122 def hdr2bibitem(hdr) xml = @conv.isolated_asciidoctor_convert(hdr[:text], backend: hdr2bibitem_type(hdr), header_footer: true) b = Nokogiri::XML(xml).at("//xmlns:bibdata") b.name = "bibitem" b.delete("type") (b, hdr) b.to_xml end |
#hdr2bibitem_type(hdr) ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 133 def hdr2bibitem_type(hdr) m = /:mn-document-class: (\S+)/.match(hdr[:text]) if m then m[1].to_sym else Processor.new.asciidoctor_backend # don't want processor() : we will leave embedded headers as standoc, # not local flavour end end |
#indirect_eref_to_xref(eref, ident, id_map = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 63 def indirect_eref_to_xref(eref, ident, id_map = nil) loc = eref.at("./localityStack[locality[@type = 'anchor']]") || eref.at("./locality[@type = 'anchor']") loc = loc&.remove&.text || ident eref.name = "xref" eref.delete("bibitemid") eref.delete("citeas") eref["target"] = loc if id_map id_map.has_key?(loc) and return else eref.document.at("//*[@anchor = '#{loc}']") and return end eref.children = %(** Missing target #{loc}) eref["target"] = ident end |
#insert_indirect_biblio(xmldoc, refs, prefix) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 50 def insert_indirect_biblio(xmldoc, refs, prefix) i = xmldoc.at("bibliography") or xmldoc.root << "<bibliography/>" and i = xmldoc.at("bibliography") i = i.add_child("<references hidden='true' normative='false'/>").first refs.each do |x| i << <<~BIB <bibitem anchor="#{x}" #{add_id_text} type="internal"> <docidentifier type="repository">#{x.sub(/^#{prefix}_/, "#{prefix}/")}</docidentifier> </bibitem> BIB end end |
#published?(stage, _xmldoc) ⇒ Boolean
208 209 210 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 208 def published?(stage, _xmldoc) stage.casecmp("published").zero? end |
#published_base?(stage, _xmldoc) ⇒ Boolean
201 202 203 204 205 206 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 201 def published_base?(stage, _xmldoc) if @stage_published @stage_published == "true" else published?(stage, _xmldoc) end end |
#resolve_local_indirect_erefs(xmldoc, refs, prefix) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 81 def resolve_local_indirect_erefs(xmldoc, refs, prefix) id_map, eref_map = resolve_local_indirect_erefs_prep(xmldoc) refs.each_with_object([]) do |r, m| id = r.sub(/^#{prefix}_/, "") if id_map[id]&.at("./ancestor-or-self::*[@type = '#{prefix}']") eref_map[r]&.each do |e| indirect_eref_to_xref(e, id, id_map) end else m << r end end end |
#resolve_local_indirect_erefs_prep(xmldoc) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 94 def resolve_local_indirect_erefs_prep(xmldoc) # Pre-index elements by anchor # (which is what bibitemid currently points to) id_map = xmldoc.xpath("//*[@anchor]") .each_with_object({}) do |node, map| map[node["anchor"]] = node end # Pre-index all <eref> elements by bibitemid eref_map = xmldoc.xpath("//eref[@bibitemid]") .group_by { |e| e["bibitemid"] } [id_map, eref_map] end |
#yaml2relaton(yaml, amend = nil) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/metanorma/cleanup/bibdata.rb', line 172 def yaml2relaton(yaml, amend = nil) r = YAML.safe_load(yaml.to_yaml, permitted_classes: [Date], symbolize_names: true) h = Relaton::Bib::HashParserV1.hash_to_bib(r) b = Relaton::Bib::ItemData.new(**h).to_xml amend and b.sub!("</bibitem>", "#{amend}</bibitem>") b end |