Class: Livetext
- Inherits:
-
Object
- Object
- Livetext
- Defined in:
- lib/livetext.rb,
lib/livetext.rb,
lib/processor.rb
Defined Under Namespace
Modules: Standard, UserAPI Classes: Functions, Processor
Constant Summary collapse
- VERSION =
"0.8.97"- Path =
File.(File.join(File.dirname(__FILE__)))
- Vars =
{}
- Space =
" "- Sigil =
Can’t change yet
"."- Comment =
rx(Sigil, Livetext::Space)
- Dotcmd =
rx(Sigil)
- Ddotcmd =
/^ *\$\.[A-Za-z]/
Class Attribute Summary collapse
-
.output ⇒ Object
both bad solutions?.
-
.parameters ⇒ Object
from outside world (process_text).
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#indentation ⇒ Object
Returns the value of attribute indentation.
-
#main ⇒ Object
readonly
Returns the value of attribute main.
-
#no_puts ⇒ Object
Returns the value of attribute no_puts.
Class Method Summary collapse
Instance Method Summary collapse
- #_check_name(name) ⇒ Object
- #_get_name(line) ⇒ Object
- #_setfile(file) ⇒ Object
- #_setfile!(file) ⇒ Object
- #_setvar(var, val) ⇒ Object
- #handle_dotcmd(line, indent = 0) ⇒ Object
- #handle_scomment(line) ⇒ Object
-
#initialize(output = ::STDOUT) ⇒ Livetext
constructor
A new instance of Livetext.
- #mixin(mod) ⇒ Object
- #process(text) ⇒ Object
-
#process_file(fname, btrace = false) ⇒ Object
FIXME process_file should call process.
- #process_file!(fname, backtrace = false) ⇒ Object
-
#process_line(line) ⇒ Object
FIXME inefficient?.
-
#process_text(text) ⇒ Object
FIXME don’t need process and process_text.
- #transform(text) ⇒ Object
-
#xform(*args, file: nil, text: nil, vars: nil) ⇒ Object
EXPERIMENTAL and incomplete.
- #xform_file(file, vars: nil) ⇒ Object
Constructor Details
#initialize(output = ::STDOUT) ⇒ Livetext
Returns a new instance of Livetext.
45 46 47 48 49 50 51 52 53 |
# File 'lib/livetext.rb', line 45 def initialize(output = ::STDOUT) @source = nil @_mixins = [] @_outdir = "." @no_puts = output.nil? @body = "" @main = Processor.new(self, output) @indentation = [0] end |
Class Attribute Details
.output ⇒ Object
both bad solutions?
31 32 33 |
# File 'lib/livetext.rb', line 31 def output @output end |
.parameters ⇒ Object
from outside world (process_text)
30 31 32 |
# File 'lib/livetext.rb', line 30 def parameters @parameters end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
25 26 27 |
# File 'lib/livetext.rb', line 25 def body @body end |
#indentation ⇒ Object
Returns the value of attribute indentation.
25 26 27 |
# File 'lib/livetext.rb', line 25 def indentation @indentation end |
#main ⇒ Object (readonly)
Returns the value of attribute main.
23 24 25 |
# File 'lib/livetext.rb', line 23 def main @main end |
#no_puts ⇒ Object
Returns the value of attribute no_puts.
24 25 26 |
# File 'lib/livetext.rb', line 24 def no_puts @no_puts end |
Class Method Details
.rx(str, space = nil) ⇒ Object
37 38 39 |
# File 'lib/livetext.rb', line 37 def self.rx(str, space=nil) Regexp.compile("^" + Regexp.escape(str) + "#{space}") end |
Instance Method Details
#_check_name(name) ⇒ Object
202 203 204 205 206 207 |
# File 'lib/livetext.rb', line 202 def _check_name(name) @main._error! "Name '#{name}' is not permitted" if @main._disallowed?(name) @main._error! "Mismatched 'end'" if name == "end" name = "_" + name if %w[def include].include?(name) name end |
#_get_name(line) ⇒ Object
209 210 211 212 213 214 |
# File 'lib/livetext.rb', line 209 def _get_name(line) name, data = line.split(" ", 2) name = name[1..-1] # chop off sigil @main.data = data name = _check_name(name) end |
#_setfile(file) ⇒ Object
65 66 67 68 |
# File 'lib/livetext.rb', line 65 def _setfile(file) _setvar(:File, file) _setvar(:FileDir, File.(file)) end |
#_setfile!(file) ⇒ Object
70 71 72 |
# File 'lib/livetext.rb', line 70 def _setfile!(file) _setvar(:File, file) end |
#_setvar(var, val) ⇒ Object
59 60 61 62 63 |
# File 'lib/livetext.rb', line 59 def _setvar(var, val) str, sym = var.to_s, var.to_sym Livetext::Vars[str] = val Livetext::Vars[sym] = val end |
#handle_dotcmd(line, indent = 0) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/livetext.rb', line 216 def handle_dotcmd(line, indent = 0) indent = @indentation.last # top of stack name = _get_name(line).to_sym result = nil if @main.respond_to?(name) result = @main.send(name) else @main._error! "Name '#{name}' is unknown; version = #{Livetext::VERSION}" return end result rescue => err @main._error!(err) puts @body @body = "" return @body end |
#handle_scomment(line) ⇒ Object
199 200 |
# File 'lib/livetext.rb', line 199 def handle_scomment(line) end |
#mixin(mod) ⇒ Object
55 56 57 |
# File 'lib/livetext.rb', line 55 def mixin(mod) @main._mixin(mod) end |
#process(text) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/livetext.rb', line 92 def process(text) _setfile!("(string)") enum = text.each_line front = text.match(/.*?\n/).to_a.first.chomp @main.source(enum, "STDIN: '#{front}...'", 0) loop do line = @main.nextline break if line.nil? process_line(line) end end |
#process_file(fname, btrace = false) ⇒ Object
FIXME process_file should call process
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/livetext.rb', line 165 def process_file(fname, btrace=false) _setfile(fname) text = File.readlines(fname) enum = text.each @backtrace = btrace @main.source(enum, fname, 0) loop do line = @main.nextline break if line.nil? process_line(line) end val = @main.finalize if @main.respond_to? :finalize @body rescue => err STDERR.puts "ERROR #{err} in process_file" err.backtrace.each {|x| STDERR.puts " " + x } @body = "" end |
#process_file!(fname, backtrace = false) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/livetext.rb', line 184 def process_file!(fname, backtrace=false) _setfile(fname) raise "No such file '#{fname}' to process" unless File.exist?(fname) @main.output = StringIO.new enum = File.readlines(fname).each @backtrace = backtrace @main.source(enum, fname, 0) loop do line = @main.nextline break if line.nil? process_line(line) end @main.finalize if @main.respond_to? :finalize end |
#process_line(line) ⇒ Object
FIXME inefficient?
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/livetext.rb', line 74 def process_line(line) # FIXME inefficient? nomarkup = true case line # must apply these in order when Comment handle_scomment(line) when Dotcmd handle_dotcmd(line) when Ddotcmd indent = line.index("$") + 1 @indentation.push(indent) line.sub!(/^ *\$/, "") handle_dotcmd(line) indentation.pop else @main._passthru(line) end end |
#process_text(text) ⇒ Object
FIXME don’t need process and process_text
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/livetext.rb', line 141 def process_text(text) _setfile!("(string)") text = text.split("\n") if text.is_a? String enum = text.each @backtrace = false front = text[0].chomp @main.source(enum, "(text): '#{front}...'", 0) loop do line = @main.nextline break if line.nil? process_line(line) end val = @main.finalize if @main.respond_to? :finalize val rescue => err puts "process_text: err = #{err}" # puts err.backtrace.join("\n") puts @body @body = "" return @body end |
#transform(text) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/livetext.rb', line 125 def transform(text) _setfile!("(string)") @output = ::Livetext.output enum = text.each_line front = text.match(/.*?\n/).to_a.first.chomp rescue "..." @main.source(enum, "STDIN: '#{front}...'", 0) loop do line = @main.nextline break if line.nil? process_line(line) # transform_line ??? end @body end |
#xform(*args, file: nil, text: nil, vars: nil) ⇒ Object
EXPERIMENTAL and incomplete
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/livetext.rb', line 105 def xform(*args, file: nil, text: nil, vars: nil) case when file && text.nil? xform_file(file) when file.nil? && text when file.nil? && text.nil? raise "Must specify file or text" when file && text raise "Cannot specify file and text" end self.process_file(file) self.body end |
#xform_file(file, vars: nil) ⇒ Object
119 120 121 122 123 |
# File 'lib/livetext.rb', line 119 def xform_file(file, vars: nil) Livetext::Vars.replace(vars) unless vars.nil? self.process_file(file) self.body end |