#!/usr/bin/ruby
=begin
= Table2RT
* HTMLのtableからRTへの変換を行う。
* copyright (c) 2002 Kazuhiro NISHIYAMA.
== 仕様という名の制限事項
* 入れ子のtableには対応していない。
* 属性やマークアップはrowspanとcolspan以外はばっさり捨てる。
* などの終了タグは省略してはいけない。
* (({,}))が含まれる場合を考慮していない。
=end
module Table2RT
def table2rt(str)
result = ''
rowspanlist = []
str.scan(%r[
]im) do |table,|
table.scan(%r[]*>(.+?)
]im) do |line,|
lineary = []
line.scan(%r[]*)>(.*?)]im) do |attr, cell|
if /\browspan\s*=\s*['"]?(\d+)["']?/i === attr
rowspan = $1.to_i
else
rowspan = 1
end
if /\bcolspan\s*=\s*['"]?(\d+)["']?/i === attr
colspan = $1.to_i
else
colspan = 1
end
lineary << [
cell.gsub(%r[<(/?\w+)\b.*?>], '').gsub(/\s+/, ' '),
rowspan,
colspan,
]
end
i = 0
lineresult = []
until lineary.empty?
if 0 < rowspanlist[i].to_i
rowspanlist[i] -= 1
lineresult << '||'
else
cell, rowspan, colspan = lineary.shift
lineresult << cell
rowspanlist[i] = rowspan - 1
while 0 < (colspan -= 1)
lineresult << '=='
end
end
i += 1
end
result << lineresult.join(', ') << "\n"
end
end
result
end
module_function :table2rt
end
if __FILE__ == $0
puts Table2RT::table2rt(ARGF.read)
end