k0kubun / hamlit

High Performance Haml Implementation
https://rubygems.org/gems/hamlit
Other
981 stars 59 forks source link

:ruby filter API differs from haml's #152

Closed 0xdevalias closed 4 years ago

0xdevalias commented 4 years ago

Creates an IO object named haml_io, anything written to it is output into the Haml document. In previous version this filter redirected any output to $stdout to the Haml document, this was not threadsafe and has been removed, you should use haml_io instead.

I expected one of the old or new methods to work, but playing around it seems neither do:

[46] pry(main)> Hamlit::Template.new() { ":ruby\n  puts 'aaa'" }.render
aaa
=> ""

[47] pry(main)> Hamlit::Template.new() { ":ruby\n  haml_io.puts 'aaa'" }.render
NameError: undefined local variable or method `haml_io' for #<Object:0x00007f9ec2f178a0>
from (__TEMPLATE__):1:in `__tilt_70159905806200'

[48] pry(main)> Hamlit::Template.new() { ":ruby\n  hamlit_io.puts 'aaa'" }.render
NameError: undefined local variable or method `hamlit_io' for #<Object:0x00007f9ec1f000c0>
from (__TEMPLATE__):1:in `__tilt_70159905806200'

Playing around further it seems that we're meant to return the value we want from it, which in hindsight makes sense, but is different to all other documented API's for haml equivalents:

[49] pry(main)> Hamlit::Template.new() { ":ruby\n  'aaa'" }.render
=> ""

[50] pry(main)> Hamlit::Template.new() { ":ruby\n  return 'aaa'" }.render
=> "aaa"

There also didn't seem to be anything useful in the documentation to point this out:

k0kubun commented 4 years ago

Thanks to report.

I expected one of the old or new methods to work, but playing around it seems neither do

TBH I didn't know this feature of Haml. You know Haml better than me :)

Supporting this feature would slightly degrade :ruby filter's performance when haml_io is not used (or complicate the filter's implementation for optimization). So I'd like to be careful for introducing the support.

For the time being, I created https://github.com/hamlit/hamlit-haml_io for you. This may be default once it becomes popular.

There also didn't seem to be anything useful in the documentation to point this out: https://www.rubydoc.info/gems/hamlit/2.7.2/Hamlit/Filters/Ruby

Sorry about that, but I'm not maintaining the documentation. https://github.com/k0kubun/hamlit/blob/master/REFERENCE.md is the only document I officially maintain for Hamlit.

Since I believe Hamlit::Filters::Ruby is an internal implementation detail, I'd like to avoid using it for documenting :ruby filter. So I added https://github.com/k0kubun/hamlit/commit/ec6f6f4aa7bbacb09beb1f52f4d69c00722880f1.

0xdevalias commented 4 years ago

That's fair enough. Thanks for the super quick turnaround on a workaround and reference update!