From 9f64d60b5b02f28b7ca34d259d257c096db4a8c1 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 4 Apr 2007 01:46:09 +0000 Subject: [PATCH] Rewrote the tag parser in BSTemplate::_parsePhrases(): - Now the indices in the tag actually correspond to the appropriate argument - It looks a lot cooler? --- Template.php | 112 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 40 deletions(-) diff --git a/Template.php b/Template.php index 3f629ac..315218c 100644 --- a/Template.php +++ b/Template.php @@ -432,67 +432,99 @@ class BSTemplate */ private function _parsePhrases($template) { - $tag_start = ' 0) + { + // set aside the index and restart capturing + $varNum = $capture; + $capture = ''; + $capturePos = $i; + } + else + { + trigger_error('Invalid language variable index "' . $capture . '"'); + } + } + else if ($template[$i] == '>' AND $template[$i - 1] == '"') + { + // the final variable substitution + $varMap[intval($varNum)] = BSFunctions::Substring($template, $capturePos + 3, $i - 2); + $varEnds = $i; + break; + } + + $capture .= $template[$i]; + $i++; } - // Extract the language object - $phrase_bunch = substr($template, $location_start, ($location_end + strlen($tag_end)) - $location_start); - - // Find the close to the opening - $close_of_open = strpos($phrase_bunch, $tag_start_end); - if ($close_of_open === false) + // locate the end tag + $end = strpos($template, $tagEnd, $i); + if ($end === false) { break; } - // Extract the opening tag so it can be parsed - $init_tag = substr($phrase_bunch, 0, ($close_of_open + strlen($tag_start_end))); - $init_tag = str_replace($tag_start, '', $init_tag); - $init_tag = substr($init_tag, 0, strlen($init_tag) - 1); + // this is the string that gets variable replacement + $str = BSFunctions::Substring($template, $varEnds + 1, $end); - // Get the args out of the tag - $args = preg_split('#([0-9].*?)=#', $init_tag); - foreach ($args AS $arg) + // create the complete varmap + + for ($i = max(array_keys($varMap)); $i > 0; $i--) { - if ($arg AND $arg != ' ') + if (!isset($varMap[$i])) { - $arg = trim($arg); - $arg = substr($arg, 2); - $arg = substr($arg, 0, strlen($arg) - 2); - $arglist[] = $arg; + $varMap[$i] = '[MISSING SUBSTITUTION INDEX: ' . $i . ']'; } } - // Just get the phrase name - $phrase_name = preg_replace('#(.*?)#i', '$2', $phrase_bunch); - - // Wrap the parsed data into the build function - $function_wrap = '" . ' . $this->langconst . '("' . $phrase_name . '", "' . implode('", "', $arglist) . '") . "'; - - // Replace the fully-parsed string back into the template - $template = substr_replace($template, $function_wrap, $location_start, $location_end + strlen($tag_end) - $location_start); + // put all the keys in corresponding argument order + ksort($varMap); - unset($arglist); + // FINALLY, construct the call to sprintf() + $template = substr_replace($template, '" . ' . $this->langconst . '(\'' . $str . '\', "' . implode('", "', $varMap) . '") . "', $start, ($end + strlen($tagEnd)) - $start); } return $template; -- 2.22.5