Class: AlTemplate
- Inherits:
-
Object
- Object
- AlTemplate
- Defined in:
- lib/al_erb.rb,
lib/al_template.rb,
lib/al_template_main.rb
Overview
テンプレートマネージャ
Constant Summary
- CACHE_SIGNATURE =
"AL TEMPLATE CACHE. Version 1.00"
- EMBEDDED_PATTERN =
/<%(=|-|\#|%)?(.*?)([-=])?%>([ \t]*\r?\n)?/m
- COMMAND_EXTRACTOR =
/^ *(include|expand|header_section|body_section|footer_section|h|u)(([ (].*)|$)/
Instance Attribute Summary (collapse)
-
- (Array<Stfing>) component_files
readonly
テンプレートを構成するサブファイル名の配列.
-
- (String) src_prim
テンプレートコンパイル結果.
-
- (Boolean) trim
文の改行を取り除くモード.
Class Method Summary (collapse)
-
+ (String) _exec(src, ctxt = nil)
テンプレートを実行し、結果を返す。(内部メソッド).
-
+ (String, NilClass) _read_cachefile(cachefile)
キャッシュファイルの読み込み(内部メソッド).
-
+ (Object) _result_file(filename)
erbファイルのコンパイル結果を返す。(内部メソッド).
-
+ (AlTemplate) load_file(filename)
ファイル名を指定してオブジェクトを生成.
-
+ (Object) run(filename, ctxt = nil)
テンプレートファイルを適用し、レンダリングし、表示する。.
-
+ (Object) run_str(tstr, ctxt = nil)
テンプレート文字列を適用し、レンダリングし、表示する。.
Instance Method Summary (collapse)
-
- (Object) _save_cache(cachefile, fname_abs)
コンパイル結果をキャッシュへ保存(内部メソッド).
-
- (Object) convert(input)
erbソースをコンパイルして保持。.
-
- (AlTemplate) initialize(input = nil)
constructor
constractor.
-
- (String) result(ctxt = nil)
テンプレートを実行し、結果を返す。.
-
- (String) src
erbコンパイル結果を返す。.
Constructor Details
- (AlTemplate) initialize(input = nil)
constractor
198 199 200 201 202 203 |
# File 'lib/al_template.rb', line 198 def initialize( input=nil ) require 'al_template_main' @trim = true convert( input ) end |
Instance Attribute Details
- (Array<Stfing>) component_files (readonly)
テンプレートを構成するサブファイル名の配列
192 193 194 |
# File 'lib/al_template.rb', line 192 def component_files @component_files end |
- (String) src_prim
テンプレートコンパイル結果
189 190 191 |
# File 'lib/al_template.rb', line 189 def src_prim @src_prim end |
- (Boolean) trim
文の改行を取り除くモード
186 187 188 |
# File 'lib/al_template.rb', line 186 def trim @trim end |
Class Method Details
+ (String) _exec(src, ctxt = nil)
テンプレートを実行し、結果を返す。(内部メソッド)
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/al_template.rb', line 136 def self._exec( src, ctxt=nil ) if ctxt.class == Binding return eval( src, ctxt ) elsif ctxt return ctxt.instance_eval( src ) elsif defined?( $AlController ) return $AlController.instance_eval( src ) else return eval( src, TOPLEVEL_BINDING ) end end |
+ (String, NilClass) _read_cachefile(cachefile)
キャッシュファイルの読み込み(内部メソッド)
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 |
# File 'lib/al_template.rb', line 90 def self._read_cachefile( cachefile ) # open cachefile begin file = File.open( cachefile, "r" ) rescue return nil end cachefile_mtime = File.mtime( cachefile ) file.flock( File::LOCK_SH ) catch(:error_exit) do # check signature throw :error_exit if file.gets().chomp != CACHE_SIGNATURE # check component file's mtime while text = file.gets() do text.chomp! break if text == "" begin throw :error_exit if File.mtime( text ) > cachefile_mtime rescue throw :error_exit end end # read cache source of erb. src = file.read() file.close() return src end # error_exit file.close() return nil end |
+ (Object) _result_file(filename)
キャッシュがあれば、それを使う。 結果に、プリ・ポストアンブルは付かない。
erbファイルのコンパイル結果を返す。(内部メソッド)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/al_template.rb', line 59 def self._result_file( filename ) # decide filepath case filename[0] when '/', '\\', '.' fname_abs = filename else fname_abs = File.( File.join( AL_TEMPLATE_DIR, filename ) ) end if AL_TEMPLATE_CACHE cachefile = File.join( AL_TEMPLATE_CACHE, Alone::encode_uri_component( fname_abs ) ) cached_src = _read_cachefile( cachefile ) return cached_src if cached_src tobj = AlTemplate.new( File.read( fname_abs ) ) tobj._save_cache( cachefile, fname_abs ) else tobj = AlTemplate.new( File.read( fname_abs ) ) end return tobj.src_prim end |
+ (AlTemplate) load_file(filename)
キャッシュがあれば、それを使う。
ファイル名を指定してオブジェクトを生成
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 |
# File 'lib/al_template.rb', line 157 def self.load_file( filename ) # decide filepath case filename[0] when '/', '\\', '.' fname_abs = filename else fname_abs = File.( File.join( AL_TEMPLATE_DIR, filename ) ) end if AL_TEMPLATE_CACHE cachefile = File.join( AL_TEMPLATE_CACHE, Alone::encode_uri_component( fname_abs ) ) cached_src = _read_cachefile( cachefile ) if cached_src tobj = AlTemplate.new() tobj.src_prim = cached_src return tobj end tobj = AlTemplate.new( File.read( fname_abs ) ) tobj._save_cache( cachefile, fname_abs ) else tobj = AlTemplate.new( File.read( fname_abs ) ) end return tobj end |
+ (Object) run(filename, ctxt = nil)
al_config中に指示があれば、コンパイル結果をキャッシュする。
テンプレートファイルを適用し、レンダリングし、表示する。
42 43 44 45 |
# File 'lib/al_erb.rb', line 42 def self.run( filename, ctxt=nil ) s = _result_file( filename ) print _exec( "_buf='';#{s}\n_buf.to_s\n", ctxt ) end |
+ (Object) run_str(tstr, ctxt = nil)
キャッシュはしない。
テンプレート文字列を適用し、レンダリングし、表示する。
45 46 47 48 |
# File 'lib/al_template.rb', line 45 def self.run_str( tstr, ctxt=nil ) tobj = AlTemplate.new( tstr ) print tobj.result( ctxt ) end |
Instance Method Details
- (Object) _save_cache(cachefile, fname_abs)
コンパイル結果をキャッシュへ保存(内部メソッド)
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/al_template_main.rb', line 76 def _save_cache( cachefile, fname_abs ) file = File.open( cachefile, "w" ) file.flock( File::LOCK_EX ) file.puts( CACHE_SIGNATURE ) file.puts( fname_abs ) @component_files.each do |a| file.puts( a ) end file.puts( "" ) file.puts( @src_prim ) file.close() end |
- (Object) convert(input)
実質の、初期化メソッド。
erbソースをコンパイルして保持。
37 38 39 40 41 42 |
# File 'lib/al_template_main.rb', line 37 def convert( input ) @src_prim = "" @component_files = [] convert_input( @src_prim, input ) if input end |
- (String) result(ctxt = nil)
テンプレートを実行し、結果を返す。
61 62 63 64 65 66 67 68 |
# File 'lib/al_template_main.rb', line 61 def result( ctxt=nil ) begin return AlTemplate::_exec( src, ctxt ) rescue => ex raise ex, "Error in template. #{ex.}" end end |
- (String) src
erbコンパイル結果を返す。
50 51 52 |
# File 'lib/al_template_main.rb', line 50 def src() return "_buf='';#{@src_prim}\n_buf.to_s\n" end |