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,v 1.2 2004/04/03 12:09:00 alan_k Exp $
  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,v 1.2 2004/04/03 12:09:00 alan_k Exp $
  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 = 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 '{templateConfigLoad(#'.$args['file'].'#,#'.$args['section'].')}';
  195.            
  196.             case ($str == 'ldelim'):
  197.                 return '{';
  198.             case ($str == 'rdelim'):
  199.                 return '}';
  200.                 
  201.                 
  202.             case (preg_match('/^if \$(\S+)$/'$str,$matches)):
  203.             case (preg_match('/^if \$(\S+)\seq\s""$/'$str,$matches)):
  204.                 // simple if variable..
  205.                 // convert to : {if:sssssss}
  206.                 $this->stack['if']++;
  207.                 $var =  $this->_convertVar('$'.$matches[1]);
  208.                 return '{if:'.substr($var,1);
  209.                 
  210.             case (preg_match('/^if #(\S+)#$/'$str,$matches)):
  211.             case (preg_match('/^if #(\S+)#\sne\s""$/'$str,$matches)):
  212.                 // simple if variable..
  213.                 // convert to : {if:sssssss}
  214.                 $this->stack['if']++;
  215.                 $var =  $this->_convertConfigVar('#'.$matches[1].'#');
  216.                 return '{if:'.substr($var,1);
  217.             
  218.             // negative matches
  219.             case (preg_match('/^if\s!\s\$(\S+)$/'$str,$matches)):
  220.             case (preg_match('/^if \$(\S+)\seq\s""$/'$str,$matches)):
  221.                 // simple if variable..
  222.                 // convert to : {if:sssssss}
  223.                 $this->stack['if']++;
  224.                 $var =  $this->_convertVar('$'.$matches[1]);
  225.                 return '{if:!'.substr($var,1);
  226.                 
  227.              case ($str == 'else'):
  228.                 if (!$this->stack['if']{
  229.                     break;
  230.                 }
  231.                 return '{else:}';
  232.                 
  233.                 
  234.             case ($str == '/if'):
  235.                 if (!$this->stack['if']{
  236.                     break;
  237.                 }
  238.                 $this->stack['if']--;
  239.                 return '{end:}';
  240.             
  241.             
  242.         }
  243.         
  244.         return "<!--   UNSUPPORTED TAG: $str FOUND -->";
  245.                 
  246.         
  247.     
  248.     
  249.     }
  250.     
  251.     /**
  252.     * convert a smarty var into a flexy one.
  253.     *
  254.     * @param   string       the inside of the smart tag
  255.     *
  256.     * @return   string      a flexy version of it.
  257.     * @access   private
  258.     */
  259.   
  260.     function _convertVar($str
  261.     {
  262.         // look for modfiers first.
  263.         $mods explode('|'$str);
  264.         $var array_shift($mods);
  265.         $var substr($var,1)// strip $
  266.         
  267.         // various formats :
  268.         // aaaa.bbbb.cccc => aaaa[bbbb][cccc]
  269.         // aaaa[bbbb] => aaa[bbbb]
  270.         // aaaa->bbbb => aaaa.bbbb
  271.         
  272.         $bits explode('.',$var);
  273.         $var array_shift($bits);
  274.         foreach($bits as $k{
  275.             $var.= '['.$k .']';
  276.         }
  277.         $bits explode('->',$var);
  278.         $var implode('.',$bits);
  279.         $mods implode('|',$mods);
  280.         if (strlen($mods)) {
  281.             $mods = "<!--  UNSUPPORTED MODIFIERS: $mods -->";
  282.         }
  283.         return '{'.$var .'}' $mods;
  284.     }
  285.     /**
  286.     * convert a smarty key="value" string into a key value array
  287.     * cheap and cheerfull - doesnt handle spaces inside the strings...
  288.     *
  289.     * @param   string       the key value part of the tag..
  290.     *
  291.     * @return   array      key value array
  292.     * @access   private
  293.     */
  294.     function convertAttributesToKeyVal($str
  295.     {
  296.         $atts explode(' '$str);
  297.         $ret = array();
  298.         foreach($atts as $bit{
  299.             $bits explode('=',$bit);
  300.             // loose stuff!!!
  301.             if (count($bits!= 2{
  302.                 continue;
  303.             }
  304.             $ret[$bits[0]] ($bits[1]{0== '"'substr($bits[1],1,-1$bits[1]
  305.         }
  306.         return $ret;
  307.     }
  308.      /**
  309.     * convert a smarty config var into a flexy one.
  310.     *
  311.     * @param   string       the inside of the smart tag
  312.     *
  313.     * @return   string      a flexy version of it.
  314.     * @access   private
  315.     */
  316.   
  317.     function _convertConfigVar($str
  318.     {
  319.         $mods explode('|'$str);
  320.         $var array_shift($mods);
  321.         $var substr($var,1,-1)// strip #'s
  322.         $mods implode('|',$mods);
  323.         if (strlen($mods)) {
  324.             $mods = "<!-- UNSUPPORTED MODIFIERS: $mods -->";
  325.         }
  326.         return '{configVars.'.$var .'}' $mods;
  327.     }
  328. }

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