includes/render.php
changeset 174 4c5c2b66a34d
parent 163 ad00dc1f8706
child 178 4c19952406db
child 189 fd0e9c7a7b28
--- a/includes/render.php	Sat Oct 06 20:47:09 2007 -0400
+++ b/includes/render.php	Sun Oct 07 08:39:40 2007 -0400
@@ -270,7 +270,8 @@
       }
     }
     
-    $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
+    //$template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
+    $template_regex = "/\{\{(.+)((\n|\|[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
     $i = 0;
     while ( preg_match($template_regex, $text) )
     {
@@ -498,46 +499,6 @@
     return $text;
   }
   
-  /* *
-   * Replaces template inclusions with the templates
-   * @param string $message The text to format
-   * @return string
-   * /
-   
-  function old_include_templates($message)
-  {
-    $random_id = md5( time() . mt_rand() );
-    preg_match_all('#\{\{(.+?)\}\}#s', $message, $matchlist);
-    foreach($matchlist[1] as $m)
-    {
-      $mn = $m;
-      // Strip out wikilinks and re-add them after the explosion (because of the "|")
-      preg_match_all('#\[\[(.+?)\]\]#i', $m, $linklist);
-      //echo '<pre>'.print_r($linklist, true).'</pre>';
-      for($i=0;$i<sizeof($linklist[1]);$i++)
-      {
-        $mn = str_replace('[['.$linklist[1][$i].']]', '{WIKILINK:'.$random_id.':'.$i.'}', $mn);
-      }
-      
-      $ar = explode('|', $mn);
-      
-      for($j=0;$j<sizeof($ar);$j++)
-      {
-        for($i=0;$i<sizeof($linklist[1]);$i++)
-        {
-          $ar[$j] = str_replace('{WIKILINK:'.$random_id.':'.$i.'}', '[['.$linklist[1][$i].']]', $ar[$j]);
-        }
-      }
-      
-      $tp = $ar[0];
-      unset($ar[0]);
-      $tp = str_replace(' ', '_', $tp);
-      $message = str_replace('{{'.$m.'}}', RenderMan::getTemplate($tp, $ar), $message);
-    }
-    return $message;
-  }
-  */
-  
   /**
    * Parses a partial template tag in wikitext, and return an array with the parameters.
    * @param string The portion of the template tag that contains the parameters.
@@ -553,16 +514,26 @@
   
   function parse_template_vars($input)
   {
-    $input = explode("\n", trim( $input ));
+    if ( !preg_match('/^(\|[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?))*$/is', trim($input)) )
+    {
+      $using_pipes = false;
+      $input = explode("\n", trim( $input ));
+    }
+    else
+    {
+      $using_pipes = true;
+      $input = substr($input, 1);
+      $input = explode("|", trim( $input ));
+    }
     $parms = Array();
     $current_line = '';
     $current_parm = '';
     foreach ( $input as $num => $line )
     {
-      if ( preg_match('/^([ ]*?)([A-z0-9_]+?)([ ]*?)=([ ]*?)(.+?)$/i', $line, $matches) )
+      if ( preg_match('/^[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?)$/is', $line, $matches) )
       {
-        $parm =& $matches[2];
-        $text =& $matches[5];
+        $parm =& $matches[1];
+        $text =& $matches[4];
         if ( $parm == $current_parm )
         {
           $current_line .= $text;
@@ -595,6 +566,7 @@
   
   /**
    * Processes all template tags within a block of wikitext.
+   * Updated in 1.0.2 to also parse template tags in the format of {{Foo |a = b |b = c |c = therefore, a}}
    * @param string The text to process
    * @return string Formatted text
    * @example
@@ -603,16 +575,18 @@
      parm1 = Foo
      parm2 = Bar
      }}';
-   $text = include_templates($text);
+   $text = RenderMan::include_templates($text);
    * </code>
    */
   
   function include_templates($text)
   {
     global $db, $session, $paths, $template, $plugins; // Common objects
-    $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
+    // $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
+    $template_regex = "/\{\{(.+)(((\n|[ ]*\|)[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
     if ( $count = preg_match_all($template_regex, $text, $matches) )
     {
+      //die('<pre>' . print_r($matches, true) . '</pre>');
       for ( $i = 0; $i < $count; $i++ )
       {
         $matches[1][$i] = sanitize_page_id($matches[1][$i]);
@@ -620,10 +594,9 @@
         if ( !empty($parmsection) )
         {
           $parms = RenderMan::parse_template_vars($parmsection);
-          foreach ( $parms as $j => $parm )
-          {
-            $parms[$j] = $parm;
-          }
+          if ( !is_array($parms) )
+            // Syntax error
+            $parms = array();
         }
         else
         {