rttool 1.0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +62 -0
- data/GPL +340 -0
- data/bin/rt/rdrt2 +2 -0
- data/bin/rt/rt2 +162 -0
- data/examples/easiest.html +19 -0
- data/examples/easiest.rt +3 -0
- data/examples/escape.html +17 -0
- data/examples/escape.rt +4 -0
- data/examples/rttest.html +40 -0
- data/examples/rttest.rd +20 -0
- data/examples/test1.html +23 -0
- data/examples/test1.rt +7 -0
- data/examples/test2.html +23 -0
- data/examples/test2.rt +7 -0
- data/lib/rd/rt-filter.rb +11 -0
- data/lib/rt/rt2html-lib.rb +91 -0
- data/lib/rt/rt2txt-lib.rb +15 -0
- data/lib/rt/rtparser.rb +226 -0
- data/lib/rt/rtvisitor.rb +68 -0
- data/lib/rt/w3m.rb +47 -0
- data/rttool.en.html +299 -0
- data/rttool.en.rd +265 -0
- data/rttool.ja.html +383 -0
- data/rttool.ja.rd +316 -0
- data/test/rttool-sub.rb +1023 -0
- data/test/test-rt2html-lib.rb +65 -0
- data/test/test-rtparser.rb +209 -0
- data/test/test.rb +88 -0
- metadata +75 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
<!-- setup -->
|
2
|
+
<table border="1">
|
3
|
+
<!-- setup end -->
|
4
|
+
|
5
|
+
<!-- Header -->
|
6
|
+
<!-- Header end -->
|
7
|
+
|
8
|
+
<!-- Body -->
|
9
|
+
<tbody>
|
10
|
+
<tr><td align="right">1</td><td align="right">2</td><td align="right">3</td></tr>
|
11
|
+
<tr><td align="right">4</td><td align="right">5</td><td align="right">6</td></tr>
|
12
|
+
<tr><td align="right">7</td><td align="right">8</td><td align="right">9</td></tr>
|
13
|
+
</tbody>
|
14
|
+
<!-- Body end -->
|
15
|
+
|
16
|
+
<!-- teardown -->
|
17
|
+
</table>
|
18
|
+
<!-- teardown end -->
|
19
|
+
|
data/examples/easiest.rt
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
<!-- setup -->
|
2
|
+
<table border="1">
|
3
|
+
<!-- setup end -->
|
4
|
+
|
5
|
+
<!-- Header -->
|
6
|
+
<!-- Header end -->
|
7
|
+
|
8
|
+
<!-- Body -->
|
9
|
+
<tbody>
|
10
|
+
<tr><td align="left">\z</td><td align="left">;1</td><td align="right">2</td></tr>
|
11
|
+
</tbody>
|
12
|
+
<!-- Body end -->
|
13
|
+
|
14
|
+
<!-- teardown -->
|
15
|
+
</table>
|
16
|
+
<!-- teardown end -->
|
17
|
+
|
data/examples/escape.rt
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
<?xml version="1.0" ?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
<html xmlns="http://www.w3.org/1999/xhtml">
|
6
|
+
<head>
|
7
|
+
<title>rttest.rd</title>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
<h1><a name="label-0" id="label-0">Sample RD/RT</a></h1><!-- RDLabel: "Sample RD/RT" -->
|
11
|
+
<p>This RD contains a table.
|
12
|
+
It is so-called RD/RT.</p>
|
13
|
+
<!-- setup -->
|
14
|
+
<table border="1">
|
15
|
+
<caption>Test Table</caption>
|
16
|
+
<!-- setup end -->
|
17
|
+
|
18
|
+
<!-- Header -->
|
19
|
+
<thead>
|
20
|
+
<tr><th rowspan="2"></th><th colspan="2">Human</th><th colspan="2">Dog</th></tr>
|
21
|
+
<tr><th>M</th><th>F</th><th>M</th><th>F</th></tr>
|
22
|
+
</thead>
|
23
|
+
<!-- Header end -->
|
24
|
+
|
25
|
+
<!-- Body -->
|
26
|
+
<tbody>
|
27
|
+
<tr><td align="left">x</td><td align="right">1.0</td><td align="right">2.0</td><td align="right">1.1</td><td align="right">1.2</td></tr>
|
28
|
+
<tr><td align="left">y</td><td align="right">0.4</td><td align="right">0.5</td><td align="right">0.3</td><td align="right">0.1</td></tr>
|
29
|
+
</tbody>
|
30
|
+
<!-- Body end -->
|
31
|
+
|
32
|
+
<!-- teardown -->
|
33
|
+
</table>
|
34
|
+
<!-- teardown end -->
|
35
|
+
|
36
|
+
|
37
|
+
<p>It is simple.</p>
|
38
|
+
|
39
|
+
</body>
|
40
|
+
</html>
|
data/examples/rttest.rd
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
=begin
|
2
|
+
= Sample RD/RT
|
3
|
+
|
4
|
+
This RD contains a table.
|
5
|
+
It is so-called RD/RT.
|
6
|
+
|
7
|
+
=end
|
8
|
+
=begin RT
|
9
|
+
caption = Test Table
|
10
|
+
|
11
|
+
, Human, == , Dog , ==
|
12
|
+
|| , M , F ,M,F
|
13
|
+
|
14
|
+
x , 1.0 , 2.0, 1.1, 1.2
|
15
|
+
y , 0.4 , 0.5, 0.3, 0.1
|
16
|
+
|
17
|
+
=end
|
18
|
+
=begin
|
19
|
+
It is simple.
|
20
|
+
=end
|
data/examples/test1.html
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
<!-- setup -->
|
2
|
+
<table border="1">
|
3
|
+
<caption>Test Table</caption>
|
4
|
+
<!-- setup end -->
|
5
|
+
|
6
|
+
<!-- Header -->
|
7
|
+
<thead>
|
8
|
+
<tr><th rowspan="2"></th><th colspan="2">Human</th><th colspan="2">Dog</th></tr>
|
9
|
+
<tr><th>M</th><th>F</th><th>M</th><th>F</th></tr>
|
10
|
+
</thead>
|
11
|
+
<!-- Header end -->
|
12
|
+
|
13
|
+
<!-- Body -->
|
14
|
+
<tbody>
|
15
|
+
<tr><td align="left">x</td><td align="right">1.0</td><td align="right">2.0</td><td align="right">1.1</td><td align="right">1.2</td></tr>
|
16
|
+
<tr><td align="left">y</td><td align="right">0.4</td><td align="right">0.5</td><td align="right">0.3</td><td align="right">0.1</td></tr>
|
17
|
+
</tbody>
|
18
|
+
<!-- Body end -->
|
19
|
+
|
20
|
+
<!-- teardown -->
|
21
|
+
</table>
|
22
|
+
<!-- teardown end -->
|
23
|
+
|
data/examples/test1.rt
ADDED
data/examples/test2.html
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
<!-- setup -->
|
2
|
+
<table border="1">
|
3
|
+
<caption>Test Table</caption>
|
4
|
+
<!-- setup end -->
|
5
|
+
|
6
|
+
<!-- Header -->
|
7
|
+
<thead>
|
8
|
+
<tr><th rowspan="2"></th><th colspan="2">Human</th><th colspan="2">Dog</th></tr>
|
9
|
+
<tr><th>M</th><th>F</th><th>M</th><th>F</th></tr>
|
10
|
+
</thead>
|
11
|
+
<!-- Header end -->
|
12
|
+
|
13
|
+
<!-- Body -->
|
14
|
+
<tbody>
|
15
|
+
<tr><td align="left">x</td><td align="right">1.0</td><td align="right">2.0</td><td align="right">1.1</td><td align="right">1.2</td></tr>
|
16
|
+
<tr><td align="left">y</td><td align="right">0.4</td><td align="right">0.5</td><td align="right">0.3</td><td align="right">0.1</td></tr>
|
17
|
+
</tbody>
|
18
|
+
<!-- Body end -->
|
19
|
+
|
20
|
+
<!-- teardown -->
|
21
|
+
</table>
|
22
|
+
<!-- teardown end -->
|
23
|
+
|
data/examples/test2.rt
ADDED
data/lib/rd/rt-filter.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
$RC["filter"]["rt"] = Filter.new(:target) do |inn, out|
|
3
|
+
ext = $Visitor_Class.const_get('OUTPUT_SUFFIX')
|
4
|
+
if ext == 'txt'
|
5
|
+
ext = 'html'
|
6
|
+
end
|
7
|
+
tmpf = Tempfile.new("rt")
|
8
|
+
tmpf.write inn.read
|
9
|
+
tmpf.close
|
10
|
+
out.print `rt2 -r rt/rt2#{ext}-lib #{tmpf.path}`
|
11
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
=begin
|
3
|
+
rt2html-lib.rb
|
4
|
+
$Id: rt2html-lib.rb 603 2005-10-22 08:05:44Z rubikitch $
|
5
|
+
=end
|
6
|
+
require 'rt/rtvisitor'
|
7
|
+
|
8
|
+
module RT
|
9
|
+
class RT2HTMLVisitor < RTVisitor
|
10
|
+
OUTPUT_SUFFIX = "html"
|
11
|
+
INCLUDE_SUFFIX = ["html"]
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def block(name)
|
18
|
+
%Q[<!-- #{name} -->\n] +
|
19
|
+
yield +
|
20
|
+
%Q[<!-- #{name} end -->\n\n]
|
21
|
+
end
|
22
|
+
private :block
|
23
|
+
|
24
|
+
def setup
|
25
|
+
block('setup') do
|
26
|
+
s = %Q[<table border="1">\n]
|
27
|
+
s << %Q[<caption>#{caption}</caption>\n] if caption
|
28
|
+
s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def teardown
|
33
|
+
block('teardown') do
|
34
|
+
%Q[</table>\n]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def cell_element(cell, name)
|
39
|
+
rs, cs = cell.rowspan, cell.colspan
|
40
|
+
if rs == 1 and cs == 1
|
41
|
+
ret = "<#{name}>"
|
42
|
+
elsif rs == 1
|
43
|
+
ret = %Q[<#{name} colspan="#{cs}">]
|
44
|
+
elsif cs == 1
|
45
|
+
ret = %Q[<#{name} rowspan="#{rs}">]
|
46
|
+
else
|
47
|
+
ret = %Q[<#{name} colspan="#{cs}" rowspan="#{rs}">]
|
48
|
+
end
|
49
|
+
ret
|
50
|
+
end
|
51
|
+
private :cell_element
|
52
|
+
|
53
|
+
def visit_Header(ary = @header)
|
54
|
+
block('Header') do
|
55
|
+
if ary.empty?
|
56
|
+
""
|
57
|
+
else
|
58
|
+
ret = "<thead>\n"
|
59
|
+
ary.each do |line|
|
60
|
+
ret << '<tr>'
|
61
|
+
each_cell(line) do |cell|
|
62
|
+
ret << cell_element(cell, 'th')
|
63
|
+
ret << cell.value
|
64
|
+
ret << '</th>'
|
65
|
+
end
|
66
|
+
ret << "</tr>\n"
|
67
|
+
end
|
68
|
+
ret << "</thead>\n"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def visit_Body(ary = @body)
|
74
|
+
block('Body') do
|
75
|
+
ret = "<tbody>\n"
|
76
|
+
ary.each do |line|
|
77
|
+
ret << '<tr>'
|
78
|
+
each_cell(line) do |cell|
|
79
|
+
ret << cell_element(cell, %Q[td align="#{cell.align.id2name}"])
|
80
|
+
ret << cell.value
|
81
|
+
ret << '</td>'
|
82
|
+
end
|
83
|
+
ret << "</tr>\n"
|
84
|
+
end
|
85
|
+
ret << "</tbody>\n"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end # RT2HTMLVisitor
|
90
|
+
end
|
91
|
+
$Visitor_Class = RT::RT2HTMLVisitor
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require 'rt/w3m'
|
3
|
+
require 'rt/rt2html-lib'
|
4
|
+
|
5
|
+
module RT
|
6
|
+
class RT2TXTVisitor < RT2HTMLVisitor
|
7
|
+
OUTPUT_SUFFIX = "txt"
|
8
|
+
INCLUDE_SUFFIX = ["txt"]
|
9
|
+
|
10
|
+
def visit(parsed)
|
11
|
+
W3M::html2txt(super)
|
12
|
+
end
|
13
|
+
end # RT2TXTVisitor
|
14
|
+
end
|
15
|
+
$Visitor_Class = RT::RT2TXTVisitor
|
data/lib/rt/rtparser.rb
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
=begin
|
3
|
+
rtparser.rb
|
4
|
+
$Id: rtparser.rb 1370 2006-09-20 00:19:07Z rubikitch $
|
5
|
+
=end
|
6
|
+
module RT
|
7
|
+
class RTCell
|
8
|
+
def initialize(value, align = nil)
|
9
|
+
@rowspan = @colspan = 1
|
10
|
+
@value = value
|
11
|
+
@align = case align # {:left, :center :right}
|
12
|
+
when :left, :center, :right
|
13
|
+
align
|
14
|
+
when nil
|
15
|
+
if /^[+\-]?[0-9]+\.?[0-9]*/ === value
|
16
|
+
:right
|
17
|
+
else
|
18
|
+
:left
|
19
|
+
end
|
20
|
+
else
|
21
|
+
raise "[BUG]Illegal align type"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
def == (x)
|
25
|
+
case x
|
26
|
+
when self.class
|
27
|
+
self.value == "" && x.value == "" || self.value == x.value && self.align == x.align
|
28
|
+
else
|
29
|
+
false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
def inspect
|
33
|
+
if value==""
|
34
|
+
"()"
|
35
|
+
else
|
36
|
+
a = case align
|
37
|
+
when :left
|
38
|
+
"l"
|
39
|
+
when :center
|
40
|
+
"c"
|
41
|
+
when :right
|
42
|
+
"r"
|
43
|
+
else
|
44
|
+
raise "[BUG]Illegal align type"
|
45
|
+
end
|
46
|
+
"#{a}(#{value})"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
attr_reader :value, :align
|
51
|
+
attr_accessor :rowspan, :colspan
|
52
|
+
end
|
53
|
+
|
54
|
+
class RTParser
|
55
|
+
DefaultConfig = {
|
56
|
+
'delimiter' => "[,\t]",
|
57
|
+
'rowspan' => "||",
|
58
|
+
'colspan' => "==",
|
59
|
+
'escape' => nil,
|
60
|
+
'caption' => nil,
|
61
|
+
}
|
62
|
+
|
63
|
+
def initialize(str="")
|
64
|
+
@str = str.dup
|
65
|
+
normalize_linefeed! @str
|
66
|
+
@config_line = []
|
67
|
+
@header_line = []
|
68
|
+
@body_line = []
|
69
|
+
@config = DefaultConfig.dup
|
70
|
+
@header = []
|
71
|
+
@body = []
|
72
|
+
end
|
73
|
+
attr_reader :str, :config, :header, :body
|
74
|
+
|
75
|
+
def normalize_linefeed!(str)
|
76
|
+
str.gsub!("\r\n", "\n")
|
77
|
+
str.gsub!("\r", "\n")
|
78
|
+
end
|
79
|
+
|
80
|
+
def self::parse(str)
|
81
|
+
obj = self::new str
|
82
|
+
obj.make_blocks
|
83
|
+
obj.parse_config
|
84
|
+
obj.parse_header
|
85
|
+
obj.parse_body
|
86
|
+
obj.calc_span(obj.header)
|
87
|
+
obj.calc_span(obj.body)
|
88
|
+
obj
|
89
|
+
end
|
90
|
+
|
91
|
+
def blocks
|
92
|
+
[@config_line, @header_line, @body_line]
|
93
|
+
end
|
94
|
+
|
95
|
+
def make_blocks(str = @str)
|
96
|
+
part = str.split(/\n\n/).collect{|x| x.split(/\n/)}
|
97
|
+
case part.length
|
98
|
+
when 0
|
99
|
+
when 1
|
100
|
+
@body_line, = part
|
101
|
+
when 2
|
102
|
+
@config_line, @body_line = part
|
103
|
+
when 3
|
104
|
+
@config_line, @header_line, @body_line = part
|
105
|
+
else
|
106
|
+
raise "RT: blocks are too many."
|
107
|
+
end
|
108
|
+
self
|
109
|
+
end
|
110
|
+
|
111
|
+
def parse_config(lines = @config_line)
|
112
|
+
lines.each do |line|
|
113
|
+
case line
|
114
|
+
when /^#/ # comment
|
115
|
+
when /^\s*(\S+)\s*=\s*(.+)$/
|
116
|
+
@config[$1] = $2
|
117
|
+
else
|
118
|
+
raise "RT: syntax error in config block"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
self
|
122
|
+
end
|
123
|
+
|
124
|
+
def split2(str,re)
|
125
|
+
ret = str.split(re, -1)
|
126
|
+
end
|
127
|
+
private :split2
|
128
|
+
|
129
|
+
ESCAPE_TMP = "\001\002"
|
130
|
+
def _escape!(str)
|
131
|
+
esc = config['escape']
|
132
|
+
str.gsub!(/#{Regexp.quote(esc)}#{config['delimiter']}/, ESCAPE_TMP) if esc
|
133
|
+
end
|
134
|
+
private :_escape!
|
135
|
+
|
136
|
+
def _unescape!(str)
|
137
|
+
str.gsub!(/#{ESCAPE_TMP}/, config['delimiter'])
|
138
|
+
end
|
139
|
+
private :_unescape!
|
140
|
+
|
141
|
+
def parse_table_data(lines) # iterator
|
142
|
+
ret = []
|
143
|
+
lines.each do |line|
|
144
|
+
case line
|
145
|
+
when /^#/ # comment
|
146
|
+
else
|
147
|
+
_escape! line
|
148
|
+
ret << split2(line, /\s*#{config['delimiter']}\s*/).collect {|x|
|
149
|
+
_unescape! x
|
150
|
+
yield(x.strip)
|
151
|
+
}
|
152
|
+
end
|
153
|
+
end
|
154
|
+
unless ret.find_all{|x| x.length == ret[0].length} == ret
|
155
|
+
raise "RT: different column size"
|
156
|
+
end
|
157
|
+
ret
|
158
|
+
end
|
159
|
+
private :parse_table_data
|
160
|
+
|
161
|
+
def _make_cell(x, align)
|
162
|
+
case x
|
163
|
+
when config['rowspan'], config['colspan']
|
164
|
+
x
|
165
|
+
else
|
166
|
+
RTCell::new(x, align)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
private :_make_cell
|
170
|
+
|
171
|
+
def parse_header(lines = @header_line)
|
172
|
+
@header = parse_table_data(lines) {|x|
|
173
|
+
_make_cell x, :center
|
174
|
+
}
|
175
|
+
self
|
176
|
+
end
|
177
|
+
|
178
|
+
def parse_body(lines = @body_line)
|
179
|
+
@body = parse_table_data(lines) {|x|
|
180
|
+
_make_cell x, nil
|
181
|
+
}
|
182
|
+
self
|
183
|
+
end
|
184
|
+
|
185
|
+
def calc_span(tbl)
|
186
|
+
return if tbl.empty?
|
187
|
+
cols = tbl[0].length
|
188
|
+
tbl.each do |row|
|
189
|
+
row.each_with_index do |elm, j|
|
190
|
+
case elm
|
191
|
+
when String
|
192
|
+
when RTCell
|
193
|
+
nspan = 1
|
194
|
+
1.upto(cols-j-1) do |k|
|
195
|
+
break unless row[j+k] == config['colspan']
|
196
|
+
nspan += 1
|
197
|
+
end
|
198
|
+
row[j].colspan = nspan
|
199
|
+
else
|
200
|
+
raise "[BUG] invalid cell"
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
rows = tbl.length
|
206
|
+
0.upto(cols-1) do |j|
|
207
|
+
0.upto(rows-1) do |i|
|
208
|
+
case tbl[i][j]
|
209
|
+
when String
|
210
|
+
when RTCell
|
211
|
+
nspan = 1
|
212
|
+
1.upto(rows-i-1) do |k|
|
213
|
+
break unless tbl[i+k][j] == config['rowspan']
|
214
|
+
nspan += 1
|
215
|
+
end
|
216
|
+
tbl[i][j].rowspan = nspan
|
217
|
+
else
|
218
|
+
raise "[BUG] invalid cell"
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end # class RTCell, RTParser
|
224
|
+
end # module RT
|
225
|
+
|
226
|
+
|