Class: Livetext

Inherits:
Object
  • Object
show all
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.91"
Path =
File.expand_path(File.join(File.dirname(__FILE__)))
Vars =
{}
Space =
" "
Sigil =

Can’t change yet

"."

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = ::STDOUT) ⇒ Livetext

Returns a new instance of Livetext.



37
38
39
40
41
42
43
44
45
# File 'lib/livetext.rb', line 37

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

Class Attribute Details

.outputObject

both bad solutions?



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

def output
  @output
end

.parametersObject

from outside world (process_text)



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

def parameters
  @parameters
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#indentationObject

Returns the value of attribute indentation.



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

def indentation
  @indentation
end

#mainObject (readonly)

Returns the value of attribute main.



23
24
25
# File 'lib/livetext.rb', line 23

def main
  @main
end

#no_putsObject

Returns the value of attribute no_puts.



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

def no_puts
  @no_puts
end

Instance Method Details

#_check_name(name) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/livetext.rb', line 177

def _check_name(name)
  @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

#_get_name(line) ⇒ Object



185
186
187
188
189
190
# File 'lib/livetext.rb', line 185

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



53
54
55
56
# File 'lib/livetext.rb', line 53

def _setfile(file)
  _setvar(:File, file)
  _setvar(:FileDir, File.expand_path(file))
end

#_setfile!(file) ⇒ Object



58
59
60
# File 'lib/livetext.rb', line 58

def _setfile!(file)
  _setvar(:File, file)
end

#_setvar(var, val) ⇒ Object



47
48
49
50
51
# File 'lib/livetext.rb', line 47

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/livetext.rb', line 192

def handle_dotcmd(line, indent = 0)
  indent = @indentation.last # top of stack
  name = _get_name(line)
  result = nil
  if @main.respond_to?(name)
    result = @main.send(name)
  else
    @main._error! "Name '#{name}' is unknown"
    return
  end
  result
rescue => err
  @main._error!(err)
puts @body
@body = ""
  return @body
end

#handle_scomment(line) ⇒ Object



174
175
# File 'lib/livetext.rb', line 174

def handle_scomment(line)
end

#process(text) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/livetext.rb', line 84

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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/livetext.rb', line 136

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
return @body
  val
rescue
  puts @body
  @body = ""
end

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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/livetext.rb', line 155

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/livetext.rb', line 62

def process_line(line)
  nomarkup = true
  # FIXME inefficient
  comment  = rx(Sigil, Livetext::Space)  # apply these in order
  dotcmd   = rx(Sigil)
  ddotcmd  = /^ *\$\.[A-Za-z]/
  case line
    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



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

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

#rx(str, space = nil) ⇒ Object



170
171
172
# File 'lib/livetext.rb', line 170

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

#transform(text) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/livetext.rb', line 96

def transform(text)
  _setfile!("(string)")
  @output = ::Livetext.output
  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)  # transform_line ???
  end
  @body
end