From e24243b2c619b782d6ba9de6eba443397a0bb396 Mon Sep 17 00:00:00 2001 From: andrey Date: Mon, 23 Apr 2001 22:16:38 +0000 Subject: [PATCH] Do it more thoroughly. --- misc/fix_vars.php | 147 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 138 insertions(+), 9 deletions(-) diff --git a/misc/fix_vars.php b/misc/fix_vars.php index 231ea90f..ce138799 100644 --- a/misc/fix_vars.php +++ b/misc/fix_vars.php @@ -7,10 +7,20 @@ * referencing deeply nested structures of arbitrary complexity. */ +/* + * Set these to match your template delimeters. + */ +$left_delimiter = '{'; +$right_delimiter = '}'; + + if ($argc < 2) { die("\nUsage: php -q fix_vars.php \n\n"); } +$ldq = preg_quote($left_delimiter, '!'); +$rdq = preg_quote($right_delimiter, '!'); + $qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\''; $pwd = $HTTP_ENV_VARS['PWD']; @@ -24,23 +34,111 @@ foreach (array_slice($argv, 1) as $template) { die("\nError: could not open $template.out for writing\n\n"); } - if (function_exists("preg_replace_callback")) - $output = preg_replace_callback('!\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!s', 'fix_var', $input); - else - $output = preg_replace('!\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!mF', 'fix_var', $input); + /* Gather all template tags. */ + preg_match_all("!({$ldq}\s*)(.*?)(\s*{$rdq})!s", $input, $match); + $template_tags = $match[2]; + $template_pre_tags = $match[1]; + $template_post_tags = $match[3]; + /* Split content by template tags to obtain non-template content. */ + $text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $input); + $fixed_tags = array(); + for ($i = 0; $i < count($template_tags); $i++) { + $fixed_tags[] = fix_tag($template_tags[$i]); + } + + $output = ''; + /* Interleave the compiled contents and text blocks to get the final result. */ + for ($i = 0; $i < count($fixed_tags); $i++) { + $output .= $text_blocks[$i].$template_pre_tags[$i].$fixed_tags[$i].$template_post_tags[$i]; + } + $output .= $text_blocks[$i]; + fwrite($fp, $output); fclose($fp); - copy($template.'.out', $template); - unlink($template.'.out'); + //copy($template.'.out', $template); + //unlink($template.'.out'); print "Fixed $template.\n"; } -function fix_var($match) +function fix_tag($template_tag) { - $var_expr = $match[0]; + global $qstr_regexp; + if ($template_tag{0} == '*' && $template_tag{strlen($template_tag)-1} == '*') + return $template_tag; + + /* Split tag into two parts: command and the arguments. */ + preg_match('/^( + (?: ' . $qstr_regexp . ' | (?>[^"\'\s]+))+ + ) + (\s+(.*))? + /xs', $template_tag, $match); + list(, $tag_command, $tag_args) = $match; + if (preg_match('!^\$\w+(?>(\[\w+(\.\w+)?\])|(\.\w+))*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) + $tag_command = fix_var($tag_command); + else if (preg_match('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) + $tag_command = fix_other_var($tag_command); + else if (preg_match('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) + $tag_command = fix_other_var($tag_command); + + if (function_exists("preg_replace_callback")) { + $tag_args = preg_replace_callback('!(?<=[\s(:=])\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!', 'fix_var_match', $tag_args); + $tag_args = preg_replace_callback('!(?<=[\s(:=])#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!', 'fix_other_var_match', $tag_args); + $tag_args = preg_replace_callback('!(?<=[\s(:=])%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!', 'fix_other_var_match', $tag_args); + } else { + $tag_args = preg_replace('!(?<=[\s(:=])\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!F', 'fix_var_match', $tag_args); + $tag_args = preg_replace('!(?<=[\s(:=])#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!F', 'fix_other_var_match', $tag_args); + $tag_args = preg_replace('!(?<=[\s(:=])%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!F', 'fix_other_var_match', $tag_args); + } + + return $tag_command.$tag_args; +} + +function fix_vars_props($tokens) +{ + global $qstr_regexp; + + $var_exprs = preg_grep('!^\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens); + $conf_var_exprs = preg_grep('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens); + $sect_prop_exprs = preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens); + + if (count($var_exprs)) { + foreach ($var_exprs as $expr_index => $var_expr) { + $tokens[$expr_index] = fix_var($var_expr); + } + } + + /* + if (count($conf_var_exprs)) { + foreach ($conf_var_exprs as $expr_index => $var_expr) { + $tokens[$expr_index] = $this->_parse_conf_var($var_expr); + } + } + + if (count($sect_prop_exprs)) { + foreach ($sect_prop_exprs as $expr_index => $section_prop_expr) { + $tokens[$expr_index] = $this->_parse_section_prop($section_prop_expr); + } + } + */ + + return $tokens; +} + +function fix_var_match($match) +{ + return fix_var($match[0]); +} + +function fix_other_var_match($match) +{ + return fix_other_var($match[0]); +} + +function fix_var($var_expr) +{ list($var_ref, $modifiers) = explode('|', substr($var_expr, 1), 2); $sections = explode('/', $var_ref); @@ -56,7 +154,38 @@ function fix_var($match) $output .= ".".implode('.', $props); if ($modifiers) - $output .= '|' . $modifiers; + $output .= fix_modifiers($modifiers); + + return $output; +} + +function fix_other_var($var_expr) +{ + list($var_ref, $modifiers) = explode('|', $var_expr, 2); + + $output = $var_ref; + if ($modifiers) + $output .= fix_modifiers($modifiers); + + return $output; +} + +function fix_modifiers($modifier_string) +{ + global $qstr_regexp; + + preg_match_all('!\|(@?\w+)((?>:(?:'. $qstr_regexp . '|[^|]+))*)!', '|' . $modifier_string, $match); + list(, $modifiers, $modifier_arg_strings) = $match; + $output = ''; + for ($i = 0; $i < count($modifiers); $i++) { + $modifier_name = $modifiers[$i]; + preg_match_all('!:(' . $qstr_regexp . '|[^:]+)!', $modifier_arg_strings[$i], $match); + $modifier_args = fix_vars_props($match[1]); + + $output .= '|' . $modifier_name; + if ($modifier_args) + $output .= ':'.implode(':', $modifier_args); + } return $output; }