X-Git-Url: https://jasonwoof.com/gitweb/?p=wfpl.git;a=blobdiff_plain;f=template.php;h=077aa8daf4547b33693af1df1f008811e41862db;hp=b6025731b62a13d1d0a474bcdcba52b9501ca25a;hb=HEAD;hpb=812c501d8bc3d69ac9a8766ade468ccc2d14263a diff --git a/template.php b/template.php index b602573..077aa8d 100644 --- a/template.php +++ b/template.php @@ -1,19 +1,9 @@ -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This program is in the public domain within the United States. Additionally, +# we waive copyright and related rights in the work worldwide through the CC0 +# 1.0 Universal public domain dedication, which can be found at +# http://creativecommons.org/publicdomain/zero/1.0/ # This is a simple template-handling system. You pass it a big data @@ -37,9 +27,9 @@ # tem_auto_* function to munge the data, automating certain common use # cases. See the comments on the tem_auto functions for more details. -require_once('code/wfpl/encode.php'); -require_once('code/wfpl/file.php'); -require_once('code/wfpl/misc.php'); +require_once(__DIR__.'/'.'encode.php'); +require_once(__DIR__.'/'.'file.php'); +require_once(__DIR__.'/'.'misc.php'); # Top-Level Functions @@ -53,19 +43,18 @@ function template_file($filename, $data) { return fill_template(parse_template_file($filename), $data); } -function parse_template_file($filename) { +function &parse_template_file($filename) { return parse_template(file_get_contents($filename)); } -# We parse the template string into a tree of strings and sub-templates. +# We parse the template string into a tree of strings and sub-templates. # A template is a hash with a name string, a pieces array, and possibly # an args array. -function parse_template($string) { +function &parse_template($string) { $tem =& tem_push(); $tem['pieces'] = array(); - # note: for some reason this captures ''. - $matches = preg_split("/()/", $string, -1, PREG_SPLIT_DELIM_CAPTURE); + $matches = preg_split('/()/', preg_replace('//', '$1', $string), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach($matches as $match) { if($match == '~~') $match = '~'; if(substr($match,0,1) == '~' and strlen($match) > 2) { @@ -74,24 +63,24 @@ function parse_template($string) { if(count($args) == 1 and $args[0] == '}') $name = ''; else $name = array_shift($args); - if(last($args) == '{') { # open block + if(count($args) && $args[count($args)-1] == '{') { # open block array_pop($args); # drop '{' $tem =& tem_push($tem); # create a new sub-template $tem['parent']['pieces'][] =& $tem; # as a piece of the parent $tem['name'] = $name; $tem['pieces'] = array(); $tem['args'] = $args; - } elseif(last($args) == '}') { # close block + } elseif(count($args) && $args[count($args)-1] == '}') { # close block array_pop($args); # drop '}' $cur = $tem['name']; if($name && $name != $cur) { - die("Invalid template: tried to close '$name', but '$cur' is current."); + die("Invalid template: tried to close '$name', but '$cur' is current."); } $tem =& $tem['parent']; } else { # value slot $tem['pieces'][] = array('name' => $name, 'args' => $args); } - } elseif($match and $match != '