includes/render.php
changeset 174 4c5c2b66a34d
parent 163 ad00dc1f8706
child 178 4c19952406db
child 189 fd0e9c7a7b28
equal deleted inserted replaced
170:250aeb408ed7 174:4c5c2b66a34d
   268       {
   268       {
   269         $text = str_replace('(_'.$m.'_)', $paths->getParam((int)$m), $text);
   269         $text = str_replace('(_'.$m.'_)', $paths->getParam((int)$m), $text);
   270       }
   270       }
   271     }
   271     }
   272     
   272     
   273     $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   273     //$template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
       
   274     $template_regex = "/\{\{(.+)((\n|\|[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
   274     $i = 0;
   275     $i = 0;
   275     while ( preg_match($template_regex, $text) )
   276     while ( preg_match($template_regex, $text) )
   276     {
   277     {
   277       $i++;
   278       $i++;
   278       if ( $i == 5 )
   279       if ( $i == 5 )
   496     }
   497     }
   497     
   498     
   498     return $text;
   499     return $text;
   499   }
   500   }
   500   
   501   
   501   /* *
       
   502    * Replaces template inclusions with the templates
       
   503    * @param string $message The text to format
       
   504    * @return string
       
   505    * /
       
   506    
       
   507   function old_include_templates($message)
       
   508   {
       
   509     $random_id = md5( time() . mt_rand() );
       
   510     preg_match_all('#\{\{(.+?)\}\}#s', $message, $matchlist);
       
   511     foreach($matchlist[1] as $m)
       
   512     {
       
   513       $mn = $m;
       
   514       // Strip out wikilinks and re-add them after the explosion (because of the "|")
       
   515       preg_match_all('#\[\[(.+?)\]\]#i', $m, $linklist);
       
   516       //echo '<pre>'.print_r($linklist, true).'</pre>';
       
   517       for($i=0;$i<sizeof($linklist[1]);$i++)
       
   518       {
       
   519         $mn = str_replace('[['.$linklist[1][$i].']]', '{WIKILINK:'.$random_id.':'.$i.'}', $mn);
       
   520       }
       
   521       
       
   522       $ar = explode('|', $mn);
       
   523       
       
   524       for($j=0;$j<sizeof($ar);$j++)
       
   525       {
       
   526         for($i=0;$i<sizeof($linklist[1]);$i++)
       
   527         {
       
   528           $ar[$j] = str_replace('{WIKILINK:'.$random_id.':'.$i.'}', '[['.$linklist[1][$i].']]', $ar[$j]);
       
   529         }
       
   530       }
       
   531       
       
   532       $tp = $ar[0];
       
   533       unset($ar[0]);
       
   534       $tp = str_replace(' ', '_', $tp);
       
   535       $message = str_replace('{{'.$m.'}}', RenderMan::getTemplate($tp, $ar), $message);
       
   536     }
       
   537     return $message;
       
   538   }
       
   539   */
       
   540   
       
   541   /**
   502   /**
   542    * Parses a partial template tag in wikitext, and return an array with the parameters.
   503    * Parses a partial template tag in wikitext, and return an array with the parameters.
   543    * @param string The portion of the template tag that contains the parameters.
   504    * @param string The portion of the template tag that contains the parameters.
   544    * @example
   505    * @example
   545    * <code>
   506    * <code>
   551    * [bar] => dolor sit amet
   512    * [bar] => dolor sit amet
   552    */
   513    */
   553   
   514   
   554   function parse_template_vars($input)
   515   function parse_template_vars($input)
   555   {
   516   {
   556     $input = explode("\n", trim( $input ));
   517     if ( !preg_match('/^(\|[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?))*$/is', trim($input)) )
       
   518     {
       
   519       $using_pipes = false;
       
   520       $input = explode("\n", trim( $input ));
       
   521     }
       
   522     else
       
   523     {
       
   524       $using_pipes = true;
       
   525       $input = substr($input, 1);
       
   526       $input = explode("|", trim( $input ));
       
   527     }
   557     $parms = Array();
   528     $parms = Array();
   558     $current_line = '';
   529     $current_line = '';
   559     $current_parm = '';
   530     $current_parm = '';
   560     foreach ( $input as $num => $line )
   531     foreach ( $input as $num => $line )
   561     {
   532     {
   562       if ( preg_match('/^([ ]*?)([A-z0-9_]+?)([ ]*?)=([ ]*?)(.+?)$/i', $line, $matches) )
   533       if ( preg_match('/^[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?)$/is', $line, $matches) )
   563       {
   534       {
   564         $parm =& $matches[2];
   535         $parm =& $matches[1];
   565         $text =& $matches[5];
   536         $text =& $matches[4];
   566         if ( $parm == $current_parm )
   537         if ( $parm == $current_parm )
   567         {
   538         {
   568           $current_line .= $text;
   539           $current_line .= $text;
   569         }
   540         }
   570         else
   541         else
   593     return $parms;
   564     return $parms;
   594   }
   565   }
   595   
   566   
   596   /**
   567   /**
   597    * Processes all template tags within a block of wikitext.
   568    * Processes all template tags within a block of wikitext.
       
   569    * Updated in 1.0.2 to also parse template tags in the format of {{Foo |a = b |b = c |c = therefore, a}}
   598    * @param string The text to process
   570    * @param string The text to process
   599    * @return string Formatted text
   571    * @return string Formatted text
   600    * @example
   572    * @example
   601    * <code>
   573    * <code>
   602    $text = '{{Template
   574    $text = '{{Template
   603      parm1 = Foo
   575      parm1 = Foo
   604      parm2 = Bar
   576      parm2 = Bar
   605      }}';
   577      }}';
   606    $text = include_templates($text);
   578    $text = RenderMan::include_templates($text);
   607    * </code>
   579    * </code>
   608    */
   580    */
   609   
   581   
   610   function include_templates($text)
   582   function include_templates($text)
   611   {
   583   {
   612     global $db, $session, $paths, $template, $plugins; // Common objects
   584     global $db, $session, $paths, $template, $plugins; // Common objects
   613     $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   585     // $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
       
   586     $template_regex = "/\{\{(.+)(((\n|[ ]*\|)[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
   614     if ( $count = preg_match_all($template_regex, $text, $matches) )
   587     if ( $count = preg_match_all($template_regex, $text, $matches) )
   615     {
   588     {
       
   589       //die('<pre>' . print_r($matches, true) . '</pre>');
   616       for ( $i = 0; $i < $count; $i++ )
   590       for ( $i = 0; $i < $count; $i++ )
   617       {
   591       {
   618         $matches[1][$i] = sanitize_page_id($matches[1][$i]);
   592         $matches[1][$i] = sanitize_page_id($matches[1][$i]);
   619         $parmsection = trim($matches[2][$i]);
   593         $parmsection = trim($matches[2][$i]);
   620         if ( !empty($parmsection) )
   594         if ( !empty($parmsection) )
   621         {
   595         {
   622           $parms = RenderMan::parse_template_vars($parmsection);
   596           $parms = RenderMan::parse_template_vars($parmsection);
   623           foreach ( $parms as $j => $parm )
   597           if ( !is_array($parms) )
   624           {
   598             // Syntax error
   625             $parms[$j] = $parm;
   599             $parms = array();
   626           }
       
   627         }
   600         }
   628         else
   601         else
   629         {
   602         {
   630           $parms = Array();
   603           $parms = Array();
   631         }
   604         }