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
- #admonition_cleanup(docxml) ⇒ Object
- #break_up_long_strings(t) ⇒ Object
- #break_up_long_strings1(w1) ⇒ Object
- #cleanup(docxml) ⇒ Object
- #example_cleanup(docxml) ⇒ Object
- #figure_aside_process(f, aside, key) ⇒ Object
-
#figure_cleanup(docxml) ⇒ Object
move footnotes into key, and get rid of footnote reference since it is in diagram.
- #figure_get_or_make_dl(t) ⇒ Object
- #footnote_cleanup(docxml) ⇒ Object
- #footnote_reference_format(a) ⇒ Object
- #inline_header_cleanup(docxml) ⇒ Object
- #merge_fnref_into_fn_text(a) ⇒ Object
- #new_fullcolspan_row(t, tfoot) ⇒ Object
- #remove_bottom_border(td) ⇒ Object
- #symbols_cleanup(docxml) ⇒ Object
- #table_cleanup(docxml) ⇒ Object
- #table_footnote_cleanup(docxml) ⇒ Object
- #table_footnote_reference_format(a) ⇒ Object
- #table_get_or_make_tfoot(t) ⇒ Object
- #table_long_strings_cleanup(docxml) ⇒ Object
- #table_note_cleanup(docxml) ⇒ Object
- #textcleanup(docxml) ⇒ Object
Instance Method Details
#admonition_cleanup(docxml) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/isodoc/function/cleanup.rb', line 49 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(t) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/isodoc/function/cleanup.rb', line 31 def break_up_long_strings(t) t.split(/(?=\s)/).map do |w| (/^\s*$/.match(t) or w.size < 30) ? w : w.scan(/.{,30}/).map do |w1| (w1.size < 30) ? w1 : break_up_long_strings1(w1) end.join end.join end |
#break_up_long_strings1(w1) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/isodoc/function/cleanup.rb', line 40 def break_up_long_strings1(w1) s = w1.split(%r{(?<=[,.?+;/=])}) if s.size == 1 then w1 + " " else s[-1] = " " + s[-1] s.join end end |
#cleanup(docxml) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/isodoc/function/cleanup.rb', line 11 def cleanup(docxml) 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
58 59 60 61 62 63 |
# File 'lib/isodoc/function/cleanup.rb', line 58 def example_cleanup(docxml) docxml.xpath("//table[@class = 'example']//p[not(@class)]").each do |p| p["class"] = "example" end docxml end |
#figure_aside_process(f, aside, key) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/isodoc/function/cleanup.rb', line 78 def figure_aside_process(f, aside, key) # get rid of footnote link, it is in diagram f&.at("./a[@class='TableFootnoteRef']")&.remove fnref = f.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
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/isodoc/function/cleanup.rb', line 93 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(t) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/isodoc/function/cleanup.rb', line 65 def figure_get_or_make_dl(t) dl = t.at(".//dl") if dl.nil? t.add_child("<p><b>#{@key_lbl}</b></p><dl></dl>") dl = t.at(".//dl") end dl end |
#footnote_cleanup(docxml) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/isodoc/function/cleanup.rb', line 117 def footnote_cleanup(docxml) docxml.xpath('//a[@epub:type = "footnote"]/sup').each_with_index do |x, i| x.content = (i + 1).to_s end docxml end |
#footnote_reference_format(a) ⇒ Object
198 199 200 |
# File 'lib/isodoc/function/cleanup.rb', line 198 def footnote_reference_format(a) a end |
#inline_header_cleanup(docxml) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/isodoc/function/cleanup.rb', line 104 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(a) ⇒ Object
124 125 126 127 128 |
# File 'lib/isodoc/function/cleanup.rb', line 124 def merge_fnref_into_fn_text(a) fn = a.at('.//span[@class="TableFootnoteRef"]/..') n = fn.next_element n&.children&.first&.add_previous_sibling(fn.remove) end |
#new_fullcolspan_row(t, tfoot) ⇒ Object
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/isodoc/function/cleanup.rb', line 162 def new_fullcolspan_row(t, tfoot) # how many columns in the table? cols = 0 t.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 |
#remove_bottom_border(td) ⇒ Object
146 147 148 149 |
# File 'lib/isodoc/function/cleanup.rb', line 146 def remove_bottom_border(td) td["style"] = td["style"].gsub(/border-bottom:[^;]+;/, "border-bottom:0pt;") end |
#symbols_cleanup(docxml) ⇒ Object
191 192 |
# File 'lib/isodoc/function/cleanup.rb', line 191 def symbols_cleanup(docxml) end |
#table_cleanup(docxml) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/isodoc/function/cleanup.rb', line 184 def table_cleanup(docxml) table_footnote_cleanup(docxml) table_note_cleanup(docxml) table_long_strings_cleanup(docxml) docxml end |
#table_footnote_cleanup(docxml) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/isodoc/function/cleanup.rb', line 130 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 # preempt html2doc putting MsoNormal there docxml.xpath("//p[not(self::*[@class])]"\ "[ancestor::*[@class = 'TableFootnote']]").each do |p| p["class"] = "TableFootnote" end end |
#table_footnote_reference_format(a) ⇒ Object
194 195 196 |
# File 'lib/isodoc/function/cleanup.rb', line 194 def table_footnote_reference_format(a) a end |
#table_get_or_make_tfoot(t) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/isodoc/function/cleanup.rb', line 151 def table_get_or_make_tfoot(t) tfoot = t.at(".//tfoot") if tfoot.nil? t.add_child("<tfoot></tfoot>") tfoot = t.at(".//tfoot") else tfoot.xpath(".//td | .//th").each { |td| remove_bottom_border(td) } end tfoot end |
#table_long_strings_cleanup(docxml) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/isodoc/function/cleanup.rb', line 22 def table_long_strings_cleanup(docxml) docxml.xpath("//td | //th").each do |d| d.traverse do |n| next unless n.text? n.replace(break_up_long_strings(n.text)) end end end |
#table_note_cleanup(docxml) ⇒ Object
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/isodoc/function/cleanup.rb', line 173 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
3 4 5 6 7 8 9 |
# File 'lib/isodoc/function/cleanup.rb', line 3 def textcleanup(docxml) docxml. gsub(/\[TERMREF\]\s*/, l10n("[#{@source_lbl}: ")). gsub(/\s*\[MODIFICATION\]\s*\[\/TERMREF\]/, l10n(", #{@modified_lbl} [/TERMREF]")). gsub(/\s*\[\/TERMREF\]\s*/, l10n("]")). gsub(/\s*\[MODIFICATION\]/, l10n(", #{@modified_lbl} — ")) end |