module IsoDoc::WordFunction::Body
Constants
- SW1
- WORD_DT_ATTRS
Public Instance Methods
body_attr()
click to toggle source
# File lib/isodoc/word_function/body.rb, line 16 def body_attr { lang: "EN-US", link: "blue", vlink: "#954F72" } end
colgroup(node, t)
click to toggle source
# File lib/isodoc/word_function/table.rb, line 46 def colgroup(node, t) colgroup = node.at(ns("./colgroup")) or return t.colgroup do |cg| colgroup.xpath(ns("./col")).each do |c| cg.col **{ width: c["width"] } end end end
define_head(head, filename, _dir)
click to toggle source
Calls superclass method
# File lib/isodoc/word_function/body.rb, line 6 def define_head(head, filename, _dir) head.style do |style| loc = File.join(File.dirname(__FILE__), "..", "base_style", "metanorma_word.scss") stylesheet = File.read(loc, encoding: "utf-8") style.comment "\n#{stylesheet}\n" end super end
dl_parse(node, out)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 87 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)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 103 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)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 77 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)
click to toggle source
Calls superclass method
# File lib/isodoc/word_function/body.rb, line 183 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-anchor-horizontal:column;"\ "mso-table-overlap:never;border-collapse:collapse;"\ "#{keep_style(node)}", }) end
figure_aside_process(fig, aside, key)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 122 def figure_aside_process(fig, aside, key) # get rid of footnote link, it is in diagram fig&.at("./a[@class='TableFootnoteRef']")&.remove fnref = fig.at(".//span[@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(node)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 113 def figure_get_or_make_dl(node) dl = node.at(".//table[@class = 'dl']") if dl.nil? node.add_child("<p><b>#{@i18n.key}</b></p><table class='dl'></table>") dl = node.at(".//table[@class = 'dl']") end dl end
formula_parse1(node, out)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 201 def formula_parse1(node, out) out.div **attr_code(class: "formula") do |div| div.p do |_p| parse(node.at(ns("./stem")), div) insert_tab(div, 1) if lbl = node&.at(ns("./name"))&.text div << "(#{lbl})" end end end end
formula_where(deflist, out)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 193 def formula_where(deflist, out) return unless deflist out.p { |p| p << @i18n.where } parse(deflist, out) out.parent.at("./table")["class"] = "formula_dl" end
image_parse(node, out, caption)
click to toggle source
# File lib/isodoc/word_function/inline.rb, line 37 def image_parse(node, out, caption) attrs = { src: imgsrc(node), height: node["height"], alt: node["alt"], title: node["title"], width: node["width"] } out.img **attr_code(attrs) image_title_parse(out, caption) end
imgsrc(node)
click to toggle source
# File lib/isodoc/word_function/inline.rb, line 31 def imgsrc(node) ret = svg_to_emf(node) and return ret return node["src"] unless %r{^data:}.match node["src"] save_dataimage(node["src"]) end
inkscape_installed?()
click to toggle source
# File lib/isodoc/word_function/inline.rb, line 63 def inkscape_installed? cmd = "inkscape" exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each do |ext| exe = File.join(path, "#{cmd}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) end end nil end
insert_tab(out, count)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 49 def insert_tab(out, count) out.span **attr_code(style: "mso-tab-count:#{count}") do |span| [1..count].each { span << "  " } end end
li_parse(node, out)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 213 def li_parse(node, out) out.li **attr_code(id: node["id"]) do |li| if node["uncheckedcheckbox"] == "true" li << '<span class="zzMoveToFollowing">☐ </span>' elsif node["checkedcheckbox"] == "true" li << '<span class="zzMoveToFollowing">☑ </span>' end node.children.each { |n| parse(n, li) } end end
make_body1(body, _docxml)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 20 def make_body1(body, _docxml) body.div **{ class: "WordSection1" } do |div1| div1.p { |p| p << " " } # placeholder end section_break(body) end
make_body2(body, docxml)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 27 def make_body2(body, docxml) body.div **{ class: "WordSection2" } do |div2| boilerplate docxml, div2 preface_block docxml, div2 abstract docxml, div2 foreword docxml, div2 introduction docxml, div2 preface docxml, div2 acknowledgements docxml, div2 div2.p { |p| p << " " } # placeholder end section_break(body) end
make_body3(body, docxml)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 41 def make_body3(body, docxml) body.div **{ class: "WordSection3" } do |div3| middle docxml, div3 footnotes div3 comments div3 end end
make_tr_attr(td, row, totalrows, _header)
click to toggle source
# File lib/isodoc/word_function/table.rb, line 23 def make_tr_attr(td, row, totalrows, _header) 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"], valign: td["valign"], align: td["align"], style: style.gsub(/\n/, "") } end
new_fullcolspan_row(t, tfoot)
click to toggle source
# File lib/isodoc/word_function/table.rb, line 11 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)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 136 def note_p_parse(node, div) name = node&.at(ns("./name"))&.remove div.p **{ class: "Note" } do |p| p.span **{ class: "note_label" } do |s| name&.children&.each { |n| parse(n, s) } 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)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 148 def note_parse1(node, div) name = node&.at(ns("./name"))&.remove div.p **{ class: "Note" } do |p| p.span **{ class: "note_label" } do |s| name&.children&.each { |n| parse(n, s) } end insert_tab(p, 1) end node.children.each { |n| parse(n, div) } end
page_break(out)
click to toggle source
# File lib/isodoc/word_function/inline.rb, line 9 def page_break(out) out.p do |p| p.br **{ clear: "all", style: "mso-special-character:line-break;"\ "page-break-before:always" } end end
pagebreak_parse(node, out)
click to toggle source
# File lib/isodoc/word_function/inline.rb, line 17 def pagebreak_parse(node, out) return page_break(out) if node["orientation"].nil? out.p do |p| p.br **{clear: "all", class: "section", orientation: node["orientation"] } end end
para_attrs(node)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 172 def para_attrs(node) attrs = { class: para_class(node), id: node["id"], style: "" } unless node["align"].nil? attrs[:align] = node["align"] unless node["align"] == "justify" attrs[:style] += "text-align:#{node['align']};" end attrs[:style] += keep_style(node).to_s attrs[:style] = nil if attrs[:style].empty? attrs end
para_class(_node)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 55 def para_class(_node) classtype = nil classtype = "Note" if @note classtype = "MsoCommentText" if in_comment classtype = "Sourcecode" if @annotation classtype end
para_parse(node, out)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 63 def para_parse(node, out) out.p **attr_code(para_attrs(node)) do |p| unless @termdomain.empty? p << "<#{@termdomain}> " @termdomain = "" end node.children.each { |n| parse(n, p) unless n.name == "note" } end node.xpath(ns("./note")).each { |n| parse(n, out) } end
remove_bottom_border(td)
click to toggle source
# File lib/isodoc/word_function/table.rb, line 3 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
section_break(body)
click to toggle source
# File lib/isodoc/word_function/inline.rb, line 3 def section_break(body) body.p do |p| p.br **{ clear: "all", class: "section" } end end
suffix_url(url)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 224 def suffix_url(url) return url if %r{^https?://}.match?(url) return url unless File.extname(url).empty? url.sub(/#{File.extname(url)}$/, ".doc") end
svg_parse(node, out)
click to toggle source
# File lib/isodoc/word_function/inline.rb, line 25 def svg_parse(node, out) svg = Base64.strict_encode64(node.to_xml) r = node.replace("<img src='data:image/svg+xml;base64,#{svg}' mimetype='image/svg+xml'/>").first image_parse(r, out, nil) end
svg_to_emf(node)
click to toggle source
# File lib/isodoc/word_function/inline.rb, line 51 def svg_to_emf(node) return unless node["mimetype"] == "image/svg+xml" uri = node["src"] %r{^data:}.match(uri) and uri = save_dataimage(uri) ret = svg_to_emf_filename(uri) File.exists?(ret) and return ret exe = inkscape_installed? or return nil system %(#{exe} --export-type="emf" #{uri}) and return ret nil end
svg_to_emf_filename(uri)
click to toggle source
# File lib/isodoc/word_function/inline.rb, line 47 def svg_to_emf_filename(uri) File.join(File.dirname(uri), File.basename(uri, ".*")) + ".emf" end
table_attrs(node)
click to toggle source
Calls superclass method
# File lib/isodoc/word_function/table.rb, line 36 def table_attrs(node) super.merge(attr_code({ summary: node["summary"], width: node["width"], style: "mso-table-anchor-horizontal:column;"\ "mso-table-overlap:never;border-spacing:0;border-width:1px;#{keep_style(node)}", class: (node.text.length > 4000 ? "MsoISOTableBig" : "MsoISOTable") })) end
table_parse(node, out)
click to toggle source
# File lib/isodoc/word_function/table.rb, line 55 def table_parse(node, out) @in_table = true table_title_parse(node, out) out.div **{ align: "center", class: "table_container" } do |div| div.table **table_attrs(node) do |t| colgroup(node, 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)
click to toggle source
# File lib/isodoc/word_function/body.rb, line 159 def termnote_parse(node, out) name = node&.at(ns("./name"))&.remove out.div **note_attrs(node) do |div| div.p **{ class: "Note" } do |p| if name name.children.each { |n| parse(n, p) } p << l10n(": ") end para_then_remainder(node.first_element_child, node, p, div) end end end
xref_parse(node, out)
click to toggle source
# File lib/isodoc/word_function/inline.rb, line 75 def xref_parse(node, out) target = /#/.match(node["target"]) ? node["target"].sub(/#/, ".doc#") : "##{node["target"]}" out.a(**{ "href": target }) do |l| node.children.each { |n| parse(n, l) } end end