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

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