Skip to content

Instantly share code, notes, and snippets.

@seamusabshere
Created October 9, 2012 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seamusabshere/3859446 to your computer and use it in GitHub Desktop.
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.
#!/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