Changeset 18 for trunk/patXMLPretty/Renderer
- Timestamp:
- 01/09/06 21:23:34 (3 years ago)
- Files:
-
- trunk/patXMLPretty/Renderer/HTML/Simple.php (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/patXMLPretty/Renderer/HTML/Simple.php
r16 r18 3 3 class patXMLPretty_Renderer_HTML_Simple extends patXMLPretty_Renderer 4 4 { 5 /** 6 * Stores available renderer options: 7 * 8 * <ul> 9 * <li> 10 * <b>renderXMLDeclaration</b> [yes|no] Default: yes<br/> 11 * Sets whether to include the XML declaration in the highlighted code. Note: this also affects the doctype declaration, which is only included if the XML declaration is on. 12 * </li> 13 * <li> 14 * <b>replaceEntities</b> [yes|no] Default: no<br/> 15 * Sets whether to replace entities by their content instead of displaying the entity's name (the default behavior). 16 * </li> 17 * <li> 18 * <b>indentCharacter</b> Default: nonbreaking space<br/> 19 * Sets which character to use as indenting character. Note that this also depends on the indent and baseIndent configuration settings of the active configuration reader. 20 * </ul> 21 * 22 * @access private 23 * @var 24 */ 25 var $options = array( 26 'renderXMLDeclaration' => 'yes', 27 'replaceEntities' => 'no', 28 'indentCharacter' => ' ', 29 ); 30 31 /** 32 * Intelligent CDATA renderer which wraps and indents 33 * content naturally for easiest reading. 34 * 35 * @access private 36 * @param string 37 * @param string 38 * @param int 39 * @return string 40 */ 5 41 function _renderCDATA( $tagName, $data, $depth ) 6 42 { … … 44 80 } 45 81 82 /** 83 * Renders the content used to display the indentation 84 * for the given depth. 85 * 86 * @access private 87 * @param int 88 * @return string 89 */ 46 90 function _renderIndent( $depth ) 47 91 { 48 $indent = str_repeat( ' ', $this->configReader->getIndent( $depth ) );92 $indent = str_repeat( $this->getOption( 'indentCharacter' ), $this->configReader->getIndent( $depth ) ); 49 93 return $indent; 50 94 } 51 95 96 /** 97 * Renders the content required to highlight the selected content 98 * in the specified colour. 99 * 100 * @access private 101 * @param string 102 * @param string Hexadecimal value. 103 * @return string 104 */ 52 105 function _renderColor( $content, $color ) 53 106 { … … 56 109 } 57 110 111 /** 112 * Renders an attribute collection with the specified colours. 113 * 114 * @access private 115 * @param array Associative array with name => value pairs. 116 * @param string 117 * @param string 118 * @return string 119 */ 58 120 function _renderAttributes( $attributes, $attribColor = null, $valueColor = null ) 59 121 { … … 75 137 } 76 138 139 /** 140 * Escapes the given string for output to screen. 141 * 142 * @access private 143 * @param string 144 * @return string 145 */ 77 146 function _escapeOutput( $output ) 78 147 { … … 81 150 } 82 151 83 function render( $documentInfo, $elementsTree ) 152 /** 153 * Main rendering method that delegates rendering 154 * for the available subsections to specialized methods. 155 * 156 * @access private 157 * @param array 158 * @param array 159 * @param array 160 * @return string 161 */ 162 function render( $documentInfo, $elementsTree, $doctype ) 84 163 { 85 164 $render = '<div style="white-space:nowrap;">'; 86 $render .= $this->_renderXMLDeclaration( $documentInfo ); 165 166 if( $this->getOption( 'renderXMLDeclaration' ) == 'yes' ) { 167 $render .= $this->_renderXMLDeclaration( $documentInfo ); 168 $render .= $this->_renderDoctype( $doctype ); 169 } 170 87 171 $render .= $this->_renderElement( $elementsTree, 0 ); 88 172 $render .= '</div>'; … … 96 180 $render .= $this->_renderAttributes( $documentInfo, $this->configReader->getColor( 'xmlDeclarationAttribName' ), $this->configReader->getColor( 'xmlDeclarationAttribValue' ) ); 97 181 $render .= $this->_renderColor( ' ?>', $this->configReader->getColor( 'xmlDeclarationTag' ) ); 182 $render .= '<br/>'; 183 184 return $render; 185 } 186 187 function _renderDoctype( $doctype ) 188 { 189 // no doctype detected 190 if( is_null( $doctype ) ) { 191 $render = ''; 192 return $render; 193 } 194 195 // tag colors 196 $tagColor = $this->configReader->getColor( 'xmlDeclarationTag' ); 197 $nameColor = $this->configReader->getColor( 'xmlDeclarationAttribName' ); 198 $elementsColor = $this->configReader->getColor( 'xmlDeclarationAttribValue' ); 199 200 // render the doctype 201 $render = $this->_renderColor( '<!DOCTYPE ', $tagColor ); 202 $render .= $this->_renderColor( $doctype['name'], $nameColor ); 203 $render .= $this->_renderColor( ' [', $tagColor ); 204 $render .= '<br/>'; 205 foreach( $doctype['elements'] as $element ) { 206 $element = $this->_escapeOutput( $element ); 207 $render .= $this->_renderIndent( 1 ); 208 $render .= $this->_renderColor( $element, $elementsColor ); 209 $render .= '<br/>'; 210 } 211 $render .= $this->_renderColor( ']>', $tagColor ); 98 212 $render .= '<br/>'; 99 213 … … 148 262 } 149 263 264 /** 265 * Renders a comment and returns the highlighted source 266 * 267 * @access private 268 * @param array 269 * @param int 270 * @return string 271 */ 150 272 function _renderElement_comment( $element, $depth ) 151 273 { … … 160 282 } 161 283 284 /** 285 * Renders the content of a cdata section and returns 286 * the highlighted source. 287 * 288 * @access private 289 * @param array 290 * @param int 291 * @return string 292 */ 162 293 function _renderElement_cdata( $element, $depth ) 163 294 { 295 // process the cdata content. This is done by splitting it by 296 // newlines and separately rendering each line to make sure the 297 // indent and everything else is in the right place. 164 298 $cdata = trim( $element['text'] ); 165 299 $lines = explode( "\n", $cdata ); … … 175 309 } 176 310 311 // flatten the rendered individual lines 177 312 $cdata = implode( '<br/>', $lines ); 178 313 314 // colors we'll use 315 $tagC = $this->configReader->getColor( 'cdataTag' ); 316 $contentC = $this->configReader->getColor( 'cdataContent' ); 317 318 // render the cdata 179 319 $render = $this->_renderIndent( $depth ); 180 $render .= $this->_renderColor( '<![CDATA[', $t his->configReader->getColor( 'cdataTag' ));181 $render .= '<br/>'; 182 $render .= $this->_renderColor( $cdata, $ this->configReader->getColor( 'cdataContent' ));320 $render .= $this->_renderColor( '<![CDATA[', $tagC ); 321 $render .= '<br/>'; 322 $render .= $this->_renderColor( $cdata, $contentC ); 183 323 $render .= '<br/>'; 184 324 $render .= $this->_renderIndent( $depth ); 185 $render .= $this->_renderColor( ']]>', $this->configReader->getColor( 'cdataTag' ) ); 186 $render .= '<br/>'; 187 188 return $render; 189 } 190 325 $render .= $this->_renderColor( ']]>', $tagC ); 326 $render .= '<br/>'; 327 328 return $render; 329 } 330 331 /** 332 * Renders an entity reference (like &) and returns the 333 * highlighted content. 334 * 335 * @access private 336 * @param array 337 * @param int 338 * @return string 339 */ 191 340 function _renderElement_entityReference( $element, $depth ) 192 341 { 342 if( $this->getOption( 'replaceEntities' ) == 'yes' ){ 343 $render = $this->_renderIndent( $depth ); 344 $render .= $this->_renderColor( $this->_escapeOutput( $element['text'] ), $this->configReader->getColor( 'cdata' ) ); 345 $render .= '<br/>'; 346 return $render; 347 } 348 349 // render the entity as an entity placeholder 193 350 $content = $this->_escapeOutput( '&'.$element['name'].';' ); 194 351 195 352 $render = $this->_renderIndent( $depth ); 196 353 $render .= '<span title="Entity value: '.$this->_escapeOutput( '"'.$element['text'].'"' ).'">'; 197 $render .= $this->_renderColor( $content, $this->configReader->getColor( ' cdataTag' ) );354 $render .= $this->_renderColor( $content, $this->configReader->getColor( 'entityReference' ) ); 198 355 $render .= '</span>'; 199 356 $render .= '<br/>';
