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

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