Template-Toolkitメモ
genre:perl,programming,memo
Template-ToolkitのテンプレートのParseは、Template/Parser.pm中のparse()で行われている。
まず、$textに読み込まれたテンプレートをsplit_text($text)でテンプレートコマンド([%?%]など)単位で切り出す(以下)。
# extract all directives from the text
while ($text =~ s/
^(.*?) # $1 – start of line up to directive
(?:
$start # start of tag
(.*?) # $2 – tag contents
$end # end of tag
)
//sx) {
($pre, $dir) = ($1, $2);
$pre = ” unless defined $pre;
$dir = ” unless defined $dir;
$prelines = ($pre =~ tr/\n//); # newlines in preceeding text
$dirlines = ($dir =~ tr/\n//); # newlines in directive tag
$postlines = 0; # newlines chomped after tag で、タグのディレクティブ($dirに格納)の空白などを取り除いて正規化して、非ディレクティブ(テキスト部分)とともに@tokenに順にぶち込んでいく。@tokenにはTEXTかTAG(DIRECTIVE)かという情報とともにテキストとディレクティブが順にぶち込まれる。
あとは@tokenの頭から、ぶちこまれたものを取り出して、_parse()でTAGを解釈していく。