Module: Livetext::Standard

Included in:
Processor
Defined in:
lib/standard.rb

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>] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_dataObject (readonly)

Returns the value of attribute _data.



9
10
11
# File 'lib/standard.rb', line 9

def _data
  @_data
end

Instance Method Details

#_append(name) ⇒ Object



100
101
102
103
104
105
# File 'lib/standard.rb', line 100

def _append(name)
  @_outdir ||= "."  # FIXME
  @output.close unless @output == STDOUT
  @output = File.open(@_outdir + "/" + name, "a")
  @output.puts "<meta charset='UTF-8'>\n\n"
end

#_defObject



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

def _def
  name = @_args[0]
  str = "def #{name}\n"
  raise "Illegal name '#{name}'" if _disallowed?(name)
  str += _body_text(true)
  str += "end\n"
  eval str
rescue => err
  _error!(err)
end

#_includeObject



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

def _include
  file = @_args.first
  _check_existence(file, "No such include file '#{file}'")
  @parent.process_file(file)
  _optional_blank_line
end

#_next_output(tag = "sec", num = nil) ⇒ Object



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

def _next_output(tag = "sec", num = nil)
  @_file_num = num ? num : @_file_num + 1
  @_file_num = @_file_num.to_i
  name = "#{'%03d' % @_file_num}-#{tag}.html"
  _output(name)
end

#_output(name) ⇒ Object



93
94
95
96
97
98
# File 'lib/standard.rb', line 93

def _output(name)
  @_outdir ||= "."  # FIXME
  @output.close unless @output == STDOUT
  @output = File.open(@_outdir + "/" + name, "w")
  @output.puts "<meta charset='UTF-8'>\n\n"
end

#appendObject



113
114
115
116
# File 'lib/standard.rb', line 113

def append
  file = @_args[0]
  _append(file)
end

#backtraceObject



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

def backtrace
  arg = @_args.first
  @backtrace = true
  @backtrace = false if arg == "off"
end


67
68
69
70
71
72
73
# File 'lib/standard.rb', line 67

def banner
  str = _formatting(@_data)
  n = str.length - 1
  _errout "-"*n
  _errout str
  _errout "-"*n
end

#bitsObject

dumb name - bold, italic, teletype, striketrough



17
18
19
20
21
22
23
# File 'lib/standard.rb', line 17

def bits  # dumb name - bold, italic, teletype, striketrough
  b0, b1, i0, i1, t0, t1, s0, s1 = *@_args
  SimpleFormats[:b] = [b0, b1]
  SimpleFormats[:i] = [i0, i1]
  SimpleFormats[:t] = [t0, t1]
  SimpleFormats[:s] = [s0, s1]
end

#brObject



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

def br
  n = _args.first || "1"
  out = ""
  n.to_i.times { out << "<br>" }
  _puts out
end

#cleanupObject



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

def cleanup
  @_args.each do |item| 
    if ::File.directory?(item)
      system("rm -f #{item}/*")
    else
      ::FileUtils.rm(item)
    end
  end
end

#commentObject



31
32
33
# File 'lib/standard.rb', line 31

def comment
  _body
end

#copyObject



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

def copy
  file = @_args.first
  _check_existence(file, "No such file '#{file}' to copy")
  @output.puts grab_file(file)
  _optional_blank_line
end

#data=(val) ⇒ Object



11
12
13
14
15
# File 'lib/standard.rb', line 11

def data=(val)
  @_data = val
  @_args = val.split rescue []
  @_mixins = []
end

#debugObject



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

def debug
  arg = @_args.first
  self._debug = true
  self._debug = false if arg == "off"
end

#dlistObject



280
281
282
283
284
285
286
287
288
289
290
# File 'lib/standard.rb', line 280

def dlist
  delim = _args.first
  _puts "<dl>"
  _body do |line|
    line = _formatting(line)
    term, defn = line.split(delim)
    _puts "<dt>#{term}</dt>"
    _puts "<dd>#{defn}</dd>"
  end
  _puts "</dl>"
end

#erroutObject



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

def errout
  TTY.puts @_data
end

#funcObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/standard.rb', line 41

def func
  funcname = @_args[0]
  _error! "Illegal name '#{funcname}'" if _disallowed?(funcname)
  func_def = "    def \#{funcname}(*args)\n      \#{_body_text(true)}\n    end\n  EOS\n  Livetext::Functions.class_eval func_def\nend\n"

#headingObject



259
260
261
262
263
# File 'lib/standard.rb', line 259

def heading
  _print "<center><font size=+1><b>"
  _print @_data
  _print "</b></font></center>"
end

#heredocObject



164
165
166
167
168
169
# File 'lib/standard.rb', line 164

def heredoc
  var = @_args[0]
  str = _body_text
  Livetext::Vars[var] = str
  _optional_blank_line
end

#include!Object

FIXME huh?



178
179
180
181
182
183
184
185
# File 'lib/standard.rb', line 178

def include!    # FIXME huh?
  file = @_args.first
  return unless File.exist?(file)

  lines = @parent.process_file(file)
  File.delete(file)
  _optional_blank_line
end

#invoke(str) ⇒ Object



270
271
# File 'lib/standard.rb', line 270

def invoke(str)
end


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

def link
  url = _args.first
  text = _args[2..-1].join(" ")
  _puts "<a href='#{url}'>#{text}</a>"
end

#mixinObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/standard.rb', line 187

def mixin
  name = @_args.first   # Expect a module name
  file = "#{Plugins}/" + name.downcase + ".rb"
  return if @_mixins.include?(name)
  file = "./#{name}.rb" unless File.exist?(file)
  _check_existence(file, "No such mixin '#{name}'")

  @_mixins << name
  meths = grab_file(file)
  modname = name.gsub("/","_").capitalize
  string = "module ::#{modname}\n#{meths}\nend"
  eval(string)
  newmod = Object.const_get("::" + modname)
  self.extend(newmod)
  init = "init_#{name}"
  self.send(init) if self.respond_to? init
  _optional_blank_line
end

#monoObject



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

def mono
  _puts "<pre>"
  _body(true) {|line| puts line }
  _puts "</pre>"
  _optional_blank_line
end

#newpageObject



265
266
267
268
# File 'lib/standard.rb', line 265

def newpage
  _puts '<p style="page-break-after:always;"></p>'
  _puts "<p/>"
end

#next_outputObject



118
119
120
121
122
# File 'lib/standard.rb', line 118

def next_output
  tag, num = @_args
  _next_output(tag, num)
  _optional_blank_line
end

#noparaObject



255
256
257
# File 'lib/standard.rb', line 255

def nopara
  @_nopara = true
end

#nopassObject



240
241
242
# File 'lib/standard.rb', line 240

def nopass
  @_nopass = true
end

#old_dlistObject



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

def old_dlist
  delim = _args.first
  _puts "<table>"
  _body do |line|
    line = _formatting(line)
    term, defn = line.split(delim)
    _puts "<tr>"
    _puts "<td width=3%><td width=10%>#{term}</td><td>#{defn}</td>"
    _puts "</tr>"
  end
  _puts "</table>"
end

#outdirObject



80
81
82
83
# File 'lib/standard.rb', line 80

def outdir
  @_outdir = @_args.first
  _optional_blank_line
end

#outdir!Object

FIXME ?



85
86
87
88
89
90
91
# File 'lib/standard.rb', line 85

def outdir!  # FIXME ?
  @_outdir = @_args.first
  raise "No output directory specified" if @_outdir.nil?
  raise "No output directory specified" if @_outdir.empty?
  system("rm -f #@_outdir/*.html")
  _optional_blank_line
end

#outputObject



107
108
109
110
111
# File 'lib/standard.rb', line 107

def output
  name = @_args.first
  _debug "Redirecting output to: #{name}"
  _output(name)
end

#paraObject



244
245
246
247
248
249
250
251
252
253
# File 'lib/standard.rb', line 244

def para
  # FIXME - add check for args size (helpers); _onoff helper??
  onoff = _args.first
  case onoff
    when nil;   @_nopara = false
    when "on";  @_nopara = false
    when "off"; @_nopara = true
    else _error!("Unknown arg '#{onoff}'")
  end
end

#passthruObject



229
230
231
232
233
234
235
236
237
238
# File 'lib/standard.rb', line 229

def passthru
  # FIXME - add check for args size (helpers); _onoff helper??
  onoff = _args.first
  case onoff
    when nil;   @_nopass = false
    when "on";  @_nopass = false
    when "off"; @_nopass = true
    else _error!("Unknown arg '#{onoff}'")
  end
end

#quitObject



75
76
77
78
# File 'lib/standard.rb', line 75

def quit
  @output.close
  exit
end

#rObject



213
214
215
216
# File 'lib/standard.rb', line 213

def r
# STDERR.puts "@_data = #{@_data.inspect}"
  _puts @_data  # No processing at all
end

#rawObject



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

def raw
  # No processing at all (terminate with __EOF__)
  _puts _raw_body  
end

#sayObject



61
62
63
64
65
# File 'lib/standard.rb', line 61

def say
  str = _formatting(@_data)
  TTY.puts str
  _optional_blank_line
end

#setObject



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

def set
  assigns = @_data.chomp.split(/, */)
  # Do a better way?
  assigns.each do |a| 
    var, val = a.split("=")
    val = val[1..-2] if val[0] == ?" and val[-1] == ?"
    val = val[1..-2] if val[0] == ?' and val[-1] == ?'
    Livetext::Vars[var] = val
  end
  _optional_blank_line
end

#shellObject



35
36
37
38
39
# File 'lib/standard.rb', line 35

def shell
  cmd = @_data
  _errout("Running: #{cmd}")
  system(cmd)
end

#shell!Object



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

def shell!
  cmd = @_data
  system(cmd)
end

#xtableObject

Borrowed from bookish - FIXME



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/standard.rb', line 311

def xtable   # Borrowed from bookish - FIXME
#   @table_num ||= 0
#   @table_num += 1
  title = @_data
  delim = " :: "
  _puts "<br><center><table border=1 width=90% cellpadding=5>"
  lines = _body(true)
  maxw = nil
  lines.each do |line|
    _formatting(line)  # May split into multiple lines!
    line.gsub!(/\n+/, "<br>")
    cells = line.split(delim)
    wide = cells.map {|x| x.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 }

  lines.each do |line|
    cells = line.split(delim)
    _puts "<tr>"
    cells.each.with_index do |cell, i| 
      _puts "  <td valign=top>#{cell}</td>"
    end
    _puts "</tr>"
  end
  _puts "</table></center>"
#   @toc << "#{_nbsp(8)}<b>Table #@chapter.#@table_num</b> #{title}<br>"
# _next_output(_slug("table_#{title}"))
#   _puts "<b>Table #@chapter.#@table_num &nbsp;&nbsp; #{title}</b></center><br>"
end