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

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