/usr/lib/ruby/1.8/rt/rtparser.rb is in librt-ruby1.8 1.0.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | #!/usr/bin/ruby
=begin
rtparser.rb
$Id: rtparser.rb 1370 2006-09-20 00:19:07Z rubikitch $
=end
module RT
class RTCell
def initialize(value, align = nil)
@rowspan = @colspan = 1
@value = value
@align = case align # {:left, :center :right}
when :left, :center, :right
align
when nil
if /^[+\-]?[0-9]+\.?[0-9]*/ === value
:right
else
:left
end
else
raise "[BUG]Illegal align type"
end
end
def == (x)
case x
when self.class
self.value == "" && x.value == "" || self.value == x.value && self.align == x.align
else
false
end
end
def inspect
if value==""
"()"
else
a = case align
when :left
"l"
when :center
"c"
when :right
"r"
else
raise "[BUG]Illegal align type"
end
"#{a}(#{value})"
end
end
attr_reader :value, :align
attr_accessor :rowspan, :colspan
end
class RTParser
DefaultConfig = {
'delimiter' => "[,\t]",
'rowspan' => "||",
'colspan' => "==",
'escape' => nil,
'caption' => nil,
}
def initialize(str="")
@str = str.dup
normalize_linefeed! @str
@config_line = []
@header_line = []
@body_line = []
@config = DefaultConfig.dup
@header = []
@body = []
end
attr_reader :str, :config, :header, :body
def normalize_linefeed!(str)
str.gsub!("\r\n", "\n")
str.gsub!("\r", "\n")
end
def self::parse(str)
obj = self::new str
obj.make_blocks
obj.parse_config
obj.parse_header
obj.parse_body
obj.calc_span(obj.header)
obj.calc_span(obj.body)
obj
end
def blocks
[@config_line, @header_line, @body_line]
end
def make_blocks(str = @str)
part = str.split(/\n\n/).collect{|x| x.split(/\n/)}
case part.length
when 0
when 1
@body_line, = part
when 2
@config_line, @body_line = part
when 3
@config_line, @header_line, @body_line = part
else
raise "RT: blocks are too many."
end
self
end
def parse_config(lines = @config_line)
lines.each do |line|
case line
when /^#/ # comment
when /^\s*(\S+)\s*=\s*(.+)$/
@config[$1] = $2
else
raise "RT: syntax error in config block"
end
end
self
end
def split2(str,re)
ret = str.split(re, -1)
end
private :split2
ESCAPE_TMP = "\001\002"
def _escape!(str)
esc = config['escape']
str.gsub!(/#{Regexp.quote(esc)}#{config['delimiter']}/, ESCAPE_TMP) if esc
end
private :_escape!
def _unescape!(str)
str.gsub!(/#{ESCAPE_TMP}/, config['delimiter'])
end
private :_unescape!
def parse_table_data(lines) # iterator
ret = []
lines.each do |line|
case line
when /^#/ # comment
else
_escape! line
ret << split2(line, /\s*#{config['delimiter']}\s*/).collect {|x|
_unescape! x
yield(x.strip)
}
end
end
unless ret.find_all{|x| x.length == ret[0].length} == ret
raise "RT: different column size"
end
ret
end
private :parse_table_data
def _make_cell(x, align)
case x
when config['rowspan'], config['colspan']
x
else
RTCell::new(x, align)
end
end
private :_make_cell
def parse_header(lines = @header_line)
@header = parse_table_data(lines) {|x|
_make_cell x, :center
}
self
end
def parse_body(lines = @body_line)
@body = parse_table_data(lines) {|x|
_make_cell x, nil
}
self
end
def calc_span(tbl)
return if tbl.empty?
cols = tbl[0].length
tbl.each do |row|
row.each_with_index do |elm, j|
case elm
when String
when RTCell
nspan = 1
1.upto(cols-j-1) do |k|
break unless row[j+k] == config['colspan']
nspan += 1
end
row[j].colspan = nspan
else
raise "[BUG] invalid cell"
end
end
end
rows = tbl.length
0.upto(cols-1) do |j|
0.upto(rows-1) do |i|
case tbl[i][j]
when String
when RTCell
nspan = 1
1.upto(rows-i-1) do |k|
break unless tbl[i+k][j] == config['rowspan']
nspan += 1
end
tbl[i][j].rowspan = nspan
else
raise "[BUG] invalid cell"
end
end
end
end
end # class RTCell, RTParser
end # module RT
|