Class: Livetext
- Inherits:
-
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.85"
- Path =
File.expand_path(File.join(File.dirname(__FILE__)))
- Vars =
{}
- Space =
" "
- Sigil =
"."
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
.output ⇒ Object
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
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
|
Instance Method Details
#_check_name(name) ⇒ Object
176
177
178
179
180
181
182
|
# File 'lib/livetext.rb', line 176
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
184
185
186
187
188
189
|
# File 'lib/livetext.rb', line 184
def _get_name(line)
name, data = line.split(" ", 2)
name = name[1..-1]
@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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/livetext.rb', line 191
def handle_dotcmd(line, indent = 0)
indent = @indentation.last
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
end
|
173
174
|
# File 'lib/livetext.rb', line 173
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) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/livetext.rb', line 134
def process_file(fname)
_setfile(fname)
up = Dir.pwd
Dir.chdir(File.dirname(fname))
base = File.basename(fname)
raise "No such file '#{fname}' to process" unless File.exist?(base)
text = File.readlines(base)
enum = text.each
@backtrace = false
@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
Dir.chdir(up)
val
end
|
#process_file!(fname, backtrace = false) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/livetext.rb', line 154
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
= rx(Sigil, Livetext::Space)
dotcmd = rx(Sigil)
ddotcmd = /^ *\$\.[A-Za-z]/
case line
when
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
|
# 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
end
|
#rx(str, space = nil) ⇒ Object
169
170
171
|
# File 'lib/livetext.rb', line 169
def rx(str, space=nil)
Regexp.compile("^" + Regexp.escape(str) + "#{space}")
end
|
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)
end
@body
end
|