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

Source for file Flexy.php

Documentation is available at Flexy.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: Flexy.php,v 1.14 2005/01/25 04:39:07 alan_k Exp $
  20. //
  21. //  Base Compiler Class
  22. //  Standard 'Original Flavour' Flexy compiler
  23.  
  24. // this does the main conversion, (eg. for {vars and methods}) 
  25. // it relays into Compiler/Tag & Compiler/Flexy for tags and namespace handling.
  26.  
  27.  
  28.  
  29.  
  30. require_once 'HTML/Template/Flexy/Tokenizer.php';
  31. require_once 'HTML/Template/Flexy/Token.php';
  32.  
  33. class HTML_Template_Flexy_Compiler_Flexy extends HTML_Template_Flexy_Compiler {
  34.     
  35.     
  36.         
  37.     /**
  38.     * The current template (Full path)
  39.     *
  40.     * @var string 
  41.     * @access public
  42.     */
  43.     var $currentTemplate;
  44.     /**
  45.     * The compile method.
  46.     * 
  47.     * @params   object HTML_Template_Flexy
  48.     * @params   string|false string to compile of false to use a file.
  49.     * @return   string   filename of template
  50.     * @access   public
  51.     */
  52.     function compile(&$flexy,$string=false
  53.     {
  54.         // read the entire file into one variable
  55.         
  56.         // note this should be moved to new HTML_Template_Flexy_Token
  57.         // and that can then manage all the tokens in one place..
  58.         global $_HTML_TEMPLATE_FLEXY_COMPILER;
  59.         
  60.         $this->currentTemplate  $flexy->currentTemplate;
  61.         
  62.         
  63.         $gettextStrings &$_HTML_TEMPLATE_FLEXY_COMPILER['gettextStrings'];
  64.         $gettextStrings = array()// reset it.
  65.         
  66.         if (@$this->options['debug']{
  67.             echo "compiling template $flexy->currentTemplate<BR>";
  68.             
  69.         }
  70.          
  71.         // reset the elements.
  72.         $flexy->_elements = array();
  73.         
  74.         // replace this with a singleton??
  75.         
  76.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']  $this->options;
  77.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements']        = array();
  78.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['filename']        $flexy->currentTemplate;
  79.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput']    '';
  80.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['compiledTemplate']$flexy->compiledTemplate;
  81.         
  82.         
  83.         // initialize Translation 2, and 
  84.         $this->initializeTranslator();
  85.         
  86.         
  87.         // load the template!
  88.         $data $string;
  89.         $res = false;
  90.         if ($string === false{
  91.             $data file_get_contents($flexy->currentTemplate);
  92.         }
  93.          
  94.         // PRE PROCESS {_(.....)} translation markers.
  95.         if (strpos($data,'{_('!== false{
  96.             $data $this->preProcessTranslation($data);
  97.         }
  98.         
  99.         // Tree generation!!!
  100.         
  101.         
  102.         
  103.         if (!$this->options['forceCompile'&& isset($_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)])) {
  104.             $res $_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)];
  105.         else {
  106.         
  107.              
  108.             $tokenizer = new HTML_Template_Flexy_Tokenizer($data);
  109.             $tokenizer->fileName = $flexy->currentTemplate;
  110.             
  111.             
  112.               
  113.             //$tokenizer->debug=1;
  114.             $tokenizer->options['ignore_html'$this->options['nonHTML'];
  115.             
  116.           
  117.             require_once 'HTML/Template/Flexy/Token.php';
  118.             $res = HTML_Template_Flexy_Token::buildTokens($tokenizer);
  119.             if (is_a($res,'PEAR_Error')) {
  120.                 return $res;
  121.             }       
  122.             $_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)$res;
  123.             
  124.         }
  125.         // technically we shouldnt get here as we dont cache errors..
  126.         if (is_a($res,'PEAR_Error')) {
  127.             return $res;
  128.         }   
  129.         
  130.         // turn tokens into Template..
  131.         
  132.         $data $res->compile($this);
  133.         
  134.         if (is_a($data,'PEAR_Error')) {
  135.             return $data;
  136.         }
  137.         
  138.         $data $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput'$data;
  139.         
  140.         if (   $flexy->options['debug'> 1{
  141.             echo "<B>Result: </B><PRE>".htmlspecialchars($data)."</PRE><BR>\n";
  142.         }
  143.  
  144.         if ($this->options['nonHTML']{
  145.            $data =  str_replace("?>\n","?>\n\n",$data);
  146.         }
  147.         
  148.          
  149.         
  150.         
  151.         // at this point we are into writing stuff...
  152.         if ($flexy->options['compileToString']{
  153.             if (   $flexy->options['debug']{
  154.                 echo "<B>Returning string:<BR>\n";
  155.             }
  156.  
  157.             $flexy->elements =  $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'];
  158.             return $data;
  159.         }
  160.         
  161.         
  162.         
  163.         
  164.         // error checking?
  165.         $file  $flexy->compiledTemplate;
  166.         if (isset($flexy->options['output.block'])) {
  167.             list($file,$partexplode('#',$file  );
  168.         }
  169.         
  170.         if( ($cfp fopen$file 'w' )) ) {
  171.             if ($flexy->options['debug']{
  172.                 echo "<B>Writing: </B>$file<BR>\n";
  173.             }
  174.             fwrite($cfp,$data);
  175.             fclose($cfp);
  176.             
  177.             chmod($file,0775);
  178.             // make the timestamp of the two items match.
  179.             clearstatcache();
  180.             touch($filefilemtime($flexy->currentTemplate));
  181.             if ($file != $flexy->compiledTemplate{
  182.                 chmod($flexy->compiledTemplate,0775);
  183.                 // make the timestamp of the two items match.
  184.                 clearstatcache();
  185.                 touch($flexy->compiledTemplatefilemtime($flexy->currentTemplate));
  186.             }
  187.              
  188.             
  189.         else {
  190.             return HTML_Template_Flexy::raiseError('HTML_Template_Flexy::failed to write to '.$flexy->compiledTemplate,
  191.         }
  192.         // gettext strings
  193.          
  194.         if (file_exists($flexy->getTextStringsFile)) {
  195.             unlink($flexy->getTextStringsFile);
  196.         }
  197.         
  198.         if($gettextStrings && ($cfp fopen$flexy->getTextStringsFile'w') ) ) {
  199.             
  200.             fwrite($cfp,serialize(array_unique($gettextStrings)));
  201.             fclose($cfp);
  202.         }
  203.         
  204.         // elements
  205.         if (file_exists($flexy->elementsFile)) {
  206.             unlink($flexy->elementsFile);
  207.         }
  208.         
  209.         if$GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'&&
  210.             ($cfp fopen$flexy->elementsFile'w') ) ) {
  211.             fwrite($cfp,serialize$GLOBALS['_HTML_TEMPLATE_FLEXY']['elements']));
  212.             fclose($cfp);
  213.             // now clear it.
  214.         
  215.         }
  216.         
  217.         return true;
  218.     }
  219.     
  220.     
  221.     /**
  222.     * Initilalize the translation methods.
  223.     *
  224.     * Loads Translation2 if required.
  225.     * 
  226.      *
  227.     * @return   none 
  228.     * @access   public
  229.     */
  230.     function initializeTranslator({
  231.     
  232.         if (is_array($this->options['Translation2'])) {
  233.             require_once 'Translation2.php';
  234.             $this->options['Translation2'=  &Translation2::factory(
  235.                 $this->options['Translation2']['driver'],
  236.                 isset($this->options['Translation2']['options']$this->options['Translation2']['options': array(),
  237.                 isset($this->options['Translation2']['params']$this->options['Translation2']['params': array()
  238.             );
  239.         }
  240.                 
  241.         if (is_a($this->options['Translation2'],'Translation2')) {
  242.             $this->options['Translation2']->setLang($this->options['locale']);
  243.             // fixme - needs to be more specific to which template to use..
  244.             foreach ($this->options['templateDir'as $tt{
  245.                 $n basename($this->currentTemplate);
  246.                 if (substr($this->currentTemplate,0,strlen($tt)) == $tt{
  247.                     $n substr($this->currentTemplate,strlen($tt)+1);
  248.                 }
  249.                 //echo $n;
  250.             }
  251.             $this->options['Translation2']->setPageID($n);
  252.         else {
  253.             setlocale(LC_ALL$this->options['locale']);
  254.         }
  255.         
  256.     }
  257.     
  258.     
  259.     
  260.     /**
  261.     * do the early tranlsation of {_(......)_} text
  262.     *
  263.     * 
  264.     * @param    input string
  265.     * @return   output string
  266.     * @access   public
  267.     */
  268.     function preProcessTranslation($data{
  269.         global $_HTML_TEMPLATE_FLEXY_COMPILER;
  270.         $matches = array();
  271.         $lmatches explode ('{_('$data);
  272.         array_shift($lmatches);
  273.         // shift the first..
  274.         foreach ($lmatches as $k{
  275.             if (false === strpos($k,')_}')) {
  276.                 continue;
  277.             }
  278.             $x explode(')_}',$k);
  279.             $matches[$x[0];
  280.         }
  281.     
  282.     
  283.        //echo '<PRE>';print_r($matches);
  284.         // we may need to do some house cleaning here...
  285.         $_HTML_TEMPLATE_FLEXY_COMPILER['gettextStrings'$matches;
  286.         
  287.         
  288.         // replace them now..  
  289.         // ** leaving in the tag (which should be ignored by the parser..
  290.         // we then get rid of the tags during the toString method in this class.
  291.         foreach($matches as $v{
  292.             $data str_replace('{_('.$v.')_}''{_('.$this->translateString($v).')_}',$data);
  293.         }
  294.         return $data;
  295.     }    
  296.  
  297.     
  298.     
  299.     
  300.     
  301.     /**
  302.     * Flag indicating compiler is inside {_( .... )_} block, and should not
  303.     * add to the gettextstrings array.
  304.     *
  305.     * @var boolean 
  306.     * @access public
  307.     */
  308.     var $inGetTextBlock = false;
  309.     
  310.     /**
  311.     * This is the base toString Method, it relays into toString{TokenName}
  312.     *
  313.     * @param    object    HTML_Template_Flexy_Token_* 
  314.     * 
  315.     * @return   string     string to build a template
  316.     * @access   public
  317.     * @see      toString*
  318.     */
  319.   
  320.  
  321.     function toString($element
  322.     {
  323.         static $len = 26; // strlen('HTML_Template_Flexy_Token_');
  324.         if ($this->options['debug'> 1{
  325.             $x $element;
  326.             unset($x->children);
  327.             echo htmlspecialchars(print_r($x,true))."<BR>\n";
  328.         }
  329.         if ($element->token == 'GetTextStart'{
  330.             $this->inGetTextBlock = true;
  331.             return '';
  332.         }
  333.         if ($element->token == 'GetTextEnd'{
  334.             $this->inGetTextBlock = false;
  335.             return '';
  336.         }
  337.         
  338.             
  339.         $class get_class($element);
  340.         if (strlen($class>= $len{
  341.             $type substr($class,$len);
  342.             return $this->{'toString'.$type}($element);
  343.         }
  344.         
  345.         $ret $element->value;
  346.         $add $element->compileChildren($this);
  347.         if (is_a($add,'PEAR_Error')) {
  348.             return $add;
  349.         }
  350.         $ret .= $add;
  351.         
  352.         if ($element->close{
  353.             $add $element->close->compile($this);
  354.             if (is_a($add,'PEAR_Error')) {
  355.                 return $add;
  356.             }
  357.             $ret .= $add;
  358.         }
  359.         
  360.         return $ret;
  361.     }
  362.  
  363.  
  364.     /**
  365.     *   HTML_Template_Flexy_Token_Else toString
  366.     *
  367.     * @param    object    HTML_Template_Flexy_Token_Else 
  368.     * 
  369.     * @return   string     string to build a template
  370.     * @access   public
  371.     * @see      toString*
  372.     */
  373.   
  374.  
  375.     function toStringElse($element
  376.      {
  377.         // pushpull states to make sure we are in an area.. - should really check to see 
  378.         // if the state it is pulling is a if...
  379.         if ($element->pullState(=== false{
  380.             return $this->appendHTML(
  381.                 "<font color=\"red\">Unmatched {else:} on line: {$element->line}</font>"
  382.                 );
  383.         }
  384.         $element->pushState();
  385.         return $this->appendPhp("} else {");
  386.     }
  387.     
  388.     /**
  389.     *   HTML_Template_Flexy_Token_End toString
  390.     *
  391.     * @param    object    HTML_Template_Flexy_Token_Else 
  392.     * 
  393.     * @return   string     string to build a template
  394.     * @access   public
  395.     * @see      toString*
  396.     */
  397.   
  398.     function toStringEnd($element
  399.     {
  400.         // pushpull states to make sure we are in an area.. - should really check to see 
  401.         // if the state it is pulling is a if...
  402.         if ($element->pullState(=== false{
  403.             return $this->appendHTML(
  404.                 "<font color=\"red\">Unmatched {end:} on line: {$element->line}</font>"
  405.                 );
  406.         }
  407.          
  408.         return $this->appendPhp("}");
  409.     }
  410.  
  411.     /**
  412.     *   HTML_Template_Flexy_Token_EndTag toString
  413.     *
  414.     * @param    object    HTML_Template_Flexy_Token_EndTag 
  415.     * 
  416.     * @return   string     string to build a template
  417.     * @access   public
  418.     * @see      toString*
  419.     */
  420.   
  421.  
  422.  
  423.     function toStringEndTag($element
  424.     {
  425.         return $this->toStringTag($element);
  426.     }
  427.         
  428.     
  429.     
  430.     /**
  431.     *   HTML_Template_Flexy_Token_Foreach toString
  432.     *
  433.     * @param    object    HTML_Template_Flexy_Token_Foreach 
  434.     * 
  435.     * @return   string     string to build a template
  436.     * @access   public
  437.     * @see      toString*
  438.     */
  439.   
  440.     
  441.     function toStringForeach($element
  442.     {
  443.     
  444.         $loopon $element->toVar($element->loopOn);
  445.         if (is_a($loopon,'PEAR_Error')) {
  446.             return $loopon;
  447.         }
  448.         
  449.         $ret 'if ($this->options[\'strict\'] || ('.
  450.             'is_array('$loopon')  || ' .
  451.             'is_object(' $loopon  '))) ' .
  452.             'foreach(' $loopon  " ";
  453.             
  454.         $ret .= "as \${$element->key}";
  455.         
  456.         if ($element->value{
  457.             $ret .=  " => \${$element->value}";
  458.         }
  459.         $ret .= ") {";
  460.         
  461.         $element->pushState();
  462.         $element->pushVar($element->key);
  463.         $element->pushVar($element->value);
  464.         return $this->appendPhp($ret);
  465.     }
  466.     /**
  467.     *   HTML_Template_Flexy_Token_If toString
  468.     *
  469.     * @param    object    HTML_Template_Flexy_Token_If 
  470.     * 
  471.     * @return   string     string to build a template
  472.     * @access   public
  473.     * @see      toString*
  474.     */
  475.   
  476.     function toStringIf($element
  477.     {
  478.         
  479.         $var $element->toVar($element->condition);
  480.         if (is_a($var,'PEAR_Error')) {
  481.             return $var;
  482.         }
  483.         
  484.         $ret "if (".$element->isNegative . $var .")  {";
  485.         $element->pushState();
  486.         return $this->appendPhp($ret);
  487.     }
  488.  
  489.    /**
  490.     *  get Modifier Wrapper
  491.     *
  492.     * converts :h, :u, :r , .....
  493.     * @param    object    HTML_Template_Flexy_Token_Method|Var
  494.     * 
  495.     * @return   array prefix,suffix
  496.     * @access   public
  497.     * @see      toString*
  498.     */
  499.  
  500.     function getModifierWrapper($element
  501.     {
  502.         $prefix 'echo ';
  503.         
  504.         $suffix '';
  505.         $modifier strlen(trim($element->modifier)) $element->modifier : ' ';
  506.         
  507.         switch ($modifier{
  508.             case 'h':
  509.                 break;
  510.             case 'u':
  511.                 $prefix 'echo urlencode(';
  512.                 $suffix ')';
  513.                 break;
  514.             case 'r':
  515.                 $prefix 'echo \'<pre>\'; echo htmlspecialchars(print_r(';
  516.                 $suffix ',true)); echo \'</pre>\';';
  517.                 break;                
  518.             case 'n'
  519.                 // blank or value..
  520.                 $numberformat @$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['numberFormat'];
  521.                 $prefix 'echo number_format(';
  522.                 $suffix $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['numberFormat'')';
  523.                 break;
  524.             case 'b'// nl2br + htmlspecialchars
  525.                 $prefix 'echo nl2br(htmlspecialchars(';
  526.                 
  527.                 // add language ?
  528.                 $suffix '))';
  529.                 break;
  530.             case ' ':
  531.                 $prefix 'echo htmlspecialchars(';
  532.                 // add language ?
  533.                 $suffix ')';
  534.                 break;
  535.             default:
  536.                $prefix 'echo $this->plugin("'.trim($element->modifier.'",';
  537.                $suffix ')'
  538.             
  539.             
  540.         }
  541.         
  542.         return array($prefix,$suffix);
  543.     }
  544.  
  545.  
  546.  
  547.   /**
  548.     *   HTML_Template_Flexy_Token_Var toString
  549.     *
  550.     * @param    object    HTML_Template_Flexy_Token_Method 
  551.     * 
  552.     * @return   string     string to build a template
  553.     * @access   public
  554.     * @see      toString*
  555.     */
  556.   
  557.     function toStringVar($element
  558.     {
  559.         // ignore modifier at present!!
  560.         
  561.         $var $element->toVar($element->value);
  562.         if (is_a($var,'PEAR_Error')) {
  563.             return $var;
  564.         }
  565.         list($prefix,$suffix$this->getModifierWrapper($element);
  566.         return $this->appendPhp$prefix $var $suffix .';');
  567.     }
  568.    /**
  569.     *   HTML_Template_Flexy_Token_Method toString
  570.     *
  571.     * @param    object    HTML_Template_Flexy_Token_Method 
  572.     * 
  573.     * @return   string     string to build a template
  574.     * @access   public
  575.     * @see      toString*
  576.     */
  577.   
  578.     function toStringMethod($element
  579.     {
  580.  
  581.               
  582.         // set up the modifier at present!!
  583.         list($prefix,$suffix$this->getModifierWrapper($element);
  584.         
  585.         // add the '!' to if
  586.         
  587.         if ($element->isConditional{
  588.             $prefix 'if ('.$element->isNegative;
  589.             $element->pushState();
  590.             $suffix ')';
  591.         }  
  592.         
  593.         
  594.         // check that method exists..
  595.         // if (method_exists($object,'method');
  596.         $bits explode('.',$element->method);
  597.         $method array_pop($bits);
  598.         
  599.         $object implode('.',$bits);
  600.         
  601.         $var $element->toVar($object);
  602.         if (is_a($var,'PEAR_Error')) {
  603.             return $var;
  604.         }
  605.         
  606.         if (($object == 'GLOBALS'&& 
  607.             $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['globalfunctions']{
  608.             // we should check if they something weird like: GLOBALS.xxxx[sdf](....)
  609.             $var $method;
  610.         else {
  611.             $prefix 'if ($this->options[\'strict\'] || (isset('.$var.
  612.                 ') && method_exists('.$var .",'{$method}'))) " . $prefix;
  613.             $var $element->toVar($element->method);
  614.         }
  615.         
  616.  
  617.         if (is_a($var,'PEAR_Error')) {
  618.             return $var;
  619.         }
  620.         
  621.         $ret  =  $prefix;
  622.         $ret .=  $var "(";
  623.         $s =0;
  624.          
  625.        
  626.          
  627.         foreach($element->args as $a{
  628.              
  629.             if ($s{
  630.                 $ret .= ",";
  631.             }
  632.             $s =1;
  633.             if ($a{0== '#'{
  634.                 if (is_numeric(substr($a,1,-1))) {
  635.                     $ret .= substr($a,1,-1);
  636.                 else {
  637.                     $ret .= '"'addslashes(substr($a,1,-1)) '"';
  638.                 }
  639.                 continue;
  640.             }
  641.             
  642.             $var $element->toVar($a);
  643.             if (is_a($var,'PEAR_Error')) {
  644.                 return $var;
  645.             }
  646.             $ret .= $var;
  647.             
  648.         }
  649.         $ret .= ")" $suffix;
  650.         
  651.         if ($element->isConditional{
  652.             $ret .= ' { ';
  653.         else {
  654.             $ret .= ";";
  655.         }
  656.         
  657.         
  658.         
  659.         return $this->appendPhp($ret)
  660.         
  661.          
  662.  
  663.    }
  664.    /**
  665.     *   HTML_Template_Flexy_Token_Processing toString
  666.     *
  667.     * @param    object    HTML_Template_Flexy_Token_Processing 
  668.     * 
  669.     * @return   string     string to build a template
  670.     * @access   public
  671.     * @see      toString*
  672.     */
  673.  
  674.  
  675.     function toStringProcessing($element
  676.     {
  677.         // if it's XML then quote it..
  678.         if (strtoupper(substr($element->value,2,3)) == 'XML'
  679.             return $this->appendPhp("echo '" str_replace("'","\\"."'"$element->value"';");
  680.         }
  681.         // otherwise it's PHP code - so echo it..
  682.         return $element->value;
  683.     }
  684.     
  685.     /**
  686.     *   HTML_Template_Flexy_Token_Text toString
  687.     *
  688.     * @param    object    HTML_Template_Flexy_Token_Text 
  689.     * 
  690.     * @return   string     string to build a template
  691.     * @access   public
  692.     * @see      toString*
  693.     */
  694.  
  695.  
  696.  
  697.     function toStringText($element
  698.     {
  699.         
  700.         // first get rid of stuff thats not translated etc.
  701.         // empty strings => output.
  702.         // comments -> just output
  703.         // our special tags -> output..
  704.         
  705.         if (!strlen(trim($element->value) )) {
  706.             return $this->appendHtml($element->value);
  707.         }
  708.         // dont add comments to translation lists.
  709.          
  710.         if (substr($element->value,0,4== '<!--'{
  711.             return $this->appendHtml($element->value);
  712.         }
  713.         // ignore anything wrapped with {_( .... )_}
  714.         if ($this->inGetTextBlock{
  715.             return $this->appendHtml($element->value);
  716.         }
  717.         
  718.         
  719.         if (!$element->isWord()) {
  720.             return $this->appendHtml($element->value);
  721.         }
  722.         
  723.         // grab the white space at start and end (and keep it!
  724.         
  725.         $value ltrim($element->value);
  726.         $front substr($element->value,0,-strlen($value));
  727.         $value rtrim($element->value);
  728.         $rear  substr($element->value,strlen($value));
  729.         $value trim($element->value);
  730.         
  731.         
  732.         // convert to escaped chars.. (limited..)
  733.         //$value = strtr($value,$cleanArray);
  734.         
  735.         $this->addStringToGettext($value);
  736.         $value $this->translateString($value);
  737.         // its a simple word!
  738.         return $this->appendHtml($front $value $rear);
  739.         
  740.     }
  741.     
  742.     
  743.     
  744.       /**
  745.     *   HTML_Template_Flexy_Token_Cdata toString
  746.     *
  747.     * @param    object    HTML_Template_Flexy_Token_Cdata ?
  748.     * 
  749.     * @return   string     string to build a template
  750.     * @access   public
  751.     * @see      toString*
  752.     */
  753.  
  754.  
  755.  
  756.     function toStringCdata($element
  757.     {
  758.         return $this->appendHtml($element->value);
  759.     }
  760.     
  761.     
  762.     
  763.     
  764.     
  765.     
  766.     
  767.     
  768.     
  769.     
  770.     /**
  771.     * addStringToGettext
  772.     *
  773.     * Adds a string to the gettext array.
  774.     * 
  775.     * @param   mixed        preferably.. string to store
  776.     *
  777.     * @return   none 
  778.     * @access   public
  779.     */
  780.     
  781.     function addStringToGettext($string
  782.     {
  783.     
  784.         
  785.         
  786.         
  787.         if (!is_string($string)) {
  788.             return;
  789.         }
  790.         
  791.         if (!preg_match('/[a-z]+/i'$string)) {
  792.             return;
  793.         }
  794.         $string trim($string);
  795.         
  796.         if (substr($string,0,4== '<!--'{
  797.             return;
  798.         }
  799.         
  800.         $GLOBALS['_HTML_TEMPLATE_FLEXY_COMPILER']['gettextStrings'][$string;
  801.     }
  802.     
  803.     
  804.     /**
  805.     * translateString - a gettextWrapper
  806.     *
  807.     * tries to do gettext or falls back on File_Gettext
  808.     * This has !!!NO!!! error handling - if it fails you just get english..
  809.     * no questions asked!!!
  810.     * 
  811.     * @param   string       string to translate
  812.     *
  813.     * @return   string      translated string..
  814.     * @access   public
  815.     */
  816.   
  817.     function translateString($string)
  818.     {
  819.          
  820.         
  821.         
  822.         if (is_a($this->options['Translation2'],'Translation2')) {
  823.             $result $this->options['Translation2']->get($string);
  824.             if (!empty($result)) {
  825.                 return $result;
  826.             }
  827.             return $string;
  828.         }
  829.         
  830.         // note this stuff may have been broken by removing the \n replacement code 
  831.         // since i dont have a test for it... it may remain broken..
  832.         // use Translation2 - it has gettext backend support
  833.         // and should sort out the mess that \n etc. entail.
  834.         
  835.         
  836.         $prefix basename($GLOBALS['_HTML_TEMPLATE_FLEXY']['filename']).':';
  837.         if (@$this->options['debug']{
  838.             echo __CLASS__.":TRANSLATING $string<BR>\n";
  839.         }
  840.         
  841.         if (function_exists('gettext'&& !$this->options['textdomain']{
  842.             if (@$this->options['debug']{
  843.                 echo __CLASS__.":USING GETTEXT?<BR>";
  844.             }
  845.             $t gettext($string);
  846.             
  847.             if ($t != $string{
  848.                 return $t;
  849.             }
  850.             $tt gettext($prefix.$string);
  851.             if ($tt != $prefix.$string{
  852.                 return $tt;
  853.             }
  854.                 // give up it's not translated anywhere...
  855.             return $string;
  856.              
  857.         }
  858.         if (!$this->options['textdomain'|| !$this->options['textdomainDir']{
  859.             // text domain is not set..
  860.             if (@$this->options['debug']{
  861.                 echo __CLASS__.":MISSING textdomain settings<BR>";
  862.             }
  863.             return $string;
  864.         }
  865.         $pofile $this->options['textdomainDir'
  866.                 '/' $this->options['locale'
  867.                 '/LC_MESSAGES/' $this->options['textdomain''.po';
  868.         
  869.         
  870.         // did we try to load it already..
  871.         if (@$GLOBALS['_'.__CLASS__]['PO'][$pofile=== false{
  872.             if (@$this->options['debug']{
  873.                 echo __CLASS__.":LOAD failed (Cached):<BR>";
  874.             }
  875.             return $string;
  876.         }
  877.         if (!@$GLOBALS['_'.__CLASS__]['PO'][$pofile]{
  878.             // default - cant load it..
  879.             $GLOBALS['_'.__CLASS__]['PO'][$pofile= false;
  880.             if (!file_exists($pofile)) {
  881.                  if (@$this->options['debug']{
  882.                 echo __CLASS__.":LOAD failed: {$pofile}<BR>";
  883.             }
  884.                 return $string;
  885.             }
  886.             
  887.             if (!@include_once 'File/Gettext.php'{
  888.                 if (@$this->options['debug']{
  889.                     echo __CLASS__.":LOAD no File_gettext:<BR>";
  890.                 }
  891.                 return $string;
  892.             }
  893.             
  894.             $GLOBALS['_'.__CLASS__]['PO'][$pofile= File_Gettext::factory('PO',$pofile);
  895.             $GLOBALS['_'.__CLASS__]['PO'][$pofile]->load();
  896.             //echo '<PRE>'.htmlspecialchars(print_r($GLOBALS['_'.__CLASS__]['PO'][$pofile]->strings,true));
  897.             
  898.         }
  899.         $po &$GLOBALS['_'.__CLASS__]['PO'][$pofile];
  900.         // we should have it loaded now...
  901.         // this is odd - data is a bit messed up with CR's
  902.         $string str_replace('\n',"\n",$string);
  903.         
  904.         if (isset($po->strings[$prefix.$string])) {
  905.             return $po->strings[$prefix.$string];
  906.         }
  907.         
  908.         if (!isset($po->strings[$string])) {
  909.             if (@$this->options['debug']{
  910.                     echo __CLASS__.":no match:<BR>";
  911.             }
  912.             return $string;
  913.         }
  914.         if (@$this->options['debug']{
  915.             echo __CLASS__.":MATCHED: {$po->strings[$string]}<BR>";
  916.         }
  917.         
  918.         // finally we have a match!!!
  919.         return $po->strings[$string];
  920.         
  921.     
  922.      /**
  923.     *   HTML_Template_Flexy_Token_Tag toString
  924.     *
  925.     * @param    object    HTML_Template_Flexy_Token_Tag 
  926.     * 
  927.     * @return   string     string to build a template
  928.     * @access   public
  929.     * @see      toString*
  930.     */
  931.   
  932.     function toStringTag($element{
  933.         
  934.         $original $element->getAttribute('ALT');
  935.         if (($element->tag == 'IMG'&& is_string($original&& strlen($original)) {
  936.             $this->addStringToGettext($original);
  937.             $quote $element->ucAttributes['ALT']{0};
  938.             $element->ucAttributes['ALT'$quote  $this->translateString($original)$quote;
  939.         }
  940.         
  941.         
  942.         if (strpos($element->tag,':'=== false{
  943.             $namespace 'Tag';
  944.         else {
  945.             $bits =  explode(':',$element->tag);
  946.             $namespace $bits[0];
  947.         }
  948.         if ($namespace{0== '/'{
  949.             $namespace substr($namespace,1);
  950.         }
  951.         if (empty($this->tagHandlers[$namespace])) {
  952.             
  953.             require_once 'HTML/Template/Flexy/Compiler/Flexy/Tag.php';
  954.             $this->tagHandlers[$namespace&HTML_Template_Flexy_Compiler_Flexy_Tag::factory($namespace,$this);
  955.             if (!$this->tagHandlers[$namespace{
  956.                 return HTML_Template_Flexy::raiseError('HTML_Template_Flexy::failed to create Namespace Handler '.$namespace 
  957.                     ' in file ' $GLOBALS['_HTML_TEMPLATE_FLEXY']['filename'],
  958.                     HTML_TEMPLATE_FLEXY_ERROR_SYNTAX ,HTML_TEMPLATE_FLEXY_ERROR_RETURN);
  959.             }
  960.                 
  961.         }
  962.         return $this->tagHandlers[$namespace]->toString($element);
  963.         
  964.         
  965.     }
  966.     
  967.  
  968. }

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