Created
August 30, 2013 21:03
-
-
Save trishume/6394261 to your computer and use it in GitHub Desktop.
Liquid Templating Language REPL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment