cvs: pear /Template/Template/Filter ColourizeCode.php TrimWhiteSpace.php

From: Date: Fri, 25 Jun 2004 12:51:30 +0000
Subject: cvs: pear /Template/Template/Filter ColourizeCode.php TrimWhiteSpace.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-21112@lists.php.net to get a copy of this message
alan_k Fri Jun 25 08:51:30 2004 EDT Added files: /pear/Template/Template/Filter ColourizeCode.php TrimWhiteSpace.php Log: adding pauls filters http://cvs.php.net/co.php/pear/Template/Template/Filter/ColourizeCode.php?r=1.1&p=1 Index: pear/Template/Template/Filter/ColourizeCode.php +++ pear/Template/Template/Filter/ColourizeCode.php <?php /** * * Colorizes all text between <code>...</code> tags with PHP's * highlight_string function. Additionally, this will convert HTML * entities within <code> blocks, so you can demonstrate HTML tags * without them being honored in the browser. Also converts tabs to four * spaces. * * To mark the beginning of php code in a <code> block, use the custom * tag <php>, and use </php> to mark the end. You can't use the normal * php code tags because Savant will try to execute that code when the * template runs. * * @author Paul M. Jones <pmjones@ciaweb.net> * * @version $Id: ColourizeCode.php,v 1.1 2004/06/25 12:51:30 alan_k Exp $ * */ class Template_Filter_ColorizeCode extends Template_Filter { /** * do the filtering * * * @param string input string * * @return string|PEAR_Error * @access public * @see Template_Filter::filter() */ function filter(&$text) { // break the source into blocks by the beginning <code> tag. // this will remove the text "<code>" from the block, so // we'll need to add it in again later. $blocks = explode('<code>', $text); // loop through each block and convert text within // <code></code> tags. foreach ($blocks as $key => $val) { // now find then the ending </code> within the block $pos = strpos($val, '</code>'); if ($pos === false) { // there was no </code> tag -- do nothing } else { // change all <php> and </php> tags $val = str_replace('<php>', '<?php', $val); $val = str_replace('</php>', '?>', $val); // <?php // $tmp[0] will be the part before </code>, and // thus the part we want to modify. // // $tmp[1] will be the part after the // <code></code> block, which we will leave // alone. // // this will remove the text "</code>" from the // text, so we'll need to add it in again when modifying // the text. $tmp = explode('</code>', $val); // set entities by highlighting the string. we do the // output buffering ob() thing because the native // highlight_string() dumps the output to the screen // instead of returning to a variable (before PHP // 4.2.2). $tmp[0] = trim($tmp[0]); ob_start(); highlight_string($tmp[0]); $tmp[0] = ob_get_contents(); ob_end_clean(); // remove break tags from the highlighted text $tmp[0] = str_replace("<br />", "\n", $tmp[0]); // convert tabs to 4-spaces and then // re-surround with <code> tags $tmp[0] = str_replace("\t", ' ', $tmp[0]); // save the modified text in the block $blocks[$key] = $tmp[0] . $tmp[1]; } } // reassemble the blocks $text = implode('', $blocks); } } http://cvs.php.net/co.php/pear/Template/Template/Filter/TrimWhiteSpace.php?r=1.1&p=1 Index: pear/Template/Template/Filter/TrimWhiteSpace.php +++ pear/Template/Template/Filter/TrimWhiteSpace.php <?php /** * * Trim leading white space and blank lines from template source after it * gets interpreted, cleaning up code and saving bandwidth. Does not * affect <<PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br> * * @author Monte Ohrt <monte@ispi.net> * * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de> * * @author Converted to a Savant filter by Paul M. Jones * <pmjones@ciaweb.net> * * @version $Id: TrimWhiteSpace.php,v 1.1 2004/06/25 12:51:30 alan_k Exp $ * */ class Template_Filter_TrimWhiteSpace extends Template_Filter { /** * Filter the string * * * @param string input string * * @return string output string * @access public */ function filter(&$source) { // Pull out the script blocks preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match); $_script_blocks = $match[0]; $source = preg_replace("!<script[^>]+>.*?</script>!is", '@@@TEMPLATE:TRIM:SCRIPT@@@', $source); // Pull out the pre blocks preg_match_all("!<pre>.*?</pre>!is", $source, $match); $_pre_blocks = $match[0]; $source = preg_replace("!<pre>.*?</pre>!is", '@@@TEMPLATE:TRIM:PRE@@@', $source); // Pull out the textarea blocks preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $source, $match); $_textarea_blocks = $match[0]; $source = preg_replace("!<textarea[^>]+>.*?</textarea>!is", '@@@TEMPLATE:TRIM:TEXTAREA@@@', $source); // remove all leading spaces, tabs and carriage returns NOT // preceeded by a php close tag. $source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source)); // replace script blocks $this->_replace( "@@@TEMPLATE:TRIM:SCRIPT@@@",$_script_blocks, $source); // replace pre blocks $this->_replace( "@@@TEMPLATE:TRIM:PRE@@@",$_pre_blocks, $source); // replace textarea blocks $this->_replace( "@@@TEMPLATE:TRIM:TEXTAREA@@@",$_textarea_blocks, $source); return $source; } /** * put back the special elements that cant have space trimmed from them.. * * @param string matching string. * @param array array of source matches * @param string the full string to modify.. * * * @return none * @access private */ function _replace($search_str, $replace, &$subject) { $_len = strlen($search_str); $_pos = 0; for ($_i=0, $_count=count($replace); $_i<$_count; $_i++) { if (($_pos=strpos($subject, $search_str, $_pos))!==false) { $subject = substr_replace($subject, $replace[$_i], $_pos, $_len); } else { break; } } } } ?>

« previous php.pear.cvs (#21112) next »