Show
Ignore:
Timestamp:
01/09/06 21:23:34 (3 years ago)
Author:
argh
Message:

Added some documentation; Implemented the renderXMLDeclaration option; Implemented the _renderDoctype method; Implemented the replaceEntities option.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/patXMLPretty/Renderer/HTML/Simple.php

    r16 r18  
    33class patXMLPretty_Renderer_HTML_Simple extends patXMLPretty_Renderer 
    44{ 
     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' => '&#160;', 
     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    */ 
    541    function _renderCDATA( $tagName, $data, $depth ) 
    642    { 
     
    4480    } 
    4581 
     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    */ 
    4690    function _renderIndent( $depth ) 
    4791    { 
    48         $indent = str_repeat( '&#160;', $this->configReader->getIndent( $depth ) ); 
     92        $indent = str_repeat( $this->getOption( 'indentCharacter' ), $this->configReader->getIndent( $depth ) ); 
    4993        return $indent; 
    5094    } 
    5195 
     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    */ 
    52105    function _renderColor( $content, $color ) 
    53106    { 
     
    56109    } 
    57110 
     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    */ 
    58120    function _renderAttributes( $attributes, $attribColor = null, $valueColor = null ) 
    59121    { 
     
    75137    } 
    76138 
     139   /** 
     140    * Escapes the given string for output to screen. 
     141    * 
     142    * @access   private 
     143    * @param    string 
     144    * @return   string 
     145    */ 
    77146    function _escapeOutput( $output ) 
    78147    { 
     
    81150    } 
    82151 
    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 ) 
    84163    { 
    85164        $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 
    87171        $render .= $this->_renderElement( $elementsTree, 0 ); 
    88172        $render .= '</div>'; 
     
    96180        $render .= $this->_renderAttributes( $documentInfo, $this->configReader->getColor( 'xmlDeclarationAttribName' ), $this->configReader->getColor( 'xmlDeclarationAttribValue' ) ); 
    97181        $render .= $this->_renderColor( ' ?&gt;', $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( '&lt;!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( ']&gt;', $tagColor ); 
    98212        $render .= '<br/>'; 
    99213 
     
    148262    } 
    149263 
     264   /** 
     265    * Renders a comment and returns the highlighted source 
     266    * 
     267    * @access   private 
     268    * @param    array 
     269    * @param    int 
     270    * @return   string 
     271    */ 
    150272    function _renderElement_comment( $element, $depth ) 
    151273    { 
     
    160282    } 
    161283 
     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    */ 
    162293    function _renderElement_cdata( $element, $depth ) 
    163294    { 
     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. 
    164298        $cdata = trim( $element['text'] ); 
    165299        $lines = explode( "\n", $cdata ); 
     
    175309        } 
    176310 
     311        // flatten the rendered individual lines 
    177312        $cdata = implode( '<br/>', $lines ); 
    178313 
     314        // colors we'll use 
     315        $tagC = $this->configReader->getColor( 'cdataTag' ); 
     316        $contentC = $this->configReader->getColor( 'cdataContent' ); 
     317 
     318        // render the cdata 
    179319        $render = $this->_renderIndent( $depth ); 
    180         $render .= $this->_renderColor( '&lt;![CDATA[', $this->configReader->getColor( 'cdataTag' ) ); 
    181         $render .= '<br/>'; 
    182         $render .= $this->_renderColor( $cdata, $this->configReader->getColor( 'cdataContent' ) ); 
     320        $render .= $this->_renderColor( '&lt;![CDATA[', $tagC ); 
     321        $render .= '<br/>'; 
     322        $render .= $this->_renderColor( $cdata, $contentC ); 
    183323        $render .= '<br/>'; 
    184324        $render .= $this->_renderIndent( $depth ); 
    185         $render .= $this->_renderColor( ']]&gt;', $this->configReader->getColor( 'cdataTag' ) ); 
    186         $render .= '<br/>'; 
    187  
    188         return $render; 
    189     } 
    190  
     325        $render .= $this->_renderColor( ']]&gt;', $tagC ); 
     326        $render .= '<br/>'; 
     327 
     328        return $render; 
     329    } 
     330 
     331   /** 
     332    * Renders an entity reference (like &amp;) and returns the 
     333    * highlighted content. 
     334    * 
     335    * @access   private 
     336    * @param    array 
     337    * @param    int 
     338    * @return   string 
     339    */ 
    191340    function _renderElement_entityReference( $element, $depth ) 
    192341    { 
     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 
    193350        $content = $this->_escapeOutput( '&'.$element['name'].';' ); 
    194351 
    195352        $render = $this->_renderIndent( $depth ); 
    196353        $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' ) ); 
    198355        $render .= '</span>'; 
    199356        $render .= '<br/>';