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 | |
|
|
class constructor, check for PEAR installation
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
|
|
|
||||||||||||||||
|
highlight a content, this function highlights all code-snippets between <pre lang="language"> Tags. "language" means the programming language for the current snippet.
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
|
|
||||||||||||||||
|
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.
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
|
|
||||||||||||||||||||
|
highlight a given file
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
|
|
||||||||||||||||||||
|
highlight a given string
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
|
|
|
callback function for preg_replace_callback
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 }
|
|
|
we will reverse the htmlspecialchars, to convert XML entities back to the right character.
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
|
|
|
open file and give back code
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
|
|
|
output the results, use this function to get back highlighted string or content
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> <span class="[A-Za-z0-9\-]+"></span></li>', '', $_res); 00229 $_res = eregi_replace('<li> <span class="[A-Za-z0-9\-]+"> </span></li>', '', $_res); 00230 $_res = eregi_replace('<span class="[A-Za-z0-9\-]+"> </span>', ' ', $_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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1.4.4