lib/livetext/userapi.rb in livetext-0.9.27 vs lib/livetext/userapi.rb in livetext-0.9.30

- old
+ new

@@ -1,8 +1,8 @@ +require_relative 'expansion' +require_relative 'html' -require_relative 'lineparser' # FIXME meh, because of #format - # Encapsulate the UserAPI as a class class Livetext::UserAPI KBD = File.new("/dev/tty", "r") @@ -13,42 +13,59 @@ attr_accessor :data, :args def initialize(live) @live = live @vars = live.vars + @html = HTML.new(self) + @expander = Livetext::Expansion.new(live) end def api @live.api end + def html + @html + end + def dot @live end - def setvar(var, val) - Livetext::Vars[var] = val # Now indifferent and "safe" + def setvar(var, val) # FIXME + # Livetext::Vars[var] = val # Now indifferent and "safe" + @live.vars.set(var, val) end + def setvars(pairs) + pairs = pairs.to_a if pairs.is_a?(Hash) + pairs.each do |var, value| + @live.vars.set(var, value) + end + end + def check_existence(file, msg) STDERR.puts msg unless File.exist?(file) end def data=(value) -# TTY.puts "in #{__FILE__}: api = #{@live.api.inspect}" @data = value @args = format(@data).chomp.split end + def data + @data + end + def args return @args unless block_given? @args.each {|arg| yield arg } end - def vars - @vars - end + def vars + @vars + end def optional_blank_line peek = @live.peek_nextline # ??? return if peek.nil? @line = @live.nextline if peek =~ /^ *$/ @@ -91,11 +108,11 @@ break if @line.nil? @line.chomp! break if end?(@line) next if comment?(@line) @line = format(@line) unless raw - lines << @line + lines << @line end raise "Expected .end, found end of file" unless end?(@line) # use custom exception optional_blank_line # FIXME Delete this?? return lines unless block_given? lines.each {|line| yield line } # FIXME what about $. ? @@ -117,25 +134,31 @@ str end def format(line) return "" if line == "\n" || line.nil? - line2 = Livetext::LineParser.parse!(line) + line2 = @expander.format(line) line2 end def passthru(line) return if @live.nopass - out "<p>" if line == "\n" and ! @live.nopara - line = format(line) - out line + if line == "\n" + unless @live.nopara + out "<p>" + out + end + else + text = @expander.format(line.chomp) + out text + end end def out(str = "", file = nil) return if str.nil? return file.puts str unless file.nil? - @live.body << str + @live.body << str @live.body << "\n" unless str.end_with?("\n") end def out!(str = "") @live.body << str # no newline @@ -148,14 +171,14 @@ def err(*args) STDERR.puts *args end def puts(*args) - @live.output.puts *args + @live.output.puts *args end def print(*args) - @live.output.print *args + @live.output.print *args end def debug=(val) @live.debug = val end