Class: LiquidEval::EvalBlock
- Inherits:
-
Liquid::Block
- Object
- Liquid::Block
- LiquidEval::EvalBlock
- Defined in:
- lib/liquid-eval.rb
Instance Attribute Summary collapse
-
#code_html ⇒ Object
readonly
Returns the value of attribute code_html.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
-
#initialize(name, language, tokens) ⇒ EvalBlock
constructor
A new instance of EvalBlock.
- #interpret(interpreter, extension) ⇒ Object
- #render(context) ⇒ Object
Constructor Details
#initialize(name, language, tokens) ⇒ EvalBlock
Returns a new instance of EvalBlock.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/liquid-eval.rb', line 14 def initialize(name, language, tokens) super @code = @nodelist[0].to_s.gsub(/^$\n/, '') @code_html = CGI.escapeHTML(@code) @language = language.strip case @language when 'ruby' @result = interpret('ruby', 'rb') when 'python' @result = interpret('python', 'py') when 'perl' @result = interpret('perl', 'pl') when 'php' @result = interpret('php', 'php') when 'bash' @result = interpret('bash', 'sh') else @result = 'Language not currently supported by Liquid::EvalBlock.' end end |
Instance Attribute Details
#code_html ⇒ Object (readonly)
Returns the value of attribute code_html.
12 13 14 |
# File 'lib/liquid-eval.rb', line 12 def code_html @code_html end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
12 13 14 |
# File 'lib/liquid-eval.rb', line 12 def result @result end |
Instance Method Details
#interpret(interpreter, extension) ⇒ Object
44 45 46 47 48 |
# File 'lib/liquid-eval.rb', line 44 def interpret(interpreter, extension) filename = "/tmp/#{interpreter}-codeblock.#{extension}" File.open(filename, 'w') {|f| f.write(@code)} CGI.escapeHTML(`#{interpreter} #{filename} 2>&1`) end |
#render(context) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/liquid-eval.rb', line 36 def render(context) if @result == "" %Q[<h6>Code:</h6><pre><code>#{@code_html}</code></pre>] else %Q[<h6>Code:</h6><pre><code>#{@code_html}</code></pre><h6>Output:</h6><pre class="output"><code>#{@result}</code></pre>] end end |