module Livetext::Standard
SimpleFormats = # Move this?
{ b: %w[ ],
i: %w[ ],
t: %w[ ],
s: %w[ ] }
attr_reader :_data
def data=(val)
@_data = val
@_args = val.split
@_mixins = []
end
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
def backtrace
arg = @_args.first
@backtrace = true
@backtrace = false if arg == "off"
end
def comment
_body
end
def shell
cmd = @_data
_errout("Running: #{cmd}")
system(cmd)
end
def func
funcname = @_args[0]
_error! "Illegal name '#{funcname}'" if _disallowed?(funcname)
func_def = <<-EOS
def #{funcname}(*args)
#{_body_text(true)}
end
EOS
Livetext::Functions.class_eval func_def
end
def shell!
cmd = @_data
system(cmd)
end
def errout
TTY.puts @_data
end
def say
str = _formatting(@_data)
TTY.puts str
_optional_blank_line
end
def banner
str = _formatting(@_data)
n = str.length - 1
_errout "-"*n
_errout str
_errout "-"*n
end
def quit
@output.close
exit
end
def outdir
@_outdir = @_args.first
_optional_blank_line
end
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
def _output(name)
@_outdir ||= "." # FIXME
@output.close unless @output == STDOUT
@output = File.open(@_outdir + "/" + name, "w")
@output.puts "\n\n"
end
def _append(name)
@_outdir ||= "." # FIXME
@output.close unless @output == STDOUT
@output = File.open(@_outdir + "/" + name, "a")
@output.puts "\n\n"
end
def output
name = @_args.first
_debug "Redirecting output to: #{name}"
_output(name)
end
def append
file = @_args[0]
_append(file)
end
def next_output
tag, num = @_args
_next_output(tag, num)
_optional_blank_line
end
def cleanup
@_args.each do |item|
if ::File.directory?(item)
system("rm -f #{item}/*")
else
::FileUtils.rm(item)
end
end
end
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
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
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
def heredoc
var = @_args[0]
str = _body_text
Livetext::Vars[var] = str
_optional_blank_line
end
def _include
file = @_args.first
_check_existence(file, "No such include file '#{file}'")
@parent.process_file(file)
_optional_blank_line
end
def include! # FIXME huh?
file = @_args.first
return unless File.exist?(file)
lines = @parent.process_file(file)
File.delete(file)
_optional_blank_line
end
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
def copy
file = @_args.first
_check_existence(file, "No such file '#{file}' to copy")
@output.puts grab_file(file)
_optional_blank_line
end
def r
# STDERR.puts "@_data = #{@_data.inspect}"
_puts @_data # No processing at all
end
def raw
# No processing at all (terminate with __EOF__)
_puts _raw_body
end
def debug
arg = @_args.first
self._debug = true
self._debug = false if arg == "off"
end
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
def nopass
@_nopass = true
end
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
def nopara
@_nopara = true
end
def heading
_print "
"
_print @_data
_print ""
end
def newpage
_puts ''
_puts ""
end
def invoke(str)
end
def mono
_puts ""
_body(true) {|line| puts line }
_puts "
"
_optional_blank_line
end
def dlist
delim = _args.first
_puts ""
_body do |line|
line = _formatting(line)
term, defn = line.split(delim)
_puts ""
_puts " | #{term} | #{defn} | "
_puts "
"
end
_puts "
"
end
def link
url = _args.first
text = _args[2..-1].join(" ")
_puts "#{text}"
end
def xtable # Borrowed from bookish - FIXME
# @table_num ||= 0
# @table_num += 1
title = @_data
delim = " :: "
_puts "
"
lines = _body(true)
maxw = nil
lines.each do |line|
_formatting(line) # May split into multiple lines!
line.gsub!(/\n+/, "
")
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 ""
cells.each.with_index do |cell, i|
_puts " #{cell} | "
end
_puts "
"
end
_puts "
"
# @toc << "#{_nbsp(8)}Table #@chapter.#@table_num #{title}
"
# _next_output(_slug("table_#{title}"))
# _puts "Table #@chapter.#@table_num #{title}
"
end
end