Skip to content

Instantly share code, notes, and snippets.

@felixrabe
Created February 28, 2012 16:05
Show Gist options
  • Save felixrabe/1933369 to your computer and use it in GitHub Desktop.
Save felixrabe/1933369 to your computer and use it in GitHub Desktop.
Filter for Haml files to convert %tag{:attr => "value"} into %tag(attr="value")
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# See: https://gist.github.com/1933369
# Filter for Haml files to convert %tag{:attr => "value"} into %tag(attr="value")
$tag_attr_order = {
"link" => %w( rel sizes href ),
"meta" => %w( name content )
}
def order tag, attributes
new_attributes = []
if attr_order = $tag_attr_order[tag]
new_attributes = attr_order.select{|n| attributes.has_key? n}.map{|n| attributes.delete(n)}
end
new_attributes + attributes.values
end
hash_re = /\s*(?::(\w+)\s*=>|(\w+):)\s*(\".*?\"|'.*?')\s*,?\s*/
tag_re = /%(\w+)(.*?)\{((?:#{hash_re})*)\}/
print ARGF.read.gsub(tag_re) {
tag = $1
selectors = $2
attributes = {}
$3.scan hash_re do |key, key2, value|
key ||= key2
attributes[key] = "#{key}=#{value}"
end
attributes = order tag, attributes
"%#{tag}#{selectors}(#{attributes.join " "})"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment