module Bookish
def hardbreaks(args = nil, body = nil)
@hard = false
@hard = true unless api.args.first == "off"
end
def hardbreaks?
@hard
end
def credit(args = nil, body = nil)
# really just a place marker in source
end
# These are duplicated. Remove safely
def h1; api.out "
#{api.data}
"; end
def h2; api.out "#{api.data}
"; end
def h3; api.out "#{api.data}
"; end
def alpha_columns(args = nil, body = nil)
n = api.args.first.to_i # FIXME: what if missing?
words = []
api.body do |line|
words << line.chomp
end
words.sort!
api.out ""
words.each_slice(n) do |w|
items = w.map {|x| "#{x}" }
api.out " | " + items.join(" | ") + " |
"
end
api.out "
"
end
def _errout(*args)
::STDERR.puts *args
end
def _nbsp(n)
" "*n
end
def _slug(str)
str2 = str.chomp.strip
str2 = str2.gsub(/[?:,()'"\/]/,"")
str2 = str2.gsub(/ /, "-")
str2.downcase!
str2
end
# FIXME duplicated?
def image(args = nil, body = nil)
name = api.args[0]
api.out ""
end
def figure(args = nil, body = nil)
name = api.args[0]
num = api.args[1]
title = api.args[2..-1].join(" ")
title = api.format(title)
api.out ""
api.out "Figure #{num} #{title}"
end
def chapter(args = nil, body = nil)
# _errout("chapter")
@chapter = api.args.first.to_i
@sec = @sec2 = 0
title = api.data.split(" ",2)[1]
@toc << "
#@chapter #{title}
"
api.data = _slug(title)
next_output
api.out "#{@chapter}. #{title}"
api.out <<-HTML
Chapter #{@chapter}
#{title}
HTML
end
def chapterN(args = nil, body = nil)
@chapter += 1
@sec = @sec2 = 0
title = api.data # .split(" ",2)[1]
_errout("Chapter #@chapter: #{title}")
@toc << "
#@chapter #{title}
"
api.data = _slug(title)
next_output
api.out "#{@chapter}. #{title}"
api.out <<-HTML
Chapter #{@chapter}
#{title}
HTML
end
def sec(args = nil, body = nil)
@sec += 1
@sec2 = 0
@section = "#@chapter.#@sec"
title = api.data.dup
@toc << "#{_nbsp(3)}#@section #{title}
"
api.data = _slug(api.data)
next_output
api.out "#@section #{title}
\n"
rescue => err
::STDERR.puts "#{err}\n#{err.backtrace}"
exit
end
def subsec(args = nil, body = nil)
@sec2 += 1
@subsec = "#@chapter.#@sec.#@sec2"
title = api.data.dup
@toc << "#{_nbsp(6)}#@subsec #{title}
"
api.data = _slug(api.data)
next_output
api.out "#@subsec #{title}
\n"
end
def definition_table(args = nil, body = nil)
title = api.data
wide = "95"
delim = " :: "
api.out "
"
lines = api.body(true)
lines.map! {|line| api.format(line) }
lines.each do |line|
cells = line.split(delim)
api.out ""
cells.each.with_index do |cell, i|
width = (i == 0) ? "width=15%" : ""
api.out " #{cell} | "
end
api.out "
"
end
api.out "
"
api.optional_blank_line
end
def table2(args = nil, body = nil)
title = api.data
wide = "90"
extra = api.args[2]
delim = " :: "
api.out "
"
lines = api.body(true)
lines.map! {|line| api.format(line) }
lines.each do |line|
cells = line.split(delim)
percent = (100/cells.size.to_f).round
api.out ""
cells.each do |cell|
api.out " #{cell} | "
end
api.out "
"
end
api.out "
"
api.optional_blank_line
end
def simple_table(args = nil, body = nil)
title = api.data
delim = " :: "
api.out ""
lines = api.body(true)
maxw = nil
lines.each do |line|
api.format(line)
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)
api.out ""
cells.each.with_index do |cell, i|
api.out " " +
"#{cell} | "
end
api.out "
"
end
api.out "
"
api.optional_blank_line
end
def table(args = nil, body = nil)
@table_num ||= 0
@table_num += 1
title = api.data
delim = " :: "
api.out "
"
lines = api.body(true)
maxw = nil
lines.each do |line|
api.format(line)
cells = line.split(delim)
wide = cells.map {|x| x.length }
maxw = [0] * cells.size
maxw = maxw.map.with_index {|x, i| [x, wide[i]+2].max }
end
sum = maxw.inject(0, :+)
maxw.map! {|x| (x/sum*100).floor }
lines.each do |line|
cells = line.split(delim)
api.out ""
cells.each.with_index do |cell, i|
api.out " " +
"#{cell} | "
end
api.out "
"
end
api.out "
"
@toc << "#{_nbsp(8)}Table #@chapter.#@table_num #{title}
"
# _next_output(_slug("table_#{title}"))
api.out "Table #@chapter.#@table_num #{title}
"
api.optional_blank_line
end
def toc!(args = nil, body = nil)
_debug "Closing TOC"
@toc.close
rescue => err
puts @parent.body
@parent.body = ""
_errout "Exception: #{err.inspect}"
end
def toc2(args = nil, body = nil)
file = api.args[0]
@toc.close
::File.write(file, <<-EOS)
Fake (non-hyperlinked) Table of Contents
EOS
system("cat toc.tmp >>#{file}")
end
def missing(args = nil, body = nil)
@toc << "#{_nbsp(8)}TBD: #{api.data}
"
stuff = api.data.empty? ? "" : ": #{api.data}"
api.out "
[Material missing#{stuff}]
\n "
end
def TBC(args = nil, body = nil)
@toc << "#{_nbsp(8)}To be continued...
"
api.out "
To be continued...
"
end
def note(args = nil, body = nil)
api.out "
Note: "
api.out api.data
api.out "
\n "
end
def quote(args = nil, body = nil)
api.out ""
api.body {|line| api.out line }
api.out "
"
rescue => err
::STDERR.puts "#{err}\n#{err.backtrace}"
exit
end
def init_bookish
@toc_file = "toc.tmp"
@toc = ::File.new(@toc_file, "w")
@chapter = -1
end
end