Evaluate compiled Ruby code safely on in debug option

See #762
This commit is contained in:
Norman Clarke 2015-06-08 16:56:29 -03:00
parent 1e5a4ba48c
commit 3dbf1bf3d3
2 changed files with 17 additions and 1 deletions

View File

@ -18,6 +18,8 @@
to the source Haml file from which it was generated. Thanks [Alex Babkin](https://github.com/ababkin). to the source Haml file from which it was generated. Thanks [Alex Babkin](https://github.com/ababkin).
* Fix #@foo and #$foo style interpolation that was not working in html_safe mode. (Akira Matsuda) * Fix #@foo and #$foo style interpolation that was not working in html_safe mode. (Akira Matsuda)
* Add `haml_tag_if` to render a block, conditionally wrapped in another element (Matt Wildig) * Add `haml_tag_if` to render a block, conditionally wrapped in another element (Matt Wildig)
* The `haml` command's debug option (`-d`) no longer executes the Haml code, but
rather checks the generated Ruby syntax for errors.
## 4.0.6 ## 4.0.6

View File

@ -309,7 +309,12 @@ END
if @options[:debug] if @options[:debug]
puts engine.precompiled puts engine.precompiled
puts '=' * 100 error = validate_ruby(engine.precompiled)
if error
puts '=' * 100
puts error.message.split("\n")[0]
end
return
end end
result = engine.to_html result = engine.to_html
@ -326,6 +331,15 @@ END
output.write(result) output.write(result)
output.close() if output.is_a? File output.close() if output.is_a? File
end end
def validate_ruby(code)
begin
eval("BEGIN {return nil}; #{code}")
# Not sure why, but rescuing "SyntaxError" does not work here.
rescue Exception
$!
end
end
end end
end end
end end