Module: Livetext::Standard
Overview
Module Standard comprises most of the standard or “common” methods.
Constant Summary
collapse
- TTY =
::File.open("/dev/tty", "w")
- SimpleFormats =
{ b: %w[<b> </b>],
i: %w[<i> </i>],
t: ["<font size=+1><tt>", "</tt></font>"],
s: %w[<strike> </strike>] }
Constants included
from Helpers
Helpers::Comment, Helpers::DollarDot, Helpers::DotCmd, Helpers::ESCAPING, Helpers::Sigil, Helpers::Space
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#backtrace(args = nil, body = nil) ⇒ Object
def setvars(pairs) pairs.each do |var, value| api.setvar(var, value) end end.
-
#banner(args = nil, body = nil) ⇒ Object
-
#bits ⇒ Object
dumb name - bold, italic, teletype, striketrough.
-
#br(args = nil, body = nil) ⇒ Object
-
#cinclude(args = nil, body = nil) ⇒ Object
-
#cleanup(args = nil, body = nil) ⇒ Object
-
#comment(args = nil, body = nil) ⇒ Object
-
#copy(args = nil, body = nil) ⇒ Object
-
#debug(args = nil, body = nil) ⇒ Object
-
#dlist(args = nil, body = nil) ⇒ Object
-
#dot_def(args = nil, body = nil) ⇒ Object
-
#dot_include(args = nil, body = nil) ⇒ Object
-
#errout(args = nil, body = nil) ⇒ Object
-
#func(args = nil, body = nil) ⇒ Object
-
#h1(args = nil, body = nil) ⇒ Object
FIXME - move these to a single universal place in code.
-
#h2(args = nil, body = nil) ⇒ Object
-
#h3(args = nil, body = nil) ⇒ Object
-
#h4(args = nil, body = nil) ⇒ Object
-
#h5(args = nil, body = nil) ⇒ Object
-
#h6(args = nil, body = nil) ⇒ Object
-
#heading(args = nil, body = nil) ⇒ Object
-
#heredoc(args = nil, body = nil) ⇒ Object
-
#heredoc!(args = nil, body = nil) ⇒ Object
-
#image(args = nil, body = nil) ⇒ Object
-
#import(args = nil, body = nil) ⇒ Object
-
#inherit(args = nil, body = nil) ⇒ Object
-
#link(args = nil, body = nil) ⇒ Object
-
#list(args = nil, body = nil) ⇒ Object
-
#list!(args = nil, body = nil) ⇒ Object
-
#mixin(args = nil, body = nil) ⇒ Object
-
#mono(args = nil, body = nil) ⇒ Object
-
#newpage(args = nil, body = nil) ⇒ Object
-
#nopara(args = nil, body = nil) ⇒ Object
-
#nopass(args = nil, body = nil) ⇒ Object
-
#para(args = nil, body = nil) ⇒ Object
-
#passthru(args = nil, body = nil) ⇒ Object
-
#quit(args = nil, body = nil) ⇒ Object
-
#r(args = nil, body = nil) ⇒ Object
-
#raw(args = nil, body = nil) ⇒ Object
-
#reflection(args = nil, body = nil) ⇒ Object
-
#say(args = nil, body = nil) ⇒ Object
-
#seek(args = nil, body = nil) ⇒ Object
like include, but search upward as needed.
-
#set(args = nil, body = nil) ⇒ Object
-
#shell(args = nil, body = nil) ⇒ Object
-
#shell!(args = nil, body = nil) ⇒ Object
-
#table(args = nil, body = nil) ⇒ Object
-
#ttyout(args = nil, body = nil) ⇒ Object
-
#variables(args = nil, body = nil) ⇒ Object
-
#variables!(args = nil, body = nil) ⇒ Object
FIXME really these should be one method…
-
#xtable(args = nil, body = nil) ⇒ Object
Borrowed from bookish - FIXME.
Methods included from Helpers
#check_disallowed, #check_file_exists, #escape_html, #find_file, #friendly_error, #get_name_data, #grab_file, #graceful_error, #handle_dollar_dot, #handle_dotcmd, #handle_scomment, #include_file, #invoke_dotcmd, #onoff, #process_file, #process_line, #read_variables, rx, #search_upward, #set_variables, #setfile, #setfile!, #setvar, #showme
Instance Attribute Details
Returns the value of attribute data.
24
25
26
|
# File 'lib/livetext/standard.rb', line 24
def data
@data
end
|
Instance Method Details
#backtrace(args = nil, body = nil) ⇒ Object
def setvars(pairs)
pairs.each do |var, value|
api.setvar(var, value)
end
end
43
44
45
46
|
# File 'lib/livetext/standard.rb', line 43
def backtrace(args = nil, body = nil)
@backtrace = onoff(api.args.first)
api.optional_blank_line
end
|
#banner(args = nil, body = nil) ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/livetext/standard.rb', line 125
def banner(args = nil, body = nil)
str = api.format(api.data)
num = str.length
decor = "-"*num + "\n"
api.tty decor + str + "\n" + decor
api.optional_blank_line
end
|
dumb name - bold, italic, teletype, striketrough
28
29
30
31
32
33
34
35
|
# File 'lib/livetext/standard.rb', line 28
def bits b0, b1, i0, i1, t0, t1, s0, s1 = *api.args
SimpleFormats[:b] = [b0, b1]
SimpleFormats[:i] = [i0, i1]
SimpleFormats[:t] = [t0, t1]
SimpleFormats[:s] = [s0, s1]
api.optional_blank_line
end
|
#br(args = nil, body = nil) ⇒ Object
459
460
461
462
463
464
465
|
# File 'lib/livetext/standard.rb', line 459
def br(args = nil, body = nil)
num = api.args.first || "1"
str = ""
num.to_i.times { str << "<br>" }
api.out str
api.optional_blank_line
end
|
#cinclude(args = nil, body = nil) ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
253
254
|
# File 'lib/livetext/standard.rb', line 242
def cinclude(args = nil, body = nil) file = api.expand_variables(api.args.first) if api.args.size > 1 processed = api.expand_variables(api.args[1])
if File.exist?(processed) && File.mtime(processed) > File.mtime(file)
api.args = [processed]
copy
end
end
check_file_exists(file)
@parent.process_file(file)
api.optional_blank_line
end
|
#cleanup(args = nil, body = nil) ⇒ Object
137
138
139
140
141
142
143
|
# File 'lib/livetext/standard.rb', line 137
def cleanup(args = nil, body = nil)
api.args.each do |item|
cmd = ::File.directory?(item) ? "rm -f #{item}/*" : "rm #{item}"
system(cmd)
end
api.optional_blank_line
end
|
48
49
50
51
|
# File 'lib/livetext/standard.rb', line 48
def (args = nil, body = nil)
api.body
api.optional_blank_line
end
|
#copy(args = nil, body = nil) ⇒ Object
300
301
302
303
304
305
306
307
308
|
# File 'lib/livetext/standard.rb', line 300
def copy(args = nil, body = nil)
file = api.args.first
ok = check_file_exists(file)
self.parent.graceful_error FileNotFound(file) unless ok api.out grab_file(file)
api.optional_blank_line
[ok, file]
end
|
#debug(args = nil, body = nil) ⇒ Object
323
324
325
326
|
# File 'lib/livetext/standard.rb', line 323
def debug(args = nil, body = nil)
@debug = onoff(api.args.first)
api.optional_blank_line
end
|
#dlist(args = nil, body = nil) ⇒ Object
370
371
372
373
374
375
376
377
378
379
380
381
382
|
# File 'lib/livetext/standard.rb', line 370
def dlist(args = nil, body = nil)
delim = api.args.first
html.wrap(:dl) do
api.body do |line|
line = api.format(line)
term, defn = line.split(delim)
api.out html.tag(:dt, cdata: term)
api.out " " + html.tag(:dd, cdata: defn)
end
end
api.out ""
api.optional_blank_line
end
|
#dot_def(args = nil, body = nil) ⇒ Object
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/livetext/standard.rb', line 145
def dot_def(args = nil, body = nil)
name = api.args[0]
check_disallowed(name)
str = "def #{name}\n"
str << api.body(true).join("\n")
str << "\nend\n"
eval str
api.optional_blank_line
end
|
#dot_include(args = nil, body = nil) ⇒ Object
256
257
258
259
260
261
262
|
# File 'lib/livetext/standard.rb', line 256
def dot_include(args = nil, body = nil) file = api.expand_variables(api.args.first) check_file_exists(file)
checkpoint
@parent.process_file(file)
api.optional_blank_line
end
|
#errout(args = nil, body = nil) ⇒ Object
108
109
110
111
|
# File 'lib/livetext/standard.rb', line 108
def errout(args = nil, body = nil)
::STDERR.puts api.data
api.optional_blank_line
end
|
#func(args = nil, body = nil) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/livetext/standard.rb', line 59
def func(args = nil, body = nil)
funcname = api.args[0]
funcname = funcname.gsub(/\./, "__")
func_def = <<~EOS
def #{funcname}(param)
#{api.body(true).to_a.join("\n")}
end
EOS
api.optional_blank_line
Livetext::Functions.class_eval func_def
return true
end
|
#h1(args = nil, body = nil) ⇒ Object
FIXME - move these to a single universal place in code
75
|
# File 'lib/livetext/standard.rb', line 75
def h1(args = nil, body = nil); api.out html.tag(:h1, cdata: api.data); return true; end
|
#h2(args = nil, body = nil) ⇒ Object
76
|
# File 'lib/livetext/standard.rb', line 76
def h2(args = nil, body = nil); api.out html.tag(:h2, cdata: api.data); return true; end
|
#h3(args = nil, body = nil) ⇒ Object
77
|
# File 'lib/livetext/standard.rb', line 77
def h3(args = nil, body = nil); api.out html.tag(:h3, cdata: api.data); return true; end
|
#h4(args = nil, body = nil) ⇒ Object
78
|
# File 'lib/livetext/standard.rb', line 78
def h4(args = nil, body = nil); api.out html.tag(:h4, cdata: api.data); return true; end
|
#h5(args = nil, body = nil) ⇒ Object
79
|
# File 'lib/livetext/standard.rb', line 79
def h5(args = nil, body = nil); api.out html.tag(:h5, cdata: api.data); return true; end
|
#h6(args = nil, body = nil) ⇒ Object
80
|
# File 'lib/livetext/standard.rb', line 80
def h6(args = nil, body = nil); api.out html.tag(:h6, cdata: api.data); return true; end
|
#heading(args = nil, body = nil) ⇒ Object
350
351
352
353
354
355
|
# File 'lib/livetext/standard.rb', line 350
def heading(args = nil, body = nil)
api.print "<center><font size=+1><b>"
api.print api.data
api.print "</b></font></center>"
api.optional_blank_line
end
|
#heredoc(args = nil, body = nil) ⇒ Object
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/livetext/standard.rb', line 206
def heredoc(args = nil, body = nil)
var = api.args[0]
text = api.body.join("\n")
rhs = ""
text.each_line do |line|
str = api.format(line.chomp)
rhs << str + "<br>\n"
end
indent = @parent.indentation.last
indented = " " * indent
api.setvar(var, rhs.chomp)
api.optional_blank_line
end
|
#heredoc!(args = nil, body = nil) ⇒ Object
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/livetext/standard.rb', line 220
def heredoc!(args = nil, body = nil) var = api.args[0]
text = api.body.join("\n")
rhs = ""
text.each_line do |line|
str = api.format(line.chomp)
rhs << str + "\n"
end
indent = @parent.indentation.last
indented = " " * indent
api.setvar(var, rhs.chomp)
api.optional_blank_line
end
|
#image(args = nil, body = nil) ⇒ Object
451
452
453
454
455
456
457
|
# File 'lib/livetext/standard.rb', line 451
def image(args = nil, body = nil)
name, wide, high = api.args
geom = ""
geom = "width=#{wide} height=#{high}" if wide || high
api.out "<img src='#{name} #{geom}'></img>"
api.optional_blank_line
end
|
#import(args = nil, body = nil) ⇒ Object
288
289
290
291
292
293
294
295
296
297
298
|
# File 'lib/livetext/standard.rb', line 288
def import(args = nil, body = nil)
name = api.args.first @imports ||= []
return if @imports.include?(name)
@imports << name
mod = Livetext::Handler::Import.get_module(name, @parent)
self.extend(mod)
init = "init_#{name}"
self.send(init) rescue nil api.optional_blank_line
end
|
#inherit(args = nil, body = nil) ⇒ Object
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/livetext/standard.rb', line 264
def inherit(args = nil, body = nil)
file = api.args.first
upper = "../#{file}"
got_upper, got_file = File.exist?(upper), File.exist?(file)
good = got_upper || got_file
STDERR.puts "File #{file} not found (local or parent)" unless good
@parent.process_file(upper) if got_upper
@parent.process_file(file) if got_file
api.optional_blank_line
end
|
#link(args = nil, body = nil) ⇒ Object
384
385
386
387
388
389
|
# File 'lib/livetext/standard.rb', line 384
def link(args = nil, body = nil)
url = api.args.first
text = api.args[2..-1].join(" ")
api.out "<a style='text-decoration: none' href='#{url}'>#{text}</a>"
api.optional_blank_line
end
|
#list(args = nil, body = nil) ⇒ Object
82
83
84
85
86
87
|
# File 'lib/livetext/standard.rb', line 82
def list(args = nil, body = nil)
html.wrap :ul do
api.body {|line| api.out html.tag(:li, cdata: line) }
end
api.optional_blank_line
end
|
#list!(args = nil, body = nil) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/livetext/standard.rb', line 89
def list!(args = nil, body = nil)
html.wrap(:ul) do
lines = api.body.each loop do
line = lines.next
line = api.format(line)
str = line[0] == " " ? line : html.tag(:li, cdata: line)
api.out str
end
end
api.optional_blank_line
end
|
#mixin(args = nil, body = nil) ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/livetext/standard.rb', line 276
def mixin(args = nil, body = nil)
name = api.args.first @mixins ||= []
return if @mixins.include?(name)
@mixins << name
mod = Livetext::Handler::Mixin.get_module(name, @parent)
self.extend(mod)
init = "init_#{name}"
self.send(init) rescue nil api.optional_blank_line
end
|
#mono(args = nil, body = nil) ⇒ Object
363
364
365
366
367
368
|
# File 'lib/livetext/standard.rb', line 363
def mono(args = nil, body = nil)
html.wrap ":pre" do
api.body(true) {|line| api.out line }
end
api.optional_blank_line
end
|
#newpage(args = nil, body = nil) ⇒ Object
357
358
359
360
361
|
# File 'lib/livetext/standard.rb', line 357
def newpage(args = nil, body = nil)
api.out '<p style="page-break-after:always;"></p>'
api.out "<p/>"
api.optional_blank_line
end
|
#nopara(args = nil, body = nil) ⇒ Object
345
346
347
348
|
# File 'lib/livetext/standard.rb', line 345
def nopara(args = nil, body = nil)
@nopara = true
api.optional_blank_line
end
|
#nopass(args = nil, body = nil) ⇒ Object
334
335
336
337
|
# File 'lib/livetext/standard.rb', line 334
def nopass(args = nil, body = nil)
@nopass = true
api.optional_blank_line
end
|
#para(args = nil, body = nil) ⇒ Object
339
340
341
342
343
|
# File 'lib/livetext/standard.rb', line 339
def para(args = nil, body = nil)
@nopara = ! onoff(api.args.first)
api.optional_blank_line
end
|
#passthru(args = nil, body = nil) ⇒ Object
328
329
330
331
332
|
# File 'lib/livetext/standard.rb', line 328
def passthru(args = nil, body = nil)
@nopass = ! onoff(api.args.first)
api.optional_blank_line
end
|
#quit(args = nil, body = nil) ⇒ Object
133
134
135
|
# File 'lib/livetext/standard.rb', line 133
def quit(args = nil, body = nil)
@output.close
end
|
#r(args = nil, body = nil) ⇒ Object
310
311
312
313
314
315
|
# File 'lib/livetext/standard.rb', line 310
def r(args = nil, body = nil)
api.out api.args.join(" ")
api.optional_blank_line
end
|
#raw(args = nil, body = nil) ⇒ Object
317
318
319
320
321
|
# File 'lib/livetext/standard.rb', line 317
def raw(args = nil, body = nil)
api.raw_body {|line| api.out line } api.optional_blank_line
end
|
#reflection(args = nil, body = nil) ⇒ Object
467
468
469
470
471
472
473
474
|
# File 'lib/livetext/standard.rb', line 467
def reflection(args = nil, body = nil) list = self.methods
obj = Object.instance_methods
diff = (list - obj).sort
api.out "#{diff.size} methods:"
api.out diff.inspect
api.optional_blank_line
end
|
#say(args = nil, body = nil) ⇒ Object
118
119
120
121
122
123
|
# File 'lib/livetext/standard.rb', line 118
def say(args = nil, body = nil)
data = args || api.args.join(" ")
str = api.format(data)
TTY.puts str
api.optional_blank_line
end
|
#seek(args = nil, body = nil) ⇒ Object
like include, but search upward as needed
234
235
236
237
238
239
240
|
# File 'lib/livetext/standard.rb', line 234
def seek(args = nil, body = nil) file = api.args.first
file = search_upward(file)
check_file_exists(file)
@parent.process_file(file)
api.optional_blank_line
end
|
#set(args = nil, body = nil) ⇒ Object
156
157
158
159
160
161
|
# File 'lib/livetext/standard.rb', line 156
def set(args = nil, body = nil)
line = api.args.join(" ") pairs = Livetext::ParseSet.new(line).parse
api.setvars(pairs)
api.optional_blank_line
end
|
#shell(args = nil, body = nil) ⇒ Object
53
54
55
56
57
|
# File 'lib/livetext/standard.rb', line 53
def shell(args = nil, body = nil)
cmd = api.data
system(cmd)
api.optional_blank_line
end
|
#shell!(args = nil, body = nil) ⇒ Object
102
103
104
105
106
|
# File 'lib/livetext/standard.rb', line 102
def shell!(args = nil, body = nil)
cmd = api.data
system(cmd)
api.optional_blank_line
end
|
#table(args = nil, body = nil) ⇒ Object
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
# File 'lib/livetext/standard.rb', line 421
def table(args = nil, body = nil) title = api.data
delim = " :: "
api.out "<br>\n\n<center><table width=90% cellpadding=5>"
lines = api.body(true)
maxw = nil
processed = []
lines.each do |line|
line = api.format(line)
line.gsub!(/\n+/, "<br>\n")
processed << line
cells = line.split(delim)
wide = cells.map {|cell| cell.length }
maxw = [0] * cells.size
maxw = maxw.map.with_index {|x, i| [x, wide[i]].max }
end
sum = maxw.inject(0, :+)
maxw.map! {|x| (x/sum*100).floor }
processed.each do |line|
cells = line.split(delim)
html.wrap :tr do
cells.each {|cell| api.out " <td valign=top>#{cell}</td>" }
end
end
api.out "</table></center>"
api.optional_blank_line
end
|
#ttyout(args = nil, body = nil) ⇒ Object
113
114
115
116
|
# File 'lib/livetext/standard.rb', line 113
def ttyout(args = nil, body = nil)
TTY.puts api.data
api.optional_blank_line
end
|
#variables(args = nil, body = nil) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/livetext/standard.rb', line 180
def variables(args = nil, body = nil)
prefix = api.args[0]
fname = api.args[1]
prefix = nil if prefix == "-" fdir = ::Livetext::Vars[:FileDir] + "/" if fname
path0 = fdir + fname
pname = Pathname.new(path0)
rpath = pname.realpath
path, dir, base = rpath.to_s, rpath.dirname.to_s, rpath.basename.to_s
dok, fok = Dir.exist?(dir), File.exist?(path)
raise "No such dir #{dir.inspect} (file #{path})" unless dok
raise "No such file #{path.inspect} (file #{path})" unless fok
lines = File.readlines(path)
else
lines = api.body
end
pairs = Livetext::ParseGeneral.parse_vars(lines, prefix: nil)
api.setvars(pairs)
api.optional_blank_line
rescue => err
fatal(err)
end
|
#variables!(args = nil, body = nil) ⇒ Object
FIXME really these should be one method…
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/livetext/standard.rb', line 165
def variables!(args = nil, body = nil) prefix = api.args[0]
file = api.args[1]
prefix = nil if prefix == "-" if file
here = "" lines = File.readlines(here + file)
else
lines = api.body
end
pairs = Livetext::ParseGeneral.parse_vars(lines, prefix: nil)
api.setvars(pairs)
api.optional_blank_line
end
|
#xtable(args = nil, body = nil) ⇒ Object
Borrowed from bookish - FIXME
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
# File 'lib/livetext/standard.rb', line 391
def xtable(args = nil, body = nil) title = api.data
delim = " :: "
api.out "<br>\n\n<center><table width=90% cellpadding=5>"
lines = api.body(true)
maxw = nil
processed = []
lines.each do |line|
line = api.format(line)
line.gsub!(/\n+/, "<br>\n")
processed << line
cells = line.split(delim)
wide = cells.map {|cell| cell.length }
maxw = [0] * cells.size
maxw = maxw.map.with_index {|x, i| [x, wide[i]].max }
end
sum = maxw.inject(0, :+)
maxw.map! {|x| (x/sum*100).floor }
processed.each do |line|
cells = line.split(delim)
html.wrap :tr do
cells.each {|cell| api.out " <td valign=top>#{cell}</td>" }
end
end
api.out "</table></center>"
api.optional_blank_line
end
|