[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 3 require_once 'Horde.php'; 4 require_once 'Horde/String.php'; 5 6 /** 7 * Highlights quoted messages with different colors for the different quoting 8 * levels. 9 * 10 * CSS class names called "quoted1" ... "quoted{$cssLevels}" must be present. 11 * The text to be passed in must have already been passed through 12 * htmlspecialchars(). 13 * 14 * Parameters: 15 * <pre> 16 * 'citeblock' -- Display cite blocks? Defaults to true. 17 * 'cssLevels' -- Number of defined CSS class names. Defaults to 5. 18 * 'hideBlocks' -- Hide quoted text blocks by default? Defaults to false. 19 * </pre> 20 * 21 * $Horde: framework/Text_Filter/Filter/highlightquotes.php,v 1.6.8.16 2006/08/16 16:56:00 slusarz Exp $ 22 * 23 * Copyright 2004-2006 Michael Slusarz <slusarz@curecanti.org> 24 * Copyright 2004-2006 Jan Schneider <jan@horde.org> 25 * 26 * See the enclosed file COPYING for license information (LGPL). If you did 27 * not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 28 * 29 * @author Michael Slusarz <slusarz@curecanti.org> 30 * @author Jan Schneider <jan@horde.org> 31 * @since Horde 3.0 32 * @package Horde_Text 33 */ 34 class Text_Filter_highlightquotes extends Text_Filter { 35 36 /** 37 * Filter parameters. 38 * 39 * @var array 40 */ 41 var $_params = array( 42 'citeblock' => true, 43 'cssLevels' => 5, 44 'hideBlocks' => false 45 ); 46 47 /** 48 * Executes any code necessaray before applying the filter patterns. 49 * 50 * @param string $text The text before the filtering. 51 * 52 * @return string The modified text. 53 */ 54 function preProcess($text) 55 { 56 /* Tack a newline onto the beginning of the string so that we 57 * correctly highlight when the first character in the string is a 58 * quote character. */ 59 return "\n$text"; 60 } 61 62 /** 63 * Returns a hash with replace patterns. 64 * 65 * @return array Patterns hash. 66 */ 67 function getPatterns() 68 { 69 /* Remove extra spaces before quoted text as the CSS formatting will 70 * automatically add a bit of space for us. */ 71 if ($this->_params['citeblock']) { 72 return array('regexp' => array("/<br \/>\s*\n\s*<br \/>\s*\n\s*((>\s?)+)/m" => "<br />\n\\1")); 73 } 74 75 return array(); 76 } 77 78 /** 79 * Executes any code necessaray after applying the filter patterns. 80 * 81 * @param string $text The text after the filtering. 82 * 83 * @return string The modified text. 84 */ 85 function postProcess($text) 86 { 87 /* Is there a blank line(s) before the current line? */ 88 $blankline = false; 89 90 /* Use cite blocks to display the different quoting levels? */ 91 $cb = $this->_params['citeblock']; 92 93 /* Cite level before parsing the current line. */ 94 $qlevel = 0; 95 96 /* Generated output. */ 97 $text_out = ''; 98 99 /* Add show/hide toggles for whole quote blocks. */ 100 $text = preg_replace_callback('/(\n)(( *(>\s?)+(?! ?>).*?)(\n|$)(?! *(> ?)+))/s', 101 array($this, '_addQuoteToggles'), $text); 102 103 /* Parse text line by line. */ 104 foreach (explode("\n", $text) as $line) { 105 /* Cite level of current line. */ 106 $clevel = $closelevel = 0; 107 $matches = array(); 108 /* Do we have a citation line? */ 109 if (preg_match('/^\s*((>\s?)+)/m', $line, $matches)) { 110 /* Count number of > characters => cite level */ 111 $clevel = $closelevel = count(preg_split('/>\s?/', $matches[1])) - 1; 112 } 113 114 if ($cb && isset($matches[1])) { 115 /* Strip all > characters. */ 116 $line = substr($line, String::length($matches[1])); 117 } 118 119 /* Is this cite level lower than the current level? */ 120 if ($clevel < $qlevel) { 121 /* Strip more than one blank line in front of the cite 122 * blocks. */ 123 if ($blankline) { 124 while (($br_str = substr($text_out, -7)) == "<br />\n") { 125 $text_out = substr($text_out, 0, -7); 126 }; 127 } 128 /* Add quote block end tags for each cite level. */ 129 for ($i = $clevel; $i < $qlevel; $i++) { 130 $text_out .= ($cb) ? '</div>' : '</font>'; 131 --$closelevel; 132 } 133 /* Strip trailing line break if using cite blocks, we already 134 * have a div tag. */ 135 if ($cb && preg_match('/\<br \/>$/', $line)) { 136 $line = substr($line, 0, -6); 137 } 138 /* Is this cite level higher than the current level? */ 139 } elseif ($clevel > $qlevel) { 140 /* Strip more than one blank line in front of the cite 141 * blocks. */ 142 if ($blankline) { 143 while (($br_str = substr($text_out, -7)) == "<br />\n") { 144 $text_out = substr($text_out, 0, -7); 145 }; 146 } 147 for ($i = $qlevel; $i < $clevel; $i++) { 148 /* Add quote block start tags for each cite level. */ 149 $text_out .= ($cb) ? '<div class="citation ' : '<font class="'; 150 $text_out .= 'quoted' . (($i % $this->_params['cssLevels']) + 1) . '">'; 151 } 152 } 153 154 /* Count blank lines. */ 155 $blankline = ($line == '<br />'); 156 157 $text_out .= $line . "\n"; 158 $qlevel = $clevel; 159 } 160 161 /* Make sure all div/font tags are closed. */ 162 if ($closelevel > 0) { 163 $text_out = preg_replace('/<\/div>$/', str_repeat(($cb) ? '</div>' : '</font>', $closelevel) . '</div>', $text_out); 164 } 165 166 /* Remove the leading newline we added above, if it's still there. */ 167 if ($text_out[0] == "\n") { 168 $text_out = substr($text_out, 1); 169 } 170 171 return $text_out; 172 } 173 174 /** 175 * Adds links to show and hide quoted blocks, hiding them by default if 176 * the 'hideBlocks' parameter is true. 177 * 178 * @access private 179 * 180 * @param array $matches The matches from the regexp. 181 */ 182 function _addQuoteToggles($matches) 183 { 184 static $i = 0; 185 186 /* Don't toggle small blocks; doesn't provide a UI benefit and looks 187 * annoying. */ 188 $lines = substr_count($matches[0], "\n"); 189 if ($lines < 8) { 190 return $matches[0]; 191 } 192 193 Horde::addScriptFile('hideable.js', 'horde', true); 194 Horde::addScriptFile('quoteBlocks.js', 'horde'); 195 196 $text = ''; 197 if ($this->_params['citeblock']) { 198 $text = '<br />'; 199 }; 200 $text .= '<div id="qt_' . $i . '">' . 201 Horde::link('#', '', 'widget togglequote', '', 'toggleQuoteBlock(\'' . $i . '\', \'' . $lines . '\'); return false;', '', '', array('style' => 'font-size:70%')) . 202 ($this->_params['hideBlocks'] ? sprintf(_("[Show Quoted Text - %s lines]"), $lines) : _("[Hide Quoted Text]")) . 203 '</a></div><div id="qb_' . $i . '"' . ($this->_params['hideBlocks'] ? ' style="display:none;"' : '') . '>' . $matches[0] . '</div>'; 204 205 $i++; 206 207 return $text; 208 } 209 210 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |