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

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