Permalink
Browse files

Merge pull request #8 from luvtechno/ruby19-hash

Add an option to use Ruby 1.9 hash style
  • Loading branch information...
norman committed Feb 16, 2013
2 parents 0195b2f + 2749854 commit d7e162923b48baea13de4308fa032b47772fe1ad
Showing with 18 additions and 0 deletions.
  1. +1 −0 README.md
  2. +4 −0 lib/html2haml/exec.rb
  3. +6 −0 lib/html2haml/html.rb
  4. +7 −0 test/html2haml_test.rb
View
@@ -31,6 +31,7 @@ See `html2haml --help`:
--no-rhtml Deprecated; same as --no-erb.
-x, --xhtml Parse the input using the more strict XHTML parser.
--html-attributes Use HTML style attributes instead of Ruby hash style.
+ --ruby19-hash Use Ruby 1.9 hash style attributes instead of Ruby 1.8 hash style when possible.
-E ex[:in] Specify the default external and internal character encodings.
-s, --stdin Read input from standard input instead of an input file
--trace Show a full traceback on error
View
@@ -224,6 +224,10 @@ def set_opts(opts)
@module_opts[:html_style_attributes] = true
end
+ opts.on("--ruby19-hash", "Use Ruby 1.9 hash style attributes instead of Ruby 1.8 hash style when possible.") do
+ @module_opts[:ruby19_hash_style_attributes] = true
+ end
+
unless RUBY_VERSION < "1.9"
opts.on('-E ex[:in]', 'Specify the default external and internal character encodings.') do |encoding|
external, internal = encoding.split(':')
View
@@ -420,6 +420,12 @@ def haml_attribute_pair(name, value, options)
value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect
if options[:html_style_attributes]
"#{name}=#{value}"
+ elsif options[:ruby19_hash_style_attributes]
+ if name.index(/\W/)
+ "#{name.inspect} => #{value}"
+ else
+ "#{name}: #{value}"
+ end
else
name = name.index(/\W/) ? name.inspect : ":#{name}"
"#{name} => #{value}"
View
@@ -45,6 +45,13 @@ def test_should_have_html_style_attributes
render('<meta http-equiv="Content-Type" content="text/html" />', :html_style_attributes => true))
end
+ def test_should_have_ruby_19_hash_style_attributes
+ assert_equal('%input{name: "login", type: "text"}/',
+ render('<input type="text" name="login" />', :ruby19_hash_style_attributes => true))
+ assert_equal('%meta{content: "text/html", "http-equiv" => "Content-Type"}/',
+ render('<meta http-equiv="Content-Type" content="text/html" />', :ruby19_hash_style_attributes => true))
+ end
+
def test_class_with_dot_and_hash
assert_equal('%div{:class => "foo.bar"}', render("<div class='foo.bar'></div>"))
assert_equal('%div{:class => "foo#bar"}', render("<div class='foo#bar'></div>"))

0 comments on commit d7e1629

Please sign in to comment.