Module: Metanorma::Standoc::Ref

Included in:
Cleanup
Defined in:
lib/metanorma/cleanup/ref.rb

Instance Method Summary collapse

Instance Method Details

#biblio_annex(xmldoc) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/metanorma/cleanup/ref.rb', line 99

def biblio_annex(xmldoc)
  xmldoc.xpath("//annex[references/references]").each do |t|
    t.xpath("./clause | ./references | ./terms").size == 1 or next
    r = t.at("./references")
    r.xpath("./references").each { |b| b["normative"] = r["normative"] }
    r.replace(r.elements)
  end
end

#biblio_cleanup(xmldoc) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/metanorma/cleanup/ref.rb', line 69

def biblio_cleanup(xmldoc)
  biblio_reorder(xmldoc)
  biblio_annex(xmldoc)
  biblio_nested(xmldoc)
  biblio_renumber(xmldoc)
  biblio_linkonly(xmldoc)
  biblio_hidden_inherit(xmldoc)
  biblio_no_ext(xmldoc)
end

#biblio_linkonly(xmldoc) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/metanorma/cleanup/ref.rb', line 79

def biblio_linkonly(xmldoc)
  xmldoc.at("//xref[@hidden]") or return
  ins = xmldoc.at("//bibliography")
    .add_child("<references hidden='true' normative='true'/>").first
  refs = xmldoc.xpath("//xref[@hidden]").each_with_object([]) do |x, m|
    @refids << x["target"]
    m << { id: x["target"], ref: x["hidden"] }
    x.delete("hidden")
  end
  ins << insert_hidden_bibitems(refs)
end

#biblio_nested(xmldoc) ⇒ Object



108
109
110
111
# File 'lib/metanorma/cleanup/ref.rb', line 108

def biblio_nested(xmldoc)
  biblio_nested_initial_items(xmldoc)
  biblio_nested_sections(xmldoc)
end

#biblio_nested_initial_items(xmldoc) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/metanorma/cleanup/ref.rb', line 113

def biblio_nested_initial_items(xmldoc)
  xmldoc.xpath("//references[references][bibitem]").each do |t|
    r = t.at("./references")
    ref = t.at("./bibitem")
      .before("<references unnumbered='true'></references>").previous
    (ref.xpath("./following-sibling::*") &
     r.xpath("./preceding-sibling::*")).each do |x|
      ref << x
    end
  end
end

#biblio_nested_sections(xmldoc) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/metanorma/cleanup/ref.rb', line 125

def biblio_nested_sections(xmldoc)
  xmldoc.xpath("//references[references]").each do |t|
    t.name = "clause"
    t.xpath("./references").each { |r| r["normative"] = t["normative"] }
    t.delete("normative")
  end
end

#biblio_renumber(xmldoc) ⇒ Object

default presuppose that all citations in biblio numbered consecutively, but that standards codes are preserved as is: only numeric references are renumbered



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/metanorma/cleanup/ref.rb', line 40

def biblio_renumber(xmldoc)
  i = 0
  xmldoc.xpath("//references[not(@normative = 'true')]" \
               "[not(@hidden = 'true')]").each do |r|
    r.xpath("./bibitem[not(@hidden = 'true')]").each do |b|
      i += 1
      docid = b.at("./docidentifier[@type = 'metanorma']") or next
      /^\[\d+\]$/.match?(docid.text) or next
      docid.children = "[#{i}]"
    end
  end
end

#biblio_reorder(xmldoc) ⇒ Object



9
10
11
12
13
# File 'lib/metanorma/cleanup/ref.rb', line 9

def biblio_reorder(xmldoc)
  xmldoc.xpath("//references[@normative = 'false']").each do |r|
    biblio_reorder1(r)
  end
end

#biblio_reorder1(refs) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/metanorma/cleanup/ref.rb', line 15

def biblio_reorder1(refs)
  fold_notes_into_biblio(refs)
  bib = refs.xpath("./bibitem")
  @sort_biblio and bib = sort_biblio(bib)
  insert_sorted_bibitems(refs, bib)
  extract_notes_from_biblio(refs)
  refs.xpath("./references").each { |r| biblio_reorder1(r) }
end

#insert_hidden_bibitems(bib) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/metanorma/cleanup/ref.rb', line 91

def insert_hidden_bibitems(bib)
  refs = bib.each_with_object([]) do |b, m|
    m << @conv
      .reference1code(%(<ref id="#{b[:id]}">[#{b[:ref]}]</ref>), nil)
  end
  @conv.reference_populate(refs)
end

#insert_sorted_bibitems(refs, bib) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/metanorma/cleanup/ref.rb', line 24

def insert_sorted_bibitems(refs, bib)
  insert = refs.at("./bibitem")&.previous_element
  refs.xpath("./bibitem").each(&:remove)
  bib.reverse_each do |b|
    (insert and insert.next = b.to_xml) or
      refs.children.first.add_previous_sibling b.to_xml
  end
end

#normref_cleanup(xmldoc) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/metanorma/cleanup/ref.rb', line 61

def normref_cleanup(xmldoc)
  r = xmldoc.at(self.class::NORM_REF) || return
  preface = ((r.xpath("./title/following-sibling::*") & # intersection
              r.xpath("./bibitem[1]/preceding-sibling::*")) -
  r.xpath("./note[@type = 'boilerplate']/descendant-or-self::*"))
  preface.each(&:remove)
end

#ref_cleanup(xmldoc) ⇒ Object

move ref before p



54
55
56
57
58
59
# File 'lib/metanorma/cleanup/ref.rb', line 54

def ref_cleanup(xmldoc)
  xmldoc.xpath("//p/ref").each do |r|
    parent = r.parent
    parent.previous = r.remove
  end
end

#sort_biblio(bib) ⇒ Object



33
34
35
# File 'lib/metanorma/cleanup/ref.rb', line 33

def sort_biblio(bib)
  bib
end