<?php

vendor
('geshi/class.geshi');

/**
 * Code Highlighter class file.
 *
 * A CakePHP's Helper class and wrapper for GenSHi. 
 * Go to http://www.gignus.com/blog/posts/view/11 to learn more about it.
 *
 * @filesource
 * @author Matias Lespiau
 * @link http://www.gignus.com/blog/posts/view/11
 * @version    1
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
 * @package app
 * @subpackage app.views.helpers
 */

class CodeHelper extends AppHelper {
    public 
$validContainers = array( 'pre' ); 
    public 
$validLanguages = array( 'php''javascript' );
    public 
$return '';
    
    
    function 
highlight$htmlString ) {
        
$this->return $htmlString;
        
        
$dom = new DOMDocument();
                
        if( @
$dom->loadHTML($htmlString) ) {
        
            foreach ( 
$this->validContainers as $container ) {
                
$containerList $dom->getElementsByTagName$container );
                for (
$i 0$i $containerList->length$i++ ) {                        
                    if( 
$language $this->getLanguage$containerList->item($i) ) ) {                    
                        
$text $this->getNodeValueEscapedTags$containerList->item($i) );
                        
$geshi = new GeSHi($text$language );                            
                        
$textNode $dom->createTextNode$geshi->parseCode() );                        
                        
$this->removeAllChilds($containerList->item($i));                                
                        
$containerList->item($i)->appendChild($textNode);                    
                    }                            
                                
                }
                
    
            }
        }    
        return 
$this->outputhtml_entity_decode($dom->saveXML(), ENT_QUOTES'utf-8'  ) );
    }
    
    function 
removeAllChilds$node ) { 
        
$node->nodeValue '';
    }
    
    function 
getNodeValueEscapedTags$node$recursion ) {
        if( 
$node->nodeName == '#text' ) { return $node->nodeValue; } 
        
        
$result '';
        
        if( 
$recursion ) {
            
$result .= '<' $node->nodeName;
            if( 
$node->hasAttributes() ) { 
                
$result .= ' ' $this->getAttributesAsString($node);
            }
            
$result .= '>';
        }
        
        
        if( 
$list $node->childNodes ) { 
            for (
$i 0$i $list->length$i++ ) {
                
$result .= $this->getNodeValueEscapedTags($list->item($i), $recursion );                
            }                
        }
        
        if ( 
$recursion ) {                 
            
$result .= '</' $node->nodeName '>';
        }
        
        return 
$result;
    }    
    
    function 
getAttributesAsString$node ) {
        
$return  '';
        
$attributes = array();
        if( 
$node->hasAttributes() ) {
            for(
$i 0$i $node->attributes->length$i++ ) {
                
$attributes[] = $node->attributes->item($i)->nodeName '="' $node->attributes->item($i)->nodeValue '"';
            }
            
$return .= implode' '$attributes );            
        }
        return 
$return;
    }
    
    
    function 
isHighlightable$node )  {
        if( 
in_array($node->parentNode->nodeName$this->validContainers) ) { return false; }
        return 
true;
    }
    
    function 
getLanguage$node ) {
        if( !
$this->isHighlightable$node ) ) { return false; }
        @list( 
$code$language ) = explode(' '$node->getAttribute('class') );
        if( !
$code || ( $code != 'code' ) ) { return false; }
        if( !
$language ) { $language 'php'; }        
        if( !
in_array$language$this->validLanguages) ) { return false; }
        return 
$language;
    }
    
    
}

?>