includes/render.php
changeset 717 236360cf79a0
parent 715 29663506e85c
child 745 0a3866f74faa
equal deleted inserted replaced
716:d9533bb07563 717:236360cf79a0
   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);
   571    * @return array Example:
   572    * @return array Example:
   572    * [foo] => lorem ipsum
   573    * [foo] => lorem ipsum
   573    * [bar] => dolor sit amet
   574    * [bar] => dolor sit amet
   574    */
   575    */
   575   
   576   
   576   public static function parse_template_vars($input)
   577   public static function parse_template_vars($input, $newlinemode = true)
   577   {
   578   {
   578     if ( !preg_match('/^(\|[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?))*$/is', trim($input)) )
   579     $parms = array();
   579     {
   580     $input = trim($input);
   580       $using_pipes = false;
   581     if ( $newlinemode )
   581       $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);
   582     }
   592     }
   583     else
   593     else
   584     {
   594     {
   585       $using_pipes = true;
   595       $result = preg_match_all('/
   586       $input = substr($input, 1);
   596                                   (?:^|[ ]*)\|         # start of parameter - string start or series of spaces
   587       $input = explode("|", trim( $input ));
   597                                   [ ]*
   588     }
   598                                   (?:
   589     $parms = Array();
   599                                     ([A-z0-9_]+)       # variable name
   590     $current_line = '';
   600                                     [ ]* = [ ]*        # assignment
   591     $current_parm = '';
   601                                   )?                   # name section is optional - if the parameter name is not given, a numerical index is assigned
   592     foreach ( $input as $num => $line )
   602                                   ([^\|]+|.+?\n[ ]*\|) # value
   593     {
   603                                 /x', trim($input), $matches);
   594       if ( preg_match('/^[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?)$/is', $line, $matches) )
   604     }                   
   595       {
   605     if ( $result )
   596         $parm =& $matches[1];
   606     {
   597         $text =& $matches[4];
   607       $pi = 0;
   598         if ( $parm == $current_parm )
   608       for ( $i = 0; $i < count($matches[0]); $i++ )
   599         {
   609       {
   600           $current_line .= $text;
   610         $matches[1][$i] = trim($matches[1][$i]);
   601         }
   611         $parmname = !empty($matches[1][$i]) ? $matches[1][$i] : strval(++$pi);
   602         else
   612         $parms[ $parmname ] = $matches[2][$i];
   603         {
   613       }
   604           // New parameter
       
   605           if ( $current_parm != '' )
       
   606             $parms[$current_parm] = $current_line;
       
   607           $current_line = $text;
       
   608           $current_parm = $parm;
       
   609         }
       
   610       }
       
   611       else if ( $num == 0 )
       
   612       {
       
   613         // Syntax error
       
   614         return false;
       
   615       }
       
   616       else
       
   617       {
       
   618         $current_line .= "\n$line";
       
   619       }
       
   620     }
       
   621     if ( !empty($current_parm) && !empty($current_line) )
       
   622     {
       
   623       $parms[$current_parm] = $current_line;
       
   624     }
   614     }
   625     return $parms;
   615     return $parms;
   626   }
   616   }
   627   
   617   
   628   /**
   618   /**
   631    * @param string The text to process
   621    * @param string The text to process
   632    * @return string Formatted text
   622    * @return string Formatted text
   633    * @example
   623    * @example
   634    * <code>
   624    * <code>
   635    $text = '{{Template
   625    $text = '{{Template
   636      parm1 = Foo
   626        | parm1 = Foo
   637      parm2 = Bar
   627        | parm2 = Bar
   638      }}';
   628      }}';
   639    $text = RenderMan::include_templates($text);
   629    $text = RenderMan::include_templates($text);
   640    * </code>
   630    * </code>
   641    */
   631    */
   642   
   632   
   643   public static function include_templates($text)
   633   public static function include_templates($text)
   644   {
   634   {
   645     global $db, $session, $paths, $template, $plugins; // Common objects
   635     global $db, $session, $paths, $template, $plugins; // Common objects
   646     // $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   636     // $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   647     $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";
   648     if ( $count = preg_match_all($template_regex, $text, $matches) )
   646     if ( $count = preg_match_all($template_regex, $text, $matches) )
   649     {
   647     {
   650       //die('<pre>' . print_r($matches, true) . '</pre>');
   648       //die('<pre>' . print_r($matches, true) . '</pre>');
   651       for ( $i = 0; $i < $count; $i++ )
   649       for ( $i = 0; $i < $count; $i++ )
   652       {
   650       {
   653         $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" );
   654         $parmsection = trim($matches[2][$i]);
   653         $parmsection = trim($matches[2][$i]);
   655         if ( !empty($parmsection) )
   654         if ( !empty($parmsection) )
   656         {
   655         {
   657           $parms = RenderMan::parse_template_vars($parmsection);
   656           $parms = RenderMan::parse_template_vars($parmsection, $newlinemode);
   658           if ( !is_array($parms) )
   657           if ( !is_array($parms) )
   659             // Syntax error
   658             // Syntax error
   660             $parms = array();
   659             $parms = array();
   661         }
   660         }
   662         else
   661         else