Thread: [phpwebapp-commits] CVS: web_app/convert convert.php,NONE,1.1 class.Template.php,NONE,1.1 class.Rend
Brought to you by:
dashohoxha
From: Dashamir H. <das...@us...> - 2004-07-20 16:51:23
|
Update of /cvsroot/phpwebapp/web_app/convert In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6986/convert Added Files: convert.php class.Template.php class.Render.php class.Parser.php README Log Message: converting old templates for the new version of the framework (which requires XML templates) --- NEW FILE: convert.php --- #!/usr/bin/php -q <?php include 'class.Template.php'; include 'class.Parser.php'; include 'class.Render.php'; $tpl_collection = array(); $parser = new Parser; $render = new Render; for ($i=1; $i < $argc; $i++) { $fname = $argv[$i]; print "$fname\n"; $tpl = $parser->parse_file($fname); $render->render_tpl($tpl); $tpl_str = $render->get_result(); $tpl_str = reformat($tpl_str); //backup and write the file print shell_exec("cp $fname $fname.orig"); $fp = fopen($fname, 'w'); fputs($fp, $tpl_str); fclose($fp); //print $tpl_str; } function reformat($str) { $str = str_replace('<>', '!=', $str); //match any starting or ending tag and make it lowercase $pattern = '#</?(\w+)[^<>]*>#'; $str = preg_replace_callback($pattern, 'lowercase', $str); //handle empty tags $empty_tag = '(br|hr|img|input|meta|link|include|parameter)'; $pattern = '#(<'.$empty_tag.'([^>]*[^/])?)>#'; $str = preg_replace($pattern, '$1 />', $str); //match any attribute name and make it lowercase $pattern = '#\s+(\w+)\s*=\s*(["\'])[^"\']*["\']#'; $str = preg_replace_callback($pattern, 'lowercase', $str); //replace {{checked}} by checked="{{checked}}" $pattern = '#(<input[^<>]+type=.checkbox[^<>]+\s)(\{\{[^\}]+\}\})#'; $str = preg_replace($pattern, '${1}checked="$2"', $str); //replace {{selected}} by selected="{{selected}}" $pattern = '#(<option[^<>]+)(\{\{[^\}]+\}\})#'; $str = preg_replace($pattern, '${1}selected="$2"', $str); return $str; } function lowercase($matches) { $str = str_replace($matches[1], strtolower($matches[1]), $matches[0]); return $str; } ?> --- NEW FILE: class.Template.php --- <?php class Template { var $id; var $type; var $contents; /** Constructor */ function Template($type ="Template") { static $default_id = "Template_001"; $this->id = $default_id++; $this->type = $type; $this->contents = array(); } } ?> --- NEW FILE: class.Render.php --- <?php class Render { var $result; function Render() { $this->result = ''; } function get_result() { $result = $this->result; $this->result = ''; return $result; } /** Renders the given template including all subtemplates. */ function render_tpl($tpl) { global $tpl_collection;; $i = 0; while ($i < sizeof($tpl->content)) { $line = $tpl->content[$i++]; if (chop($line)<>"##") { $this->result .= $line; } else //subtemplate indicator found { //get the subtemplate $line = $tpl->content[$i++]; $subtpl_id = trim($line); $subtpl = $tpl_collection[$subtpl_id]; //render the subtemplate if ($subtpl->type=='RepeatTpl') $this->render_RepeatTpl($subtpl); else $this->render_tpl($subtpl); } } } function render_RepeatTpl($tpl) { //decrease the indentation of header $header = $tpl->header; for ($i=0; $i < sizeof($header->content); $i++) { $line = $header->content[$i]; $line = ereg_replace('^ ', '', $line); $header->content[$i] = $line; } //decrease the indentation of footer $footer = $tpl->footer; for ($i=0; $i < sizeof($footer->content); $i++) { $line = $footer->content[$i]; $line = ereg_replace('^ ', '', $line); $footer->content[$i] = $line; } //increase the indentation of <Repeat> and <RepeatBody> for ($i=0; $i < sizeof($tpl->content); $i++) { $line = $tpl->content[$i]; if (ereg('^ *</?Repeat', $line)) { $tpl->content[$i] = ' '.$line; } } $this->render_tpl($header); $this->render_tpl($tpl); $this->render_tpl($footer); } } ?> --- NEW FILE: class.Parser.php --- <?php class Parser { /** A stack of templates that are being parsed. */ var $tpl_stack; /** The template that is being parsed currently. */ var $current_tpl; /** file pointer */ var $fp; function Parser() { $this->tpl_stack = array(); $this->current_tpl = UNDEFINED; } function parse_file($filename) { if ( !file_exists($filename) ) { print "'$filename' not found\n"; return UNDEFINED; } $tpl = new Template; $this->fp = fopen($filename, "r"); //open file //parse the file into $tpl $this->push($tpl); $this->parse(); $tpl = $this->pop(); //close the file fclose($this->fp); return $tpl; } /** * Loads the current template from * the file that is being parsed currently. */ function parse() { $closing_tag = false; while (!$closing_tag and !feof($this->fp)) { $line = $this->get_line(); if ($line=='') continue; $closing_tag = $this->parse_line($line); } } /** * Return true when the line is the closing tag of a template; * if the line contains the opening tag of another template, call * a function to parse this template, else add the line to the * current template. */ function parse_line($line) { $tag = Parser::get_tag_name($line); switch ($tag) { default: $this->add_line($line); break; case "<Repeat": $this->parse_Repeat($line); break; case "</Repeat": $this->add_line($line); return true; break; case "<Header": case "<Footer": $this->parse_Repeat_subtpl($line); break; case "</Header": case "</Footer": return true; break; } return false; } /** * Make the given $tpl current, what will be * parsed from now on goes to this template. */ function push(&$tpl) { //push the current_tpl on tpl_stack array_push($this->tpl_stack, $this->current_tpl); //make current the new template $this->current_tpl = &$tpl; } /** * The parsing of the current template has finished; * make current the top of the stack. */ function pop() { //pop the last template from tpl_stack into current_tpl $tpl = $this->current_tpl; $last = count($this->tpl_stack) - 1; $this->current_tpl = &$this->tpl_stack[$last]; array_pop($this->tpl_stack); //add the template in the template collection global $tpl_collection; $tpl_collection[$tpl->id] = $tpl; return $tpl; } function parse_Repeat($line) { $tpl = new Template('RepeatTpl'); $tpl->header = new Template; $tpl->footer = new Template; //set a pointer from this template to the new template $this->add_line("##\n"); $this->add_line($tpl->id."\n"); //parse the new template into $tpl $this->push($tpl); $this->add_line($line); $this->parse(); $this->pop(); } /** Parse <Header> and <Footer> */ function parse_Repeat_subtpl($line) { $tag = Parser::get_tag_name($line); $tag = substr($tag, 1); $tpl = new Template; $this->push($tpl); $this->parse(); $tpl = $this->pop(); switch ($tag) { case "Header": $this->current_tpl->header = $tpl; break; case "Footer": $this->current_tpl->footer = $tpl; break; } } /** returns a line from the file that is being parsed */ function get_line() { $line = fgets($this->fp,896); return $line; } /** add a line to the current template */ function add_line($line) { $this->current_tpl->content[] = $line; } /** * If the $line starts with an opening or closing tag * ('<tag_name' or '</tag_name') then it returns it, * otherwise returns UNDEFINED. */ function get_tag_name($line) { $line = trim($line); if ( ereg("^(</?[^[:space:] >]*)", $line, $regs) ) { return $regs[1]; } else { return UNDEFINED; } } /** * Returns the value of the given attribute from the given * line (assuming that the line is a start element), or * UNDEFINED if not found. */ function get_attr_value($line, $attrib) { $line = trim($line); $pattern = '[[:space:]]+'.$attrib.'[[:space:]]*=[[:space:]]*"([^"]*)"'; if ( ereg($pattern, $line, $regs) ) { $attr = $regs[1]; return $attr; } else { return UNDEFINED; } } } ?> --- NEW FILE: README --- The PHP script 'convert.php' (which includes also 'class.Template.php', 'class.Parser.php' and 'class.Render.php') is used to convert the templates of an existing web_app application for use with the new version of the framework. What is does is this: - Removes the elements <Header> and <Footer> from the <Repeat> element and puts their contents respectivly before and after the <Repeat> element. - Replaces 'not-equal operator' '<>' by '!='. - Makes all the tags lowercase. - Makes all the names of the attributes lowercase. - Replaces: <input type="checkbox" {{checked}}> by <input type="checkbox" checked="{{checked}}"> and <option {{selected}}> by <option selected="{{selected}}"> It can be used like this: $ ./convert.sh ../templates/*.html $ find ../templates/ -name '*.html' | xargs ./convert.sh After a template is converted, it is saved with the same name, however it is copied first to the backup file filename.orig (e.g. page.html.orig). After making sure that all the templates are converted successfully, the backup files can be deleted with the command: $ find ../templates/ -name '*.html.orig' -exec rm {} \; All the backup files can be restored like this: $ find ../templates/ -name '*.html' -exec cp {}.orig {} \; |