wethu
8/1/2016 - 2:56 AM

Liquid Templating Language REPL

Liquid Templating Language REPL

require 'liquid'
require 'readline'
require 'colorize'

$state = {'test' => 5, 'wat' => "yup"}

def eval_liquid(str)
    temp = Liquid::Template.parse(str)
    temp.render($state)
end

def switch_command(str)
  case str
  when /^assign (.*) = (.*)$/
    $state[$1] = eval($2)
    "Assigned #{$1} to #{$2}"
  when /^mode (\w+)/
    Liquid::Template.error_mode = $1.intern
  when /^rb (.*)$/
    eval($1)
  else
    eval_liquid(str)
  end
end

def do_command(str)
  begin
    out = switch_command(str)
  rescue Exception => e
    puts "!> ".red + e.message.to_s
  else
    puts "=> ".green + out.to_s
  end
end

buf = ''
while buf << Readline.readline("liquid> ", true)
  break if buf == "exit"
  if buf[-1] != '\\'
    liquid = buf.gsub(/\\/, "\n")
    do_command(liquid)
    buf = ''
  end
end