Created
October 9, 2012 15:14
-
-
Save seamusabshere/3859446 to your computer and use it in GitHub Desktop.
hamledit PATH_TO_XML_FILE: Converts XML to haml, loads it with `subl -w`, and then converts it back to xml in-place.
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
#!/usr/bin/env ruby | |
# Usage: hamledit [-v] PATH_TO_XML_FILE | |
# -v means "view only" | |
# | |
# Converts XML to haml, loads it with `subl -w`, and then converts it back to xml in-place. | |
require 'fileutils' | |
require 'posix-spawn' | |
orig = ARGV.last | |
viewonly = (ARGV.first == '-v') | |
unless viewonly | |
backup = orig + ".backup.#{Time.now.to_f}.xml" | |
FileUtils.cp orig, backup | |
end | |
# convert to HAML | |
tmp = orig + '.tmp.haml' | |
pid = POSIX::Spawn.spawn('html2haml', '--html-attributes', '--unix-newlines', '-x', orig, :out => tmp) | |
Process.waitpid pid | |
raise "died converting to haml" unless $?.success? | |
# edit in subl -w | |
pid = POSIX::Spawn.spawn('subl', '-w', tmp) | |
Process.waitpid pid | |
unless viewonly | |
# write back out to the original file (dangerous!) | |
pid = POSIX::Spawn.spawn('haml', '--double-quote-attributes', '--format', 'xhtml', '--unix-newlines', tmp, :out => orig) | |
Process.waitpid pid | |
end | |
FileUtils.rm tmp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment