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
|
##
## $Release:$
## $Copyright$
##
require 'erubis/generator'
require 'erubis/converter'
require 'erubis/evaluator'
require 'erubis/context'
module Erubis
##
## (abstract) abstract engine class.
## subclass must include evaluator and converter module.
##
class Engine
#include Evaluator
#include Converter
#include Generator
def initialize(input=nil, properties={})
#@input = input
init_generator(properties)
init_converter(properties)
init_evaluator(properties)
@src = convert(input) if input
end
##
## convert input string and set it to @src
##
def convert!(input)
@src = convert(input)
end
##
## load file, write cache file, and return engine object.
## this method create code cache file automatically.
## cachefile name can be specified with properties[:cachename],
## or filname + 'cache' is used as default.
##
def self.load_file(filename, properties={})
cachename = properties[:cachename] || (filename + '.cache')
properties[:filename] = filename
timestamp = File.mtime(filename)
if test(?f, cachename) && timestamp == File.mtime(cachename)
engine = self.new(nil, properties)
engine.src = File.read(cachename)
else
input = File.open(filename, 'rb') {|f| f.read }
engine = self.new(input, properties)
tmpname = cachename + rand().to_s[1,8]
File.open(tmpname, 'wb') {|f| f.write(engine.src) }
File.rename(tmpname, cachename)
File.utime(timestamp, timestamp, cachename)
end
engine.src.untaint # ok?
return engine
end
##
## helper method to convert and evaluate input text with context object.
## context may be Binding, Hash, or Object.
##
def process(input, context=nil, filename=nil)
code = convert(input)
filename ||= '(erubis)'
if context.is_a?(Binding)
return eval(code, context, filename)
else
context = Context.new(context) if context.is_a?(Hash)
return context.instance_eval(code, filename)
end
end
##
## helper method evaluate Proc object with contect object.
## context may be Binding, Hash, or Object.
##
def process_proc(proc_obj, context=nil, filename=nil)
if context.is_a?(Binding)
filename ||= '(erubis)'
return eval(proc_obj, context, filename)
else
context = Context.new(context) if context.is_a?(Hash)
return context.instance_eval(&proc_obj)
end
end
end # end of class Engine
##
## (abstract) base engine class for Eruby, Eperl, Ejava, and so on.
## subclass must include generator.
##
class Basic::Engine < Engine
include Evaluator
include Basic::Converter
include Generator
end
class PI::Engine < Engine
include Evaluator
include PI::Converter
include Generator
end
end
|