lib/isodoc/function/utils.rb in isodoc-1.7.5 vs lib/isodoc/function/utils.rb in isodoc-1.7.6

- old
+ new

@@ -35,29 +35,29 @@ l.gsub(/\s*\n/, "") end end def attr_code(attributes) - attributes = attributes.reject { |_, val| val.nil? }.map + attributes = attributes.compact.map attributes.map do |k, v| [k, v.is_a?(String) ? HTMLEntities.new.decode(v) : v] end.to_h end DOCTYPE_HDR = "<!DOCTYPE html SYSTEM "\ - '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' + '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' def to_xhtml(xml) xml = to_xhtml_prep(xml) begin Nokogiri::XML.parse(xml, &:strict) rescue Nokogiri::XML::SyntaxError => e File.open("#{@filename}.#{@format}.err", "w:UTF-8") do |f| f.write xml end abort "Malformed Output XML for #{@format}: #{e} "\ - "(see #{@filename}.#{@format}.err)" + "(see #{@filename}.#{@format}.err)" end end def to_xhtml_prep(xml) xml.gsub!(/<\?xml[^>]*>/, "") @@ -65,11 +65,11 @@ xml.split(/(&[^ \r\n\t#;]+;)/).map do |t| if /^(&[^ \t\r\n#;]+;)/.match?(t) HTMLEntities.new.encode(HTMLEntities.new.decode(t), :hexadecimal) else t end - end.join("") + end.join end def to_xhtml_fragment(xml) doc = ::Nokogiri::XML.parse(NOKOHEAD) doc.fragment(xml) @@ -146,22 +146,16 @@ elem.name == "span" && elem["class"] == "MsoCommentReference" || elem.name == "a" && elem["class"] == "FootnoteRef" || elem.name == "span" && /mso-bookmark/.match(elem["style"]) end -=begin def liquid(doc) - self.class.liquid(doc) - end -=end - - def liquid(doc) # unescape HTML escapes in doc doc = doc.split(%r<(\{%|%\})>).each_slice(4).map do |a| a[2] = a[2].gsub(/&lt;/, "<").gsub(/&gt;/, ">") if a.size > 2 - a.join("") - end.join("") + a.join + end.join Liquid::Template.parse(doc) end def empty2nil(str) return nil if !str.nil? && str.is_a?(String) && str.empty? @@ -179,12 +173,13 @@ template.render(meta.map { |k, v| [k.to_s, empty2nil(v)] }.to_h) .gsub("&lt;", "&#x3c;").gsub("&gt;", "&#x3e;").gsub("&amp;", "&#x26;") end def save_dataimage(uri, _relative_dir = true) - %r{^data:(image|application)/(?<imgtype>[^;]+);base64,(?<imgdata>.+)$} =~ uri - imgtype.sub!(/\+[a-z0-9]+$/, "") # svg+xml + %r{^data:(?<imgclass>image|application)/(?<imgtype>[^;]+);(charset=[^;]+;)?base64,(?<imgdata>.+)$} =~ uri + imgtype = "emf" if emf?("#{imgclass}/#{imgtype}") + imgtype = imgtype.sub(/\+[a-z0-9]+$/, "") # svg+xml imgtype = "png" unless /^[a-z0-9]+$/.match? imgtype Tempfile.open(["image", ".#{imgtype}"]) do |f| f.binmode f.write(Base64.strict_decode64(imgdata)) @tempfile_cache << f # persist to the end @@ -203,9 +198,34 @@ end def labelled_ancestor(node) !node.ancestors("example, requirement, recommendation, permission, "\ "note, table, figure, sourcecode").empty? + end + + def emf?(type) + %w(application/emf application/x-emf image/x-emf image/x-mgx-emf + application/x-msmetafile image/x-xbitmap).include? type + end + + def cleanup_entities(text) + c = HTMLEntities.new + text.split(/([<>])/).each_slice(4).map do |a| + a[0] = c.encode(c.decode(a[0]), :hexadecimal) + a[1] = c.encode(c.decode(a[1]), :hexadecimal) + a + end.join + end + + def external_path(path) + win = !!((RUBY_PLATFORM =~ /(win|w)(32|64)$/) || + (RUBY_PLATFORM =~ /mswin|mingw/)) + if win + path.gsub!(%{/}, "\\") + path[/\s/] ? "\"#{path}\"" : path + else + path + end end end end end