Module: Asciidoctor::ISO::Cleanup

Included in:
Converter
Defined in:
lib/asciidoctor/iso/cleanup.rb,
lib/asciidoctor/iso/cleanup_ref.rb,
lib/asciidoctor/iso/cleanup_block.rb,
lib/asciidoctor/iso/cleanup_footnotes.rb

Constant Summary collapse

TEXT_ELEMS =
%w{status language script version author name callout phone
email street city state country postcode identifier referenceFrom
referenceTo docidentifier prefix initial addition surname forename
title draft secretariat title-main title-intro title-part}.freeze
ELEMS_ALLOW_NOTES =
%w[p formula ul ol dl figure].freeze
LOCALITY_REGEX_STR =

currently references cannot contain commas! extending localities to cover ISO referencing

<<~REGEXP.freeze
  ^((?<locality>section|clause|part|paragraph|chapter|page|
                table|annex|figure|example|note|formula|
                locality:[^ \\t\\n\\r:,]+)(\\s+|=)
         (?<ref>[^ \\t\\n,:-]+)(-(?<to>[^ \\t\\n,:-]+))?|
    (?<locality2>whole|locality:[^ \\t\\n\\r:,]+))[,:]?\\s*
   (?<text>.*)$
REGEXP
LOCALITY_RE =
Regexp.new(LOCALITY_REGEX_STR.gsub(/\s/, ""),
Regexp::IGNORECASE | Regexp::MULTILINE)
ISO_PUBLISHER_XPATH =
"./contributor[role/@type = 'publisher']/"\
"organization[abbreviation = 'ISO' or abbreviation = 'IEC' or "\
"name = 'International Organization for Standardization' or "\
"name = 'International Electrotechnical Commission']".freeze
PRE_NORMREF_FOOTNOTES =
"//foreword//fn | //introduction//fn |"\
"//clause[title = 'Scope']//fn" .freeze
NORMREF_FOOTNOTES =
"//references[title = 'Normative References']//fn |"\
"//references[title = 'Normative References']//bibitem/note".freeze
POST_NORMREF_FOOTNOTES =
"//clause[not(title = 'Scope')]//fn | "\
"//references[title = 'Bibliography']//fn | "\
"//references[title = 'Bibliography']//bibitem/note".freeze

Instance Method Summary collapse

Instance Method Details

#align_callouts_to_annotations(xmldoc) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/asciidoctor/iso/cleanup.rb', line 91

def align_callouts_to_annotations(xmldoc)
  xmldoc.xpath("//sourcecode").each do |x|
    callouts = x.elements.select { |e| e.name == "callout" }
    annotations = x.elements.select { |e| e.name == "annotation" }
    if callouts.size == annotations.size
      link_callouts_to_annotations(callouts, annotations)
    end
  end
end

#biblio_cleanup(xmldoc) ⇒ Object



179
180
181
182
183
# File 'lib/asciidoctor/iso/cleanup.rb', line 179

def biblio_cleanup(xmldoc)
  xmldoc.xpath("//references[references]").each do |t|
    t.name = "clause"
  end
end

#bookmark_cleanup(xmldoc) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/asciidoctor/iso/cleanup.rb', line 222

def bookmark_cleanup(xmldoc)
  xmldoc.xpath("//li[descendant::bookmark]").each do |x|
    if x&.elements&.first&.name == "p" &&
        x&.elements&.first&.elements&.first&.name == "bookmark"
      if empty_text_before_first_element(x.elements[0])
        x["id"] = x.elements[0].elements[0].remove["id"]
        strip_initial_space(x.elements[0])
      end
    end
  end
end

#bpart_cleanup(xmldoc) ⇒ Object

allows us to deal with doc relation localities, temporarily stashed to “bpart”



50
51
52
53
54
55
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 50

def bpart_cleanup(xmldoc)
  xmldoc.xpath("//relation/bpart").each do |x|
    extract_localities(x)
    x.replace(x.children)
  end
end

#callout_cleanup(xmldoc) ⇒ Object



109
110
111
112
# File 'lib/asciidoctor/iso/cleanup.rb', line 109

def callout_cleanup(xmldoc)
  merge_annotations_into_sourcecode(xmldoc)
  align_callouts_to_annotations(xmldoc)
end

#cleanup(xmldoc) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/asciidoctor/iso/cleanup.rb', line 17

def cleanup(xmldoc)
  termdef_cleanup(xmldoc)
  sections_cleanup(xmldoc)
  obligations_cleanup(xmldoc)
  table_cleanup(xmldoc)
  formula_cleanup(xmldoc)
  figure_cleanup(xmldoc)
  ref_cleanup(xmldoc)
  note_cleanup(xmldoc)
  normref_cleanup(xmldoc)
  biblio_cleanup(xmldoc)
  reference_names(xmldoc)
  xref_cleanup(xmldoc)
  bpart_cleanup(xmldoc)
  quotesource_cleanup(xmldoc)
  para_cleanup(xmldoc)
  callout_cleanup(xmldoc)
  origin_cleanup(xmldoc)
  element_name_cleanup(xmldoc)
  footnote_renumber(xmldoc)
  empty_element_cleanup(xmldoc)
  mathml_cleanup(xmldoc)
  script_cleanup(xmldoc)
  docidentifier_cleanup(xmldoc)
  bookmark_cleanup(xmldoc)
  xmldoc
end

#date_range(date) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 99

def date_range(date)
  from = date.at("./from")
  to = date.at("./to")
  on = date.at("./on")
  return on.text if on
  ret = "#{from.text}&ndash;"
  ret += to.text if to
  ret
end

#dl_table_cleanup(xmldoc) ⇒ Object

move Key dl after table footer



23
24
25
26
27
28
29
30
31
32
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 23

def dl_table_cleanup(xmldoc)
  q = "//table/following-sibling::*[1]"\
    "[self::p and normalize-space() = 'Key']"
  xmldoc.xpath(q).each do |s|
    if !s.next_element.nil? && s.next_element.name == "dl"
      s.previous_element << s.next_element.remove
      s.remove
    end
  end
end

#docidentifier_cleanup(xmldoc) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/asciidoctor/iso/cleanup.rb', line 45

def docidentifier_cleanup(xmldoc)
  id = xmldoc.at("//bibdata/docidentifier/project-number")
  return unless id

  prefix = []
  xmldoc.xpath("//bibdata/contributor[role/@type = 'publisher']"\
               "/organization").each do |x|
    x1 = x.at("abbreviation")&.text || x.at("name")&.text
    x1 == "ISO" and prefix.unshift("ISO") or prefix << x1
  end

  id.content = prefix.join("/") + " " + id.text
end

#element_name_cleanup(xmldoc) ⇒ Object



80
81
82
# File 'lib/asciidoctor/iso/cleanup.rb', line 80

def element_name_cleanup(xmldoc)
  xmldoc.traverse { |n| n.name = n.name.gsub(/_/, "-") }
end

#empty_element_cleanup(xmldoc) ⇒ Object



74
75
76
77
78
# File 'lib/asciidoctor/iso/cleanup.rb', line 74

def empty_element_cleanup(xmldoc)
  xmldoc.xpath("//" + TEXT_ELEMS.join(" | //")).each do |x|
    x.remove if x.children.empty?
  end
end

#empty_text_before_first_element(x) ⇒ Object



202
203
204
205
206
207
208
209
210
# File 'lib/asciidoctor/iso/cleanup.rb', line 202

def empty_text_before_first_element(x)
  x.children.each do |c|
    if c.text?
      return false if /\S/.match?(c.text)
    end
    return true if c.element?
  end
  true
end

#extract_localities(x) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 17

def extract_localities(x)
  text = x.children.first.remove.text
  while (m = LOCALITY_RE.match text)
    ref = m[:ref] ? "<referenceFrom>#{m[:ref]}</referenceFrom>" : ""
    refto = m[:to] ? "<referenceTo>#{m[:to]}</referenceTo>" : ""
    loc = m[:locality]&.downcase || m[:locality2]&.downcase
    x.add_child("<locality type='#{loc}'>#{ref}#{refto}</locality>")
    text = m[:text]
  end
  x.add_child(text)
end

#figure_cleanup(xmldoc) ⇒ Object



107
108
109
110
111
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 107

def figure_cleanup(xmldoc)
  figure_footnote_cleanup(xmldoc)
  figure_dl_cleanup(xmldoc)
  subfigure_cleanup(xmldoc)
end

#figure_dl_cleanup(xmldoc) ⇒ Object

include key definition list inside figure



87
88
89
90
91
92
93
94
95
96
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 87

def figure_dl_cleanup(xmldoc)
  q = "//figure/following-sibling::*"\
    "[self::p and normalize-space() = 'Key']"
  xmldoc.xpath(q).each do |s|
    if !s.next_element.nil? && s.next_element.name == "dl"
      s.previous_element << s.next_element.remove
      s.remove
    end
  end
end

#figure_footnote_cleanup(xmldoc) ⇒ Object

include footnotes inside figure



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/asciidoctor/iso/cleanup_footnotes.rb', line 13

def figure_footnote_cleanup(xmldoc)
  nomatches = false
  until nomatches
    q = "//figure/following-sibling::*[1][self::p and *[1][self::fn]]"
    nomatches = true
    xmldoc.xpath(q).each do |s|
      s.previous_element << s.first_element_child.remove
      s.remove
      nomatches = false
    end
  end
end

#footnote_renumber(xmldoc) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/asciidoctor/iso/cleanup_footnotes.rb', line 87

def footnote_renumber(xmldoc)
  table_footnote_renumber(xmldoc)
  other_footnote_renumber(xmldoc)
  xmldoc.xpath("//fn").each do |fn|
    fn.delete("table")
  end
end

#format_ref(ref, isopub) ⇒ Object



87
88
89
90
91
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 87

def format_ref(ref, isopub)
  return ref if isopub
  return "[#{ref}]" if /^\d+$/.match?(ref) && !/^\[.*\]$/.match?(ref)
  ref
end

#formula_cleanup(x) ⇒ Object

include where definition list inside stem block



75
76
77
78
79
80
81
82
83
84
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 75

def formula_cleanup(x)
  q = "//formula/following-sibling::*[1]"\
    "[self::p and text() = 'where']"
  x.xpath(q).each do |s|
    if !s.next_element.nil? && s.next_element.name == "dl"
      s.previous_element << s.next_element.remove
      s.remove
    end
  end
end

#header_rows_cleanup(xmldoc) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 44

def header_rows_cleanup(xmldoc)
  xmldoc.xpath("//table[@headerrows]").each do |s|
    thead = insert_thead(s)
    (thead.xpath("./tr").size...s["headerrows"].to_i).each do
      row = s.at("./tbody/tr")
      row.parent = thead
    end
    s.delete("headerrows")
  end
end

#insert_thead(s) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 34

def insert_thead(s)
  thead = s.at("./thead")
  return thead unless thead.nil?
  if tname = s.at("./name")
    thead = tname.add_next_sibling("<thead/>").first
    return thead
  end
  s.children.first.add_previous_sibling("<thead/>").first
end


84
85
86
87
88
89
# File 'lib/asciidoctor/iso/cleanup.rb', line 84

def link_callouts_to_annotations(callouts, annotations)
  callouts.each_with_index do |c, i|
    c["target"] = "_" + UUIDTools::UUID.random_create
    annotations[i]["id"] = c["target"]
  end
end

#make_bibliography(x, s) ⇒ Object



123
124
125
126
127
128
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 123

def make_bibliography(x, s)
  if x.at("//sections/references")
    biblio = s.add_next_sibling("<bibliography/>").first
    x.xpath("//sections/references").each { |r| biblio.add_child r.remove }
  end
end

#make_preface(x, s) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 113

def make_preface(x, s)
  if x.at("//foreword | //introduction")
    preface = s.add_previous_sibling("<preface/>").first
    foreword = x.at("//foreword")
    preface.add_child foreword.remove if foreword
    introduction = x.at("//introduction")
    preface.add_child introduction.remove if introduction
  end
end

#mathml_cleanup(xmldoc) ⇒ Object



234
235
236
237
238
239
240
241
242
# File 'lib/asciidoctor/iso/cleanup.rb', line 234

def mathml_cleanup(xmldoc)
  xmldoc.xpath("//stem[@type = 'MathML']").each do |x|
    math = x.text.gsub(/&lt;/, "<").gsub(/&gt;/, ">").gsub(/&quot;/, '"').
      gsub(/&amp;/, "&").gsub(/<[^:\/]+:/, "<").gsub(/<\/[^:]+:/, "</").
      gsub(/ xmlns[^>]+/, "").
      gsub(/<math>/, '<math xmlns="http://www.w3.org/1998/Math/MathML">')
    x.children = math
  end
end

#merge_annotations_into_sourcecode(xmldoc) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/asciidoctor/iso/cleanup.rb', line 101

def merge_annotations_into_sourcecode(xmldoc)
  xmldoc.xpath("//sourcecode").each do |x|
    while x&.next_element&.name == "annotation"
      x.next_element.parent = x
    end
  end
end

#normref_cleanup(xmldoc) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 79

def normref_cleanup(xmldoc)
  q = "//references[title = 'Normative References']"
  r = xmldoc.at(q) || return
  r.elements.each do |n|
    n.remove unless ["title", "bibitem"].include? n.name
  end
end

#note_cleanup(xmldoc) ⇒ Object

if a note is at the end of a section, it is left alone if a note is followed by a non-note block, it is moved inside its preceding block if it is not delimited (so there was no way of making that block include the note)



193
194
195
196
197
198
199
200
# File 'lib/asciidoctor/iso/cleanup.rb', line 193

def note_cleanup(xmldoc)
  q = "//note[following-sibling::*[not(local-name() = 'note')]]"
  xmldoc.xpath(q).each do |n|
    next unless n.ancestors("table").empty?
    prev = n.previous_element || next
    n.parent = prev if ELEMS_ALLOW_NOTES.include? prev.name
  end
end

#notes_table_cleanup(xmldoc) ⇒ Object

move notes into table



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 62

def notes_table_cleanup(xmldoc)
  nomatches = false
  until nomatches
    q = "//table/following-sibling::*[1][self::note]"
    nomatches = true
    xmldoc.xpath(q).each do |n|
      n.previous_element << n.remove
      nomatches = false
    end
  end
end

#obligations_cleanup(x) ⇒ Object



137
138
139
140
141
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 137

def obligations_cleanup(x)
  obligations_cleanup_info(x)
  obligations_cleanup_norm(x)
  obligations_cleanup_inherit(x)
end

#obligations_cleanup_info(x) ⇒ Object



143
144
145
146
147
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 143

def obligations_cleanup_info(x)
  (s = x.at("//foreword")) && s["obligation"] = "informative"
  (s = x.at("//introduction")) && s["obligation"] = "informative"
  x.xpath("//references").each { |r| r["obligation"] = "informative" }
end

#obligations_cleanup_inherit(x) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 157

def obligations_cleanup_inherit(x)
  x.xpath("//annex | //clause").each do |r|
    r["obligation"] = "normative" unless r["obligation"]
  end
  x.xpath(Utils::SUBCLAUSE_XPATH).each do |r|
    r["obligation"] = r.at("./ancestor::*/@obligation").text
  end
end

#obligations_cleanup_norm(x) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 149

def obligations_cleanup_norm(x)
  (s = x.at("//clause[title = 'Scope']")) && s["obligation"] = "normative"
  (s = x.at("//clause[title = 'Symbols and Abbreviated Terms']")) &&
    s["obligation"] = "normative"
  x.xpath("//terms").each { |r| r["obligation"] = "normative" }
  x.xpath("//symbols-abbrevs").each { |r| r["obligation"] = "normative" }
end

#origin_cleanup(xmldoc) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 63

def origin_cleanup(xmldoc)
  xmldoc.xpath("//origin").each do |x|
    x["citeas"] = @anchors&.dig(x["bibitemid"], :xref) ||
      warn("ISO: #{x['bibitemid']} is not a real reference!")
    extract_localities(x) unless x.children.empty?
  end
end

#other_footnote_renumber(xmldoc) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/asciidoctor/iso/cleanup_footnotes.rb', line 73

def other_footnote_renumber(xmldoc)
  seen = {}
  i = 0
  xmldoc.xpath(PRE_NORMREF_FOOTNOTES).each do |fn|
    i, seen = other_footnote_renumber1(fn, i, seen)
  end
  xmldoc.xpath(NORMREF_FOOTNOTES).each do |fn|
    i, seen = other_footnote_renumber1(fn, i, seen)
  end
  xmldoc.xpath(POST_NORMREF_FOOTNOTES).each do |fn|
    i, seen = other_footnote_renumber1(fn, i, seen)
  end
end

#other_footnote_renumber1(fn, i, seen) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/asciidoctor/iso/cleanup_footnotes.rb', line 48

def other_footnote_renumber1(fn, i, seen)
  unless fn["table"]
    if seen[fn.text] then outnum = seen[fn.text]
    else
      i += 1
      outnum = i
      seen[fn.text] = outnum
    end
    fn["reference"] = outnum.to_s
  end
  [i, seen]
end

#para_cleanup(xmldoc) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 12

def para_cleanup(xmldoc)
  xmldoc.xpath("//p[not(@id)]").each do |x|
    x["id"] = Utils::anchor_or_uuid
  end
  xmldoc.xpath("//note[not(@id)][not(ancestor::bibitem)]"\
               "[not(ancestor::table)]").each do |x|
    x["id"] = Utils::anchor_or_uuid
  end
end

#quotesource_cleanup(xmldoc) ⇒ Object



57
58
59
60
61
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 57

def quotesource_cleanup(xmldoc)
  xmldoc.xpath("//quote/source | //terms/source").each do |x|
    xref_to_eref(x)
  end
end

#ref_cleanup(xmldoc) ⇒ Object

move ref before p



72
73
74
75
76
77
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 72

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

#reference_names(xmldoc) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 109

def reference_names(xmldoc)
  xmldoc.xpath("//bibitem[not(ancestor::bibitem)]").each do |ref|
    isopub = ref.at(ISO_PUBLISHER_XPATH)
    docid = ref.at("./docidentifier")
    date = ref.at("./date[@type = 'published']")
    reference = format_ref(docid.text, isopub)
    reference += ": #{date_range(date)}" if date && isopub
    @anchors[ref["id"]] = { xref: reference }
  end
end

#script_cleanup(xmldoc) ⇒ Object

it seems Nokogiri::XML is treating the content of <script> as cdata, because of its use in HTML. Bad nokogiri. Undoing that, since we use script as a normal tag



68
69
70
71
72
# File 'lib/asciidoctor/iso/cleanup.rb', line 68

def script_cleanup(xmldoc)
  xmldoc.xpath("//script").each do |x|
    x.content = x.to_str
  end
end

#sections_cleanup(x) ⇒ Object



130
131
132
133
134
135
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 130

def sections_cleanup(x)
  s = x.at("//sections")
  make_preface(x, s)
  make_bibliography(x, s)
  x.xpath("//sections/annex").reverse_each { |r| s.next = r.remove }
end

#strip_initial_space(x) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/asciidoctor/iso/cleanup.rb', line 212

def strip_initial_space(x)
  if x.children[0].text?
    if !/\S/.match?(x.children[0].text)
      x.children[0].remove
    else
      x.children[0].content = x.children[0].text.gsub(/^ /, "")
    end
  end
end

#subfigure_cleanup(xmldoc) ⇒ Object

examples containing only figures become subfigures of figures



99
100
101
102
103
104
105
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 99

def subfigure_cleanup(xmldoc)
  nodes = xmldoc.xpath("//example/figure")
  while !nodes.empty?
    nodes[0].parent.name = "figure"
    nodes = xmldoc.xpath("//example/figure")
  end
end

#table_cleanup(xmldoc) ⇒ Object



55
56
57
58
59
# File 'lib/asciidoctor/iso/cleanup_block.rb', line 55

def table_cleanup(xmldoc)
  dl_table_cleanup(xmldoc)
  notes_table_cleanup(xmldoc)
  header_rows_cleanup(xmldoc)
end

#table_footnote_renumber(xmldoc) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/asciidoctor/iso/cleanup_footnotes.rb', line 38

def table_footnote_renumber(xmldoc)
  xmldoc.xpath("//table | //figure").each do |t|
    seen = {}
    i = 0
    t.xpath(".//fn").each do |fn|
      i, seen = table_footnote_renumber1(fn, i, seen)
    end
  end
end

#table_footnote_renumber1(fn, i, seen) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/asciidoctor/iso/cleanup_footnotes.rb', line 26

def table_footnote_renumber1(fn, i, seen)
  if seen[fn.text] then outnum = seen[fn.text]
  else
    i += 1
    outnum = i
    seen[fn.text] = outnum
  end
  fn["reference"] = (outnum - 1 + "a".ord).chr
  fn["table"] = true
  [i, seen]
end

#termdef_boilerplate_cleanup(xmldoc) ⇒ Object



152
153
154
# File 'lib/asciidoctor/iso/cleanup.rb', line 152

def termdef_boilerplate_cleanup(xmldoc)
  xmldoc.xpath("//terms/p | //terms/ul").each(&:remove)
end

#termdef_cleanup(xmldoc) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/asciidoctor/iso/cleanup.rb', line 169

def termdef_cleanup(xmldoc)
  termdef_unnest_cleanup(xmldoc)
  termdef_stem_cleanup(xmldoc)
  termdomain_cleanup(xmldoc)
  termdefinition_cleanup(xmldoc)
  termdef_boilerplate_cleanup(xmldoc)
  termdef_subclause_cleanup(xmldoc)
  termdocsource_cleanup(xmldoc)
end

#termdef_stem_cleanup(xmldoc) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/asciidoctor/iso/cleanup.rb', line 114

def termdef_stem_cleanup(xmldoc)
  xmldoc.xpath("//term/p/stem").each do |a|
    if a.parent.elements.size == 1
      # para containing just a stem expression
      t = Nokogiri::XML::Element.new("admitted", xmldoc)
      parent = a.parent
      t.children = a.remove
      parent.replace(t)
    end
  end
end

#termdef_subclause_cleanup(xmldoc) ⇒ Object



156
157
158
159
160
# File 'lib/asciidoctor/iso/cleanup.rb', line 156

def termdef_subclause_cleanup(xmldoc)
  xmldoc.xpath("//terms[terms]").each do |t|
    t.name = "clause"
  end
end

#termdef_unnest_cleanup(xmldoc) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/asciidoctor/iso/cleanup.rb', line 143

def termdef_unnest_cleanup(xmldoc)
  # release termdef tags from surrounding paras
  nodes = xmldoc.xpath("//p/admitted | //p/deprecates")
  while !nodes.empty?
    nodes[0].parent.replace(nodes[0].parent.children)
    nodes = xmldoc.xpath("//p/admitted | //p/deprecates")
  end
end

#termdefinition_cleanup(xmldoc) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/asciidoctor/iso/cleanup.rb', line 133

def termdefinition_cleanup(xmldoc)
  xmldoc.xpath("//term").each do |d|
    first_child = d.at("./p | ./figure | ./formula") || return
    t = Nokogiri::XML::Element.new("definition", xmldoc)
    first_child.replace(t)
    t << first_child.remove
    d.xpath("./p | ./figure | ./formula").each { |n| t << n.remove }
  end
end

#termdocsource_cleanup(xmldoc) ⇒ Object



162
163
164
165
166
167
# File 'lib/asciidoctor/iso/cleanup.rb', line 162

def termdocsource_cleanup(xmldoc)
  f = xmldoc.at("//preface | //sections")
  xmldoc.xpath("//terms/termdocsource").each do |s|
    f.previous = s.remove
  end
end

#termdomain_cleanup(xmldoc) ⇒ Object



126
127
128
129
130
131
# File 'lib/asciidoctor/iso/cleanup.rb', line 126

def termdomain_cleanup(xmldoc)
  xmldoc.xpath("//p/domain").each do |a|
    prev = a.parent.previous
    prev.next = a.remove
  end
end

#textcleanup(text) ⇒ Object



13
14
15
# File 'lib/asciidoctor/iso/cleanup.rb', line 13

def textcleanup(text)
  text.gsub(/\s+<fn /, "<fn ")
end

#xref_cleanup(xmldoc) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 37

def xref_cleanup(xmldoc)
  xmldoc.xpath("//xref").each do |x|
    if refid? x["target"]
      x.name = "eref"
      xref_to_eref(x)
    else
      x.delete("type")
    end
  end
end

#xref_to_eref(x) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/asciidoctor/iso/cleanup_ref.rb', line 29

def xref_to_eref(x)
  x["bibitemid"] = x["target"]
  x["citeas"] = @anchors&.dig(x["target"], :xref) ||
    warn("ISO: #{x['target']} is not a real reference!")
  x.delete("target")
  extract_localities(x) unless x.children.empty?
end