includes/render.php
changeset 734 904fbf10f112
parent 717 236360cf79a0
child 745 0a3866f74faa
equal deleted inserted replaced
712:331e009416d5 734:904fbf10f112
   143       else
   143       else
   144       {
   144       {
   145         $p = '<b>Notice:</b> RenderMan::getTemplate(): Parameter '.$m.' is not set';
   145         $p = '<b>Notice:</b> RenderMan::getTemplate(): Parameter '.$m.' is not set';
   146       }
   146       }
   147       $text = str_replace('(_'.$m.'_)', $p, $text);
   147       $text = str_replace('(_'.$m.'_)', $p, $text);
       
   148       $text = str_replace('{{' . ( $m + 1 ) . '}}', $p, $text);
   148     }
   149     }
   149     $text = RenderMan::include_templates($text);
   150     $text = RenderMan::include_templates($text);
   150     return $text;
   151     return $text;
   151   }
   152   }
   152   
   153   
   250     if ( $paths->namespace == 'Template' )
   251     if ( $paths->namespace == 'Template' )
   251     {
   252     {
   252       $text = preg_replace('/<nodisplay>(.*?)<\/nodisplay>/is', '', $text);
   253       $text = preg_replace('/<nodisplay>(.*?)<\/nodisplay>/is', '', $text);
   253     }
   254     }
   254     
   255     
   255     preg_match_all('/<lang code="([a-z0-9_]+)">([\w\W]+?)<\/lang>/', $text, $langmatch);
   256     preg_match_all('/<lang code="([a-z0-9_-]+)">([\w\W]+?)<\/lang>/', $text, $langmatch);
   256     foreach ( $langmatch[0] as $i => $match )
   257     foreach ( $langmatch[0] as $i => $match )
   257     {
   258     {
   258       if ( $langmatch[1][$i] == $lang->lang_code )
   259       if ( $langmatch[1][$i] == $lang->lang_code )
   259       {
   260       {
   260         $text = str_replace_once($match, $langmatch[2][$i], $text);
   261         $text = str_replace_once($match, $langmatch[2][$i], $text);
   267     
   268     
   268     $code = $plugins->setHook('render_wikiformat_pre');
   269     $code = $plugins->setHook('render_wikiformat_pre');
   269     foreach ( $code as $cmd )
   270     foreach ( $code as $cmd )
   270     {
   271     {
   271       eval($cmd);
   272       eval($cmd);
   272     }
       
   273     
       
   274     if ( !$plaintext )
       
   275     {
       
   276       // Process images
       
   277       $text = RenderMan::process_image_tags($text, $taglist);
       
   278       $text = RenderMan::process_imgtags_stage2($text, $taglist);
       
   279     }
       
   280     
       
   281     if($do_params)
       
   282     {
       
   283       preg_match_all('#\(_([0-9]+)_\)#', $text, $matchlist);
       
   284       foreach($matchlist[1] as $m)
       
   285       {
       
   286         $text = str_replace('(_'.$m.'_)', $paths->getParam((int)$m), $text);
       
   287       }
       
   288     }
   273     }
   289     
   274     
   290     //$template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   275     //$template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   291     $template_regex = "/\{\{(.+)((\n|\|[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
   276     $template_regex = "/\{\{(.+)((\n|\|[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
   292     $i = 0;
   277     $i = 0;
   294     {
   279     {
   295       $i++;
   280       $i++;
   296       if ( $i == 5 )
   281       if ( $i == 5 )
   297         break;
   282         break;
   298       $text = RenderMan::include_templates($text);
   283       $text = RenderMan::include_templates($text);
       
   284     }
       
   285     
       
   286     if ( !$plaintext )
       
   287     {
       
   288       // Process images
       
   289       $text = RenderMan::process_image_tags($text, $taglist);
       
   290       $text = RenderMan::process_imgtags_stage2($text, $taglist);
       
   291     }
       
   292     
       
   293     if($do_params)
       
   294     {
       
   295       preg_match_all('#\(_([0-9]+)_\)#', $text, $matchlist);
       
   296       foreach($matchlist[1] as $m)
       
   297       {
       
   298         $text = str_replace('(_'.$m.'_)', $paths->getParam((int)$m), $text);
       
   299       }
   299     }
   300     }
   300     
   301     
   301     // Before shipping it out to the renderer, replace spaces in between headings and paragraphs:
   302     // Before shipping it out to the renderer, replace spaces in between headings and paragraphs:
   302     $text = preg_replace('/<\/(h[0-9]|div|p)>([\s]+)<(h[0-9]|div|p)( .+?)?>/i', '</\\1><\\3\\4>', $text);
   303     $text = preg_replace('/<\/(h[0-9]|div|p)>([\s]+)<(h[0-9]|div|p)( .+?)?>/i', '</\\1><\\3\\4>', $text);
   303     
   304     
   493     // stage 1 - links with alternate text
   494     // stage 1 - links with alternate text
   494     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\|(.+?)\]\]/', $text, $matches);
   495     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\|(.+?)\]\]/', $text, $matches);
   495     foreach ( $matches[0] as $i => $match )
   496     foreach ( $matches[0] as $i => $match )
   496     {
   497     {
   497       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   498       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
       
   499       if ( ($pos = strrpos($page_id, '#')) !== false )
       
   500       {
       
   501         $hash = substr($page_id, $pos);
       
   502         $page_id = substr($page_id, 0, $pos);
       
   503       }
       
   504       else
       
   505       {
       
   506         $hash = '';
       
   507       }
   498       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
   508       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
   499       
   509       
   500       $url = makeUrl($pid_clean, false, true);
   510       $url = makeUrl($pid_clean, false, true) . $hash;
   501       $inner_text = $matches[2][$i];
   511       $inner_text = $matches[2][$i];
   502       $quot = '"';
   512       $quot = '"';
   503       $exists = ( isPage($pid_clean) ) ? '' : ' class="wikilink-nonexistent"';
   513       $exists = ( isPage($pid_clean) ) ? '' : ' class="wikilink-nonexistent"';
   504       
   514       
   505       if ( $tplcode )
   515       if ( $tplcode )
   562    * @return array Example:
   572    * @return array Example:
   563    * [foo] => lorem ipsum
   573    * [foo] => lorem ipsum
   564    * [bar] => dolor sit amet
   574    * [bar] => dolor sit amet
   565    */
   575    */
   566   
   576   
   567   public static function parse_template_vars($input)
   577   public static function parse_template_vars($input, $newlinemode = true)
   568   {
   578   {
   569     if ( !preg_match('/^(\|[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?))*$/is', trim($input)) )
   579     $parms = array();
   570     {
   580     $input = trim($input);
   571       $using_pipes = false;
   581     if ( $newlinemode )
   572       $input = explode("\n", trim( $input ));
   582     {
       
   583       $result = preg_match_all('/
       
   584                                   (?:^|[\s]*)\|?    # start of parameter - string start or series of spaces
       
   585                                   [ ]*              
       
   586                                   (?:               
       
   587                                     ([A-z0-9_]+)    # variable name
       
   588                                     [ ]* = [ ]*     # assignment
       
   589                                   )?                # this is optional - if the parameter name is not given, a numerical index is assigned
       
   590                                   (.+)              # value
       
   591                                 /x', trim($input), $matches);
   573     }
   592     }
   574     else
   593     else
   575     {
   594     {
   576       $using_pipes = true;
   595       $result = preg_match_all('/
   577       $input = substr($input, 1);
   596                                   (?:^|[ ]*)\|         # start of parameter - string start or series of spaces
   578       $input = explode("|", trim( $input ));
   597                                   [ ]*
   579     }
   598                                   (?:
   580     $parms = Array();
   599                                     ([A-z0-9_]+)       # variable name
   581     $current_line = '';
   600                                     [ ]* = [ ]*        # assignment
   582     $current_parm = '';
   601                                   )?                   # name section is optional - if the parameter name is not given, a numerical index is assigned
   583     foreach ( $input as $num => $line )
   602                                   ([^\|]+|.+?\n[ ]*\|) # value
   584     {
   603                                 /x', trim($input), $matches);
   585       if ( preg_match('/^[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?)$/is', $line, $matches) )
   604     }                   
   586       {
   605     if ( $result )
   587         $parm =& $matches[1];
   606     {
   588         $text =& $matches[4];
   607       $pi = 0;
   589         if ( $parm == $current_parm )
   608       for ( $i = 0; $i < count($matches[0]); $i++ )
   590         {
   609       {
   591           $current_line .= $text;
   610         $matches[1][$i] = trim($matches[1][$i]);
   592         }
   611         $parmname = !empty($matches[1][$i]) ? $matches[1][$i] : strval(++$pi);
   593         else
   612         $parms[ $parmname ] = $matches[2][$i];
   594         {
   613       }
   595           // New parameter
       
   596           if ( $current_parm != '' )
       
   597             $parms[$current_parm] = $current_line;
       
   598           $current_line = $text;
       
   599           $current_parm = $parm;
       
   600         }
       
   601       }
       
   602       else if ( $num == 0 )
       
   603       {
       
   604         // Syntax error
       
   605         return false;
       
   606       }
       
   607       else
       
   608       {
       
   609         $current_line .= "\n$line";
       
   610       }
       
   611     }
       
   612     if ( !empty($current_parm) && !empty($current_line) )
       
   613     {
       
   614       $parms[$current_parm] = $current_line;
       
   615     }
   614     }
   616     return $parms;
   615     return $parms;
   617   }
   616   }
   618   
   617   
   619   /**
   618   /**
   622    * @param string The text to process
   621    * @param string The text to process
   623    * @return string Formatted text
   622    * @return string Formatted text
   624    * @example
   623    * @example
   625    * <code>
   624    * <code>
   626    $text = '{{Template
   625    $text = '{{Template
   627      parm1 = Foo
   626        | parm1 = Foo
   628      parm2 = Bar
   627        | parm2 = Bar
   629      }}';
   628      }}';
   630    $text = RenderMan::include_templates($text);
   629    $text = RenderMan::include_templates($text);
   631    * </code>
   630    * </code>
   632    */
   631    */
   633   
   632   
   634   public static function include_templates($text)
   633   public static function include_templates($text)
   635   {
   634   {
   636     global $db, $session, $paths, $template, $plugins; // Common objects
   635     global $db, $session, $paths, $template, $plugins; // Common objects
   637     // $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   636     // $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   638     $template_regex = "/\{\{(.+)(((\n|[ ]*\|)[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
   637     // matches:
       
   638     //  1 - template name
       
   639     //  2 - parameter section
       
   640     $template_regex = "/
       
   641                          \{\{                     # opening
       
   642                            ([^\n\t\a\r]+)         # template name
       
   643                            ((?:(?:[\s]+\|?)[ ]*(?:[A-z0-9_]+)[ ]*=[ ]*?(?:.+))*) # parameters
       
   644                          \}\}                     # closing
       
   645                        /isxU";
   639     if ( $count = preg_match_all($template_regex, $text, $matches) )
   646     if ( $count = preg_match_all($template_regex, $text, $matches) )
   640     {
   647     {
   641       //die('<pre>' . print_r($matches, true) . '</pre>');
   648       //die('<pre>' . print_r($matches, true) . '</pre>');
   642       for ( $i = 0; $i < $count; $i++ )
   649       for ( $i = 0; $i < $count; $i++ )
   643       {
   650       {
   644         $matches[1][$i] = sanitize_page_id($matches[1][$i]);
   651         $matches[1][$i] = sanitize_page_id($matches[1][$i]);
       
   652         $newlinemode = ( substr($matches[2][$i], 0, 1) == "\n" );
   645         $parmsection = trim($matches[2][$i]);
   653         $parmsection = trim($matches[2][$i]);
   646         if ( !empty($parmsection) )
   654         if ( !empty($parmsection) )
   647         {
   655         {
   648           $parms = RenderMan::parse_template_vars($parmsection);
   656           $parms = RenderMan::parse_template_vars($parmsection, $newlinemode);
   649           if ( !is_array($parms) )
   657           if ( !is_array($parms) )
   650             // Syntax error
   658             // Syntax error
   651             $parms = array();
   659             $parms = array();
   652         }
   660         }
   653         else
   661         else