Main Page | Class List | File List | Class Members | File Members

highlighter Class Reference

List of all members.

Public Member Functions

 __construct ()
 class constructor, check for PEAR installation
 highlight_string ($text, $language='', $as_table=false, $numbers=true)
 highlight a given string
 highlight_file ($file, $language='', $as_table=false, $numbers=true)
 highlight a given file
 highlight_content ($content, $as_table=false, $numbers=true)
 highlight a content, this function highlights all code-snippets between
 highlight_content_file ($file, $as_table=false, $numbers=true)
 highlight a content stored in a file, this function highlights all code-snippets between
 output ()
 output the results, use this function to get back highlighted string or content

Public Attributes

 $default_language = 'php'
 $tabsize = 2
 $max_filesize = 1000
 $max_strlength = 10000

Private Member Functions

 open_file ($file)
 open file and give back code
 highlight_string_callback ($match)
 callback function for preg_replace_callback
 addError ($msg)
 add error to error stack
 htmlunspecialchars ($code)
 we will reverse the htmlspecialchars, to convert XML entities back to the right character.

Private Attributes

 $_errors = array()
 $_hl
 $_code = ''
 $_as_table
 $_numbers

Member Function Documentation

highlighter::__construct  ) 
 

class constructor, check for PEAR installation

Author:
Stephan Spies <info(at)optima-software(dot)de>
Date:
2006/08/01
00047                                 {
00048     if (@include_once('Text/Highlighter.php')){
00049       $this->_hl = new Text_Highlighter;
00050     }
00051     if ( !is_object($this->_hl) ) {
00052       $this->addError(HL_PEAR_PKG_NOT_FOUND);
00053     }
00054   }//END function __construct

highlighter::addError msg  )  [private]
 

add error to error stack

Parameters:
$msg 
Author:
Stephan Spies <info(at)optima-software(dot)de>
Date:
2006/08/01
00204                                   {
00205     if (!empty($msg)){
00206       $this->_errors[] = array(
00207         'uri'     => $_SERVER['REQUEST_URI'],
00208         'message' => $msg
00209       );
00210     }//if if (!empty($msg))    
00211   }//END private function addError

highlighter::highlight_content content,
as_table = false,
numbers = true
 

highlight a content, this function highlights all code-snippets between

<pre lang="language"> Tags. "language" means the programming language for the current snippet.

Parameters:
$text string to highlight
$as_table [optional, default=false] output a table, not <> or <ol>, not recomended
$numbers [optional, default=true] if numbering is true we use a <ol> else a <ul>
Return values:
string content with highlighted code-snippets, better us output() for getting the results
Author:
Stephan Spies <info(at)optima-software(dot)de>
Date:
2006/08/01
00149                                                                                   { 
00150     $this->_as_table = $as_table;
00151     $this->_numbers = $numbers;
00152     $matches = array();
00153     $match = "/<pre([^>]*)>(.*?)<\/pre>/is";
00154     $this->_code = preg_replace_callback($match,
00155           array('self', 'highlight_string_callback'),
00156           $content
00157         );
00158     return $this->_code;
00159   }//END public function highlight_content

highlighter::highlight_content_file file,
as_table = false,
numbers = true
 

highlight a content stored in a file, this function highlights all code-snippets between

<pre lang="language"> Tags. "language" means the programming language for the current snippet.

Parameters:
$file file to highligh
$as_table [optional, default=false] output a table, not <> or <ol>, not recomended
$numbers [optional, default=true] if numbering is true we use a <ol> else a <ul>
Return values:
string content with highlighted code-snippets, better us output() for getting the results
Author:
Stephan Spies <info(at)optima-software(dot)de>
Date:
2006/08/01
00170                                                                                     { 
00171     if ( $content = $this->open_file($file) ){
00172       return $this->highlight_content($content, $as_table, $numbers);
00173     } else {
00174       return false;
00175     }
00176   }//END public function highlight_content

highlighter::highlight_file file,
language = '',
as_table = false,
numbers = true
 

highlight a given file

Parameters:
$file file to highlight
$language [optional, default=''] programming language for highlighting, if empty we use $this->default_language
$as_table [optional, default=false] output a table, not <> or <ol>, not recomended
$numbers [optional, default=true] if numbering is true we use a <ol> else a <ul>
Return values:
string highlighted string, better us output() for getting the results
Author:
Stephan Spies <info(at)optima-software(dot)de>
Date:
2006/08/01
00105                                                                                           { 
00106     if ( $code = $this->open_file($file) ){
00107       return $this->highlight_string($code, $language, $as_table, $numbers);
00108     } else {
00109       return false;
00110     }
00111   }// END public function highlight_file

highlighter::highlight_string text,
language = '',
as_table = false,
numbers = true
 

highlight a given string

Parameters:
$text string to highlight
$language [optional, default=''] programming language for highlighting, if empty we use $this->default_language
$as_table [optional, default=false] output a table, not <> or <ol>, not recomended
$numbers [optional, default=true] if numbering is true we use a <ol> else a <ul>
Return values:
string highlighted string, better us output() for getting the results
Author:
Stephan Spies <info(at)optima-software(dot)de>
Date:
2006/08/01
Note:
at the moment, this does nothing to PEAR

PEAR ERROR

00066                                                                                             { 
00067     if ( is_object($this->_hl) ) {  
00068       empty($language) ? $lng = $this->default_language : $lng = $language;
00069       $options = array(
00070         'numbers' => $as_table == true ? HL_NUMBERS_TABLE : ( $numbers == true ? HL_NUMBERS_LI : 0 ),
00071         'tabsize' => $this->tabsize 
00072       );
00073       $obj = $this->_hl->factory($lng, $options);
00074       if ( isset($obj->message) ) {
00076         $this->addError($obj->message);
00077         return false;
00078       } 
00079       if ( $this->max_strlength != 0 ){
00080         if ( (strlen($this->_code) + strlen($text)) > $this->max_strlength){
00081           $this->addError(sprintf(HL_STRING_TO_LONG, $this->max_strlength));
00082           return false;
00083         } 
00084       }
00085       $text = stripslashes($text);
00086       $result  = ''; 
00087       $result .= '<div class="' . $lng . '">' . "\r\n";
00088       $result .= $obj->highlight($text) . "\r\n";
00089       $result .= '</div>' . "\r\n"; 
00090       $this->_code .= $result;
00091       return $result;
00092     }//if ( is_object($this->_hl) )
00093   }//public function highlight_text

highlighter::highlight_string_callback match  )  [private]
 

callback function for preg_replace_callback

Parameters:
$match 
Return values:
string highlighted string
Author:
Stephan Spies <info(at)optima-software(dot)de>
Date:
2006/08/01
00185                                                      {
00186     $attr = stripslashes($match[1]);
00187     $code = stripslashes($match[2]);
00188     $re_lang = '/\s+lang\s*=\s*["\']?([^"\']+)["\']?/xi';
00189     $num = preg_match($re_lang, $attr, $lang);
00190     
00191     if ($num) {      
00192       $code = $this->htmlunspecialchars($code); 
00193       $res = $this->highlight_string($code, $lang[1], $this->_as_table, $this->_numbers);
00194       return $res;
00195     }
00196   }

highlighter::htmlunspecialchars code  )  [private]
 

we will reverse the htmlspecialchars, to convert XML entities back to the right character.

Parameters:
$code original code
Return values:
string 
Author:
Jan Tank <http://zeek.luli.de/>
Date:
unknown
00244                                              {
00245     $func = create_function('$match',
00246         '$value = intval($match[1]);'.
00247         'return ($value < 256) ? chr($value) : $match[1];');
00248     $tran = get_html_translation_table(HTML_ENTITIES);
00249     $tran = array_flip($tran);
00250     $code = strtr($code, $tran);
00251     $code = preg_replace_callback("/&#([0-9]{1,5});/is", $func, $code);
00252     return $code;
00253   }//END private function htmlunspecialchars      

highlighter::open_file file  )  [private]
 

open file and give back code

Parameters:
$file filename to open
Return values:
string text inside file
Author:
Stephan Spies <info(at)optima-software(dot)de>
Date:
2006/08/01
00120                                     {
00121     if (!file_exists($file) ) {
00122       $this->addError(sprintf(HL_FILE_NOT_FOUND, $file));
00123       return false;
00124     }
00125     $f = fopen($file, 'r+');
00126     if (!$f){
00127       $this->addError(sprintf(HL_FILE_CANNOT_READ, $file));
00128       return false;         
00129     }
00130     if ( $this->max_filesize != 0 ){
00131       $sizeKB = filesize($file) / 1024;
00132       if ($sizeKB > $this->max_filesize){
00133         $this->addError(sprintf(HL_FILE_TO_LARGE, round($sizeKB, 2), $this->max_filesize));
00134         return false;
00135       }
00136     }//if ( $this->max_filesize != 0 )
00137     return fread( $f, filesize($file) );
00138   }// END private function open_file

highlighter::output  ) 
 

output the results, use this function to get back highlighted string or content

Return values:
string highlighted string
Author:
Stephan Spies <info(at)optima-software(dot)de>
Date:
2006/08/01
00219                           {
00220     $_res = HL_COPY_INFO . "\r\n";
00221     if ( sizeof($this->_errors) > 0 ){
00222       foreach( $this->_errors as $error ) {
00223         $_res .= 'CLASS ERROR: ' . $error['message'] . ' REQUEST: ' . $error['uri'] . '<br />'; 
00224       }//END foreach( $this->_errors as $error   
00225     } else {
00226       $_res .= $this->_code;
00227       $this->_code = '';
00228       $_res  = eregi_replace('<li>&nbsp;<span class="[A-Za-z0-9\-]+"></span></li>', '', $_res);
00229       $_res  = eregi_replace('<li>&nbsp;<span class="[A-Za-z0-9\-]+"> </span></li>', '', $_res);
00230       $_res  = eregi_replace('<span class="[A-Za-z0-9\-]+"> </span>', '&nbsp;', $_res);
00231       $_res  = eregi_replace('<span class="[A-Za-z0-9\-]+"></span>', '', $_res);
00232     }    
00233     $_res .= "\r\n" . HL_COPY_INFO_END . "\r\n";
00234     return $_res;
00235   }//END private function output


Member Data Documentation

highlighter::$_as_table [private]
 

highlighter::$_code = '' [private]
 

highlighter::$_errors = array() [private]
 

Note:
private members

highlighter::$_hl [private]
 

highlighter::$_numbers [private]
 

highlighter::$default_language = 'php'
 

Note:
public members

highlighter::$max_filesize = 1000
 

highlighter::$max_strlength = 10000
 

highlighter::$tabsize = 2
 


The documentation for this class was generated from the following file:
Generated on Sat Aug 5 14:04:13 2006 for Source-Code Highlighter for HTML by Stephan Spies by  doxygen 1.4.4