Class: AlTemplate

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil) ⇒ AlTemplate

constructor

Parameters:

  • input (String) (defaults to: nil)

    erbソース文字列



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_filesArray<Stfing> (readonly)

Returns テンプレートを構成するサブファイル名の配列.

Returns:

  • (Array<Stfing>)

    テンプレートを構成するサブファイル名の配列



199
200
201
# File 'lib/al_template.rb', line 199

def component_files
  @component_files
end

#src_primString

Returns テンプレートコンパイル結果.

Returns:

  • (String)

    テンプレートコンパイル結果



196
197
198
# File 'lib/al_template.rb', line 196

def src_prim
  @src_prim
end

#trimBoolean

Returns 文の改行を取り除くモード.

Returns:

  • (Boolean)

    文の改行を取り除くモード



193
194
195
# File 'lib/al_template.rb', line 193

def trim
  @trim
end

Class Method Details

._exec(src, ctxt = nil) ⇒ String

テンプレートを実行し、結果を返す。(内部メソッド)

Parameters:

  • src (String)

    erbコンパイル結果

  • ctxt (Object, Binding) (defaults to: nil)

    実行コンテキスト

Returns:

  • (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

テンプレートファイル絶対パスを導出(内部メソッド)

Parameters:

  • filename (String)

    ファイル名

Returns:

  • (String)

    ファイル名絶対パス



57
58
59
60
61
62
63
64
65
# File 'lib/al_template.rb', line 57

def self._expand_path( filename )
  raise "No such file in template. '#{filename}' (AlTemplate::_expand_path() needs string.)"  if filename.class != String
  case filename[0]
  when '/', '\\', '.'
    return File.expand_path( filename )
  else
    return File.expand_path( File.join( AL_TEMPLATE_DIR, filename ) )
  end
end

._read_cachefile(cachefile) ⇒ String, NilClass

キャッシュファイルの読み込み(内部メソッド)

Parameters:

  • cachefile (String)

    キャッシュファイル名

Returns:

  • (String)

    キャッシュしたerbソース。

  • (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

Note:

erbファイルのコンパイル結果を返す。(内部メソッド)

キャッシュがあれば、それを使う。 結果に、プリ・ポストアンブルは付かない。

Parameters:

  • filename (String)

    ファイル名



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 = _expand_path( 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

Note:

ファイル名を指定してオブジェクトを生成

キャッシュがあれば、それを使う。

Parameters:

  • filename (String)

    ファイル名

Returns:



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 = _expand_path( 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

Note:

テンプレートファイルを適用し、レンダリングし、表示する。

al_config中に指示があれば、コンパイル結果をキャッシュする。

Parameters:

  • filename (String)

    ファイル名

  • ctxt (Object, Binding) (defaults to: nil)

    実行コンテキスト



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

Note:

テンプレート文字列を適用し、レンダリングし、表示する。

キャッシュはしない。

Parameters:

  • tstr (String)

    テンプレート文字列

  • ctxt (Object, Binding) (defaults to: 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

#_save_cachefile(cachefile, fname_abs) ⇒ Object

コンパイル結果をキャッシュへ保存(内部メソッド)

Parameters:

  • cachefile (String)

    キャッシュファイル名

  • fname_abs (String)

    テンプレートファイル絶対パス



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

Note:

erbソースをコンパイルして保持。

実質の、初期化メソッド。

Parameters:

  • input (String)

    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

テンプレートを実行し、結果を返す。

Parameters:

  • ctxt (Object, Binding) (defaults to: nil)

    実行コンテキスト

Returns:

  • (String)

    実行結果



63
64
65
# File 'lib/al_template_main.rb', line 63

def result( ctxt=nil )
  return AlTemplate::_exec( src, ctxt )
end

#srcString

erbコンパイル結果を返す。

Returns:

  • (String)

    コンパイル結果



52
53
54
# File 'lib/al_template_main.rb', line 52

def src()
  return "_buf='';#{@src_prim}\n_buf.to_s\n"
end