Passing options to Haml on the command line
-
05-07-2019 - |
Question
I'm using Haml from the command-line to do a basic transform of one .haml file to .html, like this:
> haml input.haml output.html
Thing is, this produces single-quotes around attributes in the resulting HTML. So how to I pass in the :attr_wrapper => '"'
option from the command-line?
Alternatively, can I just globally set :attr_wrapper
to a double-quote?
Solution 2
What I've ended up doing is adding the following to haml/exec.rb at ~line 302
opts.on('-q', '--double-quote-attribs',
'Set attribute wrapper to double-quotes (default is single).') do
@options[:for_engine][:attr_wrapper] = '"'
end
This adds a dedicated option to do what I need.
I'm not quite sure what the rationale for using single-quotes by default is; double-quotes would make a more sensible default, I would have thought. Having said that, they are perfectly valid.
I submitted this update as a patch and it'll be included in version 2.4
OTHER TIPS
I don't know of a way to pass it as an argument, but HAML is easy to use programmatically:
require 'haml'
template = ARGV.length > 0 ? File.read(ARGV.shift) : STDIN.read
haml_engine = Haml::Engine.new(template, :attr_wrapper => '"')
file = ARGV.length > 0 ? File.open(ARGV.shift, 'w') : STDOUT
file.write(haml_engine.render)
file.close
Nothing pretty, but it'll open the first argument given (or STDIN if no arguments are given), and pass rendered output to the second argument (or STDOUT of not given).
I don't know a way to specify that option to the standard command-line client. You can globally patch the default by changing haml/engine.rb line 73 (in Haml 2.2.2) to read
:attr_wrapper => '"',
That's for Haml 2.2.2. It's different in earlier versions. If you're still using one of those, you can just to a text search for ":attr_wrapper =>" and it will find the correct line.