# HG changeset patch # User Dan # Date 1247358483 14400 # Node ID e6b14d33ac556c604f6c9d1a330c56ad3a9816f5 # Parent bdbb49cf6f1b0834bfdc916921988f7edd6d852c Renderer: added "smart paragraphs" for templates.

Foo {bar}

where bar is multiline is basically turned into proper XHTML paragraphs. diff -r bdbb49cf6f1b -r e6b14d33ac55 includes/render.php --- a/includes/render.php Sat Jul 11 20:25:50 2009 -0400 +++ b/includes/render.php Sat Jul 11 20:28:03 2009 -0400 @@ -717,38 +717,54 @@ $input = trim($input); if ( $newlinemode ) { - $result = preg_match_all('/ - (?:^|[\s]*)\|? # start of parameter - string start or series of spaces - [ ]* - (?: - ([A-z0-9_]+) # variable name - [ ]* = [ ]* # assignment - )? # this is optional - if the parameter name is not given, a numerical index is assigned - (.+) # value - /x', trim($input), $matches); + // we're going by newlines. + // split by parameter, then parse each one individually + $input = explode("\n", str_replace("\r", '', $input)); + $lastparam = ''; + $i = 0; + foreach ( $input as $line ) + { + if ( preg_match('/^ *\|? *([A-z0-9_]+) *= *(.+)$/', $line, $match) ) + { + // new parameter, named + $parms[ $match[1] ] = $match[2]; + $lastparam = $match[1]; + } + else if ( preg_match('/^ *\| *(.+)$/', $line, $match) || $lastparam === '' ) + { + $parms[ $i ] = $match[1]; + $lastparam = $i; + $i++; + } + else + { + $parms[ $lastparam ] .= "\n$line"; + } + } } else { $result = preg_match_all('/ - (?:^|[ ]*)\| # start of parameter - string start or series of spaces - [ ]* - (?: - ([A-z0-9_]+) # variable name - [ ]* = [ ]* # assignment - )? # name section is optional - if the parameter name is not given, a numerical index is assigned - ([^\|]+|.+?\n[ ]*\|) # value - /x', trim($input), $matches); - } - if ( $result ) - { - $pi = 0; - for ( $i = 0; $i < count($matches[0]); $i++ ) + (?:^|[ ]*)\| # start of parameter - string start or series of spaces + [ ]* + (?: + ([A-z0-9_]+) # variable name + [ ]* = [ ]* # assignment + )? # name section is optional - if the parameter name is not given, a numerical index is assigned + ([^\|]+|.+?\n[ ]*\|) # value + /x', trim($input), $matches); + if ( $result ) { - $matches[1][$i] = trim($matches[1][$i]); - $parmname = !empty($matches[1][$i]) ? $matches[1][$i] : strval(++$pi); - $parms[ $parmname ] = $matches[2][$i]; + $pi = 0; + for ( $i = 0; $i < count($matches[0]); $i++ ) + { + $matches[1][$i] = trim($matches[1][$i]); + $parmname = !empty($matches[1][$i]) ? $matches[1][$i] : strval(++$pi); + $parms[ $parmname ] = $matches[2][$i]; + } } } + // die('
' . print_r($parms, true) . '
'); return $parms; } @@ -782,7 +798,7 @@ /isxU"; if ( $count = preg_match_all($template_regex, $text, $matches) ) { - //die('
' . print_r($matches, true) . '
'); + // die('
' . print_r($matches, true) . '
'); for ( $i = 0; $i < $count; $i++ ) { $matches[1][$i] = sanitize_page_id($matches[1][$i]); @@ -801,6 +817,38 @@ } if ( $tpl_code = RenderMan::fetch_template_text($matches[1][$i]) ) { + // Intelligent paragraphs. + // If: + // * A line is fully wrapped in a

tag + // * The line contains a variable + // * The variable contains newlines + // Then: + // * Drop the

tag, replace it fully paragraph-ized by newlines + + if ( preg_match_all('/^( *)(.*?\{([A-z0-9]+)\}.*?)<\/p> *$/m', $tpl_code, $paramatch) ) + { + $parser = new Carpenter(); + $parser->exclusive_rule('paragraph'); + + foreach ( $paramatch[0] as $j => $match ) + { + // $line is trimmed (the

is gone) + $spacing =& $paramatch[1][$i]; + $para_attrs =& $paramatch[2][$j]; + $para_attrs = str_replace(array('$', '\\'), array('\$', '\\\\'), $para_attrs); + $line =& $paramatch[3][$j]; + $varname =& $paramatch[4][$j]; + if ( isset($parms[$varname]) && strstr($parms[$varname], "\n") ) + { + $newline = str_replace('{' . $varname . '}', $parms[$varname], $line); + $paraized = $parser->render($newline); + $paraized = preg_replace('/^

/m', "$spacing", $paraized); + $paraized = $spacing . trim($paraized); + $tpl_code = str_replace_once($match, $paraized, $tpl_code); + } + } + } + $parser = $template->makeParserText($tpl_code); $parser->assign_vars($parms); $text = str_replace($matches[0][$i], $parser->run(), $text); diff -r bdbb49cf6f1b -r e6b14d33ac55 includes/wikiengine/parse_mediawiki.php --- a/includes/wikiengine/parse_mediawiki.php Sat Jul 11 20:25:50 2009 -0400 +++ b/includes/wikiengine/parse_mediawiki.php Sat Jul 11 20:28:03 2009 -0400 @@ -46,7 +46,7 @@ { $template_regex = "/\{\{(.+)((\n|\|[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU"; $i = 0; - while ( preg_match($template_regex, $text) ) + while ( preg_match($template_regex, $text, $match) ) { $i++; if ( $i == 5 ) diff -r bdbb49cf6f1b -r e6b14d33ac55 includes/wikiformat.php --- a/includes/wikiformat.php Sat Jul 11 20:25:50 2009 -0400 +++ b/includes/wikiformat.php Sat Jul 11 20:28:03 2009 -0400 @@ -310,6 +310,20 @@ } /** + * Make a rule exclusive (the only one called) + * @param string stage + * @return null + */ + + public function exclusive_rule($rule) + { + if ( is_string($rule) ) + $this->rules = array($rule); + + return null; + } + + /** * Generate a token * @param int Token index * @return string