Module: Livetext::Standard

Includes:
HTMLHelper, Helpers
Included in:
Processor
Defined in:
lib/livetext/standard.rb

Overview

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

Constant Summary collapse

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, #check_file_exists, #escape_html, #find_file, #friendly_error, #get_name, #grab_file, #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 HTMLHelper

#open_close_tags, #wrap, #wrapped, #wrapped!

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

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


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

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


133
134
135
136
137
138
# File 'lib/livetext/standard.rb', line 133

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

#bits(args = nil, body = nil) ⇒ Object

dumb name - bold, italic, teletype, striketrough



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

def bits(args = nil, body = nil)   # 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 = nil, body = nil) ⇒ Object



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

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

#cleanup(args = nil, body = nil) ⇒ Object



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

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

#comment(args = nil, body = nil) ⇒ Object



59
60
61
62
# File 'lib/livetext/standard.rb', line 59

def comment(args = nil, body = nil)
  api.body
  api.optional_blank_line
end

#copy(args = nil, body = nil) ⇒ Object



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

def copy(args = nil, body = nil)
  file = api.args.first
  ok = check_file_exists(file)

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

#debug(args = nil, body = nil) ⇒ Object



294
295
296
297
# File 'lib/livetext/standard.rb', line 294

def debug(args = nil, body = nil)
  self._debug = onoff(api.args.first)
  api.optional_blank_line
end

#dlist(args = nil, body = nil) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/livetext/standard.rb', line 341

def dlist(args = nil, body = nil)
  delim = api.args.first
  wrap(:dl) do
    api.body do |line|
      line = api.format(line)
      term, defn = line.split(delim)
      api.out wrapped(term, :dt)
      api.out wrapped(defn, :dd)
    end
  end
  api.optional_blank_line
end

#dot_def(args = nil, body = nil) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/livetext/standard.rb', line 152

def dot_def(args = nil, body = nil)
# api.tty "in #{__FILE__}: api id = #{api.inspect}"
  name = api.args[0]
# api.tty :dd1
# api.tty name.inspect
  check_disallowed(name)
# api.tty :dd2
  # Difficult to avoid eval here
  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

dot command



230
231
232
233
234
235
# File 'lib/livetext/standard.rb', line 230

def dot_include(args = nil, body = nil)   # dot command
  file = api.format(api.args.first)  # allows for variables
  check_file_exists(file)
  @parent.process_file(file)
  api.optional_blank_line
end

#errout(args = nil, body = nil) ⇒ Object



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

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

#func(args = nil, body = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/livetext/standard.rb', line 70

def func(args = nil, body = nil)
  funcname = api.args[0]
  # check_disallowed(funcname)  # should any be invalid?
  funcname = funcname.gsub(/\./, "__")
  func_def = "    def \#{funcname}(param)\n      \#{api.body.to_a.join(\"\\n\")}\n    end\n  EOS\n  api.optional_blank_line\n  Livetext::Functions.class_eval func_def\n  return true\nend\n"

#h1(args = nil, body = nil) ⇒ Object



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

def h1(args = nil, body = nil); api.out wrapped(api.data, :h1); return true; end

#h2(args = nil, body = nil) ⇒ Object



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

def h2(args = nil, body = nil); api.out wrapped(api.data, :h2); return true; end

#h3(args = nil, body = nil) ⇒ Object



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

def h3(args = nil, body = nil); api.out wrapped(api.data, :h3); return true; end

#h4(args = nil, body = nil) ⇒ Object



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

def h4(args = nil, body = nil); api.out wrapped(api.data, :h4); return true; end

#h5(args = nil, body = nil) ⇒ Object



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

def h5(args = nil, body = nil); api.out wrapped(api.data, :h5); return true; end

#h6(args = nil, body = nil) ⇒ Object



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

def h6(args = nil, body = nil); api.out wrapped(api.data, :h6); return true; end

#heading(args = nil, body = nil) ⇒ Object



321
322
323
324
325
326
# File 'lib/livetext/standard.rb', line 321

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



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

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>"
  end
  indent = @parent.indentation.last
  indented = " " * indent
  api.set(var, rhs.chomp)
  api.optional_blank_line
end

#image(args = nil, body = nil) ⇒ Object



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

def image(args = nil, body = nil)
  name = api.args[0]
  api.out "<img src='#{name}'></img>"
  api.optional_blank_line
end

#import(args = nil, body = nil) ⇒ Object



260
261
262
263
264
265
266
267
268
269
# File 'lib/livetext/standard.rb', line 260

def import(args = nil, body = nil)
  name = api.args.first   # Expect a module name
  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  # if self.respond_to? init
  api.optional_blank_line
end

#inherit(args = nil, body = nil) ⇒ Object



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

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


354
355
356
357
358
359
# File 'lib/livetext/standard.rb', line 354

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



91
92
93
94
95
96
# File 'lib/livetext/standard.rb', line 91

def list(args = nil, body = nil)
  wrap :ul do
    api.body {|line| api.out wrapped(line, :li) }
  end
  api.optional_blank_line
end

#list!(args = nil, body = nil) ⇒ Object



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

def list!(args = nil, body = nil)
  wrap(:ul) do
    lines = api.body.each   # enumerator
    loop do
      line = lines.next
      line = api.format(line)
      str = line[0] == " " ? line : wrapped(line, :li)
      api.out str
    end
  end
  api.optional_blank_line
end

#mixin(args = nil, body = nil) ⇒ Object



249
250
251
252
253
254
255
256
257
258
# File 'lib/livetext/standard.rb', line 249

def mixin(args = nil, body = nil)
  name = api.args.first   # Expect a module name
  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  # if self.respond_to? init
  api.optional_blank_line
end

#mono(args = nil, body = nil) ⇒ Object



334
335
336
337
338
339
# File 'lib/livetext/standard.rb', line 334

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

#newpage(args = nil, body = nil) ⇒ Object



328
329
330
331
332
# File 'lib/livetext/standard.rb', line 328

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



316
317
318
319
# File 'lib/livetext/standard.rb', line 316

def nopara(args = nil, body = nil)
  @nopara = true
  api.optional_blank_line
end

#nopass(args = nil, body = nil) ⇒ Object



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

def nopass(args = nil, body = nil)
  @nopass = true
  api.optional_blank_line
end

#para(args = nil, body = nil) ⇒ Object



310
311
312
313
314
# File 'lib/livetext/standard.rb', line 310

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

#passthru(args = nil, body = nil) ⇒ Object



299
300
301
302
303
# File 'lib/livetext/standard.rb', line 299

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

#quit(args = nil, body = nil) ⇒ Object



140
141
142
# File 'lib/livetext/standard.rb', line 140

def quit(args = nil, body = nil)
  @output.close
end

#r(args = nil, body = nil) ⇒ Object



281
282
283
284
285
286
# File 'lib/livetext/standard.rb', line 281

def r(args = nil, body = nil)
  # 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 = nil, body = nil) ⇒ Object



288
289
290
291
292
# File 'lib/livetext/standard.rb', line 288

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

#reflectionObject

strictly experimental!



406
407
408
409
410
411
412
413
# File 'lib/livetext/standard.rb', line 406

def reflection   # 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 = nil, body = nil) ⇒ Object



127
128
129
130
131
# File 'lib/livetext/standard.rb', line 127

def say(args = nil, body = nil)
  str = api.format(api.data)
  TTY.puts str
  api.optional_blank_line
end

#seek(args = nil, body = nil) ⇒ Object

like include, but search upward as needed



222
223
224
225
226
227
228
# File 'lib/livetext/standard.rb', line 222

def seek(args = nil, body = nil)    # 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 = nil, body = nil) ⇒ Object



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

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

#shell(args = nil, body = nil) ⇒ Object



64
65
66
67
68
# File 'lib/livetext/standard.rb', line 64

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

#shell!(args = nil, body = nil) ⇒ Object



111
112
113
114
115
# File 'lib/livetext/standard.rb', line 111

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

#ttyout(args = nil, body = nil) ⇒ Object



122
123
124
125
# File 'lib/livetext/standard.rb', line 122

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

#variables(args = nil, body = nil) ⇒ Object



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

def variables(args = nil, body = nil)
  prefix = api.args[0]
  file = api.args[1]
  prefix = nil if prefix == "-"  # FIXME dumb hack
  if file
    here = ::Livetext::Vars[:FileDir] + "/"
    lines = File.readlines(here + file)
  else
    lines = api.body
  end
  pairs = Livetext::ParseGeneral.parse_vars(lines, prefix: nil)
STDERR.puts "pairs = #{pairs.inspect}"
  api.setvars(pairs)
  api.optional_blank_line
end

#variables!(args = nil, body = nil) ⇒ Object

FIXME really these should be one method…



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/livetext/standard.rb', line 176

def variables!(args = nil, body = nil)  # 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 = api.body
  end
  pairs = Livetext::ParseGeneral.parse_vars(lines, prefix: nil)
STDERR.puts "! pairs = #{pairs.inspect}"
  api.setvars(pairs)
  api.optional_blank_line
end

#xtable(args = nil, body = nil) ⇒ Object

Borrowed from bookish - FIXME



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/livetext/standard.rb', line 361

def xtable(args = nil, body = nil)   # Borrowed from bookish - FIXME
# TTY.puts "=== #{__method__} #{__FILE__} #{__LINE__}"
  title = api.data
  delim = " :: "
  api.out "<br><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>")
    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)
    wrap :tr do
      cells.each {|cell| api.out "  <td valign=top>#{cell}</td>" }
    end
  end
  api.out "</table></center>"
  api.optional_blank_line
end