HTML_Template_Flexy
[ class tree: HTML_Template_Flexy ] [ index: HTML_Template_Flexy ] [ all elements ]

Source for file SmartyConvertor.php

Documentation is available at SmartyConvertor.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors:  Alan Knowles <alan@akbkhome.com>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: SmartyConvertor.php 162576 2004-07-03 03:46:43Z alan_k $
  20. //
  21. //  Smarty Conversion compiler
  22. //  takes a smarty template, and converts it to a flexy one.
  23. //  then does a standard flexy compile.
  24. //
  25. //  anything that is not supported gets changed to HTML comments
  26.  
  27. /* Usage:
  28. a simple script: 'convertsmarty.php'
  29.  
  30. #!/usr/bin/php
  31.    $file = $_SERVER['argv'][1];
  32.    $x = new HTML_Template_Flexy(array(
  33.                     'compileDir'    =>  dirname(__FILE__) ,      // where do you want to write to..
  34.                     'templateDir'   =>  $dir ,     // where are your templates
  35.                     'locale'        => 'en',    // works with gettext
  36.                     'forceCompile'  =>  true,  // only suggested for debugging
  37.                     'debug'         => false,   // prints a few errors
  38.                     'nonHTML'       => false,  // dont parse HTML tags (eg. email templates)
  39.                     'allowPHP'      => false,   // allow PHP in template
  40.                     'compiler'      => 'SmartyConvertor', // which compiler to use.
  41.                     'compileToString' => true,    // returns the converted template (rather than actually 
  42.                                                    // converting to PHP.
  43.                     'filters'       => array(),    // used by regex compiler..
  44.                     'numberFormat'  => ",2,'.',','",  // default number format  = eg. 1,200.00 ( {xxx:n} )
  45.                     'flexyIgnore'   => 0        // turn on/off the tag to element code
  46.                 ));
  47.     
  48.     echo $x->compile(basename($file));
  49.  
  50. then run it at the command line:
  51. php convertsmarty.php /path/to/a/smarty/template.tpl > /path/to/the/flexy/templates.html
  52. */
  53.  
  54.  
  55. require_once 'HTML/Template/Flexy/Compiler.php';
  56.  
  57. /**
  58. * The Smarty Converter implementation.
  59. * designed primarily to be used as above, to convert from one to another.
  60. * however it could be used inline to convert simple smarty templates into
  61. * flexy ones - then compile them on the fly.
  62. *
  63. @version    $Id: SmartyConvertor.php 162576 2004-07-03 03:46:43Z alan_k $
  64. */
  65. class HTML_Template_Flexy_Compiler_SmartyConvertor extends HTML_Template_Flexy_Compiler {
  66.     
  67.     /**
  68.     * compile implementation
  69.     *
  70.     * see HTML_Template_Flexy_Compiler::compile()
  71.     * 
  72.     * @param   object    The core flexy object.
  73.     * @param   string    optionally a string to compile.
  74.     *
  75.     * @return   true | string   string when compiling to String.
  76.     * @access   public
  77.     */
  78.   
  79.     function compile(&$flexy,$string=false
  80.     {
  81.         $data $string;
  82.         if ($string === false{
  83.             $data file_get_contents($flexy->currentTemplate);
  84.         }
  85.         
  86.         
  87.         
  88.         $data $this->convertToFlexy($data);
  89.         
  90.         if ($flexy->options['compileToString']{
  91.             return $data;
  92.         }
  93.         
  94.         require_once 'HTML/Template/Flexy/Compiler/Standard.php';
  95.         $flexyCompiler = new HTML_Template_Flexy_Compiler_Standard;
  96.         $flexyCompiler->compile($flexy,$data);
  97.         return true;
  98.     }
  99.     
  100.     
  101.     
  102.     /**
  103.     * The core work of parsing a smarty template and converting it into flexy.
  104.     *
  105.     * @param   string       the contents of the smarty template
  106.     *
  107.     * @return   string         the flexy version of the template.
  108.     * @access   public|private
  109.     * @see      see also methods.....
  110.     */
  111.     function convertToFlexy($data
  112.     {
  113.     
  114.         $leftq preg_quote('{''!');
  115.         $rightq preg_quote('}''!');
  116.          
  117.         preg_match_all("!" $leftq "\s*(.*?)\s*" $rightq "!s"$data$matches);
  118.         $tags $matches[1];
  119.         // find all the tags/text...
  120.         $text preg_split("!" $leftq ".*?" $rightq "!s"$data);
  121.         
  122.         $max_text count($text);
  123.         $max_tags count($tags);
  124.         
  125.         for ($i = 0 ; $i $max_tags $i++{
  126.             $compiled_tags[$this->_compileTag($tags[$i]);
  127.         }
  128.         // error handling for closing tags.
  129.         
  130.          
  131.         $data '';
  132.         for ($i = 0; $i $max_tags$i++{
  133.             $data .= $text[$i].$compiled_tags[$i];
  134.         }
  135.         $data .= $text[$i];
  136.         return $data;
  137.     
  138.     }
  139.     
  140.     /**
  141.     * stack for conditional and closers.
  142.     *
  143.     * @var array 
  144.     * @access public
  145.     */
  146.     var $stack = array(
  147.             'if' => 0,
  148.         );
  149.     
  150.     
  151.     
  152.     /**
  153.     * compile a smarty { tag } into a flexy one.
  154.     *
  155.     * @param   string           the tag
  156.     *
  157.     * @return   string      the converted version
  158.     * @access   private
  159.     */
  160.     function _compileTag($str
  161.     {
  162.         // skip comments
  163.         if (($str{0== '*'&& (substr($str,-1,1== '*')) {
  164.             return '';
  165.         }
  166.         
  167.         
  168.         switch($str{0}{
  169.             case '$':
  170.                 // its a var
  171.                 return $this->_convertVar($str);
  172.             case '#':
  173.                 // its a config var
  174.                 return $this->_convertConfigVar($str);
  175.             case '%':
  176.                 // wtf does this do
  177.                 return "<!-- what is this? $str -->";
  178.         }
  179.                 
  180.             
  181.         
  182.         
  183.         
  184.         
  185.         // this is where it gets messy
  186.         // this is very slow - but what the hell 
  187.         //   - its only done once
  188.         //   - its alot more readable than a long regext.
  189.         //   - it doesnt infringe on copyright...
  190.         switch(true{
  191.             case (preg_match('/^config_load\s/'$str)):
  192.                 // convert to $t->TemplateConfigLoad()
  193.                 $args $this->convertAttributesToKeyVal(substr($str,strpos$str,' ')));
  194.                 return '{plugin(#smartyConfigLoad#,#'.$args['file'].'#,#'.$args['section'].'#)}';
  195.             
  196.             case (preg_match('/^include\s/'$str)):
  197.                 // convert to $t->TemplateConfigLoad()
  198.                 $args $this->convertAttributesToKeyVal(substr($str,strpos$str,' ')));
  199.              
  200.                 return '{plugin(#smartyInclude#,#'.$args['file'].'#)}';
  201.            
  202.             case ($str == 'ldelim'):
  203.                 return '{';
  204.             case ($str == 'rdelim'):
  205.                 return '}';
  206.                 
  207.                 
  208.             case (preg_match('/^if \$(\S+)$/'$str,$matches)):
  209.             case (preg_match('/^if \$(\S+)\seq\s""$/'$str,$matches)):
  210.                 // simple if variable..
  211.                 // convert to : {if:sssssss}
  212.                 $this->stack['if']++;
  213.                 $var =  $this->_convertVar('$'.$matches[1]);
  214.                 return '{if:'.substr($var,1);
  215.                 
  216.             case (preg_match('/^if #(\S+)#$/'$str,$matches)):
  217.             case (preg_match('/^if #(\S+)#\sne\s""$/'$str,$matches)):
  218.                 // simple if variable..
  219.                 // convert to : {if:sssssss}
  220.                 $this->stack['if']++;
  221.                 $var =  $this->_convertConfigVar('#'.$matches[1].'#');
  222.                 return '{if:'.substr($var,1);
  223.             
  224.             // negative matches
  225.             case (preg_match('/^if\s!\s\$(\S+)$/'$str,$matches)):
  226.             case (preg_match('/^if \$(\S+)\seq\s""$/'$str,$matches)):
  227.                 // simple if variable..
  228.                 // convert to : {if:sssssss}
  229.                 $this->stack['if']++;
  230.                 $var =  $this->_convertVar('$'.$matches[1]);
  231.                 return '{if:!'.substr($var,1);
  232.                 
  233.              case ($str == 'else'):
  234.                 if (!$this->stack['if']{
  235.                     break;
  236.                 }
  237.                 return '{else:}';
  238.                 
  239.                 
  240.             case ($str == '/if'):
  241.                 if (!$this->stack['if']{
  242.                     break;
  243.                 }
  244.                 $this->stack['if']--;
  245.                 return '{end:}';
  246.             
  247.             
  248.         }
  249.         
  250.         return "<!--   UNSUPPORTED TAG: $str FOUND -->";
  251.                 
  252.         
  253.     
  254.     
  255.     }
  256.     
  257.     /**
  258.     * convert a smarty var into a flexy one.
  259.     *
  260.     * @param   string       the inside of the smart tag
  261.     *
  262.     * @return   string      a flexy version of it.
  263.     * @access   private
  264.     */
  265.   
  266.     function _convertVar($str
  267.     {
  268.         // look for modfiers first.
  269.         $mods explode('|'$str);
  270.         $var array_shift($mods);
  271.         $var substr($var,1)// strip $
  272.         
  273.         // various formats :
  274.         // aaaa.bbbb.cccc => aaaa[bbbb][cccc]
  275.         // aaaa[bbbb] => aaa[bbbb]
  276.         // aaaa->bbbb => aaaa.bbbb
  277.         
  278.         $bits explode('.',$var);
  279.         $var array_shift($bits);
  280.         foreach($bits as $k{
  281.             $var.= '['.$k .']';
  282.         }
  283.         $bits explode('->',$var);
  284.         $var implode('.',$bits);
  285.         $mods implode('|',$mods);
  286.         
  287.         if (strlen($mods)) {
  288.             return '{plugin(#smartyModifiers#,'.$var.',#'.$mods.'#):h}';
  289.         }
  290.         return '{'.$var .'}' $mods;
  291.     }
  292.     /**
  293.     * convert a smarty key="value" string into a key value array
  294.     * cheap and cheerfull - doesnt handle spaces inside the strings...
  295.     *
  296.     * @param   string       the key value part of the tag..
  297.     *
  298.     * @return   array      key value array
  299.     * @access   private
  300.     */
  301.     function convertAttributesToKeyVal($str
  302.     {
  303.         $atts explode(' '$str);
  304.         $ret = array();
  305.         foreach($atts as $bit{
  306.             $bits explode('=',$bit);
  307.             // loose stuff!!!
  308.             if (count($bits!= 2{
  309.                 continue;
  310.             }
  311.             $ret[$bits[0]] ($bits[1]{0== '"'substr($bits[1],1,-1$bits[1]
  312.         }
  313.         return $ret;
  314.     }
  315.      /**
  316.     * convert a smarty config var into a flexy one.
  317.     *
  318.     * @param   string       the inside of the smart tag
  319.     *
  320.     * @return   string      a flexy version of it.
  321.     * @access   private
  322.     */
  323.   
  324.     function _convertConfigVar($str
  325.     {
  326.         $mods explode('|'$str);
  327.         $var array_shift($mods);
  328.         $var substr($var,1,-1)// strip #'s
  329.         $mods implode('|',$mods);
  330.         if (strlen($mods)) {
  331.             $mods = "<!-- UNSUPPORTED MODIFIERS: $mods -->";
  332.         }
  333.         return '{configVars.'.$var .'}' $mods;
  334.     }
  335. }

Documentation generated on Mon, 11 Mar 2019 15:59:57 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.