Skip to content

Instantly share code, notes, and snippets.

@chrahunt
Created October 13, 2013 04:39
Show Gist options
  • Save chrahunt/6958248 to your computer and use it in GitHub Desktop.
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.
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