Module: Livetext::Standard

Includes:
Helpers
Included in:
UserAPI, Processor
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, #check_file_exists, #escape_html, #find_file, #friendly_error, #get_name_data, #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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



28
29
30
# File 'lib/livetext/standard.rb', line 28

def data
  @data
end

Instance Method Details

#backtraceObject

def setvars(pairs)

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


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

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


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

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

#bitsObject

dumb name - bold, italic, teletype, striketrough



32
33
34
35
36
37
38
39
# File 'lib/livetext/standard.rb', line 32

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

#brObject



389
390
391
392
393
394
395
# File 'lib/livetext/standard.rb', line 389

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

#cleanupObject



138
139
140
141
142
143
144
# File 'lib/livetext/standard.rb', line 138

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

#commentObject



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

def comment
  api.body
  api.optional_blank_line
end

#copyObject



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

def copy
  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

#debugObject



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

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

#dlistObject



331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/livetext/standard.rb', line 331

def dlist
  delim = api.args.first
  html.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_defObject



146
147
148
149
150
151
152
153
154
155
# File 'lib/livetext/standard.rb', line 146

def dot_def
  name = api.args[0]
  check_disallowed(name)
  # 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_includeObject

dot command



218
219
220
221
222
223
# File 'lib/livetext/standard.rb', line 218

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

#erroutObject



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

def errout
  ::STDERR.puts api.data
  api.optional_blank_line
end

#funcObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/livetext/standard.rb', line 63

def func
  funcname = api.args[0]
  # check_disallowed(funcname)  # should any be invalid?
  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

#h1Object



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

def h1; api.out wrapped(api.data, :h1); return true; end

#h2Object



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

def h2; api.out wrapped(api.data, :h2); return true; end

#h3Object



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

def h3; api.out wrapped(api.data, :h3); return true; end

#h4Object



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

def h4; api.out wrapped(api.data, :h4); return true; end

#h5Object



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

def h5; api.out wrapped(api.data, :h5); return true; end

#h6Object



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

def h6; api.out wrapped(api.data, :h6); return true; end

#headingObject



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

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

#heredocObject



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

def heredoc
  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.setvar(var, rhs.chomp)
  api.optional_blank_line
end

#imageObject



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

def image
  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

#importObject



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

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

#inheritObject



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/livetext/standard.rb', line 225

def inherit
  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


344
345
346
347
348
349
# File 'lib/livetext/standard.rb', line 344

def link
  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

#listObject



84
85
86
87
88
89
# File 'lib/livetext/standard.rb', line 84

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

#list!Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/livetext/standard.rb', line 91

def list!
  html.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

#mixinObject



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

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

#monoObject



324
325
326
327
328
329
# File 'lib/livetext/standard.rb', line 324

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

#newpageObject



318
319
320
321
322
# File 'lib/livetext/standard.rb', line 318

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

#noparaObject



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

def nopara
  @nopara = true
  api.optional_blank_line
end

#nopassObject



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

def nopass
  @nopass = true
  api.optional_blank_line
end

#paraObject



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

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

#passthruObject



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

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

#quitObject



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

def quit
  @output.close
end

#rObject



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

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

#rawObject



278
279
280
281
282
# File 'lib/livetext/standard.rb', line 278

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

#reflectionObject

strictly experimental!



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

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(arg = nil) ⇒ Object



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

def say(arg=nil)
  data = arg || api.args.join(" ")
  str = api.format(data)
  TTY.puts "(say) #{str}"
  api.optional_blank_line
end

#seekObject

like include, but search upward as needed



210
211
212
213
214
215
216
# File 'lib/livetext/standard.rb', line 210

def seek    # 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

#setObject



157
158
159
160
161
162
# File 'lib/livetext/standard.rb', line 157

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

#shellObject



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

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

#shell!Object



104
105
106
107
108
# File 'lib/livetext/standard.rb', line 104

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

#ttyoutObject



115
116
117
118
# File 'lib/livetext/standard.rb', line 115

def ttyout
  TTY.puts api.data
  api.optional_blank_line
end

#variablesObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/livetext/standard.rb', line 181

def variables
  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)
  api.setvars(pairs)
  api.optional_blank_line
end

#variables!Object

FIXME really these should be one method…



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/livetext/standard.rb', line 166

def variables!  # 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)
  api.setvars(pairs)
  api.optional_blank_line
end

#xtableObject

Borrowed from bookish - FIXME



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/livetext/standard.rb', line 351

def xtable   # Borrowed from bookish - FIXME
  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)
    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