Module: IsoDoc::WordFunction::Body
- Included in:
- IsoDoc::WordConvert
- Defined in:
- lib/isodoc/word_function/body.rb
Constant Summary collapse
- SW1 =
"solid windowtext".freeze
- WORD_DT_ATTRS =
{class: @note ? "Note" : nil, align: "left", style: "margin-left:0pt;text-align:left;"}.freeze
Instance Method Summary collapse
- #anchor_names(docxml) ⇒ Object
- #dl_parse(node, out) ⇒ Object
- #dl_parse_notes(node, v) ⇒ Object
- #dt_parse(dt, term) ⇒ Object
- #example_table_attr(node) ⇒ Object
- #figure_aside_process(f, aside, key) ⇒ Object
- #figure_get_or_make_dl(t) ⇒ Object
- #image_parse(node, out, caption) ⇒ Object
- #imgsrc(uri) ⇒ Object
- #insert_tab(out, n) ⇒ Object
- #make_body1(body, _docxml) ⇒ Object
- #make_body2(body, docxml) ⇒ Object
- #make_body3(body, docxml) ⇒ Object
- #make_table_attr(node) ⇒ Object
- #make_tr_attr(td, row, totalrows) ⇒ Object
- #new_fullcolspan_row(t, tfoot) ⇒ Object
- #note_p_parse(node, div) ⇒ Object
- #note_parse1(node, div) ⇒ Object
- #page_break(out) ⇒ Object
- #para_attrs(node) ⇒ Object
- #para_class(_node) ⇒ Object
- #remove_bottom_border(td) ⇒ Object
-
#renumber_footnotes(docxml) ⇒ Object
undo merger of seen footnotes.
- #section_break(body) ⇒ Object
- #table_parse(node, out) ⇒ Object
- #termnote_parse(node, out) ⇒ Object
- #xref_parse(node, out) ⇒ Object
Instance Method Details
#anchor_names(docxml) ⇒ Object
3 4 5 6 |
# File 'lib/isodoc/word_function/body.rb', line 3 def anchor_names(docxml) super renumber_footnotes(docxml) end |
#dl_parse(node, out) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/isodoc/word_function/body.rb', line 115 def dl_parse(node, out) out.table **{ class: "dl" } do |v| node.elements.select { |n| dt_dd? n }.each_slice(2) do |dt, dd| v.tr do |tr| tr.td **{ valign: "top", align: "left" } do |term| dt_parse(dt, term) end tr.td **{ valign: "top" } do |listitem| dd.children.each { |n| parse(n, listitem) } end end end dl_parse_notes(node, v) end end |
#dl_parse_notes(node, v) ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/isodoc/word_function/body.rb', line 131 def dl_parse_notes(node, v) return if node.elements.reject { |n| dt_dd? n }.empty? v.tr do |tr| tr.td **{ colspan: 2 } do |td| node.elements.reject { |n| dt_dd? n }.each { |n| parse(n, td) } end end end |
#dt_parse(dt, term) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/isodoc/word_function/body.rb', line 105 def dt_parse(dt, term) term.p **attr_code(WORD_DT_ATTRS) do |p| if dt.elements.empty? p << dt.text else dt.children.each { |n| parse(n, p) } end end end |
#example_table_attr(node) ⇒ Object
227 228 229 230 231 232 233 234 |
# File 'lib/isodoc/word_function/body.rb', line 227 def example_table_attr(node) super.merge({ style: "mso-table-lspace:15.0cm;margin-left:423.0pt;"\ "mso-table-rspace:15.0cm;margin-right:423.0pt;mso-table-bspace:14.2pt;"\ "mso-table-anchor-vertical:paragraph;mso-table-anchor-horizontal:column;"\ "mso-table-overlap:never;border-collapse:collapse;" }) end |
#figure_aside_process(f, aside, key) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/isodoc/word_function/body.rb', line 149 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']") tr = key.add_child("<tr></tr>").first dt = tr.add_child("<td valign='top' align='left'></td>").first dd = tr.add_child("<td valign='top'></td>").first fnref.parent = dt aside.xpath(".//p").each do |a| a.delete("class") a.parent = dd end end |
#figure_get_or_make_dl(t) ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/isodoc/word_function/body.rb', line 140 def figure_get_or_make_dl(t) dl = t.at(".//table[@class = 'dl']") if dl.nil? t.add_child("<p><b>#{@key_lbl}</b></p><table class='dl'></table>") dl = t.at(".//table[@class = 'dl']") end dl end |
#image_parse(node, out, caption) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/isodoc/word_function/body.rb', line 209 def image_parse(node, out, caption) attrs = { src: imgsrc(node["src"]), height: node["height"], width: node["width"] } if node["height"] == "auto" || node["width"] == "auto" attrs[:height] = nil attrs[:width] = nil end out.img **attr_code(attrs) image_title_parse(out, caption) end |
#imgsrc(uri) ⇒ Object
204 205 206 207 |
# File 'lib/isodoc/word_function/body.rb', line 204 def imgsrc(uri) return uri unless %r{^data:image/}.match uri save_dataimage(uri) end |
#insert_tab(out, n) ⇒ Object
43 44 45 46 47 |
# File 'lib/isodoc/word_function/body.rb', line 43 def insert_tab(out, n) out.span **attr_code(style: "mso-tab-count:#{n}") do |span| [1..n].each { span << "  " } end end |
#make_body1(body, _docxml) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/isodoc/word_function/body.rb', line 16 def make_body1(body, _docxml) FileUtils.rm_rf tmpimagedir FileUtils.mkdir tmpimagedir body.div **{ class: "WordSection1" } do |div1| div1.p { |p| p << " " } # placeholder end section_break(body) end |
#make_body2(body, docxml) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/isodoc/word_function/body.rb', line 25 def make_body2(body, docxml) body.div **{ class: "WordSection2" } do |div2| abstract docxml, div2 foreword docxml, div2 introduction docxml, div2 div2.p { |p| p << " " } # placeholder end section_break(body) end |
#make_body3(body, docxml) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/isodoc/word_function/body.rb', line 35 def make_body3(body, docxml) body.div **{ class: "WordSection3" } do |div3| middle docxml, div3 footnotes div3 comments div3 end end |
#make_table_attr(node) ⇒ Object
237 238 239 240 241 242 243 244 |
# File 'lib/isodoc/word_function/body.rb', line 237 def make_table_attr(node) { style: "mso-table-lspace:15.0cm;margin-left:423.0pt;"\ "mso-table-rspace:15.0cm;margin-right:423.0pt;mso-table-bspace:14.2pt;"\ "mso-table-anchor-vertical:paragraph;mso-table-anchor-horizontal:column;"\ "mso-table-overlap:never;" }.merge(super) end |
#make_tr_attr(td, row, totalrows) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/isodoc/word_function/body.rb', line 76 def make_tr_attr(td, row, totalrows) style = td.name == "th" ? "font-weight:bold;" : "" rowmax = td["rowspan"] ? row + td["rowspan"].to_i - 1 : row style += <<~STYLE border-top:#{row.zero? ? "#{SW1} 1.5pt;" : 'none;'} mso-border-top-alt:#{row.zero? ? "#{SW1} 1.5pt;" : 'none;'} border-bottom:#{SW1} #{rowmax == totalrows ? '1.5' : '1.0'}pt; mso-border-bottom-alt:#{SW1} #{rowmax == totalrows ? '1.5' : '1.0'}pt; STYLE { rowspan: td["rowspan"], colspan: td["colspan"], align: td["align"], style: style.gsub(/\n/, "") } end |
#new_fullcolspan_row(t, tfoot) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/isodoc/word_function/body.rb', line 65 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;mso-border-top-alt:0pt;border-bottom:#{SW1} 1.5pt;mso-border-bottom-alt:#{SW1} 1.5pt;} tfoot.add_child("<tr><td colspan='#{cols}' style='#{style}'/></tr>") tfoot.xpath(".//td").last end |
#note_p_parse(node, div) ⇒ Object
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/isodoc/word_function/body.rb', line 163 def note_p_parse(node, div) div.p **{ class: "Note" } do |p| p.span **{ class: "note_label" } do |s| s << note_label(node) end insert_tab(p, 1) node.first_element_child.children.each { |n| parse(n, p) } end node.element_children[1..-1].each { |n| parse(n, div) } end |
#note_parse1(node, div) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/isodoc/word_function/body.rb', line 174 def note_parse1(node, div) div.p **{ class: "Note" } do |p| p.span **{ class: "note_label" } do |s| s << note_label(node) end insert_tab(p, 1) end node.children.each { |n| parse(n, div) } end |
#page_break(out) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/isodoc/word_function/body.rb', line 95 def page_break(out) out.p do |p| p.br **{ clear: "all", style: "mso-special-character:line-break;page-break-before:always" } end end |
#para_attrs(node) ⇒ Object
195 196 197 198 199 200 201 202 |
# File 'lib/isodoc/word_function/body.rb', line 195 def para_attrs(node) attrs = { class: para_class(node), id: node["id"] } unless node["align"].nil? attrs[:align] = node["align"] unless node["align"] == "justify" attrs[:style] = "text-align:#{node['align']}" end attrs end |
#para_class(_node) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/isodoc/word_function/body.rb', line 49 def para_class(_node) classtype = nil classtype = "Note" if @note classtype = "MsoCommentText" if in_comment classtype = "Sourcecode" if @annotation classtype end |
#remove_bottom_border(td) ⇒ Object
57 58 59 60 61 |
# File 'lib/isodoc/word_function/body.rb', line 57 def remove_bottom_border(td) td["style"] = td["style"].gsub(/border-bottom:[^;]+;/, "border-bottom:0pt;"). gsub(/mso-border-bottom-alt:[^;]+;/, "mso-border-bottom-alt:0pt;") end |
#renumber_footnotes(docxml) ⇒ Object
undo merger of seen footnotes
9 10 11 12 13 14 |
# File 'lib/isodoc/word_function/body.rb', line 9 def renumber_footnotes(docxml) docxml.xpath(ns("//fn[not(ancestor::table or "\ "ancestor::figure)]")).each_with_index do |f, i| f["reference"] = (i + 1).to_s end end |
#section_break(body) ⇒ Object
89 90 91 92 93 |
# File 'lib/isodoc/word_function/body.rb', line 89 def section_break(body) body.p do |p| p.br **{ clear: "all", class: "section" } end end |
#table_parse(node, out) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/isodoc/word_function/body.rb', line 246 def table_parse(node, out) @in_table = true table_title_parse(node, out) out.div **{ align: "center" } do |div| div.table **make_table_attr(node) do |t| thead_parse(node, t) tbody_parse(node, t) tfoot_parse(node, t) (dl = node.at(ns("./dl"))) && parse(dl, out) node.xpath(ns("./note")).each { |n| parse(n, out) } end end @in_table = false end |
#termnote_parse(node, out) ⇒ Object
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/isodoc/word_function/body.rb', line 184 def termnote_parse(node, out) out.div **{ class: "Note" } do |div| first = node.first_element_child div.p **{ class: "Note" } do |p| anchor = get_anchors[node['id']] p << "#{anchor&.dig(:label) || '???'}: " para_then_remainder(first, node, p, div) end end end |
#xref_parse(node, out) ⇒ Object
221 222 223 224 225 |
# File 'lib/isodoc/word_function/body.rb', line 221 def xref_parse(node, out) target = /#/.match(node["target"]) ? node["target"].sub(/#/, ".doc#") : "##{node["target"]}" out.a(**{ "href": target }) { |l| l << get_linkend(node) } end |