Created
October 13, 2013 04:39
-
-
Save chrahunt/6958248 to your computer and use it in GitHub Desktop.
Guardfile (https://github.com/guard/guard) to monitor docx files in the current directory and, when changed, extract the document.xml from the changed docx file to the current directory. This is helpful when you want to see how a change made to a docx file changes the underlying xml.
This file contains 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
require 'zip/zip' | |
require 'xmlsimple' | |
require 'guard/guard' | |
# monitor docs directory | |
module ::Guard | |
class Docx < Guard | |
def run_on_changes(paths) | |
paths.each do |path| | |
#puts File.expand_path(path) | |
new_info = extract_information(path) | |
File.open("#{path}.xml", "w") {|f| f.write new_info} | |
end | |
end | |
def extract_information(path) | |
zipped = Zip::ZipFile.open(File.expand_path(path)) | |
xml_data = zipped.read('word/document.xml') | |
xml_hash = XmlSimple.xml_in(xml_data) | |
return XmlSimple.xml_out(xml_hash) | |
end | |
end | |
end | |
guard 'docx' do | |
watch(%r{\A[^\$]*\.docx\z}) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment