<?php

 
// taken straight from http://php.net/manual/en/function.highlight-string.php
 // a user post on 2008-02-19 by Daniel Suissa

 // the use of C-style character array for parsing didnt work so I 
 // did a dirty fix and replaced all those with substr()
 //  - Thomas Stokkelamd - 2012

 // I am sure someone has a slicker one out there using regex etc - I just havent gotten around
 // to look for it, this brute force for loop stuff works for now :)


/*************************************\
 CODE PANE 1.0 - SILVERWINGS - D. Suissa
\*************************************/

class HTMLcolorizer{
    private 
$pointer 0//Cursor position.
    
private $content null//content of document.
    
private $colorized null;

    function 
__construct($content){
        
$this->content $content;
    }

    function 
colorComment($position){
        
$buffer "&lt;<span class='HTMLComment'>";
        
// for($position+=1;$position < strlen($this->content) && $this->content[$position] != ">" ;$position++){
        
for($position+=1;$position strlen($this->content) && substr($this->content,$position,1) != ">" ;$position++){
            
// $buffer.= $this->content[$position];
            
$buffer.= substr($this->content,$position,1);
        }
        
$buffer .= "</span>&gt;";
        
$this->colorized .= $buffer;
        return 
$position;
    }
    function 
colorTag($position){
        
$buffer "&lt;<span class='tagName'>";
        
$coloredTagName false;
        
//As long as we're in the tag scope
        // for($position+=1;$position < strlen($this->content) && $this->content[$position] != ">" ;$position++){
        
for($position+=1;$position strlen($this->content) && substr($this->content,$position,1) != ">" ;$position++){
            
// if($this->content[$position] == " " && !$coloredTagName){
            
if(substr($this->content,$position,1) == " " && !$coloredTagName){
                
$coloredTagName true;
                
$buffer.="</span>";
            
// }else if($this->content[$position] != " " && $coloredTagName){
            
}else if(substr($this->content,$position,1) != " " && $coloredTagName){
                
//Expect attribute
                
$attribute "";
                
//While we're in the tag
                // for(;$position < strlen($this->content) && $this->content[$position] != ">" ;$position++){
                
for(;$position strlen($this->content) && substr($this->content,$position,1) != ">" ;$position++){
                    
// if($this->content[$position] != "="){
                    
if(substr($this->content,$position,1) != "="){
                        
// $attribute .= $this->content[$position];
                        
$attribute .= substr($this->content,$position,1);
                    }else{
                        
$value="";
                        
$buffer .= "<span class='tagAttribute'>".$attribute."</span>=";
                        
$attribute ""//initialize it
                        
$inQuote false;
                        
$QuoteType null;
                        
// for($position+=1;$position < strlen($this->content) && $this->content[$position] != ">" && $this->content[$position] != " "  ;$position++){
                        
for($position+=1;$position strlen($this->content) && substr($this->content,$position,1) != ">" && substr($this->content,$position,1) != " "  ;$position++){
                            
// if($this->content[$position] == '"' || $this->content[$position] == "'"){
                            
if(substr($this->content,$position,1) == '"' || substr($this->content,$position,1) == "'"){
                                
$inQuote true;
                                
// $QuoteType = $this->content[$position];
                                
$QuoteType substr($this->content,$position,1);
                                
$value.=$QuoteType;
                                
//Read Until next quotation mark.
                                // for($position+=1;$position < strlen($this->content) && $this->content[$position] != ">" && $this->content[$position] != $QuoteType  ;$position++){
                                
for($position+=1;$position strlen($this->content) && substr($this->content,$position,1) != ">" && substr($this->content,$position,1) != $QuoteType  ;$position++){
                                    
// $value .= $this->content[$position];
                                    
$value .= substr($this->content,$position,1);
                                }   
                                
$value.=$QuoteType;
                            }else{
//No Quotation marks.
                                // $value .= $this->content[$position];
                                
$value .= substr($this->content,$position,1);
                            }                           
                        }
                        
$buffer .= "<span class='tagValue'>".$value."</span>";
                        break;           
                    }
                   
                }
                if(
$attribute != ""){$buffer.="<span class='tagAttribute'>".$attribute."</span>";}
            }
            
// if($this->content[$position] == ">" ){break;}else{$buffer.= $this->content[$position];}
            
if(substr($this->content,$position,1) == ">" ){break;}else{$buffer.= substr($this->content,$position,1);}
           
        }
        
//In case there were no attributes.
        // if($this->content[$position] == ">" && !$coloredTagName){
        
if(substr($this->content,$position,1) == ">" && !$coloredTagName){
            
$buffer.="</span>&gt;";
            
$position++;
        }
        
$this->colorized .= $buffer;
        return --
$position;
    }
    function 
colorize(){
        
$this->colorized="";
        
$inTag false;
        for(
$pointer 0;$pointer<strlen($this->content);$pointer++){
            
// $thisChar = $this->content[$pointer];
            
$thisChar substr($this->content,$pointer,1);
            
// $nextChar = $this->content[$pointer+1];
            
$nextChar substr($this->content,$pointer+1,1);
            if(
$thisChar == "<"){
                if(
$nextChar == "!"){
                    
$pointer $this->colorComment($pointer);
                }else if(
$nextChar == "?"){
                    
//colorPHP();
                
}else{
                    
$pointer $this->colorTag($pointer);
                }
            }else{
                
// $this->colorized .= $this->content[$pointer];
                
$this->colorized .= substr($this->content,$pointer,1);
            }
        }
        return 
$this->colorized;
    }
}

?>