Module: Livetext::Helpers
- Included in:
- Livetext, Livetext::Handler::Import, Livetext::Handler::Mixin, Standard
- Defined in:
- lib/livetext/helpers.rb
Constant Summary collapse
- Space =
" "- Sigil =
Can’t change yet
"."- ESCAPING =
{ "'" => ''', '&' => '&', '"' => '"', '<' => '<', '>' => '>' }
- TTY =
::File.open("/dev/tty", "w")
- Comment =
rx(Sigil, Space)
- DotCmd =
rx(Sigil)
- DollarDot =
/^ *\$\.[A-Za-z]/
Class Method Summary collapse
Instance Method Summary collapse
- #check_disallowed(name) ⇒ Object
- #check_file_exists(file) ⇒ Object
- #debug(*args) ⇒ Object
- #escape_html(string) ⇒ Object
- #find_file(name, ext = ".rb", which = "imports") ⇒ Object
- #friendly_error(err) ⇒ Object
- #get_name_data(line) ⇒ Object
- #grab_file(fname) ⇒ Object
- #handle_dollar_dot(line) ⇒ Object
- #handle_dotcmd(line, indent = 0) ⇒ Object
- #handle_scomment(line) ⇒ Object
- #include_file(file) ⇒ Object
- #invoke_dotcmd(name, data0 = "") ⇒ Object
-
#onoff(arg) ⇒ Object
helper.
-
#process_file(fname, btrace = false) ⇒ Object
FIXME process_file should call process ?.
- #process_line(line) ⇒ Object
- #read_variables(file) ⇒ Object
- #search_upward(file) ⇒ Object
- #set_variables(pairs) ⇒ Object
- #setfile(file) ⇒ Object
-
#setfile!(file) ⇒ Object
FIXME why does this variant exist?.
- #setvar(var, val) ⇒ Object
- #showme(obj, tag = "") ⇒ Object
Class Method Details
.rx(str, space = nil) ⇒ Object
67 68 69 |
# File 'lib/livetext/helpers.rb', line 67 def self.rx(str, space=nil) Regexp.compile("^" + Regexp.escape(str) + "#{space}") end |
Instance Method Details
#check_disallowed(name) ⇒ Object
169 170 171 |
# File 'lib/livetext/helpers.rb', line 169 def check_disallowed(name) friendly_error DisallowedName(name) if disallowed?(name) end |
#check_file_exists(file) ⇒ Object
173 174 175 |
# File 'lib/livetext/helpers.rb', line 173 def check_file_exists(file) return File.exist?(file) end |
#debug(*args) ⇒ Object
51 52 53 |
# File 'lib/livetext/helpers.rb', line 51 def debug(*args) puts(*args) if ENV['debug'] end |
#escape_html(string) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/livetext/helpers.rb', line 24 def escape_html(string) enc = string.encoding unless enc.ascii_compatible? if enc.dummy? origenc = enc enc = Encoding::Converter.asciicompat_encoding(enc) string = enc ? string.encode(enc) : string.b end table = Hash[ESCAPING.map {|pair|pair.map {|s|s.encode(enc)}}] string = string.gsub(/#{"['&\"<>]".encode(enc)}/, table) string.encode!(origenc) if origenc return string end string.gsub(/['&\"<>]/, ESCAPING) end |
#find_file(name, ext = ".rb", which = "imports") ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/livetext/helpers.rb', line 55 def find_file(name, ext=".rb", which="imports") failed = "#{__method__}: expected 'imports' or 'plugin'" raise failed unless %w[imports plugin].include?(which) paths = [Livetext::Path.sub(/lib.livetext/, "#{which}/"), "./"] base = "#{name}#{ext}" paths.each do |path| file = path + base return file if File.exist?(file) end return nil end |
#friendly_error(err) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/livetext/helpers.rb', line 16 def friendly_error(err) return graceful_error(err) if self.respond_to?(:graceful_error) return self.parent.graceful_error(err) if self.respond_to?(:parent) raise err rescue => myerr TTY.puts "--- Warning: friendly_error #{myerr.inspect}" end |
#get_name_data(line) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/livetext/helpers.rb', line 153 def get_name_data(line) line = line.chomp blank = line.index(" ") if blank name = line[1..(blank-1)] data0 = line[(blank+1)..-1] else name = line[1..-1] data0 = "" end name = "dot_" + name if %w[include def].include?(name) @main.check_disallowed(name) @main.api.data = data0 # FIXME kill this? [name.to_sym, data0] end |
#grab_file(fname) ⇒ Object
186 187 188 189 190 191 |
# File 'lib/livetext/helpers.rb', line 186 def grab_file(fname) File.read(fname) rescue ::STDERR.puts "Can't find #{fname.inspect} \n " return nil end |
#handle_dollar_dot(line) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/livetext/helpers.rb', line 114 def handle_dollar_dot(line) indent = line.index("$") + 1 @indentation.push(indent) line.sub!(/^ *\$/, "") success = handle_dotcmd(line) indentation.pop success end |
#handle_dotcmd(line, indent = 0) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/livetext/helpers.rb', line 133 def handle_dotcmd(line, indent = 0) indent = @indentation.last # top of stack line = line.sub(/# .*$/, "") # FIXME Could be problematic? name, data = get_name_data(line) success = true # Be optimistic... :P case when name == :end # special case graceful_error EndWithoutOpening() when @main.respond_to?(name) success = invoke_dotcmd(name, data) # was 141 else graceful_error UnknownMethod(name) end success end |
#handle_scomment(line) ⇒ Object
149 150 151 |
# File 'lib/livetext/helpers.rb', line 149 def handle_scomment(line) return true end |
#include_file(file) ⇒ Object
214 215 216 217 |
# File 'lib/livetext/helpers.rb', line 214 def include_file(file) api.args = [file] dot_include end |
#invoke_dotcmd(name, data0 = "") ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/livetext/helpers.rb', line 123 def invoke_dotcmd(name, data0="") api.data = data0.dup # should permit _ in function names at least args0 = data0.split api.args = args0.dup retval = @main.send(name) # , *args) # was 125 retval rescue => err graceful_error(err) # , "#{__method__}: name = #{name}") end |
#onoff(arg) ⇒ Object
helper
219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/livetext/helpers.rb', line 219 def onoff(arg) # helper arg ||= "on" raise ExpectedOnOff unless String === arg case arg.downcase when "on" return true when "off" return false else raise ExpectedOnOff end end |
#process_file(fname, btrace = false) ⇒ Object
FIXME process_file should call process ?
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/livetext/helpers.rb', line 77 def process_file(fname, btrace=false) graceful_error FileNotFound(fname) unless File.exist?(fname) setfile(fname) text = File.readlines(fname) enum = text.each @backtrace = btrace @main.source(enum, fname, 0) line = nil loop do line = @main.nextline break if line.nil? success = process_line(line) break unless success end val = @main.finalize rescue nil @body # FIXME? @body.join("\n") # array return true end |
#process_line(line) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/livetext/helpers.rb', line 96 def process_line(line) success = true case line # must apply these in order when Comment success = handle_scomment(line) when DotCmd success = handle_dotcmd(line) # was 102 when DollarDot success = handle_dollar_dot(line) else api.passthru(line) # must succeed? end success rescue => err STDERR.puts "ERROR: #{err}\n#{err.backtrace.join("\n")}" exit end |
#read_variables(file) ⇒ Object
177 178 179 180 |
# File 'lib/livetext/helpers.rb', line 177 def read_variables(file) pairs = File.readlines(file).map {|x| x.chomp.split } @api.setvars(pairs) end |
#search_upward(file) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/livetext/helpers.rb', line 193 def search_upward(file) value = nil return file if File.exist?(file) count = 1 loop do front = "../" * count count += 1 here = Pathname.new(front)..dirname.to_s break if here == "/" path = front + file value = path if File.exist?(path) break if value end ::STDERR.puts "Cannot find #{file.inspect} from #{Dir.pwd}" unless value return value rescue ::STDERR.puts "Can't find #{file.inspect} from #{Dir.pwd}" return nil end |
#set_variables(pairs) ⇒ Object
182 183 184 |
# File 'lib/livetext/helpers.rb', line 182 def set_variables(pairs) @api.setvars(pairs) end |
#setfile(file) ⇒ Object
241 242 243 244 245 246 247 248 249 250 |
# File 'lib/livetext/helpers.rb', line 241 def setfile(file) if file api.setvar(:File, file) dir = File.dirname(File.(file)) api.setvar(:FileDir, dir) else api.setvar(:File, "[no file]") api.setvar(:FileDir, "[no dir]") end end |
#setfile!(file) ⇒ Object
FIXME why does this variant exist?
252 253 254 |
# File 'lib/livetext/helpers.rb', line 252 def setfile!(file) # FIXME why does this variant exist? api.setvar(:File, file) end |
#setvar(var, val) ⇒ Object
232 233 234 235 236 237 238 239 |
# File 'lib/livetext/helpers.rb', line 232 def setvar(var, val) api.setvar(var, val) # str, sym = var.to_s, var.to_sym # Livetext::Vars[str] = val # Livetext::Vars[sym] = val # @_vars[str] = val # @_vars[sym] = val end |
#showme(obj, tag = "") ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/livetext/helpers.rb', line 40 def showme(obj, tag = "") whence = caller[0] file, line, meth = whence.split(":") file = File.basename(file) meth = meth[4..-2] tag << " =" if tag hide_class = [true, false, nil].include?(obj) klass = hide_class ? "" : "(#{obj.class}) " puts " #{tag} #{klass}#{obj.inspect} in ##{meth} [#{file} line #{line}]" end |