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
- #cleanup(docxml) ⇒ Object
- #example_cleanup(docxml) ⇒ Object
- #extract_symbols_list(dl) ⇒ 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
- #inline_header_cleanup(docxml) ⇒ Object
- #merge_fnref_into_fn_text(a) ⇒ Object
- #new_fullcolspan_row(t, tfoot) ⇒ Object
- #remove_bottom_border(td) ⇒ Object
-
#symbol_key(x) ⇒ Object
We assume AsciiMath is being used in the terms & definitions.
- #symbols_cleanup(docxml) ⇒ Object
- #table_cleanup(docxml) ⇒ Object
- #table_footnote_cleanup(docxml) ⇒ Object
- #table_get_or_make_tfoot(t) ⇒ Object
- #table_note_cleanup(docxml) ⇒ Object
Instance Method Details
#admonition_cleanup(docxml) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/isodoc/function/cleanup.rb', line 14 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 |
#cleanup(docxml) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/isodoc/function/cleanup.rb', line 3 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
23 24 25 26 27 28 |
# File 'lib/isodoc/function/cleanup.rb', line 23 def example_cleanup(docxml) docxml.xpath("//table[@class = 'example']//p[not(@class)]").each do |p| p["class"] = "example" end docxml end |
#extract_symbols_list(dl) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/isodoc/function/cleanup.rb', line 163 def extract_symbols_list(dl) dl_out = [] dl.xpath("./dt | ./dd").each do |dtd| if dtd.name == "dt" dl_out << { dt: dtd.remove, key: symbol_key(dtd) } else dl_out.last[:dd] = dtd.remove end end dl_out end |
#figure_aside_process(f, aside, key) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/isodoc/function/cleanup.rb', line 43 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(".//a[@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
58 59 60 61 62 63 64 65 66 |
# File 'lib/isodoc/function/cleanup.rb', line 58 def figure_cleanup(docxml) docxml.xpath(FIGURE_WITH_FOOTNOTES).each do |f| 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
30 31 32 33 34 35 36 37 |
# File 'lib/isodoc/function/cleanup.rb', line 30 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
81 82 83 84 85 86 |
# File 'lib/isodoc/function/cleanup.rb', line 81 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 |
#inline_header_cleanup(docxml) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/isodoc/function/cleanup.rb', line 68 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
88 89 90 91 92 |
# File 'lib/isodoc/function/cleanup.rb', line 88 def merge_fnref_into_fn_text(a) fn = a.at('.//a[@class="TableFootnoteRef"]') n = fn.next_element n&.children&.first&.add_previous_sibling(fn.remove) end |
#new_fullcolspan_row(t, tfoot) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/isodoc/function/cleanup.rb', line 126 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
110 111 112 113 |
# File 'lib/isodoc/function/cleanup.rb', line 110 def remove_bottom_border(td) td["style"] = td["style"].gsub(/border-bottom:[^;]+;/, "border-bottom:0pt;") end |
#symbol_key(x) ⇒ Object
We assume AsciiMath is being used in the terms & definitions. Indices sort after letter but before any following letter (x, x_m, x_1, xa); we use colon to force that sort order. Numbers sort after letters; we use thorn to force that sort order.
158 159 160 161 |
# File 'lib/isodoc/function/cleanup.rb', line 158 def symbol_key(x) HTMLEntities.new.decode(x.text).gsub(/_/, ":").gsub(/`/, ""). gsub(/[0-9]+/, "รพ\\1") end |
#symbols_cleanup(docxml) ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/isodoc/function/cleanup.rb', line 175 def symbols_cleanup(docxml) dl = docxml.at("//div[@class = 'Symbols']/dl") return docxml unless dl dl_out = extract_symbols_list(dl) dl_out.sort! { |a, b| a[:key] <=> b[:key] } dl.replace(dl_out.map { |d| d[:dt].to_s + d[:dd].to_s }.join("\n")) docxml end |
#table_cleanup(docxml) ⇒ Object
148 149 150 151 152 |
# File 'lib/isodoc/function/cleanup.rb', line 148 def table_cleanup(docxml) table_footnote_cleanup(docxml) table_note_cleanup(docxml) docxml end |
#table_footnote_cleanup(docxml) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/isodoc/function/cleanup.rb', line 94 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_get_or_make_tfoot(t) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/isodoc/function/cleanup.rb', line 115 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_note_cleanup(docxml) ⇒ Object
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/isodoc/function/cleanup.rb', line 137 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 |