Created
February 28, 2012 16:05
-
-
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")
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 | |
# -*- 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