Class: Livetext::UserAPI
Overview
Encapsulate the UserAPI as a class
Constant Summary collapse
- KBD =
File.new("/dev/tty", "r")
- TTY =
File.new("/dev/tty", "w")
- DotSpace =
Livetext::Sigil + Livetext::Space
". "
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
- #api ⇒ Object
- #body(raw = false) ⇒ Object
- #body_text(raw = false) ⇒ Object
- #check_existence(file, msg) ⇒ Object
- #comment?(str) ⇒ Boolean
- #debug(*args) ⇒ Object
- #debug=(val) ⇒ Object
- #dot ⇒ Object
- #end?(str) ⇒ Boolean
- #err(*args) ⇒ Object
- #format(line) ⇒ Object
- #handle_escapes(str, set) ⇒ Object
- #html ⇒ Object
-
#initialize(live) ⇒ UserAPI
constructor
A new instance of UserAPI.
- #optional_blank_line ⇒ Object
- #out(str = "", file = nil) ⇒ Object
- #out!(str = "") ⇒ Object
- #passthru(line) ⇒ Object
- #print(*args) ⇒ Object
- #puts(*args) ⇒ Object
- #raw_body(tag = "__EOF__") ⇒ Object
- #raw_body! ⇒ Object
-
#setvar(var, val) ⇒ Object
FIXME.
- #setvars(pairs) ⇒ Object
- #trailing?(char) ⇒ Boolean
- #tty(*args) ⇒ Object
- #vars ⇒ Object
Constructor Details
#initialize(live) ⇒ UserAPI
Returns a new instance of UserAPI.
15 16 17 18 19 20 |
# File 'lib/livetext/userapi.rb', line 15 def initialize(live) @live = live @vars = live.vars @html = HTML.new(self) = Livetext::Expansion.new(live) end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
13 14 15 |
# File 'lib/livetext/userapi.rb', line 13 def args @args end |
#data ⇒ Object
Returns the value of attribute data.
13 14 15 |
# File 'lib/livetext/userapi.rb', line 13 def data @data end |
Instance Method Details
#api ⇒ Object
22 23 24 |
# File 'lib/livetext/userapi.rb', line 22 def api @live.api end |
#body(raw = false) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/livetext/userapi.rb', line 103 def body(raw=false) lines = [] end_found = false loop do @line = @live.nextline break if @line.nil? @line.chomp! break if end?(@line) next if comment?(@line) @line = format(@line) unless raw 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 $. ? end |
#body_text(raw = false) ⇒ Object
121 122 123 |
# File 'lib/livetext/userapi.rb', line 121 def body_text(raw=false) raw_body.join("\n") end |
#check_existence(file, msg) ⇒ Object
46 47 48 |
# File 'lib/livetext/userapi.rb', line 46 def check_existence(file, msg) STDERR.puts msg unless File.exist?(file) end |
#comment?(str) ⇒ Boolean
75 76 77 |
# File 'lib/livetext/userapi.rb', line 75 def comment?(str) str.index(DotSpace) == 0 end |
#debug(*args) ⇒ Object
187 188 189 |
# File 'lib/livetext/userapi.rb', line 187 def debug(*args) TTY.puts *args if @live.debug end |
#debug=(val) ⇒ Object
183 184 185 |
# File 'lib/livetext/userapi.rb', line 183 def debug=(val) @live.debug = val end |
#dot ⇒ Object
30 31 32 |
# File 'lib/livetext/userapi.rb', line 30 def dot @live end |
#end?(str) ⇒ Boolean
84 85 86 87 |
# File 'lib/livetext/userapi.rb', line 84 def end?(str) return true if str == ".end" || str =~ / *\$\.end/ return false end |
#err(*args) ⇒ Object
171 172 173 |
# File 'lib/livetext/userapi.rb', line 171 def err(*args) STDERR.puts *args end |
#format(line) ⇒ Object
137 138 139 140 141 |
# File 'lib/livetext/userapi.rb', line 137 def format(line) return "" if line == "\n" || line.nil? line2 = .format(line) line2 end |
#handle_escapes(str, set) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/livetext/userapi.rb', line 129 def handle_escapes(str, set) str = str.dup set.each_char do |ch| str.gsub!("\\#{ch}", ch) end str end |
#html ⇒ Object
26 27 28 |
# File 'lib/livetext/userapi.rb', line 26 def html @html end |
#optional_blank_line ⇒ Object
68 69 70 71 72 73 |
# File 'lib/livetext/userapi.rb', line 68 def optional_blank_line peek = @live.peek_nextline # ??? return if peek.nil? @line = @live.nextline if peek =~ /^ *$/ return true # This way, dot commands will usually return true end |
#out(str = "", file = nil) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/livetext/userapi.rb', line 156 def out(str = "", file = nil) return if str.nil? return file.puts str unless file.nil? @live.body << str @live.body << "\n" unless str.end_with?("\n") end |
#out!(str = "") ⇒ Object
163 164 165 |
# File 'lib/livetext/userapi.rb', line 163 def out!(str = "") @live.body << str # no newline end |
#passthru(line) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/livetext/userapi.rb', line 143 def passthru(line) return if @live.nopass if line == "\n" unless @live.nopara out "<p>" out end else text = .format(line.chomp) out text end end |
#print(*args) ⇒ Object
179 180 181 |
# File 'lib/livetext/userapi.rb', line 179 def print(*args) @live.output.print *args end |
#puts(*args) ⇒ Object
175 176 177 |
# File 'lib/livetext/userapi.rb', line 175 def puts(*args) @live.output.puts *args end |
#raw_body(tag = "__EOF__") ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/livetext/userapi.rb', line 89 def raw_body(tag = "__EOF__") lines = [] @live.save_location = @live.sources.last loop do @line = @live.nextline break if @line.nil? break if @line.chomp.strip == tag lines << @line end optional_blank_line return lines unless block_given? lines.each {|line| yield line } end |
#raw_body! ⇒ Object
125 126 127 |
# File 'lib/livetext/userapi.rb', line 125 def raw_body! raw_body(Livetext::Sigil).join("\n") end |
#setvar(var, val) ⇒ Object
FIXME
34 35 36 37 |
# File 'lib/livetext/userapi.rb', line 34 def setvar(var, val) # FIXME # Livetext::Vars[var] = val # Now indifferent and "safe" @live.vars.set(var, val) end |
#setvars(pairs) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/livetext/userapi.rb', line 39 def setvars(pairs) pairs = pairs.to_a if pairs.is_a?(Hash) pairs.each do |var, value| @live.vars.set(var, value) end end |
#trailing?(char) ⇒ Boolean
79 80 81 82 |
# File 'lib/livetext/userapi.rb', line 79 def trailing?(char) return true if ["\n", " ", nil].include?(char) return false end |
#tty(*args) ⇒ Object
167 168 169 |
# File 'lib/livetext/userapi.rb', line 167 def tty(*args) TTY.puts *args end |
#vars ⇒ Object
64 65 66 |
# File 'lib/livetext/userapi.rb', line 64 def vars @vars end |