Class: Rote::Filters::Eval

Inherits:
MacroFilter show all
Defined in:
lib/rote/filters/eval.rb

Overview

Page filter that evaluates Ruby code in it’s body in the current interpreter. The code is directly evaluated, and anything it writes to standard out becomes the macro replacement.

Obviously you can place Ruby code directly in your pages, using ERB, and for many cases that is the route you should take. There is a (somewhat) subtle difference between the to alternatives however: ERB is always evaluated right at the start of rendering, before any Text Filters are run, whereas #:eval# code is executed during the page filter stage, which happens after ERB and text filtering, but before layout is applied.

Instance Attribute Summary

Attributes inherited from MacroFilter

#handler_blk, #names

Instance Method Summary collapse

Methods inherited from MacroFilter

#filter, #handler

Constructor Details

#initialize(macro_re = MACRO_RE) ⇒ Eval

Returns a new instance of Eval.



31
32
33
# File 'lib/rote/filters/eval.rb', line 31

def initialize(macro_re = MACRO_RE)
  super([],macro_re)
end

Instance Method Details

#macro_eval(cmd, body, raw) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rote/filters/eval.rb', line 35

def macro_eval(cmd,body,raw)
  # no need to fiddle with $SAFE here is there?
  
  # FIXME this is a hack.
  
  # Utility is still limited I guess, since current Page isn't
  # readily available to the macro code. We can probably fix
  # that though.
  
  # If thread safety becomes an issue, this'll probably need 
  # to be critical sectioned.
  
  begin
    oldsio, $stdout = $stdout, StringIO.new
    eval body        
    $stdout.rewind
    $stdout.read
  ensure
    $stdout = oldsio
  end
end