Skip to content

Instantly share code, notes, and snippets.

@wefarrell
Last active February 7, 2018 03:05
Show Gist options
  • Save wefarrell/c330b9188813232e55e88dc03200bf17 to your computer and use it in GitHub Desktop.
Save wefarrell/c330b9188813232e55e88dc03200bf17 to your computer and use it in GitHub Desktop.
require 'htmltoword'
require 'mustache'
class HtmlToOpenXml
include Htmltoword::XSLTHelper
def convert(html)
node = Nokogiri::HTML(html.gsub(/>\s+</, '><'))
node = xslt(stylesheet_name: 'cleanup').transform(node)
node = xslt(stylesheet_name: 'inline_elements').transform(node)
xslt(stylesheet_path: document_xslt(false)).transform(node)
end
end
#usage HtmlToOpenXml.new.convert(html)
class TemplateParser < Mustache
def load_variables(template)
Mustache::Parser.new.compile(fix_formatting(template)).map{|arr|
arr[2][2] if arr[2]
}.compact.map(&:first).uniq
end
def plan_info
end
def fix_formatting(xml)
xml.gsub(/\{{([^}]+)\}}/){ |match|
match.gsub(/<[^>]+>/,'').split.join('')
}
end
def escapeHTML(txt)
return txt
end
end
puts TemplateParser.new.load_variables(open('document.xml').read)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment