Class: Livetext

Inherits:
Object
  • Object
show all
Defined in:
lib/livetext.rb,
lib/livetext.rb

Defined Under Namespace

Modules: Standard, UserAPI Classes: Functions, Processor

Constant Summary collapse

VERSION =
"0.8.70"
Path =
File.expand_path(File.join(File.dirname(__FILE__)))
Vars =
{}
Space =
" "

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = ::STDOUT) ⇒ Livetext

Returns a new instance of Livetext.



107
108
109
110
111
112
# File 'lib/livetext.rb', line 107

def initialize(output = ::STDOUT)
  @source = nil
  @_mixins = []
  @_outdir = "."
  @main = Processor.new(self, output)
end

Class Attribute Details

.outputObject

both bad solutions?



30
31
32
# File 'lib/livetext.rb', line 30

def output
  @output
end

.parametersObject

from outside world (process_text)



29
30
31
# File 'lib/livetext.rb', line 29

def parameters
  @parameters
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



25
26
27
# File 'lib/livetext.rb', line 25

def context
  @context
end

#mainObject (readonly)

Returns the value of attribute main.



25
26
27
# File 'lib/livetext.rb', line 25

def main
  @main
end

#no_putsObject

Returns the value of attribute no_puts.



26
27
28
# File 'lib/livetext.rb', line 26

def no_puts
  @no_puts
end

Instance Method Details

#_get_name(line, sigil = ".") ⇒ Object



227
228
229
230
231
232
233
234
235
236
# File 'lib/livetext.rb', line 227

def _get_name(line, sigil=".")
  name, data = line.split(" ", 2)
  name = name[1..-1]  # chop off sigil
  @main.data = data
  @main._error! "Name '#{name}' is not permitted" if @main._disallowed?(name)
  name = "_def" if name == "def"
  name = "_include" if name == "include"
  @main._error! "Mismatched 'end'" if name == "end"
  name
end

#handle_scomment(line, sigil = ".") ⇒ Object



224
225
# File 'lib/livetext.rb', line 224

def handle_scomment(line, sigil=".")
end

#handle_sname(line, sigil = ".") ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/livetext.rb', line 238

def handle_sname(line, sigil=".")
  name = _get_name(line, sigil)
  unless @main.respond_to?(name)
    @main._error! "Name '#{name}' is unknown"
    return
  end
  result = @main.send(name)
  result
rescue => err
  @main._error!(err)
end

#process(text) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/livetext.rb', line 150

def process(text)
  enum = text.each_line
  @main.source(enum, "STDIN", 0)
  loop do 
    line = @main.nextline
    break if line.nil?
    process_line(line)
  end
end

#process_file(fname, context = nil) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/livetext.rb', line 190

def process_file(fname, context=nil)
  context ||= binding
  @context = context
  raise "No such file '#{fname}' to process" unless File.exist?(fname)
  enum = File.readlines(fname).each
  @backtrace = false
  @main.source(enum, fname, 0)
  loop do 
    line = @main.nextline
    break if line.nil?
    process_line(line, context)
  end
  val = @main.finalize if @main.respond_to? :finalize
  val
end

#process_file!(fname, backtrace = false) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/livetext.rb', line 206

def process_file!(fname, backtrace=false)
  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, context = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/livetext.rb', line 114

def process_line(line, context=nil)
  context ||= binding
  @context = context
  sigil = "." # Can't change yet
  nomarkup = true
  # FIXME inefficient
  scomment  = rx(sigil, Livetext::Space)  # apply these in order
  sname     = rx(sigil)
  if line =~ scomment
    handle_scomment(line)
  elsif line =~ sname 
    handle_sname(line)
  else
    @main._passthru(line, context)
  end
end

#process_text(text) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/livetext.rb', line 173

def process_text(text)
  text = text.split("\n") if text.is_a? String
  enum = text.each
  @backtrace = false
  @main.source(enum, "(text)", 0)
  loop do 
    line = @main.nextline
    break if line.nil?
    process_line(line, context)
  end
  val = @main.finalize if @main.respond_to? :finalize
  val
rescue => err
  puts "process_text: err = #{err}"
  puts err.backtrace.join("\n")
end

#rx(str, space = nil) ⇒ Object



220
221
222
# File 'lib/livetext.rb', line 220

def rx(str, space=nil)
  Regexp.compile("^" + Regexp.escape(str) + "#{space}")
end

#transform(text) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/livetext.rb', line 160

def transform(text)
  @body = ""
  @output = ::Livetext.output
  enum = text.each_line
  @main.source(enum, "STDIN", 0)
  loop do 
    line = @main.nextline
    break if line.nil?
    process_line(line)
  end
  @body
end

#transform_line(line, context = nil) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/livetext.rb', line 131

def transform_line(line, context=nil)
  result = ""
  context ||= binding
  @context = context
  sigil = "." # Can't change yet
  nomarkup = true
  # FIXME inefficient
  scomment  = rx(sigil, Livetext::Space)  # apply these in order
  sname     = rx(sigil)
  if line =~ scomment
    handle_scomment(line)
  elsif line =~ sname 
    handle_sname(line)
  else
    result << line
  end
  result
end