Skip to content

Loading…

Add an option to use Ruby 1.9 hash style #8

Merged
merged 1 commit into from

3 participants

@luvtechno

I'd like to add a new option to use the newer ruby hash style introduced in 1.9.

@travisbot

This pull request passes (merged 2749854 into 3aee300).

@norman norman merged commit d7e1629 into haml:master

1 check passed

Details default The Travis build passed
@norman
Haml member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 23, 2012
  1. @luvtechno
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
1 README.md
@@ -32,6 +32,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
4 lib/html2haml/exec.rb
@@ -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
6 lib/html2haml/html.rb
@@ -413,6 +413,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
7 test/html2haml_test.rb
@@ -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>"))
Something went wrong with that request. Please try again.