Class: Html2Doc
- Inherits:
-
Object
- Object
- Html2Doc
- Defined in:
- lib/html2doc/xml.rb,
lib/html2doc/base.rb,
lib/html2doc/math.rb,
lib/html2doc/mime.rb,
lib/html2doc/lists.rb,
lib/html2doc/notes.rb,
lib/html2doc/version.rb
Constant Summary collapse
- NOKOHEAD =
"<!DOCTYPE html SYSTEM\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head> <title></title> <meta charset=\"UTF-8\" /> </head>\n<body> </body> </html>\n".freeze
- DOCTYPE =
"<!DOCTYPE html SYSTEM \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n".freeze
- PRINT_VIEW =
"\n<xml>\n<w:WordDocument>\n<w:View>Print</w:View>\n<w:Zoom>100</w:Zoom>\n<w:DoNotOptimizeForBrowser/>\n</w:WordDocument>\n</xml>\n<meta http-equiv='Content-Type' content=\"text/html; charset=utf-8\"/>\n".freeze
- MATHML_NS =
"http://www.w3.org/1998/Math/MathML".freeze
- HTML_NS =
'xmlns="http://www.w3.org/1999/xhtml"'.freeze
- OOXML_NS =
"http://schemas.openxmlformats.org/officeDocument/2006/math".freeze
- STYLE_BEARING_NODE =
%w(p div td th li).map { |x| ".//ancestor::#{x}" }.join(" | ").freeze
- IMAGE_PATH =
"//*[local-name() = 'img' or local-name() = 'imagedata']".freeze
- IMAGE_IMAGEDATA =
".//*[local-name() = 'img' or local-name() = 'imagedata']".freeze
- TOPLIST =
"[not(ancestor::ul) and not(ancestor::ol)]".freeze
- FN =
"<span class='MsoFootnoteReference'>"\ "<span style='mso-special-character:footnote'/></span>".freeze
- VERSION =
"1.10.1".freeze
Instance Method Summary collapse
- #accent_tr(xml) ⇒ Object
- #accent_tr1(accent) ⇒ Object
- #add_stylesheet(head, title, css) ⇒ Object
- #bookmarks(docxml) ⇒ Object
- #cleanup(docxml) ⇒ Object
- #clear_dir(dir) ⇒ Object
- #contentid(mhtml) ⇒ Object
- #create_dir(filename, dir) ⇒ Object
- #define_head(docxml) ⇒ Object
- #define_head1(docxml, _dir) ⇒ Object
- #derive_liststyle(list, liststyle, level) ⇒ Object
- #encode_math(elem) ⇒ Object
- #filename_substitute(head, header_filename) ⇒ Object
-
#find_page_size(stylesheet, klass, in_xml) ⇒ Object
if in_xml, CSS is embedded in XML <style> tag.
- #find_page_size_in_doc(stylesheet, doc) ⇒ Object
- #footnote?(elem) ⇒ Boolean
-
#footnote_cleanup(docxml) ⇒ Object
We expect that the content of the footnote text received is one or more text containers, p or aside or div (which we have already converted to p).
- #footnote_container(docxml, idx) ⇒ Object
- #footnote_div_to_p(elem) ⇒ Object
- #footnotes(docxml) ⇒ Object
- #from_xhtml(xml) ⇒ Object
- #generate_filelist(filename, dir) ⇒ Object
-
#header_image_cleanup(doc, dir, filename, localdir) ⇒ Object
do not parse the header through Nokogiri, since it will contain non-XML like <![if !supportFootnotes]>.
- #header_image_cleanup1(a, dir, _filename, localdir) ⇒ Object
-
#image_cleanup(docxml, dir, localdir) ⇒ Object
only processes locally stored images.
- #image_resize(img, local_filename, maxheight, maxwidth, image_count) ⇒ Object
-
#initialize(hash) ⇒ Html2Doc
constructor
A new instance of Html2Doc.
- #landscape?(img) ⇒ Boolean
- #list2para(list) ⇒ Object
- #list2para_position(list) ⇒ Object
- #list_add(xpath, liststyles, listtype, level) ⇒ Object
- #list_add1(elem, liststyles, listtype, level) ⇒ Object
- #list_add_tail(list, liststyles, listtype, level) ⇒ Object
- #lists(docxml, liststyles) ⇒ Object
- #lists1(docxml, liststyles, style) ⇒ Object
- #lists_unstyled(docxml, liststyles) ⇒ Object
- #localname(src, localdir) ⇒ Object
- #locate_landscape(_docxml) ⇒ Object
- #math_block?(ooxml, mathml) ⇒ Boolean
- #math_only_para?(node) ⇒ Boolean
- #mathml_insert_rows(math, docnamespaces) ⇒ Object
- #mathml_preserve_space(math, docnamespaces) ⇒ Object
- #mathml_to_ooml(docxml) ⇒ Object
- #mathml_to_ooml1(xml, docnamespaces) ⇒ Object
- #mime_attachment(boundary, _filename, item, dir) ⇒ Object
- #mime_boundary ⇒ Object
- #mime_package(result, filename, dir) ⇒ Object
- #mime_preamble(boundary, filename, result) ⇒ Object
- #mime_type(item) ⇒ Object
- #mkuuid ⇒ Object
- #msonormal(docxml) ⇒ Object
- #msword_fix(doc) ⇒ Object
- #namespace(root) ⇒ Object
- #newliststyle(start, liststyle, newstylename, level) ⇒ Object
-
#ooml_clean(xml) ⇒ Object
We need span and em not to be namespaced.
- #ooxml_alignment(alignnode) ⇒ Object
-
#ooxml_cleanup(math, docnamespaces) ⇒ Object
random fixes to MathML input that OOXML needs to render properly.
-
#page_dimensions(docxml) ⇒ Object
we are going to use the 2nd instance of @page in the Word CSS, skipping the cover page.
- #parse_stylesheet_line_styles ⇒ Object
- #process(result) ⇒ Object
- #process_footnote_link(docxml, elem, indexes, footnote) ⇒ Object
- #process_footnote_link1(elem) ⇒ Object
-
#process_footnote_texts(docxml, footnotes, indexes) ⇒ Object
Currently cannot deal with separate footnote containers in each chapter We may eventually need to support that.
- #process_header(headerfile) ⇒ Object
- #process_html(result) ⇒ Object
- #progress_conv(idx, step, total, threshold, msg) ⇒ Object
- #read_stylesheet(cssname) ⇒ Object
- #rename_image(img, dir, localdir) ⇒ Object
- #rm_temp_files(filename, dir, dir1) ⇒ Object
- #rootnamespace(root) ⇒ Object
- #set_footnote_link_attrs(elem, idx) ⇒ Object
- #skip_image_cleanup?(img) ⇒ Boolean
- #style_list(elem, level, liststyle, listnumber) ⇒ Object
- #stylesheet(_filename, _header_filename, _cssname) ⇒ Object
- #to_plane1(xml, font) ⇒ Object
- #to_xhtml(xml) ⇒ Object
- #transform_footnote_text(note) ⇒ Object
-
#uncenter(math, ooxml) ⇒ Object
if oomml has no siblings, by default it is centered; override this with left/right if parent is so tagged also if ooml has mathPara already, or is in para with only oMath content.
- #uncenter_unneeded(math, ooxml, alignnode) ⇒ Object
- #unitalic(math) ⇒ Object
- #units_to_px(measure) ⇒ Object
- #unwrap_accents(doc) ⇒ Object
- #warnsvg(src) ⇒ Object
- #wrap_text(elem, wrapper) ⇒ Object
Constructor Details
#initialize(hash) ⇒ Html2Doc
Returns a new instance of Html2Doc.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/html2doc/base.rb', line 7 def initialize(hash) @filename = hash[:filename] @dir = hash[:dir] @dir1 = create_dir(@filename, @dir) @header_file = hash[:header_file] @asciimathdelims = hash[:asciimathdelims] @imagedir = hash[:imagedir] @debug = hash[:debug] @liststyles = hash[:liststyles] @stylesheet = read_stylesheet(hash[:stylesheet]) @c = HTMLEntities.new end |
Instance Method Details
#accent_tr(xml) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/html2doc/math.rb', line 177 def accent_tr(xml) xml.ooxml_xpath(".//accPr/chr").each do |x| x["m:val"] &&= accent_tr1(x["m:val"]) x["val"] &&= accent_tr1(x["val"]) end xml end |
#accent_tr1(accent) ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/html2doc/math.rb', line 185 def accent_tr1(accent) case accent when "\u2192" then "\u20D7" when "^" then "\u0302" when "~" then "\u0303" else accent end end |
#add_stylesheet(head, title, css) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/html2doc/base.rb', line 125 def add_stylesheet(head, title, css) if head.children.empty? head.add_child css elsif title.nil? head.children.first.add_previous_sibling css else title.add_next_sibling css end end |
#bookmarks(docxml) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/html2doc/base.rb', line 134 def bookmarks(docxml) docxml.xpath("//*[@id][not(@name)][not(@style = 'mso-element:footnote')]") .each do |x| (x["id"].empty? || x.namespace&.prefix == "v" && %w(shapetype shape rect line group).include?(x.name)) and next if x.children.empty? then x.add_child("<a name='#{x['id']}'></a>") else x.children.first.previous = "<a name='#{x['id']}'></a>" end x.delete("id") end end |
#cleanup(docxml) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/html2doc/base.rb', line 64 def cleanup(docxml) locate_landscape(docxml) namespace(docxml.root) image_cleanup(docxml, @dir1, @imagedir) mathml_to_ooml(docxml) lists(docxml, @liststyles) footnotes(docxml) bookmarks(docxml) msonormal(docxml) docxml end |
#clear_dir(dir) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/html2doc/base.rb', line 37 def clear_dir(dir) Dir.foreach(dir) do |f| fn = File.join(dir, f) File.delete(fn) if f != "." && f != ".." end dir end |
#contentid(mhtml) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/html2doc/mime.rb', line 69 def contentid(mhtml) mhtml.gsub %r{(<img[^<>]*?src=")([^"'<]+)(['"])}m do |m| repl = "#{$1}cid:#{File.basename($2)}#{$3}" /^data:|^https?:/ =~ $2 ? m : repl end.gsub %r{(<v:imagedata[^<>]*?src=")([^"'<]+)(['"])}m do |m| repl = "#{$1}cid:#{File.basename($2)}#{$3}" /^data:|^https?:/ =~ $2 ? m : repl end end |
#create_dir(filename, dir) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/html2doc/base.rb', line 45 def create_dir(filename, dir) dir and return clear_dir(dir) dir = "#{filename}_files" FileUtils.mkdir_p(dir) clear_dir(dir) end |
#define_head(docxml) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/html2doc/base.rb', line 115 def define_head(docxml) title = docxml.at("//*[local-name() = 'head']/*[local-name() = 'title']") head = docxml.at("//*[local-name() = 'head']") css = stylesheet(@filename, @header_file, @stylesheet) add_stylesheet(head, title, css) filename_substitute(head, @header_file) define_head1(docxml, @dir1) rootnamespace(docxml.root) end |
#define_head1(docxml, _dir) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/html2doc/base.rb', line 81 def define_head1(docxml, _dir) docxml.xpath("//*[local-name() = 'head']").each do |h| h.children.first.add_previous_sibling " \#{PRINT_VIEW}\n <link rel=\"File-List\" href=\"cid:filelist.xml\"/>\n XML\n end\nend\n" |
#derive_liststyle(list, liststyle, level) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/html2doc/lists.rb', line 43 def derive_liststyle(list, liststyle, level) list["start"] && list["start"] != "1" or return liststyle @liststyledefsidx += 1 ret = "l#{@liststyledefsidx}" @newliststyledefs += newliststyle(list["start"], liststyle, ret, level) ret end |
#encode_math(elem) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/html2doc/math.rb', line 52 def encode_math(elem) elem.traverse do |e| e.text? or next e.text.strip.empty? and next e.replace(@c.encode(e.text, :hexadecimal)) end elem end |
#filename_substitute(head, header_filename) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/html2doc/base.rb', line 90 def filename_substitute(head, header_filename) return if header_filename.nil? head.xpath(".//*[local-name() = 'style']").each do |s| s1 = s.to_xml.gsub(/url\("[^"]+"\)/) do |m| /FILENAME/.match?(m) ? "url(cid:header.html)" : m end s.replace(s1) end end |
#find_page_size(stylesheet, klass, in_xml) ⇒ Object
if in_xml, CSS is embedded in XML <style> tag
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/html2doc/mime.rb', line 172 def find_page_size(stylesheet, klass, in_xml) xml_found = false found = false ret = "" stylesheet&.lines&.each do |l| in_xml && l.include?("<style") and xml_found = true and found = false in_xml && l.include?("</style>") and xml_found = false /^\s*@page\s+#{klass}/.match?(l) and found = true found && /^\s*\{?size:/.match?(l) and ret += l found && /^\s*\{?margin:/.match?(l) and ret += l if found && /}/.match?(l) !ret.blank? && (!in_xml || xml_found) and return ret ret = "" found = false end end nil end |
#find_page_size_in_doc(stylesheet, doc) ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/html2doc/mime.rb', line 163 def find_page_size_in_doc(stylesheet, doc) find_page_size(stylesheet, "WordSection2", false) || find_page_size(stylesheet, "WordSection3", false) || find_page_size(doc, "WordSection2", true) || find_page_size(doc, "WordSection3", true) || find_page_size(stylesheet, "", false) || find_page_size(doc, "", true) end |
#footnote?(elem) ⇒ Boolean
93 94 95 96 |
# File 'lib/html2doc/notes.rb', line 93 def footnote?(elem) elem["epub:type"]&.casecmp("footnote")&.zero? || elem["class"]&.casecmp("footnote")&.zero? end |
#footnote_cleanup(docxml) ⇒ Object
We expect that the content of the footnote text received is one or more text containers, p or aside or div (which we have already converted to p). We do not expect any <a name> or links back to text; if they are present in the HTML, they need to have been cleaned out before passing to this gem
110 111 112 113 114 115 116 117 |
# File 'lib/html2doc/notes.rb', line 110 def footnote_cleanup(docxml) docxml.xpath('//div[@style="mso-element:footnote"]/a') .each do |x| n = x.next_element n&.children&.first&.add_previous_sibling(x.remove) end docxml end |
#footnote_container(docxml, idx) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/html2doc/notes.rb', line 45 def footnote_container(docxml, idx) ref = docxml&.at("//a[@href='#_ftn#{idx}']")&.children&.to_xml(indent: 0) &.gsub(/>\n</, "><") || FN " <div style='mso-element:footnote' id='ftn\#{idx}'>\n <a style='mso-footnote-id:ftn\#{idx}' href='#_ftn\#{idx}'\n name='_ftnref\#{idx}' title='' id='_ftnref\#{idx}'>\#{ref.strip}</a></div>\n DIV\nend\n" |
#footnote_div_to_p(elem) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/html2doc/notes.rb', line 31 def footnote_div_to_p(elem) if %w{div aside}.include? elem.name if elem.at(".//p") elem.replace(elem.children) else elem.name = "p" elem["class"] = "MsoFootnoteText" end end end |
#footnotes(docxml) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/html2doc/notes.rb', line 4 def footnotes(docxml) #i = 1 indexes = {} @footnote_idx = 1 fn = [] docxml.xpath("//a").each do |a| process_footnote_link(docxml, a, indexes, fn) or next #i += 1 end process_footnote_texts(docxml, fn, indexes) end |
#from_xhtml(xml) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/html2doc/xml.rb', line 25 def from_xhtml(xml) xml.to_xml.sub(%{ xmlns="http://www.w3.org/1999/xhtml"}, "") .sub(DOCTYPE, "").gsub(%{ />}, "/>") .gsub(/<!-- MSWORD-COMMENT (.+?) -->/, "<!--[\\1]>") .gsub(/<!-- MSWORD-COMMENT-END -->/, "<![endif]-->") .gsub("\n-->\n", "\n-->\n") end |
#generate_filelist(filename, dir) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/html2doc/mime.rb', line 222 def generate_filelist(filename, dir) File.open(File.join(dir, "filelist.xml"), "w") do |f| f.write %{<xml xmlns:o="urn:schemas-microsoft-com:office:office"> <o:MainFile HRef="../#{filename}.htm"/>} Dir.entries(dir).sort.each do |item| (item == "." || item == ".." || /^\./.match(item)) and next f.write %{ <o:File HRef="#{item}"/>\n} end f.write("</xml>\n") end end |
#header_image_cleanup(doc, dir, filename, localdir) ⇒ Object
do not parse the header through Nokogiri, since it will contain non-XML like <![if !supportFootnotes]>
204 205 206 207 208 |
# File 'lib/html2doc/mime.rb', line 204 def header_image_cleanup(doc, dir, filename, localdir) doc.split(%r{(<img [^>]*>|<v:imagedata [^>]*>)}).each_slice(2).map do |a| header_image_cleanup1(a, dir, filename, localdir) end.join end |
#header_image_cleanup1(a, dir, _filename, localdir) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/html2doc/mime.rb', line 210 def header_image_cleanup1(a, dir, _filename, localdir) if a.size == 2 && !(/ src="https?:/.match a[1]) && !(%r{ src="data:(image|application)/[^;]+;base64}.match a[1]) m = / src=['"](?<src>[^"']+)['"]/.match a[1] m2 = /\.(?<suffix>[a-zA-Z_0-9]+)$/.match m[:src] new_filename = "#{mkuuid}.#{m2[:suffix]}" FileUtils.cp localname(m[:src], localdir), File.join(dir, new_filename) a[1].sub!(%r{ src=['"](?<src>[^"']+)['"]}, " src='cid:#{new_filename}'") end a.join end |
#image_cleanup(docxml, dir, localdir) ⇒ Object
only processes locally stored images
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/html2doc/mime.rb', line 97 def image_cleanup(docxml, dir, localdir) maxheight, maxwidth = page_dimensions(docxml) docxml.xpath(IMAGE_IMAGEDATA).each do |i| skip_image_cleanup?(i) and next local_filename = rename_image(i, dir, localdir) if tr_ancestor = i.xpath("ancestor::*[local-name() = 'tr']").first image_count = tr_ancestor.xpath(IMAGE_IMAGEDATA).count image_resize(i, local_filename, maxheight, maxwidth, image_count) else # Normal behavior for non-table images image_resize(i, local_filename, maxheight, maxwidth, 1) end end docxml end |
#image_resize(img, local_filename, maxheight, maxwidth, image_count) ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/html2doc/mime.rb', line 112 def image_resize(img, local_filename, maxheight, maxwidth, image_count) img["width"], img["height"] = if landscape?(img) Vectory.image_resize(img, local_filename, maxwidth, maxheight / image_count) else Vectory.image_resize(img, local_filename, maxheight, maxwidth / image_count) end end |
#landscape?(img) ⇒ Boolean
123 124 125 126 127 128 129 |
# File 'lib/html2doc/mime.rb', line 123 def landscape?(img) img.ancestors.each do |a| a.name == "div" or next @landscape.include?(a["class"]) and return true end false end |
#list2para(list) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/html2doc/lists.rb', line 71 def list2para(list) list.xpath("./li").empty? and return list2para_position(list) list.xpath("./li").each do |l| l.name = "p" l["class"] ||= "MsoListParagraphCxSpMiddle" l.first_element_child&.name == "p" or next l["style"] ||= "" l["style"] += l.first_element_child["style"] &.sub(/mso-list[^;]+;/, "") || "" l.first_element_child.replace(l.first_element_child.children) end list.replace(list.children) end |
#list2para_position(list) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/html2doc/lists.rb', line 86 def list2para_position(list) list.xpath("./li").first["class"] ||= "MsoListParagraphCxSpFirst" list.xpath("./li").last["class"] ||= "MsoListParagraphCxSpLast" list.xpath("./li/p").each do |p| p["class"] ||= "MsoListParagraphCxSpMiddle" end end |
#list_add(xpath, liststyles, listtype, level) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/html2doc/lists.rb', line 30 def list_add(xpath, liststyles, listtype, level) xpath.each do |l| level == 1 && l["seen"] = true and @listnumber += 1 l["id"] ||= UUIDTools::UUID.random_create liststyle = derive_liststyle(l, liststyles[listtype], level) (l.xpath(".//li") - l.xpath(".//ol//li | .//ul//li")).each do |li| style_list(li, level, liststyle, @listnumber) list_add1(li, liststyles, listtype, level) end list_add_tail(l, liststyles, listtype, level) end end |
#list_add1(elem, liststyles, listtype, level) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/html2doc/lists.rb', line 16 def list_add1(elem, liststyles, listtype, level) if i[ul ol].include? listtype list_add(elem.xpath(".//ul") - elem.xpath(".//ul//ul | .//ol//ul"), liststyles, :ul, level + 1) list_add(elem.xpath(".//ol") - elem.xpath(".//ul//ol | .//ol//ol"), liststyles, :ol, level + 1) else list_add(elem.xpath(".//ul") - elem.xpath(".//ul//ul | .//ol//ul"), liststyles, listtype, level + 1) list_add(elem.xpath(".//ol") - elem.xpath(".//ul//ol | .//ol//ol"), liststyles, listtype, level + 1) end end |
#list_add_tail(list, liststyles, listtype, level) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/html2doc/lists.rb', line 63 def list_add_tail(list, liststyles, listtype, level) list.xpath(".//ul[not(ancestor::li/ancestor::*/@id = '#{list['id']}')] | "\ ".//ol[not(ancestor::li/ancestor::*/@id = '#{list['id']}')]") .each do |li| list_add1(li.parent, liststyles, listtype, level - 1) end end |
#lists(docxml, liststyles) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/html2doc/lists.rb', line 121 def lists(docxml, liststyles) liststyles.nil? and return parse_stylesheet_line_styles liststyles.each_key { |k| lists1(docxml, liststyles, k) } lists_unstyled(docxml, liststyles) liststyles.has_key?(:ul) and docxml.xpath("//ul").each { |u| list2para(u) } liststyles.has_key?(:ol) and docxml.xpath("//ol").each { |u| list2para(u) } end |
#lists1(docxml, liststyles, style) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/html2doc/lists.rb', line 96 def lists1(docxml, liststyles, style) case style when :ul then list_add(docxml.xpath("//ul[not(@class)]#{TOPLIST}"), liststyles, :ul, 1) when :ol then list_add(docxml.xpath("//ol[not(@class)]#{TOPLIST}"), liststyles, :ol, 1) else list_add(docxml.xpath("//ol[@class = '#{style}']#{TOPLIST} | "\ "//ul[@class = '#{style}']#{TOPLIST}"), liststyles, style, 1) end end |
#lists_unstyled(docxml, liststyles) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/html2doc/lists.rb', line 109 def lists_unstyled(docxml, liststyles) liststyles.has_key?(:ul) and list_add(docxml.xpath("//ul#{TOPLIST}[not(@seen)]"), liststyles, :ul, 1) liststyles.has_key?(:ol) and list_add(docxml.xpath("//ol#{TOPLIST}[not(@seen)]"), liststyles, :ul, 1) docxml.xpath("//ul[@seen] | //ol[@seen]").each do |l| l.delete("seen") end end |
#localname(src, localdir) ⇒ Object
89 90 91 |
# File 'lib/html2doc/mime.rb', line 89 def localname(src, localdir) %r{^([A-Z]:)?/}.match?(src) ? src : File.join(localdir, src) end |
#locate_landscape(_docxml) ⇒ Object
76 77 78 79 |
# File 'lib/html2doc/base.rb', line 76 def locate_landscape(_docxml) @landscape = @stylesheet.scan(/div\.\S+\s+\{\s*page:\s*[^;]+L;\s*\}/m) .map { |e| e.sub(/^div\.(\S+).*$/m, "\\1") } end |
#math_block?(ooxml, mathml) ⇒ Boolean
205 206 207 208 209 |
# File 'lib/html2doc/math.rb', line 205 def math_block?(ooxml, mathml) # ooxml.name == "oMathPara" || mathml["displaystyle"] == "true" mathml["displaystyle"] == "true" && ooxml.xpath("./m:oMath", "m" => OOXML_NS).size <= 1 end |
#math_only_para?(node) ⇒ Boolean
196 197 198 199 200 201 202 203 |
# File 'lib/html2doc/math.rb', line 196 def math_only_para?(node) x = node.dup x.xpath(".//m:math", "m" => MATHML_NS).each(&:remove) x.xpath(".//m:oMathPara | .//m:oMath", "m" => OOXML_NS).each(&:remove) x.xpath(".//m:oMathPara | .//m:oMath").each(&:remove) # namespace can go missing during processing x.text.strip.empty? end |
#mathml_insert_rows(math, docnamespaces) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/html2doc/math.rb', line 61 def mathml_insert_rows(math, docnamespaces) math.xpath(%w(msup msub msubsup munder mover munderover) .map { |m| ".//xmlns:#{m}" }.join(" | "), docnamespaces).each do |x| next unless x.next_element && x.next_element != "mrow" x.next_element.wrap("<mrow/>") end math end |
#mathml_preserve_space(math, docnamespaces) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/html2doc/math.rb', line 71 def mathml_preserve_space(math, docnamespaces) math.xpath(".//xmlns:mtext", docnamespaces).each do |x| x.children = x.children.to_xml.gsub(/^\s/, " ").gsub(/\s$/, " ") end math end |
#mathml_to_ooml(docxml) ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/html2doc/math.rb', line 145 def mathml_to_ooml(docxml) docnamespaces = docxml.collect_namespaces m = docxml.xpath("//*[local-name() = 'math']") m.each_with_index do |x, i| progress_conv(i, 100, m.size, 500, "Math OOXML") mathml_to_ooml1(x, docnamespaces) end end |
#mathml_to_ooml1(xml, docnamespaces) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/html2doc/math.rb', line 165 def mathml_to_ooml1(xml, docnamespaces) doc = Nokogiri::XML::Document::new doc.root = ooxml_cleanup(xml, docnamespaces) # d = xml.parent["block"] != "false" # display_style ooxml = Nokogiri::XML(Plurimath::Math .parse(doc.root.to_xml(indent: 0), :mathml) .to_omml(split_on_linebreak: true)) ooxml = unitalic(accent_tr(ooxml)) ooxml = ooml_clean(uncenter(xml, ooxml)) xml.swap(ooxml) end |
#mime_attachment(boundary, _filename, item, dir) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/html2doc/mime.rb', line 23 def (boundary, _filename, item, dir) content_type = mime_type(item) text_mode = %w[text application].any? { |p| content_type.start_with? p } path = File.join(dir, item) content = text_mode ? File.read(path, encoding: "utf-8") : IO.binread(path) encoded_file = Base64.strict_encode64(content).gsub(/(.{76})/, "\\1\n") " --\#{boundary}\n Content-ID: <\#{File.basename(item)}>\n Content-Disposition: inline; filename=\"\#{File.basename(item)}\"\n Content-Transfer-Encoding: base64\n Content-Type: \#{content_type}\n\n \#{encoded_file}\n\n FILE\nend\n" |
#mime_boundary ⇒ Object
50 51 52 53 |
# File 'lib/html2doc/mime.rb', line 50 def mime_boundary salt = UUIDTools::UUID.random_create.to_s.tr("-", ".")[0..17] "----=_NextPart_#{salt}" end |
#mime_package(result, filename, dir) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/html2doc/mime.rb', line 55 def mime_package(result, filename, dir) boundary = mime_boundary mhtml = mime_preamble(boundary, "#{filename}.htm", result) mhtml += (boundary, "#{filename}.htm", "filelist.xml", dir) Dir.foreach(dir) do |item| next if item == "." || item == ".." || /^\./.match(item) || item == "filelist.xml" mhtml += (boundary, "#{filename}.htm", item, dir) end mhtml += "--#{boundary}--" File.open("#{filename}.doc", "w:UTF-8") { |f| f.write contentid(mhtml) } end |
#mime_preamble(boundary, filename, result) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/html2doc/mime.rb', line 8 def mime_preamble(boundary, filename, result) " MIME-Version: 1.0\n Content-Type: multipart/related; boundary=\"\#{boundary}\"\n\n --\#{boundary}\n Content-ID: <\#{File.basename(filename)}>\n Content-Disposition: inline; filename=\"\#{File.basename(filename)}\"\n Content-Type: text/html; charset=\"utf-8\"\n\n \#{result}\n\n PREAMBLE\nend\n" |
#mime_type(item) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/html2doc/mime.rb', line 43 def mime_type(item) types = MIME::Types.type_for(item) type = types ? types.first.to_s : 'text/plain; charset="utf-8"' type = %(#{type} charset="utf-8") if /^text/.match(type) && types type end |
#mkuuid ⇒ Object
81 82 83 |
# File 'lib/html2doc/mime.rb', line 81 def mkuuid UUIDTools::UUID.random_create.to_s end |
#msonormal(docxml) ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/html2doc/base.rb', line 146 def msonormal(docxml) docxml.xpath("//*[local-name() = 'p'][not(self::*[@class])]").each do |p| p["class"] = "MsoNormal" end docxml.xpath("//*[local-name() = 'li'][not(self::*[@class])]").each do |p| p["class"] = "MsoNormal" end end |
#msword_fix(doc) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/html2doc/xml.rb', line 33 def msword_fix(doc) # brain damage in MSWord parser doc.gsub!(%r{<w:DoNotOptimizeForBrowser></w:DoNotOptimizeForBrowser>}, "<w:DoNotOptimizeForBrowser/>") doc.gsub!(%r{<span style="mso-special-character:footnote"/>}, '<span style="mso-special-character:footnote"></span>') doc.gsub!(%r{<div style="mso-element:footnote-list"></div>}, '<div style="mso-element:footnote-list"/>') doc.gsub!(%r{(<a style="mso-comment-reference:[^<>/]+)/>}, "\\1></a>") doc.gsub!(%r{<link rel="File-List"}, "<link rel=File-List") doc.gsub!(%r{<meta http-equiv="Content-Type"}, "<meta http-equiv=Content-Type") doc.gsub!(%r{></m:jc>}, "/>") doc.gsub!(%r{></v:stroke>}, "/>") doc.gsub!(%r{></v:f>}, "/>") doc.gsub!(%r{></v:path>}, "/>") doc.gsub!(%r{></o:lock>}, "/>") doc.gsub!(%r{></v:imagedata>}, "/>") doc.gsub!(%r{></w:wrap>}, "/>") doc.gsub!(%r{<(/)?m:(span|em)\b}, "<\\1\\2") doc.gsub!(%r{&tab;|&tab;}, '<span style="mso-tab-count:1">  </span>') doc.split(%r{(<m:oMath>|</m:oMath>)}).each_slice(4).map do |a| a.size > 2 and a[2] = a[2].gsub(/>\s+</, "><") a end.join end |
#namespace(root) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/html2doc/xml.rb', line 73 def namespace(root) { o: "urn:schemas-microsoft-com:office:office", w: "urn:schemas-microsoft-com:office:word", v: "urn:schemas-microsoft-com:vml", m: "http://schemas.microsoft.com/office/2004/12/omml" }.each { |k, v| root.add_namespace_definition(k.to_s, v) } end |
#newliststyle(start, liststyle, newstylename, level) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/html2doc/lists.rb', line 51 def newliststyle(start, liststyle, newstylename, level) s = @liststyledefs[liststyle] .gsub(/@list\s+#{liststyle}/, "@list #{newstylename}") .sub(/@list\s+#{newstylename}\s+\{[^}]*\}/m, " @list \#{newstylename}\\n{mso-list-id:\#{rand(100_000_000..999_999_999)};\n mso-list-template-ids:\#{rand(100_000_000..999_999_999)};}\n LISTSTYLE\n .sub(/@list\\s+\#{newstylename}:level\#{level}\\s+\\{/m,\n \"\\\\0mso-level-start-at:\#{start};\\n\")\n \"\#{s}\\n\"\nend\n") |
#ooml_clean(xml) ⇒ Object
We need span and em not to be namespaced. Word can’t deal with explicit namespaces. We will end up stripping them out again under Nokogiri 1.11, which correctly insists on inheriting namespace from parent.
158 159 160 161 162 163 |
# File 'lib/html2doc/math.rb', line 158 def ooml_clean(xml) xml.to_xml(indent: 0) .gsub(/<\?[^>]+>\s*/, "") .gsub(/ xmlns(:[^=]+)?="[^"]+"/, "") # .gsub(%r{<(/)?(?!span)(?!em)([a-z])}, "<\\1m:\\2") end |
#ooxml_alignment(alignnode) ⇒ Object
228 229 230 231 232 233 |
# File 'lib/html2doc/math.rb', line 228 def ooxml_alignment(alignnode) dir = "left" /text-align:\s*right/.match?(alignnode["style"]) and dir = "right" /text-align:\s*center/.match?(alignnode["style"]) and dir = "center" dir end |
#ooxml_cleanup(math, docnamespaces) ⇒ Object
random fixes to MathML input that OOXML needs to render properly
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/html2doc/math.rb', line 40 def ooxml_cleanup(math, docnamespaces) # encode_math( unwrap_accents( mathml_preserve_space( mathml_insert_rows(math, docnamespaces), docnamespaces ), ) # ) math.add_namespace(nil, MATHML_NS) math end |
#page_dimensions(docxml) ⇒ Object
we are going to use the 2nd instance of @page in the Word CSS, skipping the cover page. Currently doesn’t deal with Landscape. Scan both @stylesheet and docxml.to_xml (where @standardstylesheet has ended up) Allow 0.9 * height to fit caption
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/html2doc/mime.rb', line 151 def page_dimensions(docxml) page_size = find_page_size_in_doc(@stylesheet, docxml.to_xml) or return [680, 400] m_size = /size:\s*(\S+)\s+(\S+)\s*;/.match(page_size) or return [680, 400] m_marg = /margin:\s*(\S+)\s+(\S+)\s*(\S+)\s*(\S+)\s*;/.match(page_size) or return [680, 400] [0.9 * (units_to_px(m_size[2]) - units_to_px(m_marg[1]) - units_to_px(m_marg[3])), units_to_px(m_size[1]) - units_to_px(m_marg[2]) - units_to_px(m_marg[4])] rescue StandardError [680, 400] end |
#parse_stylesheet_line_styles ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/html2doc/lists.rb', line 130 def parse_stylesheet_line_styles @listnumber = 0 result = process_stylesheet_lines(@stylesheet.split("\n")) @liststyledefs = clean_result_content(result) @newliststyledefs = "" @liststyledefsidx = @liststyledefs.keys.map do |k| k.sub(/^.*(\d+)$/, "\\1").to_i end.max end |
#process(result) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/html2doc/base.rb', line 20 def process(result) result = process_html(result) process_header(@header_file) generate_filelist(@filename, @dir1) File.open("#{@filename}.htm", "w:UTF-8") { |f| f.write(result) } mime_package result, @filename, @dir1 rm_temp_files(@filename, @dir, @dir1) unless @debug end |
#process_footnote_link(docxml, elem, indexes, footnote) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/html2doc/notes.rb', line 55 def process_footnote_link(docxml, elem, indexes, footnote) footnote?(elem) or return false href = elem["href"].gsub(/^#/, "") #require "debug"; binding.b note = docxml.at("//*[@name = '#{href}' or @id = '#{href}']") note.nil? and return false unless indexes[href] indexes[href] = @footnote_idx @footnote_idx += 1 end set_footnote_link_attrs(elem, indexes[href]) if elem.at("./span[@class = 'MsoFootnoteReference']") process_footnote_link1(elem) else elem.children = FN end footnote << transform_footnote_text(note) end |
#process_footnote_link1(elem) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/html2doc/notes.rb', line 73 def process_footnote_link1(elem) elem.children.each do |c| if c.name == "span" && c["class"] == "MsoFootnoteReference" c.replace(FN) else c.wrap("<span class='MsoFootnoteReference'></span>") end end end |
#process_footnote_texts(docxml, footnotes, indexes) ⇒ Object
Currently cannot deal with separate footnote containers in each chapter We may eventually need to support that
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/html2doc/notes.rb', line 18 def process_footnote_texts(docxml, footnotes, indexes) body = docxml.at("//body") list = body.add_child("<div style='mso-element:footnote-list'/>") footnotes.each do |f| #require 'debug'; binding.b fn = list.first.add_child(footnote_container(docxml, indexes[f["id"]])) f.parent = fn.first f["id"] = "" footnote_div_to_p(f) end footnote_cleanup(docxml) end |
#process_header(headerfile) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/html2doc/base.rb', line 29 def process_header(headerfile) headerfile.nil? and return doc = File.read(headerfile, encoding: "utf-8") doc = header_image_cleanup(doc, @dir1, @filename, File.dirname(@filename)) File.open("#{@dir1}/header.html", "w:UTF-8") { |f| f.write(doc) } end |
#process_html(result) ⇒ Object
52 53 54 55 56 |
# File 'lib/html2doc/base.rb', line 52 def process_html(result) docxml = to_xhtml(result) define_head(cleanup(docxml)) msword_fix(from_xhtml(docxml)) end |
#progress_conv(idx, step, total, threshold, msg) ⇒ Object
22 23 24 25 26 |
# File 'lib/html2doc/math.rb', line 22 def progress_conv(idx, step, total, threshold, msg) return unless (idx % step).zero? && total > threshold && idx.positive? warn "#{msg} #{idx} of #{total}" end |
#read_stylesheet(cssname) ⇒ Object
109 110 111 112 113 |
# File 'lib/html2doc/base.rb', line 109 def read_stylesheet(cssname) (cssname.nil? || cssname.empty?) and cssname = File.join(File.dirname(__FILE__), "wordstyle.css") File.read(cssname, encoding: "UTF-8") end |
#rename_image(img, dir, localdir) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/html2doc/mime.rb', line 131 def rename_image(img, dir, localdir) local_filename = localname(img["src"], localdir) new_filename = "#{mkuuid}#{File.extname(img['src'])}" FileUtils.cp local_filename, File.join(dir, new_filename) img["src"] = File.join(File.basename(dir), new_filename) local_filename end |
#rm_temp_files(filename, dir, dir1) ⇒ Object
58 59 60 61 62 |
# File 'lib/html2doc/base.rb', line 58 def rm_temp_files(filename, dir, dir1) FileUtils.rm "#{filename}.htm" FileUtils.rm_f "#{dir1}/header.html" FileUtils.rm_r dir1 unless dir end |
#rootnamespace(root) ⇒ Object
80 81 82 |
# File 'lib/html2doc/xml.rb', line 80 def rootnamespace(root) root.add_namespace(nil, "http://www.w3.org/TR/REC-html40") end |
#set_footnote_link_attrs(elem, idx) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/html2doc/notes.rb', line 98 def set_footnote_link_attrs(elem, idx) elem["style"] = "mso-footnote-id:ftn#{idx}" elem["href"] = "#_ftn#{idx}" elem["name"] = "_ftnref#{idx}" elem["title"] = "" end |
#skip_image_cleanup?(img) ⇒ Boolean
139 140 141 142 143 144 145 |
# File 'lib/html2doc/mime.rb', line 139 def skip_image_cleanup?(img) src = img["src"] (img.element? && %w(img imagedata).include?(img.name)) or return true (src.nil? || src.empty? || /^http/.match?(src) || %r{^data:(image|application)/[^;]+;base64}.match?(src)) and return true false end |
#style_list(elem, level, liststyle, listnumber) ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/html2doc/lists.rb', line 6 def style_list(elem, level, liststyle, listnumber) liststyle or return if elem["style"] elem["style"] += ";" else elem["style"] = "" end elem["style"] += "mso-list:#{liststyle} level#{level} lfo#{listnumber};" end |
#stylesheet(_filename, _header_filename, _cssname) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/html2doc/base.rb', line 101 def stylesheet(_filename, _header_filename, _cssname) stylesheet = "#{@stylesheet}\n#{@newliststyledefs}" xml = Nokogiri::XML("<style/>") xml.children.first << Nokogiri::XML::CDATA .new(xml, "\n<!--\n#{stylesheet}\n-->\n") xml.root.to_s end |
#to_plane1(xml, font) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/html2doc/math.rb', line 136 def to_plane1(xml, font) xml.traverse do |n| next unless n.text? n.replace(Plane1Converter.conv(@c.decode(n.text), font)) end xml end |
#to_xhtml(xml) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/html2doc/xml.rb', line 10 def to_xhtml(xml) xml.gsub!(/<\?xml[^<>]*>/, "") unless /<!DOCTYPE /.match? xml xml = '<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' + xml end xml = xml.gsub(/<!--\s*\[([^\<\]]+)\]>/, "<!-- MSWORD-COMMENT \\1 -->") .gsub(/<!\s*\[endif\]\s*-->/, "<!-- MSWORD-COMMENT-END -->") Nokogiri::XML.parse(xml) end |
#transform_footnote_text(note) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/html2doc/notes.rb', line 83 def transform_footnote_text(note) #note["id"] = "" note.xpath(".//div").each { |div| div.replace(div.children) } note.xpath(".//aside | .//p").each do |p| p.name = "p" p["class"] = "MsoFootnoteText" end note.remove end |
#uncenter(math, ooxml) ⇒ Object
if oomml has no siblings, by default it is centered; override this with left/right if parent is so tagged also if ooml has mathPara already, or is in para with only oMath content
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/html2doc/math.rb', line 217 def uncenter(math, ooxml) alignnode = math.xpath(STYLE_BEARING_NODE).last ooxml.document? and ooxml = ooxml.root ret = uncenter_unneeded(math, ooxml, alignnode) and return ret dir = ooxml_alignment(alignnode) ooxml.name == "oMathPara" or ooxml.wrap("<m:oMathPara></m:oMathPara>") ooxml.elements.first.previous = "<m:oMathParaPr><m:jc m:val='#{dir}'/></m:oMathParaPr>" ooxml end |
#uncenter_unneeded(math, ooxml, alignnode) ⇒ Object
235 236 237 238 239 240 241 |
# File 'lib/html2doc/math.rb', line 235 def uncenter_unneeded(math, ooxml, alignnode) (math_block?(ooxml, math) || !alignnode) and return ooxml math_only_para?(alignnode) and return nil ooxml.name == "oMathPara" and ooxml = ooxml.elements.select { |x| %w(oMath r).include?(x.name) } ooxml.size > 1 ? nil : Nokogiri::XML::NodeSet.new(math.document, ooxml) end |
#unitalic(math) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/html2doc/math.rb', line 88 def unitalic(math) math.ooxml_xpath(".//r[rPr[not(m:scr)]/sty[@m:val = 'p']]").each do |x| wrap_text(x, "<span #{HTML_NS} style='font-style:normal;'></span>") end math.ooxml_xpath(".//r[rPr[not(m:scr)]/sty[@m:val = 'bi']]").each do |x| wrap_text(x, "<span #{HTML_NS} class='nostem' style='font-weight:bold;'><em></em></span>") end math.ooxml_xpath(".//r[rPr[not(m:scr)]/sty[@m:val = 'i']]").each do |x| wrap_text(x, "<span #{HTML_NS} class='nostem'><em></em></span>") end math.ooxml_xpath(".//r[rPr[not(m:scr)]/sty[@m:val = 'b']]").each do |x| wrap_text(x, "<span #{HTML_NS} style='font-style:normal;font-weight:bold;'></span>") end math.ooxml_xpath(".//r[rPr/scr[@m:val = 'monospace']]").each do |x| to_plane1(x, :monospace) end math.ooxml_xpath(".//r[rPr/scr[@m:val = 'double-struck']]").each do |x| to_plane1(x, :doublestruck) end math.ooxml_xpath(".//r[rPr[not(m:sty) or m:sty/@m:val = 'p']/scr[@m:val = 'script']]").each do |x| to_plane1(x, :script) end math.ooxml_xpath(".//r[rPr[m:sty/@m:val = 'b']/scr[@m:val = 'script']]").each do |x| to_plane1(x, :scriptbold) end math.ooxml_xpath(".//r[rPr[not(m:sty) or m:sty/@m:val = 'p']/scr[@m:val = 'fraktur']]").each do |x| to_plane1(x, :fraktur) end math.ooxml_xpath(".//r[rPr[m:sty/@m:val = 'b']/scr[@m:val = 'fraktur']]").each do |x| to_plane1(x, :frakturbold) end math.ooxml_xpath(".//r[rPr[not(m:sty) or m:sty/@m:val = 'p']/scr[@m:val = 'sans-serif']]").each do |x| to_plane1(x, :sans) end math.ooxml_xpath(".//r[rPr[m:sty/@m:val = 'b']/scr[@m:val = 'sans-serif']]").each do |x| to_plane1(x, :sansbold) end math.ooxml_xpath(".//r[rPr[m:sty/@m:val = 'i']/scr[@m:val = 'sans-serif']]").each do |x| to_plane1(x, :sansitalic) end math.ooxml_xpath(".//r[rPr[m:sty/@m:val = 'bi']/scr[@m:val = 'sans-serif']]").each do |x| to_plane1(x, :sansbolditalic) end math end |
#units_to_px(measure) ⇒ Object
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/html2doc/mime.rb', line 191 def units_to_px(measure) m = /^(\S+)(pt|cm)/.match(measure) ret = case m[2] when "px" then (m[1].to_f * 0.75) when "pt" then m[1].to_f when "cm" then (m[1].to_f * 28.346456693) when "in" then (m[1].to_f * 72) end ret.to_i end |
#unwrap_accents(doc) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/html2doc/math.rb', line 28 def unwrap_accents(doc) doc.xpath("//*[@accent = 'true']").each do |x| x.elements.length > 1 or next x.elements[1].name == "mrow" and x.elements[1].replace(x.elements[1].children) end doc end |
#warnsvg(src) ⇒ Object
85 86 87 |
# File 'lib/html2doc/mime.rb', line 85 def warnsvg(src) warn "#{src}: SVG not supported" if /\.svg$/i.match?(src) end |
#wrap_text(elem, wrapper) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/html2doc/math.rb', line 80 def wrap_text(elem, wrapper) elem.traverse do |e| e.text? or next e.text.strip.empty? and next e.wrap(wrapper) end end |