includes/render.php
changeset 1054 e6b14d33ac55
parent 1044 ad6a22377507
child 1066 bead71f28f63
equal deleted inserted replaced
1053:bdbb49cf6f1b 1054:e6b14d33ac55
   715   {
   715   {
   716     $parms = array();
   716     $parms = array();
   717     $input = trim($input);
   717     $input = trim($input);
   718     if ( $newlinemode )
   718     if ( $newlinemode )
   719     {
   719     {
       
   720       // we're going by newlines.
       
   721       // split by parameter, then parse each one individually
       
   722       $input = explode("\n", str_replace("\r", '', $input));
       
   723       $lastparam = '';
       
   724       $i = 0;
       
   725       foreach ( $input as $line )
       
   726       {
       
   727         if ( preg_match('/^ *\|? *([A-z0-9_]+) *= *(.+)$/', $line, $match) )
       
   728         {
       
   729           // new parameter, named
       
   730           $parms[ $match[1] ] = $match[2];
       
   731           $lastparam = $match[1];
       
   732         }
       
   733         else if ( preg_match('/^ *\| *(.+)$/', $line, $match) || $lastparam === '' )
       
   734         {
       
   735           $parms[ $i ] = $match[1];
       
   736           $lastparam = $i;
       
   737           $i++;
       
   738         }
       
   739         else
       
   740         {
       
   741           $parms[ $lastparam ] .= "\n$line";
       
   742         }
       
   743       }
       
   744     }
       
   745     else
       
   746     {
   720       $result = preg_match_all('/
   747       $result = preg_match_all('/
   721                                   (?:^|[\s]*)\|?    # start of parameter - string start or series of spaces
   748                                  (?:^|[ ]*)\|         # start of parameter - string start or series of spaces
   722                                   [ ]*              
   749                                  [ ]*
   723                                   (?:               
   750                                  (?:
   724                                     ([A-z0-9_]+)    # variable name
   751                                    ([A-z0-9_]+)       # variable name
   725                                     [ ]* = [ ]*     # assignment
   752                                    [ ]* = [ ]*        # assignment
   726                                   )?                # this is optional - if the parameter name is not given, a numerical index is assigned
   753                                  )?                   # name section is optional - if the parameter name is not given, a numerical index is assigned
   727                                   (.+)              # value
   754                                  ([^\|]+|.+?\n[ ]*\|) # value
   728                                 /x', trim($input), $matches);
   755                                /x', trim($input), $matches);
   729     }
   756       if ( $result )
   730     else
   757       {
   731     {
   758         $pi = 0;
   732       $result = preg_match_all('/
   759         for ( $i = 0; $i < count($matches[0]); $i++ )
   733                                   (?:^|[ ]*)\|         # start of parameter - string start or series of spaces
   760         {
   734                                   [ ]*
   761           $matches[1][$i] = trim($matches[1][$i]);
   735                                   (?:
   762           $parmname = !empty($matches[1][$i]) ? $matches[1][$i] : strval(++$pi);
   736                                     ([A-z0-9_]+)       # variable name
   763           $parms[ $parmname ] = $matches[2][$i];
   737                                     [ ]* = [ ]*        # assignment
   764         }
   738                                   )?                   # name section is optional - if the parameter name is not given, a numerical index is assigned
   765       }
   739                                   ([^\|]+|.+?\n[ ]*\|) # value
   766     }
   740                                 /x', trim($input), $matches);
   767     // die('<pre>' . print_r($parms, true) . '</pre>');
   741     }                   
       
   742     if ( $result )
       
   743     {
       
   744       $pi = 0;
       
   745       for ( $i = 0; $i < count($matches[0]); $i++ )
       
   746       {
       
   747         $matches[1][$i] = trim($matches[1][$i]);
       
   748         $parmname = !empty($matches[1][$i]) ? $matches[1][$i] : strval(++$pi);
       
   749         $parms[ $parmname ] = $matches[2][$i];
       
   750       }
       
   751     }
       
   752     return $parms;
   768     return $parms;
   753   }
   769   }
   754   
   770   
   755   /**
   771   /**
   756    * Processes all template tags within a block of wikitext.
   772    * Processes all template tags within a block of wikitext.
   780                            ((?:(?:[\s]+\|?)[ ]*(?:[A-z0-9_]+)[ ]*=[ ]*?(?:.+))*) # parameters
   796                            ((?:(?:[\s]+\|?)[ ]*(?:[A-z0-9_]+)[ ]*=[ ]*?(?:.+))*) # parameters
   781                          \}\}                     # closing
   797                          \}\}                     # closing
   782                        /isxU";
   798                        /isxU";
   783     if ( $count = preg_match_all($template_regex, $text, $matches) )
   799     if ( $count = preg_match_all($template_regex, $text, $matches) )
   784     {
   800     {
   785       //die('<pre>' . print_r($matches, true) . '</pre>');
   801       // die('<pre>' . print_r($matches, true) . '</pre>');
   786       for ( $i = 0; $i < $count; $i++ )
   802       for ( $i = 0; $i < $count; $i++ )
   787       {
   803       {
   788         $matches[1][$i] = sanitize_page_id($matches[1][$i]);
   804         $matches[1][$i] = sanitize_page_id($matches[1][$i]);
   789         $newlinemode = ( substr($matches[2][$i], 0, 1) == "\n" );
   805         $newlinemode = ( substr($matches[2][$i], 0, 1) == "\n" );
   790         $parmsection = trim($matches[2][$i]);
   806         $parmsection = trim($matches[2][$i]);
   799         {
   815         {
   800           $parms = Array();
   816           $parms = Array();
   801         }
   817         }
   802         if ( $tpl_code = RenderMan::fetch_template_text($matches[1][$i]) )
   818         if ( $tpl_code = RenderMan::fetch_template_text($matches[1][$i]) )
   803         {
   819         {
       
   820           // Intelligent paragraphs.
       
   821           // If:
       
   822           //   * A line is fully wrapped in a <p> tag
       
   823           //   * The line contains a variable
       
   824           //   * The variable contains newlines
       
   825           // Then:
       
   826           //   * Drop the <p> tag, replace it fully paragraph-ized by newlines
       
   827           
       
   828           if ( preg_match_all('/^( *)<p(\s.+)?>(.*?\{([A-z0-9]+)\}.*?)<\/p> *$/m', $tpl_code, $paramatch) )
       
   829           {
       
   830             $parser = new Carpenter();
       
   831             $parser->exclusive_rule('paragraph');
       
   832             
       
   833             foreach ( $paramatch[0] as $j => $match )
       
   834             {
       
   835               // $line is trimmed (the <p> is gone)
       
   836               $spacing =& $paramatch[1][$i];
       
   837               $para_attrs =& $paramatch[2][$j];
       
   838               $para_attrs = str_replace(array('$', '\\'), array('\$', '\\\\'), $para_attrs);
       
   839               $line =& $paramatch[3][$j];
       
   840               $varname =& $paramatch[4][$j];
       
   841               if ( isset($parms[$varname]) && strstr($parms[$varname], "\n") )
       
   842               {
       
   843                 $newline = str_replace('{' . $varname . '}', $parms[$varname], $line);
       
   844                 $paraized = $parser->render($newline);
       
   845                 $paraized = preg_replace('/^<p>/m', "$spacing<p{$para_attrs}>", $paraized);
       
   846                 $paraized = $spacing . trim($paraized);
       
   847                 $tpl_code = str_replace_once($match, $paraized, $tpl_code);
       
   848               }
       
   849             }
       
   850           }
       
   851           
   804           $parser = $template->makeParserText($tpl_code);
   852           $parser = $template->makeParserText($tpl_code);
   805           $parser->assign_vars($parms);
   853           $parser->assign_vars($parms);
   806           $text = str_replace($matches[0][$i], $parser->run(), $text);
   854           $text = str_replace($matches[0][$i], $parser->run(), $text);
   807         }
   855         }
   808       }
   856       }