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

Source for file Standard.php

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

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