cvs: pear /HTML_Template_Flexy/Flexy/Compiler Flexy.php /HTML_Template_Flexy/Flexy/Compiler/Flexy Flexy.php Tag.php

From: Date: Thu, 08 Jul 2004 04:53:18 +0000
Subject: cvs: pear /HTML_Template_Flexy/Flexy/Compiler Flexy.php /HTML_Template_Flexy/Flexy/Compiler/Flexy Flexy.php Tag.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-21623@lists.php.net to get a copy of this message
alan_k Thu Jul 8 00:53:18 2004 EDT Added files: /pear/HTML_Template_Flexy/Flexy/Compiler Flexy.php /pear/HTML_Template_Flexy/Flexy/Compiler/Flexy Flexy.php Tag.php Log: Renaming Standard Compiler to Flexy, in long term prep for Template

http://cvs.php.net/co.php/pear/HTML_Template_Flexy/Flexy/Compiler/Flexy.php?r=1.1&p=1 Index: pear/HTML_Template_Flexy/Flexy/Compiler/Flexy.php +++ pear/HTML_Template_Flexy/Flexy/Compiler/Flexy.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Alan Knowles <alan@akbkhome.com> | // +----------------------------------------------------------------------+ // // $Id: Flexy.php,v 1.1 2004/07/08 04:53:17 alan_k Exp $ // // Base Compiler Class // Standard 'Original Flavour' Flexy compiler // this does the main conversion, (eg. for {vars and methods}) // it relays into Compiler/Tag & Compiler/Flexy for tags and namespace handling. require_once 'HTML/Template/Flexy/Tokenizer.php'; class HTML_Template_Flexy_Compiler_Flexy extends HTML_Template_Flexy_Compiler { /** * The compile method. * * @params object HTML_Template_Flexy * @params string|false string to compile of false to use a file. * @return string filename of template * @access public */ function compile(&$flexy,$string=false) { // read the entire file into one variable // note this should be moved to new HTML_Template_Flexy_Token // and that can then manage all the tokens in one place.. global $_HTML_TEMPLATE_FLEXY_COMPILER; $gettextStrings = &$_HTML_TEMPLATE_FLEXY_COMPILER['gettextStrings']; $gettextStrings = array(); // reset it. if (@$this->options['debug']) { echo "compiling template $flexy->currentTemplate<BR>"; } // reset the elements. $flexy->_elements = array(); // replace this with a singleton?? $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions'] = $this->options; $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'] = array(); $GLOBALS['_HTML_TEMPLATE_FLEXY']['filename'] = $flexy->currentTemplate; $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput'] = ''; $GLOBALS['_HTML_TEMPLATE_FLEXY']['compiledTemplate'] = $flexy->compiledTemplate; if (is_array($this->options['Translation2'])) { require_once 'Translation2.php'; $this->options['Translation2'] = new Translation2( $this->options['Translation2']['driver'], @$this->options['Translation2']['options'] ); } if (is_a($this->options['Translation2'],'Translation2')) { $this->options['Translation2']->setLang($this->options['locale']); // fixme - needs to be more specific to which template to use.. foreach ($this->options['templateDir'] as $tt) { $n = basename($flexy->currentTemplate); if (substr($flexy->currentTemplate,0,strlen($tt)) == $tt) { $n = substr($flexy->currentTemplate,strlen($tt)+1); } //echo $n; } $this->options['Translation2']->setPageID($n); } else { setlocale(LC_ALL, $this->options['locale']); } $data = $string; $res = false; if ($string === false) { $data = file_get_contents($flexy->currentTemplate); } // PRE PROCESS {_(.....)} translation markers. $got_gettext_markup = false; if (strpos($data,'{_(') !== false) { $matches = array(); $lmatches = explode ('{_(', $data); array_shift($lmatches); // shift the first.. foreach ($lmatches as $k) { if (false === strpos($k,')_}')) { continue; } $x = explode(')_}',$k); $matches[] = $x[0]; } //echo '<PRE>';print_r($matches); // we may need to do some house cleaning here... $gettextStrings = $matches; $got_gettext_markup = true; // replace them now.. // ** leaving in the tag (which should be ignored by the parser.. // we then get rid of the tags during the toString method in this class. foreach($matches as $v) { $data = str_replace('{_('.$v.')_}', '{_('.$this->translateString($v).')_}',$data); } } if (isset($_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)])) { $res = $_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)]; } else { $tokenizer = new HTML_Template_Flexy_Tokenizer($data); $tokenizer->fileName = $flexy->currentTemplate; //$tokenizer->debug=1; if ($this->options['nonHTML']) { $tokenizer->ignoreHTML = true; } if ($this->options['allowPHP']) { $tokenizer->ignorePHP = false; } $res = HTML_Template_Flexy_Token::buildTokens($tokenizer); $_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)] = $res; } if (is_a($res,'PEAR_Error')) { return $res; } // turn tokens into Template.. $data = $res->compile($this); if (is_a($data,'PEAR_Error')) { return $data; } $data = $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput'] . $data; if ( @$this->options['debug']) { echo "<B>Result: </B><PRE>".htmlspecialchars($data)."</PRE><BR>"; } if ($this->options['nonHTML']) { $data = str_replace("?>\n","?>\n\n",$data); } // at this point we are into writing stuff... if ($this->options['compileToString']) { $flexy->elements = $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements']; return $data; } // error checking? $file = $flexy->compiledTemplate; if (isset($flexy->options['output.block'])) { list($file,$part) = explode('#',$file ); } if( ($cfp = fopen( $file , 'w' )) ) { if (@$this->options['debug']) { echo "<B>Writing: </B>".htmlspecialchars($data)."<BR>"; } fwrite($cfp,$data); fclose($cfp); chmod($file,0775); // make the timestamp of the two items match. clearstatcache(); touch($file, filemtime($flexy->currentTemplate)); if ($file != $flexy->compiledTemplate) { chmod($flexy->compiledTemplate,0775); // make the timestamp of the two items match. clearstatcache(); touch($flexy->compiledTemplate, filemtime($flexy->currentTemplate)); } } else { return HTML_Template_Flexy::raiseError('HTML_Template_Flexy::failed to write to '.$flexy->compiledTemplate, HTML_TEMPLATE_FLEXY_ERROR_FILE ,HTML_TEMPLATE_FLEXY_ERROR_RETURN); } // gettext strings if (file_exists($flexy->getTextStringsFile)) { unlink($flexy->getTextStringsFile); } if($gettextStrings && ($cfp = fopen( $flexy->getTextStringsFile, 'w') ) ) { fwrite($cfp,serialize(array_unique($gettextStrings))); fclose($cfp); } // elements if (file_exists($flexy->elementsFile)) { unlink($flexy->elementsFile); } if( $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'] && ($cfp = fopen( $flexy->elementsFile, 'w') ) ) { fwrite($cfp,serialize( $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'])); fclose($cfp); // now clear it. } return true; } /** * Flag indicating compiler is inside {_( .... )_} block, and should not * add to the gettextstrings array. * * @var boolean * @access public */ var $inGetTextBlock = false; /** * This is the base toString Method, it relays into toString{TokenName} * * @param object HTML_Template_Flexy_Token_* * * @return string string to build a template * @access public * @see toString* */ function toString($element) { static $len = 26; // strlen('HTML_Template_Flexy_Token_'); if ($this->options['debug']) { $x = $element; unset($x->children); echo htmlspecialchars(print_r($x,true))."<BR>\n"; } if ($element->token == 'GetTextStart') { $this->inGetTextBlock = true; return ''; } if ($element->token == 'GetTextEnd') { $this->inGetTextBlock = false; return ''; } $class = get_class($element); if (strlen($class) >= $len) { $type = substr($class,$len); return $this->{'toString'.$type}($element); } $ret = $element->value; $add = $element->compileChildren($this); if (is_a($add,'PEAR_Error')) { return $add; } $ret .= $add; if ($element->close) { $add = $element->close->compile($this); if (is_a($add,'PEAR_Error')) { return $add; } $ret .= $add; } return $ret; } /** * HTML_Template_Flexy_Token_Else toString * * @param object HTML_Template_Flexy_Token_Else * * @return string string to build a template * @access public * @see toString* */ function toStringElse($element) { // pushpull states to make sure we are in an area.. - should really check to see // if the state it is pulling is a if... if ($element->pullState() === false) { return $this->appendHTML( "<font color=\"red\">Unmatched {else:} on line: {$element->line}</font>" ); } $element->pushState(); return $this->appendPhp("} else {"); } /** * HTML_Template_Flexy_Token_End toString * * @param object HTML_Template_Flexy_Token_Else * * @return string string to build a template * @access public * @see toString* */ function toStringEnd($element) { // pushpull states to make sure we are in an area.. - should really check to see // if the state it is pulling is a if... if ($element->pullState() === false) { return $this->appendHTML( "<font color=\"red\">Unmatched {end:} on line: {$element->line}</font>" ); } return $this->appendPhp("}"); } /** * HTML_Template_Flexy_Token_EndTag toString * * @param object HTML_Template_Flexy_Token_EndTag * * @return string string to build a template * @access public * @see toString* */ function toStringEndTag($element) { return $this->toStringTag($element); } /** * HTML_Template_Flexy_Token_Foreach toString * * @param object HTML_Template_Flexy_Token_Foreach * * @return string string to build a template * @access public * @see toString* */ function toStringForeach($element) { $loopon = $element->toVar($element->loopOn); if (is_a($loopon,'PEAR_Error')) { return $loopon; } $ret = 'if ($this->options[\'strict\'] || ('. 'is_array('. $loopon. ') || ' . 'is_object(' . $loopon . '))) ' . 'foreach(' . $loopon . " "; $ret .= "as \${$element->key}"; if ($element->value) { $ret .= " => \${$element->value}"; } $ret .= ") {"; $element->pushState(); $element->pushVar($element->key); $element->pushVar($element->value); return $this->appendPhp($ret); } /** * HTML_Template_Flexy_Token_If toString * * @param object HTML_Template_Flexy_Token_If * * @return string string to build a template * @access public * @see toString* */ function toStringIf($element) { $var = $element->toVar($element->condition); if (is_a($var,'PEAR_Error')) { return $var; } $ret = "if (".$element->isNegative . $var .") {"; $element->pushState(); return $this->appendPhp($ret); } /** * get Modifier Wrapper * * converts :h, :u, :r , ..... * @param object HTML_Template_Flexy_Token_Method|Var * * @return array prefix,suffix * @access public * @see toString* */ function getModifierWrapper($element) { $prefix = 'echo '; $suffix = ''; $modifier = strlen(trim($element->modifier)) ? $element->modifier : ' '; switch ($modifier{0}) { case 'h': break; case 'u': $prefix = 'echo urlencode('; $suffix = ')'; break; case 'r': $prefix = 'echo \'<pre>\'; echo htmlspecialchars(print_r('; $suffix = ',true)); echo \'</pre>\';'; break; case 'n': // blank or value.. $numberformat = @$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['numberFormat']; $prefix = 'echo number_format('; $suffix = $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['numberFormat'] . ')'; break; case 'b': // nl2br + htmlspecialchars $prefix = 'echo nl2br(htmlspecialchars('; // add language ? $suffix = '))'; break; case ' ': $prefix = 'echo htmlspecialchars('; // add language ? $suffix = ')'; break; default: $prefix = 'echo $this->plugin("'.trim($element->modifier) .'",'; $suffix = ')'; } return array($prefix,$suffix); } /** * HTML_Template_Flexy_Token_Var toString * * @param object HTML_Template_Flexy_Token_Method * * @return string string to build a template * @access public * @see toString* */ function toStringVar($element) { // ignore modifier at present!! $var = $element->toVar($element->value); if (is_a($var,'PEAR_Error')) { return $var; } list($prefix,$suffix) = $this->getModifierWrapper($element); return $this->appendPhp( $prefix . $var . $suffix .';'); } /** * HTML_Template_Flexy_Token_Method toString * * @param object HTML_Template_Flexy_Token_Method * * @return string string to build a template * @access public * @see toString* */ function toStringMethod($element) { // set up the modifier at present!! list($prefix,$suffix) = $this->getModifierWrapper($element); // add the '!' to if if ($element->isConditional) { $prefix = 'if ('.$element->isNegative; $element->pushState(); $suffix = ')'; } // check that method exists.. // if (method_exists($object,'method'); $bits = explode('.',$element->method); $method = array_pop($bits); $object = implode('.',$bits); $var = $element->toVar($object); if (is_a($var,'PEAR_Error')) { return $var; } if (($object == 'GLOBALS') && $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['globalfunctions']) { // we should check if they something weird like: GLOBALS.xxxx[sdf](....) $var = $method; } else { $prefix = 'if ($this->options[\'strict\'] || (isset('.$var. ') && method_exists('.$var .",'{$method}'))) " . $prefix; $var = $element->toVar($element->method); } if (is_a($var,'PEAR_Error')) { return $var; } $ret = $prefix; $ret .= $var . "("; $s =0; foreach($element->args as $a) { if ($s) { $ret .= ","; } $s =1; if ($a{0} == '#') { $ret .= '"'. addslashes(substr($a,1,-1)) . '"'; continue; } $var = $element->toVar($a); if (is_a($var,'PEAR_Error')) { return $var; } $ret .= $var; } $ret .= ")" . $suffix; if ($element->isConditional) { $ret .= ' { '; } else { $ret .= ";"; } return $this->appendPhp($ret); } /** * HTML_Template_Flexy_Token_Processing toString * * @param object HTML_Template_Flexy_Token_Processing * * @return string string to build a template * @access public * @see toString* */ function toStringProcessing($element) { // if it's XML then quote it.. if (strtoupper(substr($element->value,2,3)) == 'XML') { return $this->appendPhp("echo '" . str_replace("'","\\"."'", $element->value) . "';"); } // otherwise it's PHP code - so echo it.. return $element->value; } /** * HTML_Template_Flexy_Token_Text toString * * @param object HTML_Template_Flexy_Token_Text * * @return string string to build a template * @access public * @see toString* */ function toStringText($element) { // first get rid of stuff thats not translated etc. // empty strings => output. // comments -> just output // our special tags -> output.. if (!strlen(trim($element->value) )) { return $this->appendHtml($element->value); } // dont add comments to translation lists. if (substr($element->value,0,4) == '<!--') { return $this->appendHtml($element->value); } // ignore anything wrapped with {_( .... )_} if ($this->inGetTextBlock) { return $this->appendHtml($element->value); } // argTokens is built before the tag matching (it combined // flexy tags into %s, into the string, // and made a list of tokens in argTokens. if (!count($element->argTokens) && !$element->isWord()) { return $this->appendHtml($element->value); } // grab the white space at start and end (and keep it! $value = ltrim($element->value); $front = substr($element->value,0,-strlen($value)); $value = rtrim($element->value); $rear = substr($element->value,strlen($value)); $value = trim($element->value); // convert to escaped chars.. (limited..) //$value = strtr($value,$cleanArray); $this->addStringToGettext($value); $value = $this->translateString($value); // its a simple word! if (!count($element->argTokens)) { return $this->appendHtml($front . $value . $rear); } // there are subtokens.. // print_r($element->argTokens ); $args = array(); // these should only be text or vars.. foreach($element->argTokens as $i=>$token) { $args[] = $token->compile($this); } // we break up the translated string, and put the compiled tags // in between the values here. $bits = explode('%s',$value); $ret = $front; foreach($bits as $i=>$v) { $ret .= $v . @$args[$i]; } return $ret . $rear; } /** * addStringToGettext * * Adds a string to the gettext array. * * @param mixed preferably.. string to store * * @return none * @access public */ function addStringToGettext($string) { if (!is_string($string)) { return; } if (!preg_match('/[a-z]+/i', $string)) { return; } $string = trim($string); if (substr($string,0,4) == '<!--') { return; } $GLOBALS['_HTML_TEMPLATE_FLEXY_COMPILER']['gettextStrings'][] = $string; } /** * translateString - a gettextWrapper * * tries to do gettext or falls back on File_Gettext * This has !!!NO!!! error handling - if it fails you just get english.. * no questions asked!!! * * @param string string to translate * * @return string translated string.. * @access public */ function translateString($string) { if (is_a($this->options['Translation2'],'Translation2')) { $result = $this->options['Translation2']->get($string); if (!empty($result)) { return $result; } return $string; } // note this stuff may have been broken by removing the \n replacement code // since i dont have a test for it... it may remain broken.. // use Translation2 - it has gettext backend support // and should sort out the mess that \n etc. entail. $prefix = basename($GLOBALS['_HTML_TEMPLATE_FLEXY']['filename']).':'; if (@$this->options['debug']) { echo __CLASS__.":TRANSLATING $string<BR>"; } if (function_exists('gettext') && !$this->options['textdomain']) { if (@$this->options['debug']) { echo __CLASS__.":USING GETTEXT?<BR>"; } $t = gettext($string); if ($t != $string) { return $t; } $tt = gettext($prefix.$string); if ($tt != $prefix.$string) { return $tt; } // give up it's not translated anywhere... return $t; } if (!$this->options['textdomain'] || !$this->options['textdomainDir']) { // text domain is not set.. if (@$this->options['debug']) { echo __CLASS__.":MISSING textdomain settings<BR>"; } return $string; } $pofile = $this->options['textdomainDir'] . '/' . $this->options['locale'] . '/LC_MESSAGES/' . $this->options['textdomain'] . '.po'; // did we try to load it already.. if (@$GLOBALS['_'.__CLASS__]['PO'][$pofile] === false) { if (@$this->options['debug']) { echo __CLASS__.":LOAD failed (Cached):<BR>"; } return $string; } if (!@$GLOBALS['_'.__CLASS__]['PO'][$pofile]) { // default - cant load it.. $GLOBALS['_'.__CLASS__]['PO'][$pofile] = false; if (!file_exists($pofile)) { if (@$this->options['debug']) { echo __CLASS__.":LOAD failed: {$pofile}<BR>"; } return $string; } if (!@include_once 'File/Gettext.php') { if (@$this->options['debug']) { echo __CLASS__.":LOAD no File_gettext:<BR>"; } return $string; } $GLOBALS['_'.__CLASS__]['PO'][$pofile] = File_Gettext::factory('PO',$pofile); $GLOBALS['_'.__CLASS__]['PO'][$pofile]->load(); //echo '<PRE>'.htmlspecialchars(print_r($GLOBALS['_'.__CLASS__]['PO'][$pofile]->strings,true)); } $po = &$GLOBALS['_'.__CLASS__]['PO'][$pofile]; // we should have it loaded now... // this is odd - data is a bit messed up with CR's $string = str_replace('\n',"\n",$string); if (isset($po->strings[$prefix.$string])) { return $po->strings[$prefix.$string]; } if (!isset($po->strings[$string])) { if (@$this->options['debug']) { echo __CLASS__.":no match:<BR>"; } return $string; } if (@$this->options['debug']) { echo __CLASS__.":MATCHED: {$po->strings[$string]}<BR>"; } // finally we have a match!!! return $po->strings[$string]; } /** * HTML_Template_Flexy_Token_Tag toString * * @param object HTML_Template_Flexy_Token_Tag * * @return string string to build a template * @access public * @see toString* */ function toStringTag($element) { if (strpos($element->tag,':') === false) { $namespace = 'Tag'; } else { $bits = explode(':',$element->tag); $namespace = $bits[0]; } if ($namespace{0} == '/') { $namespace = substr($namespace,1); } if (empty($this->tagHandlers[$namespace])) { require_once 'HTML/Template/Flexy/Compiler/Flexy/Tag.php'; $this->tagHandlers[$namespace] = &HTML_Template_Flexy_Compiler_Flexy_Tag::factory($namespace,$this); if (!$this->tagHandlers[$namespace] ) { return HTML_Template_Flexy::raiseError('HTML_Template_Flexy::failed to create Namespace Handler '.$namespace . ' in file ' . $GLOBALS['_HTML_TEMPLATE_FLEXY']['filename'], HTML_TEMPLATE_FLEXY_ERROR_SYNTAX ,HTML_TEMPLATE_FLEXY_ERROR_RETURN); } } return $this->tagHandlers[$namespace]->toString($element); } } http://cvs.php.net/co.php/pear/HTML_Template_Flexy/Flexy/Compiler/Flexy/Flexy.php?r=1.1&p=1 Index: pear/HTML_Template_Flexy/Flexy/Compiler/Flexy/Flexy.php +++ pear/HTML_Template_Flexy/Flexy/Compiler/Flexy/Flexy.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Alan Knowles <alan@akkbhome.com> | // +----------------------------------------------------------------------+ // // $Id: Flexy.php,v 1.1 2004/07/08 04:53:17 alan_k Exp $ // // Handler code for the <flexy: namespace // /** * the <flexy:XXXX namespace * * * at present it handles * <flexy:toJavascript flexy:prefix="Javascript_prefix" javscriptName="PHPvar" .....> * <flexy:include src="xxx.htm"> * * * * @version $Id: Flexy.php,v 1.1 2004/07/08 04:53:17 alan_k Exp $ */ class HTML_Template_Flexy_Compiler_Flexy_Flexy { /** * Parent Compiler for * * @var object HTML_Template_Flexy_Compiler * * @access public */ var $compiler; /** * The current element to parse.. * * @var object * @access public */ var $element; /** * toString - display tag, attributes, postfix and any code in attributes. * Relays into namspace::method to get results.. * * * @see parent::toString() */ function toString($element) { list($namespace,$method) = explode(':',$element->oTag); if (!strlen($method)) { return ''; } // things we dont handle... if (!method_exists($this,$method.'ToString')) { return ''; } return $this->{$method.'ToString'}($element); } /** * toJavascript handler * <flexy:toJavascript flexy:prefix="some_prefix_" javascriptval="php.val" ....> * * @see parent::toString() */ function toJavascriptToString($element) { $ret = $this->compiler->appendPhp( "require_once 'HTML/Javascript/Convert.php';"); $ret .= $this->compiler->appendHTML("\n<script type='text/javascript'>\n"); $prefix = ''. $element->getAttribute('FLEXY:PREFIX'); foreach ($element->attributes as $k=>$v) { // skip directives.. if (strpos($k,':')) { continue; } if ($k == '/') { continue; } $v = substr($v,1,-1); $ret .= $this->compiler->appendPhp( '$__tmp = HTML_Javascript_Convert::convertVar('.$element->toVar($v) .',\''.$prefix . $k.'\',true);'. 'echo (is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;'); $ret .= $this->compiler->appendHTML("\n"); } $ret .= $this->compiler->appendHTML("</script>"); return $ret; } /** * include handler * <flexy:include src="test.html"> * * @see parent::toString() */ function includeToString($element) { // this is disabled by default... // we ignore modifier pre/suffix $arg = $element->getAttribute('SRC'); if (!$arg) { return $this->compiler->appendHTML("<B>Flexy:Include without a src=filename</B>"); } // ideally it would be nice to embed the results of one template into another. // however that would involve some complex test which would have to stat // the child templates anyway.. // compile the child template.... // output... include $this->options['compiled_templates'] . $arg . $this->options['locale'] . '.php' return $this->compiler->appendPHP( "\n". "\$x = new HTML_Template_Flexy(\$this->options);\n". "\$x->compile('{$arg}');\n". "\$x->outputObject(\$t);\n" ); } /** * Convert flexy tokens to HTML_Template_Flexy_Elements. * * @param object token to convert into a element. * @return object HTML_Template_Flexy_Element * @access public */ function toElement($element) { return ''; } /** * Handler for User defined functions in templates.. * <flexy:function name="xxxxx">.... </flexy:block> // equivilant to function xxxxx() { * <flexy:function call="{xxxxx}">.... </flexy:block> // equivilant to function {$xxxxx}() { * <flexy:function call="xxxxx">.... </flexy:block> // equivilant to function {$xxxxx}() { * * This will not handle nested blocks initially!! (and may cause even more problems with * if /foreach stuff..!! * * @param object token to convert into a element. * @access public */ function functionToString($element) { if ($arg = $element->getAttribute('NAME')) { // this is a really kludgy way of doing this!!! // hopefully the new Template Package will have a sweeter method.. $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput'] .= $this->compiler->appendPHP( "\nfunction _html_template_flexy_compiler_flexy_flexy_{$arg}(\$t,\$this) {\n"). $element->compileChildren($this->compiler) . $this->compiler->appendPHP( "\n}\n"); return ''; } if (!isset($element->ucAttributes['CALL'])) { return HTML_Template_Flexy::raiseError( ' tag flexy:function needs an argument call or name'. " Error on Line {$element->line} &lt;{$element->tag}&gt;", null, HTML_TEMPLATE_FLEXY_ERROR_DIE); } // call is a stirng : nice and simple.. if (is_string($element->ucAttributes['CALL'])) { $arg = $element->getAttribute('CALL'); return $this->compiler->appendPHP( "if (function_exists('_html_template_flexy_compiler_flexy_flexy_'.{$arg})) " . " _html_template_flexy_compiler_flexy_flexy_{$arg}(\$t,\$this);"); } // we make a big assumption here.. - it should really be error checked.. // that the {xxx} element is item 1 in the list... $e=$element->ucAttributes['CALL'][1]; $add = $e->toVar($e->value); if (is_a($add,'PEAR_Error')) { return $add; } return $this->compiler->appendPHP( "if (function_exists('_html_template_flexy_compiler_flexy_flexy_'.{$add})) ". "call_user_func_array('_html_template_flexy_compiler_flexy_flexy_'.{$add},array(\$t,\$this));"); } } http://cvs.php.net/co.php/pear/HTML_Template_Flexy/Flexy/Compiler/Flexy/Tag.php?r=1.1&p=1 Index: pear/HTML_Template_Flexy/Flexy/Compiler/Flexy/Tag.php +++ pear/HTML_Template_Flexy/Flexy/Compiler/Flexy/Tag.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Alan Knowles <alan@akbkhome> | // +----------------------------------------------------------------------+ // // $Id: Tag.php,v 1.1 2004/07/08 04:53:17 alan_k Exp $ /* FC/BC compatibility with php5 */ if ( (substr(phpversion(),0,1) < 5) && !function_exists('clone')) { eval('function clone($t) { return $t; }'); } /** * Compiler That deals with standard HTML Tag output. * Since it's pretty complex it has it's own class. * I guess this class should deal with the main namespace * and the parent (standard compiler can redirect other namespaces to other classes. * * one instance of these exists for each namespace. * * * @version $Id: Tag.php,v 1.1 2004/07/08 04:53:17 alan_k Exp $ */ class HTML_Template_Flexy_Compiler_Flexy_Tag { /** * Parent Compiler for * * @var object HTML_Template_Flexy_Compiler * * @access public */ var $compiler; /** * * Factory method to create Tag Handlers * * $type = namespace eg. <flexy:toJavascript loads Flexy.php * the default is this... (eg. Tag) * * * @param string Namespace handler for element. * @param object HTML_Template_Flexy_Compiler * * * @return object tag compiler * @access public */ function &factory($type,&$compiler) { if (!$type) { $type = 'Tag'; } // if we dont have a handler - just use the basic handler. if (!file_exists(dirname(__FILE__) . '/'. ucfirst(strtolower($type)) . '.php')) { $type = 'Tag'; } include_once 'HTML/Template/Flexy/Compiler/Flexy/' . ucfirst(strtolower($type)) . '.php'; $class = 'HTML_Template_Flexy_Compiler_Flexy_' . $type; if (!class_exists($class)) { return false; } $ret = new $class; $ret->compiler = &$compiler; return $ret; } /** * The current element to parse.. * * @var object * @access public */ var $element; /** * Flag to indicate has attribute flexy:foreach (so you cant mix it with flexy:if!) * * @var boolean * @access public */ var $hasForeach = false; /** * toString - display tag, attributes, postfix and any code in attributes. * Note first thing it does is call any parseTag Method that exists.. * * * @see parent::toString() */ function toString($element) { global $_HTML_TEMPLATE_FLEXY_TOKEN; global $_HTML_TEMPLATE_FLEXY; // store the element in a variable $this->element = $element; // echo "toString: Line {$this->element->line} &lt;{$this->element->tag}&gt;\n"; // if the FLEXYSTARTCHILDREN flag was set, only do children // normally set in BODY tag. // this will probably be superseeded by the Class compiler. if ($element->startChildren) { return $element->compileChildren($this->compiler); } $flexyignore = $this->parseAttributeIgnore(); // rewriting should be done with a tag.../flag. $this->reWriteURL("HREF"); $this->reWriteURL("SRC"); // handle elements if (($ret =$this->_parseTags()) !== false) { return $ret; } // these add to the close tag.. $ret = $this->parseAttributeForeach(); $ret .= $this->parseAttributeIf(); // spit ou the tag and attributes. if ($element->oTag{0} == '?') { $ret .= '<?php echo "<"; ?>'; } else { $ret .= "<"; } $ret .= $element->oTag; foreach ($element->attributes as $k=>$v) { // if it's a flexy tag ignore it. if (strtoupper($k) == 'FLEXY:RAW') { if (!is_array($v) || !isset($v[1]) || !is_object($v[1])) { return HTML_Template_Flexy::raiseError( 'flexy:raw only accepts a variable or method call as an argument, eg.'. ' flexy:raw="{somevalue}" you provided something else.' . " Error on Line {$this->element->line} &lt;{$this->element->tag}&gt;", null, HTML_TEMPLATE_FLEXY_ERROR_DIE); } $add = $v[1]->compile($this->compiler); if (is_a($add,'PEAR_Error')) { return $add; } $ret .= ' ' . $add; continue; } if (strtoupper(substr($k,0,6)) == 'FLEXY:') { continue; } // true == an attribute without a ="xxx" if ($v === true) { $ret .= " $k"; continue; } // if it's a string just dump it. if (is_string($v)) { $ret .= " {$k}={$v}"; continue; } // normally the value is an array of string, however // if it is an object - then it's a conditional key. // eg. if (something) echo ' SELECTED'; // the object is responsible for adding it's space.. if (is_object($v)) { $add = $v->compile($this->compiler); if (is_a($add,'PEAR_Error')) { return $add; } $ret .= $add; continue; } // otherwise its a key="sometext{andsomevars}" $ret .= " {$k}="; foreach($v as $item) { if (is_string($item)) { $ret .= $item; continue; } $add = $item->compile($this->compiler); if (is_a($add,'PEAR_Error')) { return $add; } $ret .= $add; } } $ret .= ">"; // post stuff this is probably in the wrong place... if ($element->postfix) { foreach ($element->postfix as $e) { $add = $e->compile($this->compiler); if (is_a($add,'PEAR_Error')) { return $add; } $ret .= $add; } } else if ($this->element->postfix) { // if postfixed by self.. foreach ($this->element->postfix as $e) { $add = $e->compile($this->compiler); if (is_a($add,'PEAR_Error')) { return $add; } $ret .= $add; } } // dump contents of script raw - to prevent gettext additions.. // print_r($element); if ($element->tag == 'SCRIPT') { foreach($element->children as $c) { //print_R($c); if (!$c) { continue; } if ($c->token == 'Text') { $ret .= $c->value; continue; } // techically we shouldnt have anything else inside of script tags. // as the tokeinzer is supposted to ignore it.. } } else { $add = $element->compileChildren($this->compiler); if (is_a($add,'PEAR_Error')) { return $add; } $ret .= $add; } // output the closing tag. if ($element->close) { $add = $element->close->compile($this->compiler); if (is_a($add,'PEAR_Error')) { return $add; } $ret .= $add; } // reset flexyignore $_HTML_TEMPLATE_FLEXY_TOKEN['flexyIgnore'] = $flexyignore; if (isset($_HTML_TEMPLATE_FLEXY['currentOptions']['output.block']) && ($_HTML_TEMPLATE_FLEXY['currentOptions']['output.block'] == $element->getAttribute('ID'))) { // echo $_HTML_TEMPLATE_FLEXY['compiledTemplate']; $fh = fopen($_HTML_TEMPLATE_FLEXY['compiledTemplate'],'w'); fwrite($fh,$ret); fclose($fh); } return $ret; } /** * Reads an flexy:foreach attribute - * * * @return string to add to output. * @access public */ function parseAttributeIgnore() { global $_HTML_TEMPLATE_FLEXY_TOKEN; $flexyignore = $_HTML_TEMPLATE_FLEXY_TOKEN['flexyIgnore']; if ($this->element->getAttribute('FLEXY:IGNORE') !== false) { $_HTML_TEMPLATE_FLEXY_TOKEN['flexyIgnore'] = true; $this->element->clearAttribute('FLEXY:IGNORE'); } return $flexyignore; } /** * Reads an flexy:foreach attribute - * * * @return string to add to output. * @access public */ function parseAttributeForeach() { $foreach = $this->element->getAttribute('FLEXY:FOREACH'); if ($foreach === false) { return ''; } //var_dump($foreach); $this->element->hasForeach = true; // create a foreach element to wrap this with. $foreachObj = $this->element->factory('Foreach', explode(',',$foreach), $this->element->line); // failed = probably not enough variables.. if ($foreachObj === false) { return HTML_Template_Flexy::raiseError( "Missing Arguments: An flexy:foreach attribute was foundon Line {$this->element->line} in tag &lt;{$this->element->tag} flexy:foreach=&quot;$foreach&quot; .....&gt;<BR> the syntax is &lt;sometag flexy:foreach=&quot;onarray,withvariable[,withanothervar] &gt;<BR>", null, HTML_TEMPLATE_FLEXY_ERROR_DIE); } // does it have a closetag? if (!$this->element->close) { if ($this->element->getAttribute('/') === false) { return HTML_Template_Flexy::raiseError( "A flexy:foreach attribute was found in &lt;{$this->element->name} tag without a corresponding &lt;/{$this->element->tag} tag on Line {$this->element->line} &lt;{$this->element->tag}&gt;", null, HTML_TEMPLATE_FLEXY_ERROR_DIE); } // it's an xhtml tag! $this->element->postfix = array($this->element->factory("End", '', $this->element->line)); } else { $this->element->close->postfix = array($this->element->factory("End", '', $this->element->line)); } $this->element->clearAttribute('FLEXY:FOREACH'); return $foreachObj->compile($this->compiler); } /** * Reads an flexy:if attribute - * * * @return string to add to output. * @access public */ function parseAttributeIf() { // dont use the together, if is depreciated.. $if = $this->element->getAttribute('FLEXY:IF'); if ($if === false) { return ''; } if (isset($this->element->hasForeach)) { return HTML_Template_Flexy::raiseError( "You may not use FOREACH and IF tags in the same tag on Line {$this->element->line} &lt;{$this->element->tag}&gt;", null, HTML_TEMPLATE_FLEXY_ERROR_DIE); } // allow if="!somevar" $ifnegative = ''; if ($if{0} == '!') { $ifnegative = '!'; $if = substr($if,1); } // if="xxxxx" // if="xxxx.xxxx()" - should create a method prefixed with 'if:' // these checks should really be in the if/method class..!!! if (!preg_match('/^[_A-Z][A-Z0-9_]*(\[[0-9]+\])?((\[|%5B)[A-Z0-9_]+(\]|%5D))*'. '(\.[_A-Z][A-Z0-9_]*((\[|%5B)[A-Z0-9_]+(\]|%5D))*)*(\\([^)]*\))?$/i',$if)) { return HTML_Template_Flexy::raiseError( "IF tags only accept simple object.variable or object.method() values on Line {$this->element->line} &lt;{$this->element->tag}&gt; {$if}", null, HTML_TEMPLATE_FLEXY_ERROR_DIE); } if (substr($if,-1) == ')') { // grab args.. $args = substr($if,strpos($if,'(')+1,-1); // simple explode ... $args = strlen(trim($args)) ? explode(',',$args) : array(); //print_R($args); // this is nasty... - we need to check for quotes = eg. # at beg. & end.. $args_clean = array(); for ($i=0; $i<count($args); $i++) { if ($args[$i]{0} != '#') { $args_clean[] = $args[$i]; continue; } // single # - so , must be inside.. if ((strlen($args[$i]) > 1) && ($args[$i]{strlen($args[$i])-1}=='#')) { $args_clean[] = $args[$i]; continue; } $args[$i] .=',' . $args[$i+1]; // remove args+1.. array_splice($args,$i+1,1); $i--; // reparse.. } $ifObj = $this->element->factory('Method', array('if:'.$ifnegative.substr($if,0,strpos($if,'(')), $args_clean), $this->element->line); } else { $ifObj = $this->element->factory('If', $ifnegative.$if, $this->element->line); } // does it have a closetag? - you must have one - so you will have to hack in <span flexy:if=..><img></span> on tags // that do not have close tags - it's done this way to try and avoid mistakes. if (!$this->element->close) { //echo "<PRE>";print_R($this->element); if ($this->element->getAttribute('/') !== false) { $this->element->postfix = array($this->element->factory("End",'', $this->element->line)); } else { return HTML_Template_Flexy::raiseError( "An flexy:if attribute was found in &lt;{$this->element->name} tag without a corresponding &lt;/{$this->element->name} tag on Line {$this->element->line} &lt;{$this->element->tag}&gt;", null, HTML_TEMPLATE_FLEXY_ERROR_DIE); } } else { $this->element->close->postfix = array($this->element->factory("End",'', $this->element->line)); } $this->element->clearAttribute('FLEXY:IF'); return $ifObj->compile($this->compiler); } /** * Reads Tags - and relays to parseTagXXXXXXX * * * @return string | false = html output or ignore (just output the tag) * @access private */ function _parseTags() { global $_HTML_TEMPLATE_FLEXY_TOKEN; // doesnt really need strtolower etc. as php functions are not case sensitive! if ($this->element->getAttribute('FLEXY:DYNAMIC')) { return $this->compiler->appendPhp( $this->getElementPhp( $this->element->getAttribute('ID') ) ); } if ($this->element->getAttribute('FLEXY:IGNOREONLY') !== false) { return false; } if ($_HTML_TEMPLATE_FLEXY_TOKEN['flexyIgnore']) { return false; } $tag = $this->element->tag; if (strpos($tag,':') !== false) { $bits = explode(':',$tag); $tag = $bits[1]; } $method = 'parseTag'.$tag; if (!method_exists($this,$method)) { return false; } // do any of the attributes use flexy data... foreach ($this->element->attributes as $k=>$v) { if (is_array($v)) { return false; } } //echo "call $method" . serialize($this->element->attributes). "\n"; return $this->$method(); // allow the parse methods to return output. } /** * produces the code for dynamic elements * * @return string | false = html output or ignore (just output the tag) * @access public */ function getElementPhp($id,$mergeWithName=false) { global $_HTML_TEMPLATE_FLEXY; static $tmpId=0; if (!$id) { return HTML_Template_Flexy::raiseError( "Error:{$_HTML_TEMPLATE_FLEXY['filename']} on Line {$this->element->line} &lt;{$this->element->tag}&gt;: " . " Dynamic tags require an ID value", null, HTML_TEMPLATE_FLEXY_ERROR_DIE); } // dont mix and match.. if (($this->element->getAttribute('FLEXY:IF') !== false) || ($this->element->getAttribute('FLEXY:FOREACH') !== false) ) { return HTML_Template_Flexy::raiseError( "Error:{$_HTML_TEMPLATE_FLEXY['filename']} on Line {$this->element->line} &lt;{$this->element->tag}&gt;: " . " You can not mix flexy:if= or flexy:foreach= with dynamic form elements " . " (turn off tag to element code with flexyIgnore=0, use flexy:ignore=&quot;yes&quot; in the tag" . " or put the conditional outside in a span tag", null, HTML_TEMPLATE_FLEXY_ERROR_DIE); } if ((strtolower($this->element->getAttribute('type')) == 'checkbox' ) && (substr($this->element->getAttribute('name'),-2) == '[]')) { if ($this->element->getAttribute('id') === false) { $id = 'tmpId'. (++$tmpId); $this->element->attributes['id'] = $id; $this->element->ucAttributes['ID'] = $id; } $mergeWithName = true; } if (isset($_HTML_TEMPLATE_FLEXY['elements'][$id])) { // echo "<PRE>";print_r($this);print_r($_HTML_TEMPLATE_FLEXY['elements']);echo "</PRE>"; return HTML_Template_Flexy::raiseError( "Error:{$_HTML_TEMPLATE_FLEXY['filename']} on Line {$this->element->line} in Tag &lt;{$this->element->tag}&gt;:<BR> " . "The Dynamic tag Name '$id' has already been used previously by tag &lt;{$_HTML_TEMPLATE_FLEXY['elements'][$id]->tag}&gt;", null,HTML_TEMPLATE_FLEXY_ERROR_DIE); } // this is for a case where you can use a sprintf as the name, and overlay it with a variable element.. $_HTML_TEMPLATE_FLEXY['elements'][$id] = $this->toElement($this->element); if ($var = $this->element->getAttribute('FLEXY:NAMEUSES')) { $var = 'sprintf(\''.$id .'\','.$this->element->toVar($var) .')'; return 'if (!isset($this->elements['.$var.'])) $this->elements['.$var.']= $this->elements[\''.$id.'\']; $this->elements['.$var.'] = $this->mergeElement($this->elements[\''.$id.'\'],$this->elements['.$var.']); $this->elements['.$var.']->attributes[\'name\'] = '.$var. '; echo $this->elements['.$var.']->toHtml();'; } elseif ($mergeWithName) { $name = $this->element->getAttribute('NAME'); return '$element = $this->elements[\''.$id.'\']; $element = $this->mergeElement($element,$this->elements[\''.$name.'\']); echo $element->toHtml();'; } else { return 'echo $this->elements[\''.$id.'\']->toHtml();'; } } /** * Reads an Script tag - check if PHP is allowed. * * @return false|PEAR_Error * @access public */ function parseTagScript() { $lang = $this->element->getAttribute('LANGUAGE'); if (!$lang) { return false; } $lang = strtoupper($lang); if ($GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['allowPHP']) { return false; } if ($lang == "PHP") { return HTML_Template_Flexy::raiseError('PHP code found in script', HTML_TEMPLATE_FLEXY_ERROR_SYNTAX,HTML_TEMPLATE_FLEXY_ERROR_RETURN ); } return false; } /** * Reads an Input tag - build a element object for it * * * @return string | false = html output or ignore (just output the tag) * @access public */ function parseTagInput() { global $_HTML_TEMPLATE_FLEXY; if (in_array(strtoupper($this->element->getAttribute('TYPE')), array('SUBMIT','BUTTON','INPUT',''))) { $this->compiler->addStringToGettext($this->element->getAttribute('VALUE')); } // form elements : format: //value - fill out as PHP CODE // as a general rule, this uses name, rather than ID except on // radio $mergeWithName = false; $id = $this->element->getAttribute('NAME'); // checkboxes need more work.. - at the momemnt assume one with the same value... if (in_array(strtoupper($this->element->getAttribute('TYPE')), array('RADIO'))) { if (!isset($_HTML_TEMPLATE_FLEXY['elements'][$id])) { // register it.. - so we dont overwrite it... $_HTML_TEMPLATE_FLEXY['elements'][$id] = false; } else if ($_HTML_TEMPLATE_FLEXY['elements'][$id] != false) { return HTML_Template_Flexy::raiseError( "Error:{$_HTML_TEMPLATE_FLEXY['filename']} on Line {$this->element->line} ". "in Tag &lt;{$this->element->tag}&gt;:<BR>". "The Dynamic tag Name '$id' has already been used previously by ". "tag &lt;{$_HTML_TEMPLATE_FLEXY['elements'][$id]->tag}&gt;", null, HTML_TEMPLATE_FLEXY_ERROR_DIE ); } $id = $this->element->getAttribute('ID'); if (!$id) { return HTML_Template_Flexy::raiseError("Error on Line {$this->element->line} &lt;{$this->element->tag}&gt: Radio Input's require an ID tag..", null, HTML_TEMPLATE_FLEXY_ERROR_DIE); } $mergeWithName = true; } if (!$id) { return false; } return $this->compiler->appendPhp($this->getElementPhp( $id,$mergeWithName)); } /** * Deal with a TextArea tag - build a element object for it * * @return string | false = html output or ignore (just output the tag) * @access public */ function parseTagTextArea() { return $this->compiler->appendPhp( $this->getElementPhp( $this->element->getAttribute('NAME'))); } /** * Deal with Selects - build a element object for it (unless flexyignore is set) * * * @return string | false = html output or ignore (just output the tag) * @access public */ function parseTagSelect() { return $this->compiler->appendPhp( $this->getElementPhp( $this->element->getAttribute('NAME'))); } /** * Reads an Form tag - and set up the element object header etc. * * @return string | false = html output or ignore (just output the tag) * @access public */ function parseTagForm() { global $_HTML_TEMPLATE_FLEXY; $copy = clone($this->element); $copy->children = array(); $id = $this->element->getAttribute('NAME'); if (!$id) { $id = 'form'; } // this adds the element to the elements array. $old = clone($this->element); $this->element = $copy; $this->getElementPhp($id); $this->element= $old; return $this->compiler->appendPhp('echo $this->elements[\''.$id.'\']->toHtmlnoClose();') . $this->element->compileChildren($this->compiler) . $this->compiler->appendHtml( "</{$copy->oTag}>"); } /** * reWriteURL - can using the config option 'url_rewrite' * format "from:to,from:to" * only handle left rewrite. * so * "/images:/myroot/images" * would change * /images/xyz.gif to /myroot/images/xyz.gif * /images/stylesheet/imagestyles.css to /myroot/images/stylesheet/imagestyles.css * note /imagestyles did not get altered. * will only work on strings (forget about doing /images/{someimage} * * * @param string attribute to rewrite * @return none * @access public */ function reWriteURL($which) { global $_HTML_TEMPLATE_FLEXY; if (!is_string($original = $this->element->getAttribute($which))) { return; } if ($original == '') { return; } if (empty($_HTML_TEMPLATE_FLEXY['currentOptions']['url_rewrite'])) { return; } $bits = explode(",",$_HTML_TEMPLATE_FLEXY['currentOptions']['url_rewrite']); $new = $original; foreach ($bits as $bit) { if (!strlen(trim($bit))) { continue; } $parts = explode (':', $bit); if (!isset($parts[1])) { return HTML_Template_Flexy::raiseError('HTML_Template_Flexy: url_rewrite syntax incorrect'. print_r(array($bits,$bits),true),null,HTML_TEMPLATE_FLEXY_ERROR_DIE); } $new = preg_replace('#^'.$parts[0].'#',$parts[1], $new); } if ($original == $new) { return; } $this->element->ucAttributes[$which] = '"'. $new . '"'; } /** * Convert flexy tokens to HTML_Template_Flexy_Elements. * * @param object token to convert into a element. * @return object HTML_Template_Flexy_Element * @access public */ function toElement($element) { require_once 'HTML/Template/Flexy/Element.php'; $ret = new HTML_Template_Flexy_Element; if (strtolower(get_class($element)) != 'html_template_flexy_token_tag') { $this->compiler->addStringToGettext($element->value); return $element->value; } $ret->tag = strtolower($element->tag); $ats = $element->getAttributes(); if (isset($element->attributes['flexy:xhtml'])) { $ats['flexy:xhtml'] = true; } foreach(array_keys($ats) as $a) { $ret->attributes[$a] = $this->unHtmlEntities($ats[$a]); } //print_r($ats); if (!$element->children) { return $ret; } // children - normally to deal with <element> //print_r($this->children); foreach(array_keys($element->children) as $i) { // not quite sure why this happens - but it does. if (!is_object($element->children[$i])) { continue; } $ret->children[] = $this->toElement($element->children[$i]); } return $ret; } /** * do the reverse of htmlspecialchars on an attribute.. * * copied from get-html-translation-table man page * * @param mixed from attribute values * * @return string return * @access public * @see see also methods..... */ function unHtmlEntities ($in) { if (!is_string($in)) { return $in; } $trans_tbl = get_html_translation_table (HTML_ENTITIES); $trans_tbl = array_flip ($trans_tbl); $ret = strtr ($in, $trans_tbl); return preg_replace('/&#(\d+);/me', "chr('\\1')",$ret); } }
« previous php.pear.cvs (#21623) next »