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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
# Web::Template
#
# Author:: MoonWolf (mailto:moonwolf@moonwolf.com)
# Copyright:: Copyright (c) 2003-2004 MoonWolf
# License:: Distributes under the same terms as Ruby
require "web/escape"
module Web
class Template
CACHE = Hash.new
def initialize(opt={})
@param = {}
@tree = nil
@filename = nil
#
if filename=opt['filename']
path = opt['path'] || ['.']
if opt['cache']
file = Parser.find_file(filename, path)
raise Errno::ENOENT,filename unless file
if tmp = CACHE[file]
mtime, tree, proc = tmp
if File.mtime(file) == mtime
@tree = tree
@proc = proc
return
else
#p "cache expired"
end
end
end
parser = Parser.new
@filename, tmpl = parser.load_file(filename,path)
@tree = parser.parse(tmpl)
writer = RubyWriter.new
src = "Proc.new {|param,io|\n" # }
writer.out = src
writer.run(@tree)
src << "io\n"
src << "}\n"
src.untaint
@src = src
@proc = eval(src)
if opt['cache']
self.class.cache[@filename] = [File.mtime(@filename), @tree, @proc]
end
end
end
# syntax tree
attr_accessor :tree
# execute template and return result string
def output(io='')
raise "not parsed" unless @tree
begin
@proc.call(@param,io)
rescue
puts @src
raise
end
io
end
# clear params
def clear_params
@param = {}
end
#
def param(hash=nil)
if hash
@param.update(hash)
self
else
@param
end
end
# set params
def param=(hash)
@param = hash
end
class Node
def initialize(value)
@value = value
@children = []
end
# node value
attr_reader :value
# chilren nodes(Array)
attr_reader :children
# append child node
def <<(node)
@children << node
self
end
def accept(visitor)
name = self.class.name
name.sub!(/.+::/,'')
method = "visit_#{name}"
visitor.send(method, self)
end
class RootNode < Node
end
class TextNode < Node
end
class VarNode < Node
end
class IfNode < Node
end
class UnlessNode < Node
end
class BlockNode < Node
end
class LoopNode < Node
end
end
class Parser
def self::find_file(filename, path=['.'])
path.each {|dir|
file = File.expand_path(filename,dir).untaint
return file if FileTest.exist? file
}
nil
end
def initialize
@tokens = nil
@tree = nil
end
def load_file(filename,path=['.'])
file = self.class::find_file(filename,path)
raise Errno::ENOENT,filename unless file
tmpl = nil
open(file,'rb') {|f|
tmpl = f.read || ''
}
tmpl = filter(tmpl,path)
[file, tmpl]
end
def filter(tmpl,path)
tmpl.gsub(/\{\{INCLUDE\s*(.+?)\}\}/i) {|match|
attr = $1
attrs = {}
attr.scan(/(\w+)=(?:(?:"(.*?)")|(?:'(.*?)')|(\w+))/) {
name = $1.downcase
value = $2 || $3 || $4
attrs[name] = value
}
filename = attrs['name']
file, tmp = load_file(filename, path)
tmp
}
end
def parse(src)
@tokens = tokenize(src)
#@tokens.each {|token|
# type,value = token
# puts "#{type}\t#{value.inspect}"
#}
@tree = root
end
def next_token
token = @tokens.shift
end
def tokenize(src)
tokens = []
text = ''
until src.empty?
if src =~ %r!\A\{\{([a-zA-Z]+)(\s.+?)?\}\}!
src = $'
attrs = {}
case $1.downcase
when 'var'
type = :TMPL_VAR
attrs['escape']='HTML'
when 'if'
type = :TMPL_IF
when 'unless'
type = :TMPL_UNLESS
when 'else'
type = :TMPL_ELSE
when 'loop'
type = :TMPL_LOOP
end
attr = $2
if attr
attr.scan(/(\w+)=(?:(?:"(.*?)")|(?:'(.*?)')|([0-9a-zA-Z_$\-]+))/) {
name = $1.downcase
value = $2 || $3 || $4
attrs[name] = value
}
end
unless text.empty?
tokens << [:TEXT, text]
text = ''
end
tokens << [type, attrs]
elsif src =~ %r!\A\{\{/([a-zA-Z]+)\}\}!
src = $'
type = $1.downcase.intern
unless text.empty?
tokens << [:TEXT, text]
text = ''
end
tokens << [:CLOSE, type]
elsif src =~ %r!\A[^{]+!
src = $'
text << $&
elsif src =~ %r!\A.!
src = $'
text << $&
end
end
unless text.empty?
tokens << [:TEXT, text]
end
tokens
end
def root
node = Node::RootNode.new(nil)
while token = next_token
type, value = token
case type
when :TEXT
node << Node::TextNode.new(value)
when :TMPL_VAR
node << Node::VarNode.new([value['name'],value['escape'] ? value['escape'].upcase : nil])
when :TMPL_IF
node << if_node(token)
when :TMPL_UNLESS
node << unless_node(token)
when :TMPL_LOOP
node << loop_node(token)
else
p node
raise type.to_s
end
end
node
end
def if_node(token)
type, value = token
name = value['name']
node = Node::IfNode.new(name)
node << then_block = Node::BlockNode.new(nil)
node << else_block = Node::BlockNode.new(nil)
catch(:END_IF) {
while token = next_token
type, value = token
case type
when :TEXT
then_block << Node::TextNode.new(value)
when :TMPL_VAR
then_block << Node::VarNode.new([value['name'],value['escape'] ? value['escape'].upcase : nil])
when :TMPL_IF
then_block << if_node(token)
when :TMPL_UNLESS
then_block << unless_node(token)
when :TMPL_ELSE
break
when :TMPL_LOOP
then_block << loop_node(token)
when :CLOSE
throw :END_IF
else
p type,value
raise
end
end
while token = next_token
type, value = token
case type
when :TEXT
else_block << Node::TextNode.new(value)
when :TMPL_VAR
else_block << Node::VarNode.new([value['name'],value['escape'] ? value['escape'].upcase : nil])
when :TMPL_IF
else_block << if_node(token)
when :TMPL_UNLESS
else_block << unless_node(token)
when :TMPL_LOOP
else_block << loop_node(token)
when :CLOSE
throw :END_IF
else
p type,value
raise
end
end
}
node
end
def unless_node(token)
type, value = token
name = value['name']
node = Node::UnlessNode.new(name)
node << then_block = Node::BlockNode.new(nil)
node << else_block = Node::BlockNode.new(nil)
catch(:END_IF) {
while token = next_token
type, value = token
case type
when :TEXT
then_block << Node::TextNode.new(value)
when :TMPL_VAR
then_block << Node::VarNode.new([value['name'],value['escape'] ? value['escape'].upcase : nil])
when :TMPL_IF
then_block << if_node(token)
when :TMPL_UNLESS
then_block << unless_node(token)
when :TMPL_ELSE
break
when :TMPL_LOOP
then_block << loop_node(token)
when :CLOSE
throw :END_IF
else
p type,value
raise
end
end
while token = next_token
type, value = token
case type
when :TEXT
else_block << Node::TextNode.new(value)
when :TMPL_VAR
else_block << Node::VarNode.new([value['name'],value['escape'] ? value['escape'].upcase : nil])
when :TMPL_IF
else_block << if_node(token)
when :TMPL_UNLESS
else_block << unless_node(token)
when :TMPL_LOOP
else_block << loop_node(token)
when :CLOSE
throw :END_IF
else
p type,value
raise
end
end
}
node
end
def loop_node(token)
type, value = token
name = value['name']
node = Node::LoopNode.new(name)
while token = next_token
type, value = token
case type
when :TEXT
node << Node::TextNode.new(value)
when :TMPL_VAR
node << Node::VarNode.new([value['name'],value['escape'] ? value['escape'].upcase : nil])
when :TMPL_IF
node << if_node(token)
when :TMPL_UNLESS
node << unless_node(token)
when :TMPL_LOOP
node << loop_node(token)
when :CLOSE
break
else
p node
raise type.to_s
end
end
node
end
end # Parser
class RubyWriter
def initialize
@out = ''
end
# output stream(String or IO)
attr_accessor :out
# execute tree
def run(tree)
tree.accept(self)
@out
end
def visit_children(node)
node.children.each {|n|
n.accept(self)
}
end
def visit_RootNode(node)
@out << "root = param\n"
@out << "stack = []\n"
@out << "list = []\n"
@out << "index = 0\n"
visit_children(node)
@out << "io\n"
end
def visit_TextNode(node)
@out << "io << #{node.value.dump}\n"
end
def visit_VarNode(node)
key, escape = node.value
if key=~/\A\$(\w+)/
key = $1
case escape
when "HTML"
@out << "io << Web::escapeHTML(root[#{key.dump}])\n"
when "URL"
@out << "io << Web::escape(root[#{key.dump}])\n"
else
@out << "io << root[#{key.dump}]\n"
end
else
case escape
when "HTML"
@out << "io << Web::escapeHTML(param[#{key.dump}])\n"
when "URL"
@out << "io << Web::escape(param[#{key.dump}])\n"
else
@out << "io << param[#{key.dump}]\n"
end
end
end
def visit_IfNode(node)
key = node.value
case key
when "__FIRST__"
@out << "if index==0\n"
when "__LAST__"
@out << "if index==list.size-1\n"
when "__INNER__"
@out << "if !((index==0) || (index==list.size-1))\n"
when "__ODD__"
@out << "if index % 2 == 0\n"
else
if key=~/\A\$(\w+)/
key = $1
@out << "if root[#{key.dump}]\n"
else
@out << "if param[#{key.dump}]\n"
end
end
n = node.children[0]
n.accept(self)
if (n = node.children[1]) && (n.children.size > 0)
@out << "else\n"
n.accept(self)
end
@out << "end\n"
end
def visit_UnlessNode(node)
key = node.value
case key
when "__FIRST__"
@out << "unless index==0\n"
when "__LAST__"
@out << "unless index==list.size-1\n"
when "__INNER__"
@out << "unless !((index==0) || (index==list.size-1))\n"
when "__ODD__"
@out << "unless index % 2 == 0\n"
else
if key=~/\A\$(\w+)/
key = $1
@out << "unless root[#{key.dump}]\n"
else
@out << "unless param[#{key.dump}]\n"
end
end
n = node.children[0]
n.accept(self)
if (n = node.children[1]) && (n.children.size > 0)
@out << "else\n"
n.accept(self)
end
@out << "end\n"
end
def visit_BlockNode(node)
visit_children(node)
end
def visit_LoopNode(node)
key = node.value
@out << "stack.push [param, list, index]\n"
if key=~/\A\$(\w+)/
key = $1
@out << "list = root[#{key.dump}]\n"
else
@out << "list = param[#{key.dump}]\n"
end
@out << "index = 0\n"
@out << "list.each {|param|\n"
visit_children(node)
@out << "index += 1\n"
@out << "}\n"
@out << "param,list,index = stack.pop\n"
end
end # RubyWriter
end # Template
end # Web
|