Class: AlTemplate
- Inherits:
-
Object
- Object
- AlTemplate
- Defined in:
- lib/al_template.rb,
lib/al_template_main.rb
Overview
テンプレートマネージャ
Constant Summary collapse
- CACHE_SIGNATURE =
"AL TEMPLATE CACHE. Version 1.00"
- EMBEDDED_PATTERN =
/<%(=|-|\#|%)?(.*?)([-=])?%>([ \t]*\r?\n)?/m
- COMMAND_EXTRACTOR =
/\A *(include|expand|header_section|body_section|footer_section|h|u|s)(([ (].*)|\z)/
- IS_ALL_SPACE =
/\A[ \t]*\z/
- IS_QUOTED_TEXT =
/\A[\(\s]*["']([^"']+)["'][\)\s]*\z/
Instance Attribute Summary collapse
-
#component_files ⇒ Array<Stfing>
readonly
テンプレートを構成するサブファイル名の配列.
-
#src_prim ⇒ String
テンプレートコンパイル結果.
-
#trim ⇒ Boolean
文の改行を取り除くモード.
Class Method Summary collapse
-
._exec(src, ctxt = nil) ⇒ String
テンプレートを実行し、結果を返す。(内部メソッド).
-
._expand_path(filename) ⇒ String
テンプレートファイル絶対パスを導出(内部メソッド).
-
._read_cachefile(cachefile) ⇒ String, NilClass
キャッシュファイルの読み込み(内部メソッド).
-
._result_file(filename) ⇒ Object
erbファイルのコンパイル結果を返す。(内部メソッド).
-
.load_file(filename) ⇒ AlTemplate
ファイル名を指定してオブジェクトを生成.
-
.run(filename, ctxt = nil) ⇒ Object
テンプレートファイルを適用し、レンダリングし、表示する。.
-
.run_str(tstr, ctxt = nil) ⇒ Object
テンプレート文字列を適用し、レンダリングし、表示する。.
Instance Method Summary collapse
-
#_save_cachefile(cachefile, fname_abs) ⇒ Object
コンパイル結果をキャッシュへ保存(内部メソッド).
-
#convert(input) ⇒ Object
erbソースをコンパイルして保持。.
-
#initialize(input = nil) ⇒ AlTemplate
constructor
constructor.
-
#result(ctxt = nil) ⇒ String
テンプレートを実行し、結果を返す。.
-
#src ⇒ String
erbコンパイル結果を返す。.
Constructor Details
#initialize(input = nil) ⇒ AlTemplate
constructor
206 207 208 209 210 211 |
# File 'lib/al_template.rb', line 206 def initialize( input=nil ) require 'al_template_main' @trim = true convert( input ) end |
Instance Attribute Details
#component_files ⇒ Array<Stfing> (readonly)
Returns テンプレートを構成するサブファイル名の配列.
199 200 201 |
# File 'lib/al_template.rb', line 199 def component_files @component_files end |
#src_prim ⇒ String
Returns テンプレートコンパイル結果.
196 197 198 |
# File 'lib/al_template.rb', line 196 def src_prim @src_prim end |
#trim ⇒ Boolean
Returns 文の改行を取り除くモード.
193 194 195 |
# File 'lib/al_template.rb', line 193 def trim @trim end |
Class Method Details
._exec(src, ctxt = nil) ⇒ String
テンプレートを実行し、結果を返す。(内部メソッド)
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/al_template.rb', line 149 def self._exec( src, ctxt=nil ) if ctxt.class == Binding return eval( src, ctxt ) elsif ctxt return ctxt.instance_eval( src, "(in template)" ) elsif defined?( $AlController ) return $AlController.instance_eval( src, "(in template)" ) else return eval( src, TOPLEVEL_BINDING ) end end |
._expand_path(filename) ⇒ String
テンプレートファイル絶対パスを導出(内部メソッド)
57 58 59 60 61 62 63 64 65 |
# File 'lib/al_template.rb', line 57 def self.( filename ) raise "No such file in template. '#{filename}' (AlTemplate::_expand_path() needs string.)" if filename.class != String case filename[0] when '/', '\\', '.' return File.( filename ) else return File.( File.join( AL_TEMPLATE_DIR, filename ) ) end end |
._read_cachefile(cachefile) ⇒ String, NilClass
キャッシュファイルの読み込み(内部メソッド)
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 |
# File 'lib/al_template.rb', line 103 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 |
._result_file(filename) ⇒ Object
erbファイルのコンパイル結果を返す。(内部メソッド)
キャッシュがあれば、それを使う。 結果に、プリ・ポストアンブルは付かない。
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/al_template.rb', line 76 def self._result_file( filename ) fname_abs = ( filename ) if AL_TEMPLATE_CACHE cachefile = File.join( AL_TEMPLATE_CACHE, Alone::encode_uri_component( fname_abs ) ) cached_src = _read_cachefile( cachefile ) if cached_src return cached_src end tobj = AlTemplate.new( File.read( fname_abs ) ) tobj._save_cachefile( cachefile, fname_abs ) else tobj = AlTemplate.new( File.read( fname_abs ) ) end return tobj.src_prim end |
.load_file(filename) ⇒ AlTemplate
ファイル名を指定してオブジェクトを生成
キャッシュがあれば、それを使う。
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/al_template.rb', line 170 def self.load_file( filename ) fname_abs = ( filename ) 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_cachefile( cachefile, fname_abs ) else tobj = AlTemplate.new( File.read( fname_abs ) ) end return tobj end |
.run(filename, ctxt = nil) ⇒ Object
テンプレートファイルを適用し、レンダリングし、表示する。
al_config中に指示があれば、コンパイル結果をキャッシュする。
31 32 33 34 |
# File 'lib/al_template.rb', line 31 def self.run( filename, ctxt=nil ) s = _result_file( filename ) print _exec( "_buf='';#{s}\n_buf.to_s\n", ctxt ) end |
.run_str(tstr, ctxt = nil) ⇒ Object
テンプレート文字列を適用し、レンダリングし、表示する。
キャッシュはしない。
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
#_save_cachefile(cachefile, fname_abs) ⇒ Object
コンパイル結果をキャッシュへ保存(内部メソッド)
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/al_template_main.rb', line 73 def _save_cachefile( 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 |
#convert(input) ⇒ Object
erbソースをコンパイルして保持。
実質の、初期化メソッド。
39 40 41 42 43 44 |
# File 'lib/al_template_main.rb', line 39 def convert( input ) @src_prim = "" @component_files = [] convert_input( @src_prim, input ) if input end |
#result(ctxt = nil) ⇒ String
テンプレートを実行し、結果を返す。
63 64 65 |
# File 'lib/al_template_main.rb', line 63 def result( ctxt=nil ) return AlTemplate::_exec( src, ctxt ) end |
#src ⇒ String
erbコンパイル結果を返す。
52 53 54 |
# File 'lib/al_template_main.rb', line 52 def src() return "_buf='';#{@src_prim}\n_buf.to_s\n" end |