Module: IsoDoc::Function::Cleanup

Included in:
Common
Defined in:
lib/isodoc/function/cleanup.rb

Constant Summary collapse

FIGURE_WITH_FOOTNOTES =
"//div[@class = 'figure'][descendant::aside]"\
"[not(descendant::div[@class = 'figure'])]".freeze

Instance Method Summary collapse

Instance Method Details

#admonition_cleanup(docxml) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/isodoc/function/cleanup.rb', line 64

def admonition_cleanup(docxml)
  docxml.xpath("//div[@class = 'Admonition'][title]").each do |d|
    title = d.at("./title")
    n = title.next_element
    n&.children&.first
      &.add_previous_sibling("#{title.remove.text}—")
  end
  docxml
end

#break_up_long_strings(text) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/isodoc/function/cleanup.rb', line 42

def break_up_long_strings(text)
  return text if /^\s*$/.match?(text)

  text.split(/(?=\s)/).map do |w|
    if /^\s*$/.match(text) || (w.size < 30) then w
    else
      w.scan(/.{,30}/).map do |w1|
        w1.size < 30 ? w1 : break_up_long_strings1(w1)
      end.join
    end
  end.join
end

#break_up_long_strings1(text) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/isodoc/function/cleanup.rb', line 55

def break_up_long_strings1(text)
  s = text.split(%r{(?<=[,.?+;/=])})
  if s.size == 1 then "#{text} "
  else
    s[-1] = " #{s[-1]}"
    s.join
  end
end

#cleanup(docxml) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/isodoc/function/cleanup.rb', line 16

def cleanup(docxml)
  @i18n ||= i18n_init(@lang, @script, @locale)
  comment_cleanup(docxml)
  footnote_cleanup(docxml)
  inline_header_cleanup(docxml)
  figure_cleanup(docxml)
  table_cleanup(docxml)
  symbols_cleanup(docxml)
  example_cleanup(docxml)
  admonition_cleanup(docxml)
end

#example_cleanup(docxml) ⇒ Object



74
75
76
77
78
79
# File 'lib/isodoc/function/cleanup.rb', line 74

def example_cleanup(docxml)
  docxml.xpath("//table[@class = 'example']//p[not(@class)]").each do |p|
    p["class"] = "example"
  end
  docxml
end

#figure_aside_process(elem, aside, key) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/isodoc/function/cleanup.rb', line 94

def figure_aside_process(elem, aside, key)
  # get rid of footnote link, it is in diagram
  elem&.at("./a[@class='TableFootnoteRef']")&.remove
  fnref = elem.at(".//span[@class='TableFootnoteRef']/..")
  dt = key.add_child("<dt></dt>").first
  dd = key.add_child("<dd></dd>").first
  fnref.parent = dt
  aside.xpath(".//p").each do |a|
    a.delete("class")
    a.parent = dd
  end
end

#figure_cleanup(docxml) ⇒ Object

move footnotes into key, and get rid of footnote reference since it is in diagram



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/isodoc/function/cleanup.rb', line 109

def figure_cleanup(docxml)
  docxml.xpath(FIGURE_WITH_FOOTNOTES).each do |f|
    next unless f.at(".//aside[not(ancestor::p[@class = 'FigureTitle'])]")

    key = figure_get_or_make_dl(f)
    f.xpath(".//aside").each do |aside|
      figure_aside_process(f, aside, key)
    end
  end
  docxml
end

#figure_get_or_make_dl(elem) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/isodoc/function/cleanup.rb', line 81

def figure_get_or_make_dl(elem)
  dl = elem.at(".//dl")
  if dl.nil?
    elem.add_child("<p><b>#{@i18n.key}</b></p><dl></dl>")
    dl = elem.at(".//dl")
  end
  dl
end

#footnote_cleanup(docxml) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/isodoc/function/cleanup.rb', line 134

def footnote_cleanup(docxml)
  docxml.xpath('//a[@class = "FootnoteRef"]/sup')
    .each_with_index do |x, i|
    x.content = (i + 1).to_s
  end
  docxml
end

#footnote_reference_format(link) ⇒ Object



221
222
223
# File 'lib/isodoc/function/cleanup.rb', line 221

def footnote_reference_format(link)
  link
end

#inline_header_cleanup(docxml) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/isodoc/function/cleanup.rb', line 121

def inline_header_cleanup(docxml)
  docxml.xpath('//span[@class="zzMoveToFollowing"]').each do |x|
    x.delete("class")
    n = x.next_element
    if n.nil?
      x.name = "p"
    else
      n.children.first.previous = x.remove
    end
  end
  docxml
end

#merge_fnref_into_fn_text(elem) ⇒ Object



142
143
144
145
146
# File 'lib/isodoc/function/cleanup.rb', line 142

def merge_fnref_into_fn_text(elem)
  fn = elem.at('.//span[@class="TableFootnoteRef"]/..')
  n = fn.next_element
  n&.children&.first&.add_previous_sibling(fn.remove)
end

#new_fullcolspan_row(table, tfoot) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/isodoc/function/cleanup.rb', line 184

def new_fullcolspan_row(table, tfoot)
  # how many columns in the table?
  cols = 0
  table.at(".//tr").xpath("./td | ./th").each do |td|
    cols += (td["colspan"] ? td["colspan"].to_i : 1)
  end
  style =
    %{border-top:0pt;border-bottom:#{IsoDoc::Function::Table::SW} 1.5pt;}
  tfoot.add_child("<tr><td colspan='#{cols}' style='#{style}'/></tr>")
  tfoot.xpath(".//td").last
end

#passthrough_cleanup(docxml) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/isodoc/function/cleanup.rb', line 8

def passthrough_cleanup(docxml)
  docxml.split(%r{(<passthrough>|</passthrough>)}).each_slice(4)
    .map do |a|
    a.size > 2 and a[2] = HTMLEntities.new.decode(a[2])
    [a[0], a[2]]
  end.join
end

#remove_bottom_border(cell) ⇒ Object



168
169
170
171
# File 'lib/isodoc/function/cleanup.rb', line 168

def remove_bottom_border(cell)
  cell["style"] =
    cell["style"].gsub(/border-bottom:[^;]+;/, "border-bottom:0pt;")
end

#symbols_cleanup(docxml) ⇒ Object



215
# File 'lib/isodoc/function/cleanup.rb', line 215

def symbols_cleanup(docxml); end

#table_cleanup(docxml) ⇒ Object



208
209
210
211
212
213
# File 'lib/isodoc/function/cleanup.rb', line 208

def table_cleanup(docxml)
  table_footnote_cleanup(docxml)
  table_note_cleanup(docxml)
  table_long_strings_cleanup(docxml)
  docxml
end

#table_footnote_cleanup(docxml) ⇒ Object

preempt html2doc putting MsoNormal under TableFootnote class



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/isodoc/function/cleanup.rb', line 149

def table_footnote_cleanup(docxml)
  docxml.xpath("//table[descendant::aside]").each do |t|
    t.xpath(".//aside").each do |a|
      merge_fnref_into_fn_text(a)
      a.name = "div"
      a["class"] = "TableFootnote"
      t << a.remove
    end
  end
  table_footnote_cleanup_propagate(docxml)
end

#table_footnote_cleanup_propagate(docxml) ⇒ Object



161
162
163
164
165
166
# File 'lib/isodoc/function/cleanup.rb', line 161

def table_footnote_cleanup_propagate(docxml)
  docxml.xpath("//p[not(self::*[@class])]"\
               "[ancestor::*[@class = 'TableFootnote']]").each do |p|
    p["class"] = "TableFootnote"
  end
end

#table_footnote_reference_format(link) ⇒ Object



217
218
219
# File 'lib/isodoc/function/cleanup.rb', line 217

def table_footnote_reference_format(link)
  link
end

#table_get_or_make_tfoot(table) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/isodoc/function/cleanup.rb', line 173

def table_get_or_make_tfoot(table)
  tfoot = table.at(".//tfoot")
  if tfoot.nil?
    table.add_child("<tfoot></tfoot>")
    tfoot = table.at(".//tfoot")
  else
    tfoot.xpath(".//td | .//th").each { |td| remove_bottom_border(td) }
  end
  tfoot
end

#table_long_strings_cleanup(docxml) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/isodoc/function/cleanup.rb', line 28

def table_long_strings_cleanup(docxml)
  return unless @break_up_urls_in_tables == true

  docxml.xpath("//td | //th").each do |d|
    d.traverse do |n|
      next unless n.text?

      n.replace(HTMLEntities.new.encode(
                  break_up_long_strings(n.text),
                ))
    end
  end
end

#table_note_cleanup(docxml) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/isodoc/function/cleanup.rb', line 196

def table_note_cleanup(docxml)
  docxml.xpath("//table[div[@class = 'Note' or "\
               "@class = 'TableFootnote']]").each do |t|
    tfoot = table_get_or_make_tfoot(t)
    insert_here = new_fullcolspan_row(t, tfoot)
    t.xpath("div[@class = 'Note' or @class = 'TableFootnote']")
      .each do |d|
      d.parent = insert_here
    end
  end
end

#textcleanup(docxml) ⇒ Object



4
5
6
# File 'lib/isodoc/function/cleanup.rb', line 4

def textcleanup(docxml)
  passthrough_cleanup(docxml)
end