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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
=begin
= filter.rb
Definition of Filter structure.
=end
module RD
class Filter
attr_accessor :mode
def initialize(mode = :target, &block)
@mode = mode
@block = block
end
# inn, out: RD::Part
def call(inn)
out = RD::Part.new("", nil, "w")
result = @block.call(inn, out)
if out.empty?
result
else
out.to_s
end
end
module FileInclude
def find_file(file, part)
for dir in part.tree.include_path
begin
return open(dir + "/" + file)
rescue
next
end
end
nil
end
module_function :find_file
end # Filter::Include
end # Filter
# Build-in Filter
# Simple inclusion
INCLUDE_FILTER = Filter.new(:target) do |inn, out|
inn.each do |line|
out.print(line)
end
end
# Simple RD inclusion
RD_FILTER = Filter.new(:rd) do |inn, out|
out.print("=begin\n")
inn.each do |line|
out.print(line)
end
out.print("\n=end\n")
end
# Eval ruby script
# "out.print" to output.
EVAL_FILTER = Filter.new(:target) do |inn, out|
begin
eval(inn.to_s)
rescue
out.print "!!Error occured when eval!!\n"
end
end
# RD::Part is a pseudo IO class
class Part
attr(:tree)
attr_accessor :lineno
attr_accessor :pos
def initialize(content = "", tree = nil, mode = "r")
@content = content
if mode == "r"
@content.freeze
end
@tree = tree
@pos = 0
@lineno = 0
@unget = nil
end
def each_line(rs = $/)
while line = gets
yield(line)
end
end
alias each each_line
def each_byte
while char = getc
yield(char)
end
end
def eof?
@pos == @content.size
end
alias eof eof?
def get_char(ex)
ret = nil
if @unget
ret = @unget
else
unless eof?
ret = @content[@pos]
@pos += 1
else
raise EOFError if ex
end
end
ret
end
private :get_char
def getc
get_char(nil)
end
def readchar
get_char(true)
end
def ungetc(char)
@ungetc = char
nil
end
def get_line(ex, rs)
ret = nil
unless eof?
new_pos = @content.index(rs, @pos)
if new_pos
ret = @content[@pos .. new_pos]
@pos = new_pos + 1
@lineno += 1
else
ret = @content[@pos .. @content.size - 1]
@pos = @content.size
@lineno += 1
end
else
raise EOFError if ex
end
$_ = ret
end
private :get_line
def gets(rs = $/)
get_line(nil, rs)
end
def readline(rs = $/)
get_line(true, $/)
end
def read(length = @content.size - @pos)
ret = ""
length.times do
ret << getc
end
ret
end
def readlines(rs = $/)
ret = []
each_line(rs) do |line|
ret.push(line)
end
ret
end
def rewind
@pos = 0
end
def seek(offset, whence)
case whence
when 0
@pos = offset
when 1
@pos += offset
when 2
@pos += @content.size - 1
else
raise Errno::EINVAL
end
end
alias tell pos
def << (arg)
begin
@content << arg.to_s
self
rescue
raise IOError
end
end
def print(*args)
begin
args.each do |i|
@content << i.to_s
end
nil
rescue
raise IOError
end
end
def printf(format, *args)
str = sprintf(format, *args)
begin
@content << str
nil
rescue
raise IOError
end
end
def putc(char)
self.printf("%c", char)
char
end
def puts(*args)
args.flatten.each do |i|
self.print(i, "\n")
end
end
def write(str)
@content << str.to_s
str.to_s.size
end
def empty?
@content.empty?
end
def to_s
@content
end
end
end
=begin
== script info.
filter structure.
$Id: filter.rb,v 1.7 2001/03/19 15:20:08 toshirok Exp $
=end
|