Module: Livetext::Standard

Includes:
GlobalHelpers, Helpers
Included in:
Livetext, UserAPI
Defined in:
lib/livetext/standard.rb

Overview

Module Standard comprises most of the standard or “common” methods.

Constant Summary collapse

TTY =
::File.open("/dev/tty", "w")
SimpleFormats =

Move this?

{ 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

Methods included from Helpers

#check_disallowed, #escape_html, #file_exists?, #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

Methods included from GlobalHelpers

#check_disallowed, #check_file_exists, #cwd_root?, #grab_file, #search_upward

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

Instance Method Details

#backtrace(args, data) ⇒ Object

def setvars(pairs)

  pairs.each do |var, value|
    api.setvar(var, value)
  end
end


44
45
46
47
# File 'lib/livetext/standard.rb', line 44

def backtrace(args, data)
  @backtrace = onoff(api.args.first)
  api.optional_blank_line
end


159
160
161
162
163
164
165
# File 'lib/livetext/standard.rb', line 159

def banner(args, data)
  str = api.format(api.data)
  num = str.length
  decor = "-"*num + "\n"
  api.tty decor + str + "\n" + decor
  api.optional_blank_line
end

#bitsObject

dumb name - bold, italic, teletype, striketrough



29
30
31
32
33
34
35
36
# File 'lib/livetext/standard.rb', line 29

def bits   # FIXME umm what is this?
  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, data) ⇒ Object



507
508
509
510
511
512
513
# File 'lib/livetext/standard.rb', line 507

def br(args, data)
  num = api.args.first || "1"
  str = ""
  num.to_i.times { str << "<br>" }
  api.out str
  api.optional_blank_line
end

#cinclude(args, data) ⇒ Object

dot command



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/livetext/standard.rb', line 291

def cinclude(args, data)   # dot command
  file = api.expand_variables(api.args.first)    # allows for variables
  if api.args.size > 1  # there is an HTML file
    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)
  process_file(file)
  api.optional_blank_line
end

#cleanup(args, data) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/livetext/standard.rb', line 171

def cleanup(args, data)
  api.args.each do |item|
    cmd = ::File.directory?(item) ? "rm -f #{item}/*" : "rm #{item}"
    system(cmd)
  end
  api.optional_blank_line
end

#comment(args, data, body) ⇒ Object



49
50
51
52
# File 'lib/livetext/standard.rb', line 49

def comment(args, data, body)
  # body parameter contains the processed lines
  api.optional_blank_line
end

#copy(args, data) ⇒ Object



348
349
350
351
352
353
354
355
356
# File 'lib/livetext/standard.rb', line 348

def copy(args, data)
  file = api.args.first
  ok = file_exists?(file)

  graceful_error FileNotFound(file) unless ok   # FIXME seems weird?
    api.out grab_file(file)
  api.optional_blank_line
  [ok, file]
end

#debug(args, data) ⇒ Object



371
372
373
374
# File 'lib/livetext/standard.rb', line 371

def debug(args, data)
  @debug = onoff(api.args.first)
  api.optional_blank_line
end

#dlist(args, data, body) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/livetext/standard.rb', line 418

def dlist(args, data, body)
  delim = api.args.first
  html.wrap(:dl) do
    body.each 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, data, body) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/livetext/standard.rb', line 179

def dot_def(args, data, body)
  name = api.args[0]
  check_disallowed(name)
  
  # Check for parameter type specification
  param_type = api.args[1]&.downcase
  raw_body = api.args[2]&.downcase == 'raw'
  
  # Build method signature based on param_type
  case param_type
  when nil
    str = "def #{name}\n"
  when 'args'
    str = "def #{name}(args, data)\n"
  when 'body'
    str = "def #{name}(args, data, body)\n"
  else
    raise "Invalid parameter type: #{param_type}. Use 'args' or 'body' or omit."
  end
  
  str << body.join("\n")
  str << "\nend\n"
  eval str
  api.optional_blank_line
end

#dot_include(args, data) ⇒ Object

dot command



305
306
307
308
309
310
# File 'lib/livetext/standard.rb', line 305

def dot_include(args, data)   # dot command
  file = api.expand_variables(api.args.first)  # allows for variables
  check_file_exists(file)
  process_file(file)
  api.optional_blank_line
end

#errout(args, data) ⇒ Object



142
143
144
145
# File 'lib/livetext/standard.rb', line 142

def errout(args, data)
  ::STDERR.puts api.data
  api.optional_blank_line
end

#func(args, data, body) ⇒ Object



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

def func(args, data, body)
  funcname = api.args[0]
  # check_disallowed(funcname)  # should any be invalid?
  funcname = funcname.gsub(/\./, "__")
  function_body = body.join("\n")
  func_def = <<~EOS
    def #{funcname}(param)
      #{function_body}
    end
  EOS
  api.optional_blank_line
  
  # Register in old system (this works perfectly)
  Livetext::Functions.class_eval func_def
  
  # Also register in new registry with proper delegation
  function = ->(param) do
    fobj = ::Livetext::Functions.new
    fobj.send(funcname, param)
  end
  
               function_registry.register_user(funcname, function, source: :inline, filename: @current_file)
  return true
end

#functions(args, data) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/livetext/standard.rb', line 85

def functions(args, data)
  # List all available functions with their sources
  registry = function_registry
  functions = registry.list_functions
  
  if functions.empty?
    api.out "No functions available."
    return true
  end
  
  api.out "<h3>Available Functions</h3>"
  api.out "<ul>"
  
  functions.each do |func|
    api.out "<li><strong>$$#{func[:name]}</strong> - #{func[:source]}</li>"
  end
  
  api.out "</ul>"
  api.optional_blank_line
  return true
end

#h1(args, data) ⇒ Object

FIXME - move these to a single universal place in code



109
# File 'lib/livetext/standard.rb', line 109

def h1(args, data); api.out html.tag(:h1, cdata: api.format(api.data)); return true; end

#h2(args, data) ⇒ Object



110
# File 'lib/livetext/standard.rb', line 110

def h2(args, data); api.out html.tag(:h2, cdata: api.data); return true; end

#h3(args, data) ⇒ Object



111
# File 'lib/livetext/standard.rb', line 111

def h3(args, data); api.out html.tag(:h3, cdata: api.data); return true; end

#h4(args, data) ⇒ Object



112
# File 'lib/livetext/standard.rb', line 112

def h4(args, data); api.out html.tag(:h4, cdata: api.data); return true; end

#h5(args, data) ⇒ Object



113
# File 'lib/livetext/standard.rb', line 113

def h5(args, data); api.out html.tag(:h5, cdata: api.data); return true; end

#h6(args, data) ⇒ Object



114
# File 'lib/livetext/standard.rb', line 114

def h6(args, data); api.out html.tag(:h6, cdata: api.data); return true; end

#heading(args, data) ⇒ Object



398
399
400
401
402
403
# File 'lib/livetext/standard.rb', line 398

def heading(args, data)
  api.print "<center><font size=+1><b>"
  api.print api.data
  api.print "</b></font></center>"
  api.optional_blank_line
end

#heredoc(args, data, body) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/livetext/standard.rb', line 255

def heredoc(args, data, body)
  var = api.args[0]
  text = 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, data, body) ⇒ Object

no



269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/livetext/standard.rb', line 269

def heredoc!(args, data, body)     # no <br>
  var = api.args[0]
  text = 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, data) ⇒ Object



499
500
501
502
503
504
505
# File 'lib/livetext/standard.rb', line 499

def image(args, data)
  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, data) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
# File 'lib/livetext/standard.rb', line 336

def import(args, data)
  name = args.first   # Expect a module name
  @imports ||= []
  return if @imports.include?(name)
  @imports << name
  mod = Livetext::Handler::Import.get_module(name, self)
  self.extend(mod)
  init = "init_#{name}"
  self.send(init) rescue nil  # if self.respond_to? init
  api.optional_blank_line
end

#inherit(args, data) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/livetext/standard.rb', line 312

def inherit(args, data)
  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

  process_file(upper) if got_upper
  process_file(file)  if got_file
  api.optional_blank_line
end


432
433
434
435
436
437
# File 'lib/livetext/standard.rb', line 432

def link(args, data)
  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, data, body) ⇒ Object



116
117
118
119
120
121
# File 'lib/livetext/standard.rb', line 116

def list(args, data, body)
  html.wrap :ul do
    body.each {|line| api.out html.tag(:li, cdata: line) }
  end
  api.optional_blank_line
end

#list!(args, data, body) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/livetext/standard.rb', line 123

def list!(args, data, body)
  html.wrap(:ul) do
    lines = body.each   # enumerator
    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, data) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
# File 'lib/livetext/standard.rb', line 324

def mixin(args, data)
  name = args.first   # Expect a module name
  @mixins ||= []
  return if @mixins.include?(name)
  @mixins << name
  mod = Livetext::Handler::Mixin.get_module(name, self)
  self.extend(mod)
  init = "init_#{name}"
  self.send(init) rescue nil  # if self.respond_to? init
  api.optional_blank_line
end

#mono(args, data, body) ⇒ Object



411
412
413
414
415
416
# File 'lib/livetext/standard.rb', line 411

def mono(args, data, body)
  html.wrap ":pre" do
    api.body(true) {|line| api.out line }
  end
  api.optional_blank_line
end

#newpageObject



405
406
407
408
409
# File 'lib/livetext/standard.rb', line 405

def newpage
  api.out '<p style="page-break-after:always;"></p>'
  api.out "<p/>"
  api.optional_blank_line
end

#noparaObject



393
394
395
396
# File 'lib/livetext/standard.rb', line 393

def nopara
  @nopara = true
  api.optional_blank_line
end

#nopassObject



382
383
384
385
# File 'lib/livetext/standard.rb', line 382

def nopass
  @nopass = true
  api.optional_blank_line
end

#para(args, data) ⇒ Object



387
388
389
390
391
# File 'lib/livetext/standard.rb', line 387

def para(args, data)
  # FIXME - add check for args size? (helpers)
  @nopara = ! onoff(api.args.first)
  api.optional_blank_line
end

#passthru(args, data) ⇒ Object



376
377
378
379
380
# File 'lib/livetext/standard.rb', line 376

def passthru(args, data)
  # FIXME - add check for args size? (helpers)
  @nopass = ! onoff(api.args.first)
  api.optional_blank_line
end

#quitObject



167
168
169
# File 'lib/livetext/standard.rb', line 167

def quit
  @output.close
end

#r(args, data) ⇒ Object



358
359
360
361
362
363
# File 'lib/livetext/standard.rb', line 358

def r(args, data)
  # FIXME api.data is broken
  # api.out api.data  # No processing at all
  api.out api.args.join(" ")
  api.optional_blank_line
end

#raw(args, data) ⇒ Object



365
366
367
368
369
# File 'lib/livetext/standard.rb', line 365

def raw(args, data)
  # No processing at all (terminate with __EOF__)
  api.raw_body {|line| api.out line }  # no formatting
  api.optional_blank_line
end

#reflection(args, data) ⇒ Object

strictly experimental!



515
516
517
518
519
520
521
522
# File 'lib/livetext/standard.rb', line 515

def reflection(args, data)   # strictly experimental!
  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, data) ⇒ Object



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

def say(args, data)
  data = args || api.args.join(" ")
  str = api.format(data)
  TTY.puts str
  api.optional_blank_line
end

#seek(args, data) ⇒ Object

like include, but search upward as needed



283
284
285
286
287
288
289
# File 'lib/livetext/standard.rb', line 283

def seek(args, data)    # like include, but search upward as needed
  file = api.args.first
file = search_upward(file)
  check_file_exists(file)
  @parent.process_file(file)
  api.optional_blank_line
end

#set(args, data) ⇒ Object



205
206
207
208
209
210
# File 'lib/livetext/standard.rb', line 205

def set(args, data)
  line = api.args.join(" ")  # data.chomp
  pairs = Livetext::ParseSet.new(line).parse
  api.setvars(pairs)
  api.optional_blank_line
end

#shell(args, data) ⇒ Object



54
55
56
57
58
# File 'lib/livetext/standard.rb', line 54

def shell(args, data)
  cmd = api.data
  system(cmd)
  api.optional_blank_line
end

#shell!(args, data) ⇒ Object



136
137
138
139
140
# File 'lib/livetext/standard.rb', line 136

def shell!(args, data)
  cmd = api.data
  system(cmd)
  api.optional_blank_line
end

#table(args, data, body) ⇒ Object

Same as xtable



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/livetext/standard.rb', line 469

def table(args, data, body)   # Same as xtable
  title = api.data
  delim = " :: "
  api.out "<br>\n\n<center><table width=90% cellpadding=5>"
  lines = body
  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, data) ⇒ Object



147
148
149
150
# File 'lib/livetext/standard.rb', line 147

def ttyout(args, data)
  TTY.puts api.data
  api.optional_blank_line
end

#variables(args, data, body) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/livetext/standard.rb', line 229

def variables(args, data, body)
  prefix = api.args[0]
  fname = api.args[1]
  prefix = nil if prefix == "-"  # FIXME dumb hack
  fdir  = ::Livetext::Vars[:FileDir] + "/"   # where is the file we are reading?
  if fname
    path0  = fdir + fname
    # puts ">> variables: fdir = #{fdir} fname = #{fname} path = #{path0}"
    pname = Pathname.new(path0)
    rpath = pname.realpath
    path, dir, base = rpath.to_s, rpath.dirname.to_s, rpath.basename.to_s
    # puts "              rpath = #{rpath} path = #{path}  dir = #{dir}  base = #{base}"
    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 = body
  end
  pairs = Livetext::ParseGeneral.parse_vars(lines, prefix: nil)
  api.setvars(pairs)
  api.optional_blank_line
rescue => err
  fatal(err)
end

#variables!(args, data, body) ⇒ Object

FIXME really these should be one method…



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/livetext/standard.rb', line 214

def variables!(args, data, body)  # cwd, not FileDir - weird, fix later
  prefix = api.args[0]
  file = api.args[1]
  prefix = nil if prefix == "-"  # FIXME dumb hack
  if file
    here = ""  # different for ! version
    lines = File.readlines(here + file)
  else
    lines = body
  end
  pairs = Livetext::ParseGeneral.parse_vars(lines, prefix: nil)
  api.setvars(pairs)
  api.optional_blank_line
end

#xtable(args, data, body) ⇒ Object

Borrowed from bookish - FIXME



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/livetext/standard.rb', line 439

def xtable(args, data, body)   # Borrowed from bookish - FIXME
  title = api.data
  delim = " :: "
  api.out "<br>\n\n<center><table width=90% cellpadding=5>"
  lines = body
  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