2
0
mirror of https://github.com/boostorg/website.git synced 2026-02-20 15:22:08 +00:00

Support delimiters.

This commit is contained in:
Daniel James
2016-05-17 09:10:10 +01:00
parent 5312289756
commit 5d71cde30c
2 changed files with 27 additions and 16 deletions

View File

@@ -7,7 +7,6 @@ require_once(__DIR__.'/boost.php');
*
* Lambdas
* Partials
* Set Delimiter
*
* Doesn't claim to be at all compatible with Mustache, just that it should be
* easy to switch to a proper Mustache implementation in the future.
@@ -23,25 +22,28 @@ class BoostSimpleTemplate {
}
static function parse_template($template) {
preg_match_all('@
(?P<leading_whitespace>^[ \t]*)?
(?P<tag>{{(?:
!.*?}} |
(?P<symbol_operator>[#/^&]?)\s*(?P<symbol>[\w?!\/.-]*)\s*}} |
{\s*(?P<unescaped>[\w?!\/.-]+)\s*}}} |
(?P<error>)
))
(?P<trailing_whitespace>[ \t]*(?:\r?\n|\Z))?
@xsm',
$template, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
$template_parts = array();
$operator_stack = array();
$scope_stack = array();
$template_stack = array();
$last_offset = 0;
foreach($matches as $match) {
$open_delim = '{{';
$close_delim = '}}';
while(preg_match("@
(?P<leading_whitespace>^[ \\t]*)?
(?P<tag>{$open_delim}(?:
!.*?{$close_delim} |
(?P<symbol_operator>[#/^&]?)\\s*(?P<symbol>[\\w?!/.-]*)\\s*{$close_delim} |
{\\s*(?P<unescaped>[\\w?!\\/.-]+)\\s*}{$close_delim} |
=\\s*(?P<open_delim>[^=\\s]+?)\\s*(?P<close_delim>[^=\\s]+?)\\s*={$close_delim} |
(?P<error>)
))
(?P<trailing_whitespace>[ \\t]*(?:\\r?\\n|\\Z))?
@xsm",
$template, $match, PREG_OFFSET_CAPTURE, $last_offset)) {
$operator = null;
if (array_key_exists('error', $match) && $match['error'][1] != -1) {
throw new BoostSimpleTemplateException("Invalid/unsupported tag", $match['tag'][1]);
@@ -50,6 +52,10 @@ class BoostSimpleTemplate {
$operator = '&';
$symbol = $match['unescaped'][0];
}
else if (!empty($match['open_delim'][0])) {
$operator = '=';
$symbol = null;
}
else if(!empty($match['symbol'][0])) {
$operator = $match['symbol_operator'][0] ?: '$';
$symbol = $match['symbol'][0];
@@ -99,6 +105,10 @@ class BoostSimpleTemplate {
'symbol' => $symbol,
);
break;
case '=':
$open_delim = preg_quote($match['open_delim'][0], '@');
$close_delim = preg_quote($match['close_delim'][0], '@');
break;
default:
assert(false); exit(1);
}

View File

@@ -13,8 +13,8 @@ function run_tests() {
if ($test_name[0] === '~') {
echo "*** Ignoring optional test: {$test_name}\n";
}
else if (in_array($test_name, array('delimiters', 'partials'))) {
echo "*** Checking for failure in supported test: {$test_name}\n";
else if (in_array($test_name, array('partials'))) {
echo "*** Checking for failure in unsupported test: {$test_name}\n";
foreach($test_cases->tests as $test_case) {
echo "{$test_case->name}\n";
@@ -27,6 +27,7 @@ function run_tests() {
echo "*** Running tests from: {$test_name}\n";
foreach($test_cases->tests as $test_case) {
if (property_exists($test_case, 'partials')) { continue; }
echo "{$test_case->name}\n";
Assert::same($test_case->expected,
BoostSimpleTemplate::render_to_string(