From e99f6654e118ef7f460c18346f1f7caf984f0dcc Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Thu, 16 Oct 2008 16:23:45 -0700 Subject: [PATCH] Add a SassScript repl. --- lib/haml/exec.rb | 11 +++++++++++ lib/sass/repl.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 lib/sass/repl.rb diff --git a/lib/haml/exec.rb b/lib/haml/exec.rb index 32ab8a69..f849d741 100644 --- a/lib/haml/exec.rb +++ b/lib/haml/exec.rb @@ -185,9 +185,20 @@ END 'Line Comments. Emit comments in the generated CSS indicating the corresponding sass line.') do @options[:for_engine][:line_comments] = true end + opts.on('-i', '--interactive', + 'Run an interactive SassScript shell.') do + @options[:interactive] = true + end end def process_result + if @options[:interactive] + require 'sass' + require 'sass/repl' + ::Sass::Repl.run + return + end + super input = @options[:input] output = @options[:output] diff --git a/lib/sass/repl.rb b/lib/sass/repl.rb new file mode 100644 index 00000000..798f5cf8 --- /dev/null +++ b/lib/sass/repl.rb @@ -0,0 +1,42 @@ +require 'readline' + +module Sass + module Repl + class << self + def run + environment = Environment.new + environment.set_var('important', Script::String.new('!important')) + loop do + unless text = Readline.readline('>> ') + puts + return + end + + Readline::HISTORY << text + parse_input(environment, text) + end + end + + private + + def parse_input(environment, text) + case text + when Script::MATCH + name = $1 + guarded = $2 == '||=' + val = Script::Parser.parse($3) + + unless guarded && environment.var(name) + environment.set_var(name, val.perform(environment)) + end + + p environment.var(name) + else + p Script::Parser.parse(text).perform(environment) + end + rescue Sass::SyntaxError => e + puts "SyntaxError: #{e.message}" + end + end + end +end