Class: LiquidEval::EvalBlock

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/liquid-eval.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_htmlObject (readonly)

Returns the value of attribute code_html.



12
13
14
# File 'lib/liquid-eval.rb', line 12

def code_html
  @code_html
end

#resultObject (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