includes/render.php
changeset 229 97ae8e9d5e29
parent 228 b0a4d179be85
child 313 854eecfada20
equal deleted inserted replaced
147:d8156d18ac58 229:97ae8e9d5e29
   246     if ( $paths->namespace == 'Template' )
   246     if ( $paths->namespace == 'Template' )
   247     {
   247     {
   248       $text = preg_replace('/<nodisplay>(.*?)<\/nodisplay>/is', '', $text);
   248       $text = preg_replace('/<nodisplay>(.*?)<\/nodisplay>/is', '', $text);
   249     }
   249     }
   250     
   250     
       
   251     $code = $plugins->setHook('render_wikiformat_pre');
       
   252     foreach ( $code as $cmd )
       
   253     {
       
   254       eval($cmd);
       
   255     }
       
   256     
   251     if ( !$plaintext )
   257     if ( !$plaintext )
   252     {
   258     {
   253       // Process images
   259       // Process images
   254       $text = RenderMan::process_image_tags($text, $taglist);
   260       $text = RenderMan::process_image_tags($text, $taglist);
   255       $text = RenderMan::process_imgtags_stage2($text, $taglist);
   261       $text = RenderMan::process_imgtags_stage2($text, $taglist);
   262       {
   268       {
   263         $text = str_replace('(_'.$m.'_)', $paths->getParam((int)$m), $text);
   269         $text = str_replace('(_'.$m.'_)', $paths->getParam((int)$m), $text);
   264       }
   270       }
   265     }
   271     }
   266     
   272     
   267     $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   273     //$template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
       
   274     $template_regex = "/\{\{(.+)((\n|\|[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
   268     $i = 0;
   275     $i = 0;
   269     while ( preg_match($template_regex, $text) )
   276     while ( preg_match($template_regex, $text) )
   270     {
   277     {
   271       $i++;
   278       $i++;
   272       if ( $i == 5 )
   279       if ( $i == 5 )
   288       $wiki->setRenderConf('Xhtml', 'wikilink', 'view_url', contentPath);
   295       $wiki->setRenderConf('Xhtml', 'wikilink', 'view_url', contentPath);
   289       $wiki->setRenderConf('Xhtml', 'Url', 'css_descr', 'external');
   296       $wiki->setRenderConf('Xhtml', 'Url', 'css_descr', 'external');
   290       $result = $wiki->transform($text, 'Xhtml');
   297       $result = $wiki->transform($text, 'Xhtml');
   291     }
   298     }
   292     
   299     
   293     // if ( !$plaintext )
   300     // HTML fixes
   294     // {
   301     $result = preg_replace('#<tr>([\s]*?)<\/tr>#is', '', $result);
   295     //   $result = RenderMan::process_imgtags_stage2($result, $taglist);
   302     $result = preg_replace('#<p>([\s]*?)<\/p>#is', '', $result);
   296     // }
   303     $result = preg_replace('#<br />([\s]*?)<table#is', '<table', $result);
       
   304     $result = str_replace("<pre><code>\n", "<pre><code>", $result);
       
   305     $result = preg_replace("/<p><table([^>]*?)><\/p>/", "<table\\1>", $result);
       
   306     $result = str_replace("<br />\n</td>", "\n</td>", $result);
       
   307     $result = str_replace("<p><tr>", "<tr>", $result);
       
   308     $result = str_replace("<tr><br />", "<tr>", $result);
       
   309     $result = str_replace("</tr><br />", "</tr>", $result);
       
   310     $result = str_replace("</table><br />", "</table>", $result);
       
   311     $result = preg_replace('/<\/table>$/', "</table><br /><br />", $result);
       
   312     $result = str_replace("<p></div></p>", "</div>", $result);
       
   313     $result = str_replace("<p></table></p>", "</table>", $result);
       
   314     
       
   315     $code = $plugins->setHook('render_wikiformat_post');
       
   316     foreach ( $code as $cmd )
       
   317     {
       
   318       eval($cmd);
       
   319     }
   297     
   320     
   298     // Reinsert <nowiki> sections
   321     // Reinsert <nowiki> sections
   299     for($i=0;$i<$nw;$i++)
   322     for($i=0;$i<$nw;$i++)
   300     {
   323     {
   301       $result = str_replace('{NOWIKI:'.$random_id.':'.$i.'}', $nowiki[1][$i], $result);
   324       $result = str_replace('{NOWIKI:'.$random_id.':'.$i.'}', $nowiki[1][$i], $result);
   309     
   332     
   310     return $result;
   333     return $result;
   311     
   334     
   312   }
   335   }
   313   
   336   
   314   function wikiFormat($message, $filter_links = true, $do_params = false, $plaintext = false) {
   337   function wikiFormat($message, $filter_links = true, $do_params = false, $plaintext = false)
       
   338   {
   315     global $db, $session, $paths, $template, $plugins; // Common objects
   339     global $db, $session, $paths, $template, $plugins; // Common objects
   316     
   340     
   317     return RenderMan::next_gen_wiki_format($message, $plaintext, $filter_links, $do_params);
   341     return RenderMan::next_gen_wiki_format($message, $plaintext, $filter_links, $do_params);
   318     
   342     
   319     $random_id = md5( time() . mt_rand() );
   343     $random_id = md5( time() . mt_rand() );
   382     $result = str_replace("<tr><br />", "<tr>", $result);
   406     $result = str_replace("<tr><br />", "<tr>", $result);
   383     $result = str_replace("</tr><br />", "</tr>", $result);
   407     $result = str_replace("</tr><br />", "</tr>", $result);
   384     $result = str_replace("</table></p>", "</table>", $result);
   408     $result = str_replace("</table></p>", "</table>", $result);
   385     $result = str_replace("</table><br />", "</table>", $result);
   409     $result = str_replace("</table><br />", "</table>", $result);
   386     $result = preg_replace('/<\/table>$/', "</table><br /><br />", $result);
   410     $result = preg_replace('/<\/table>$/', "</table><br /><br />", $result);
       
   411     $result = str_replace("<p></div></p>", "</div>", $result);
       
   412     $result = str_replace("<p></table></p>", "</table>", $result);
   387     
   413     
   388     $result = str_replace('<nowiki>',  '&lt;nowiki&gt;',  $result);
   414     $result = str_replace('<nowiki>',  '&lt;nowiki&gt;',  $result);
   389     $result = str_replace('</nowiki>', '&lt;/nowiki&gt;', $result);
   415     $result = str_replace('</nowiki>', '&lt;/nowiki&gt;', $result);
   390     
   416     
   391     return $result;
   417     return $result;
   458     foreach ( $matches[0] as $i => $match )
   484     foreach ( $matches[0] as $i => $match )
   459     {
   485     {
   460       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   486       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   461       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
   487       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
   462       
   488       
   463       $url = makeUrl($matches[1][$i], false, true);
   489       $url = makeUrl($pid_clean, false, true);
   464       $inner_text = htmlspecialchars(get_page_title($pid_clean));
   490       $inner_text = ( isPage($pid_clean) ) ? htmlspecialchars(get_page_title($pid_clean)) : htmlspecialchars($matches[1][$i]);
   465       $quot = '"';
   491       $quot = '"';
   466       $exists = ( isPage($pid_clean) ) ? '' : ' class="wikilink-nonexistent"';
   492       $exists = ( isPage($pid_clean) ) ? '' : ' class="wikilink-nonexistent"';
   467       
   493       
   468       $link = "<a href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a>";
   494       $link = "<a href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a>";
   469       
   495       
   470       $text = str_replace($match, $link, $text);
   496       $text = str_replace($match, $link, $text);
   471     }
   497     }
   472     
   498     
   473     return $text;
   499     return $text;
   474   }
   500   }
   475   
       
   476   /* *
       
   477    * Replaces template inclusions with the templates
       
   478    * @param string $message The text to format
       
   479    * @return string
       
   480    * /
       
   481    
       
   482   function old_include_templates($message)
       
   483   {
       
   484     $random_id = md5( time() . mt_rand() );
       
   485     preg_match_all('#\{\{(.+?)\}\}#s', $message, $matchlist);
       
   486     foreach($matchlist[1] as $m)
       
   487     {
       
   488       $mn = $m;
       
   489       // Strip out wikilinks and re-add them after the explosion (because of the "|")
       
   490       preg_match_all('#\[\[(.+?)\]\]#i', $m, $linklist);
       
   491       //echo '<pre>'.print_r($linklist, true).'</pre>';
       
   492       for($i=0;$i<sizeof($linklist[1]);$i++)
       
   493       {
       
   494         $mn = str_replace('[['.$linklist[1][$i].']]', '{WIKILINK:'.$random_id.':'.$i.'}', $mn);
       
   495       }
       
   496       
       
   497       $ar = explode('|', $mn);
       
   498       
       
   499       for($j=0;$j<sizeof($ar);$j++)
       
   500       {
       
   501         for($i=0;$i<sizeof($linklist[1]);$i++)
       
   502         {
       
   503           $ar[$j] = str_replace('{WIKILINK:'.$random_id.':'.$i.'}', '[['.$linklist[1][$i].']]', $ar[$j]);
       
   504         }
       
   505       }
       
   506       
       
   507       $tp = $ar[0];
       
   508       unset($ar[0]);
       
   509       $tp = str_replace(' ', '_', $tp);
       
   510       $message = str_replace('{{'.$m.'}}', RenderMan::getTemplate($tp, $ar), $message);
       
   511     }
       
   512     return $message;
       
   513   }
       
   514   */
       
   515   
   501   
   516   /**
   502   /**
   517    * 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.
   518    * @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.
   519    * @example
   505    * @example
   526    * [bar] => dolor sit amet
   512    * [bar] => dolor sit amet
   527    */
   513    */
   528   
   514   
   529   function parse_template_vars($input)
   515   function parse_template_vars($input)
   530   {
   516   {
   531     $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     }
   532     $parms = Array();
   528     $parms = Array();
   533     $current_line = '';
   529     $current_line = '';
   534     $current_parm = '';
   530     $current_parm = '';
   535     foreach ( $input as $num => $line )
   531     foreach ( $input as $num => $line )
   536     {
   532     {
   537       if ( preg_match('/^([ ]*?)([A-z0-9_]+?)([ ]*?)=([ ]*?)(.+?)$/i', $line, $matches) )
   533       if ( preg_match('/^[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?)$/is', $line, $matches) )
   538       {
   534       {
   539         $parm =& $matches[2];
   535         $parm =& $matches[1];
   540         $text =& $matches[5];
   536         $text =& $matches[4];
   541         if ( $parm == $current_parm )
   537         if ( $parm == $current_parm )
   542         {
   538         {
   543           $current_line .= $text;
   539           $current_line .= $text;
   544         }
   540         }
   545         else
   541         else
   568     return $parms;
   564     return $parms;
   569   }
   565   }
   570   
   566   
   571   /**
   567   /**
   572    * 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}}
   573    * @param string The text to process
   570    * @param string The text to process
   574    * @return string Formatted text
   571    * @return string Formatted text
   575    * @example
   572    * @example
   576    * <code>
   573    * <code>
   577    $text = '{{Template
   574    $text = '{{Template
   578      parm1 = Foo
   575      parm1 = Foo
   579      parm2 = Bar
   576      parm2 = Bar
   580      }}';
   577      }}';
   581    $text = include_templates($text);
   578    $text = RenderMan::include_templates($text);
   582    * </code>
   579    * </code>
   583    */
   580    */
   584   
   581   
   585   function include_templates($text)
   582   function include_templates($text)
   586   {
   583   {
   587     global $db, $session, $paths, $template, $plugins; // Common objects
   584     global $db, $session, $paths, $template, $plugins; // Common objects
   588     $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   585     // $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
       
   586     $template_regex = "/\{\{(.+)(((\n|[ ]*\|)[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
   589     if ( $count = preg_match_all($template_regex, $text, $matches) )
   587     if ( $count = preg_match_all($template_regex, $text, $matches) )
   590     {
   588     {
       
   589       //die('<pre>' . print_r($matches, true) . '</pre>');
   591       for ( $i = 0; $i < $count; $i++ )
   590       for ( $i = 0; $i < $count; $i++ )
   592       {
   591       {
   593         $matches[1][$i] = sanitize_page_id($matches[1][$i]);
   592         $matches[1][$i] = sanitize_page_id($matches[1][$i]);
   594         $parmsection = trim($matches[2][$i]);
   593         $parmsection = trim($matches[2][$i]);
   595         if ( !empty($parmsection) )
   594         if ( !empty($parmsection) )
   596         {
   595         {
   597           $parms = RenderMan::parse_template_vars($parmsection);
   596           $parms = RenderMan::parse_template_vars($parmsection);
   598           foreach ( $parms as $j => $parm )
   597           if ( !is_array($parms) )
   599           {
   598             // Syntax error
   600             $parms[$j] = $parm;
   599             $parms = array();
   601           }
       
   602         }
   600         }
   603         else
   601         else
   604         {
   602         {
   605           $parms = Array();
   603           $parms = Array();
   606         }
   604         }
   708       ':-|'     => 'face-plain.png',
   706       ':-|'     => 'face-plain.png',
   709       ':-\\'    => 'face-plain.png',
   707       ':-\\'    => 'face-plain.png',
   710       ':-/'     => 'face-plain.png',
   708       ':-/'     => 'face-plain.png',
   711       ':joke:'  => 'face-plain.png',
   709       ':joke:'  => 'face-plain.png',
   712       ']:-&gt;' => 'face-devil-grin.png',
   710       ']:-&gt;' => 'face-devil-grin.png',
       
   711       ']:->'    => 'face-devil-grin.png',
   713       ':kiss:'  => 'face-kiss.png',
   712       ':kiss:'  => 'face-kiss.png',
   714       ':-P'     => 'face-tongue-out.png',
   713       ':-P'     => 'face-tongue-out.png',
   715       ':P'      => 'face-tongue-out.png',
   714       ':P'      => 'face-tongue-out.png',
   716       ':-p'     => 'face-tongue-out.png',
   715       ':-p'     => 'face-tongue-out.png',
   717       ':p'      => 'face-tongue-out.png',
   716       ':p'      => 'face-tongue-out.png',