Created
July 14, 2011 09:16
-
-
Save maxlapshin/1082152 to your computer and use it in GitHub Desktop.
How to replace substitutions in docs file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LegalPaper | |
| attr_reader :docx_path | |
| def initialize(docx_path) | |
| @docx_path = docx_path | |
| @path = Dir.mktmpdir | |
| end | |
| def process(data) | |
| FileUtils.mkdir_p("#{@path}/legal_paper") | |
| `(cd #{@path}/legal_paper; unzip #{docx_path})` | |
| xml = File.read("#{@path}/legal_paper/word/document.xml") | |
| prepend = <<-XML | |
| <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
| <xsl:template match="/"> | |
| XML | |
| append = <<-XML | |
| </xsl:template> | |
| </xsl:stylesheet> | |
| XML | |
| xslt = prepend + xml.split(/\n/)[1..-1].join("\n") + append | |
| tpl = xslt.gsub(/(ErlyDoc[^ \<]+)/) do |path| | |
| puts path.inspect | |
| "<xsl:value-of select=\"#{path}\"/>" | |
| end | |
| File.open("#{@path}/legal_paper.xslt", "w+") do |f| | |
| f << tpl | |
| end | |
| File.open("#{@path}/legal_paper.xml", "w+") do |f| | |
| f << data.to_xml(:root => "ErlyDoc") | |
| end | |
| `(cd #{@path}/legal_paper; xsltproc #{@path}/legal_paper.xslt #{@path}/legal_paper.xml > word/document.xml; zip -r #{@path}/legal_paper.docx *)` | |
| "#{@path}/legal_paper.docx" | |
| end | |
| def destroy | |
| FileUtils.rm_rf(@path) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment