Code: Alles auswählen
//-- begin Code-Highlight-Hack by theDon
/***
* cpp2thwb (c) 2001-2002, TheTinySteini and dp, apapted to YabbSE by theDon
* Highlights C++-Code with colors set at the beginning of the function.
* Uses many regular expressions that will eat up quite a bit of cpu time, so use with care.
* Far from being perfect, but you won't really notice =)
***/
function cpp2thwb( $string )
{
// this is here for debugging so you can see how much time it takes on your server
// to highlight the code
// $starttime = microtime();
// ADJUST THE COLORS HERE
$color['comments'] = 'LimeGreen'; // /* */ or //
$color['string'] = 'Purple'; // "strings"
$color['numbers'] = 'Brown'; // 123456.789
$color['types'] = 'Blue'; // int, void, true and related
$color['keywords'] = 'Navy'; // for, if, return and related
// Begin extracting =================
// Comments and strings are extracted before parsing keywords and then
// reinserted later. I really don't like this, but as it's pretty fast,
// I left it untouched.
// block comments
if(preg_match_all("/\/\*(.*)\*\//Us", $string, $cmts))
{
$string = preg_replace("/\/\*(.*)\*\//Us","|%|C|%|",$string);
}
// line comments
if(preg_match_all("/\/\/.*\n/U", $string, $lcmts))
{
$string = preg_replace("/\/\/(.*)\n/U", "|%|LC|%|", $string);
}
// strings
if(preg_match_all("/\"(.*)\"/Us", $string, $strs))
{
$string = preg_replace("/\"(.*)\"/Us","|%|S|%|",$string);
}
// End extracting ===================
// Now parse the rest ===============
// this is the really slow part, might take some 1/10ths of a second
// Numbers
$string = preg_replace('/([^a-zA-Z0-9])([0-9.]{1,})/',"\\1[color=$color[numbers]]\\2[/color]",$string);
// reserved words
// these are blue
$string = preg_replace("/(bool|char|wchar_t|class|const|double|enum|float|int|long|private:?|protected:?|public:?|short|signed|static|struct|template|typedef|unsigned|virtual|void|namespace|using|this|sizeof|true|false|#include|#define)([ \t\r\n;,*)])/","[color=$color[types]]\\1[/color]\\2",$string);
// these are darkblue
$string = preg_replace("/(auto|break|case|catch|continue|default|delete|do|else|extern|for|friend|goto|if|inline|mutable|new|operator|register|return|switch|throw|union|volatile|while|asm|cdecl|pascal|near|far)([( \t\r\n;])/","[color=$color[keywords]][b]\\1[/b][/color]\\2",$string);
// End parsing ======================
// Begin reinserting ================
// Reinsert the comments and strings that were extracted earlier
$i = 0;
while(true)
{
$pos = strpos($string, "|%|LC|%|");
if(!is_integer($pos)) break; // no match
$string = substr($string, 0, $pos)."[color=$color[comments]]".$lcmts[0][$i]."[/color]".substr($string, $pos + 8);
$i++;
}
$i = 0;
while(true)
{
$pos = strpos($string, "|%|C|%|");
if(!is_integer($pos)) break; // no match
$string = substr($string, 0, $pos)."[color=$color[comments]]".$cmts[0][$i]."[/color]".substr($string, $pos + 7);
$i++;
}
$i = 0;
while(true)
{
$pos = strpos($string, "|%|S|%|");
if(!is_integer($pos)) break; // no match
$string = substr($string, 0, $pos)."[color=$color[string]]".$strs[0][$i]."[/color]".substr($string, $pos + 7);
$i++;
}
// End reinserting ==================
// again for debugging purposes - rip this out when satisfied =)
// $endtime = microtime();
// $end_t = explode( " ", $endtime );
// $start_t = explode( " ", $starttime );
// $total = ($end_t[1] - $start_t[1]) + $end_t[0] - $start_t[0];
return $string; // ."\n[b]Code processed in $total seconds.[/b]";
}
function ags2thwb( $string )
{
// this is here for debugging so you can see how much time it takes on your server
// to highlight the code
// $starttime = microtime();
// ADJUST THE COLORS HERE
$color['comments'] = 'LimeGreen'; // /* */ or //
$color['string'] = 'Purple'; // "strings"
$color['numbers'] = 'Brown'; // 123456.789
$color['types'] = 'Blue'; // int, void, true and related
$color['keywords'] = 'Navy'; // for, if, return and related
// Begin extracting =================
// Comments and strings are extracted before parsing keywords and then
// reinserted later. I really don't like this, but as it's pretty fast,
// I left it untouched.
// line comments
if(preg_match_all("/\/\/.*\n/U", $string, $lcmts))
{
$string = preg_replace("/\/\/(.*)\n/U", "|%|LC|%|", $string);
}
// strings
if(preg_match_all("/\"(.*)\"/Us", $string, $strs))
{
$string = preg_replace("/\"(.*)\"/Us","|%|S|%|",$string);
}
// End extracting ===================
// Now parse the rest ===============
// this is the really slow part, might take some 1/10ths of a second
// Numbers
$string = preg_replace('/([^a-zA-Z0-9])([0-9.]{1,})/',"\\1[color=$color[numbers]]\\2[/color]",$string);
// reserved words
// these are blue
$string = preg_replace("/(function|int|string|#define)([ \t\r\n;,*)])/","[color=$color[types]]\\1[/color]\\2",$string);
// these are darkblue
$string = preg_replace("/(break|case|continue|default|do|else|for|goto|if|return|switch|while)([( \t\r\n;])/","[color=$color[keywords]][b]\\1[/b][/color]\\2",$string);
// End parsing ======================
// Begin reinserting ================
// Reinsert the comments and strings that were extracted earlier
$i = 0;
while(true)
{
$pos = strpos($string, "|%|LC|%|");
if(!is_integer($pos)) break; // no match
$string = substr($string, 0, $pos)."[color=$color[comments]]".$lcmts[0][$i]."[/color]".substr($string, $pos + 8);
$i++;
}
$i = 0;
while(true)
{
$pos = strpos($string, "|%|S|%|");
if(!is_integer($pos)) break; // no match
$string = substr($string, 0, $pos)."[color=$color[string]]".$strs[0][$i]."[/color]".substr($string, $pos + 7);
$i++;
}
// End reinserting ==================
// again for debugging purposes - rip this out when satisfied =)
// $endtime = microtime();
// $end_t = explode( " ", $endtime );
// $start_t = explode( " ", $starttime );
// $total = ($end_t[1] - $start_t[1]) + $end_t[0] - $start_t[0];
return $string; // ."\n[b]Code processed in $total seconds.[/b]";
}
function cpp_parse( $string )
{
// make sure we don't run this in PHP < 4.0
if( floor(phpversion()) > 3 )
{
// somewhat hacky code to extract the [cpp]-block, parse it and reinsert it.
// might be better/faster to mess around with strpos and substr instead of
// all this preg stuff
if( preg_match_all( "/\[cpp\](.*)\[\/cpp\]/Us", $string, $cpp ) )
{
$string = preg_replace("/\[cpp\].*\[\/cpp\]/Us", "|%|CPP|%|", $string );
for( $i = 0; $i < count($cpp[0]); $i++ )
{
$cpp[0][$i] = str_replace('[cpp]', '[quote]', $cpp[0][$i]);
$cpp[0][$i] = str_replace('[/cpp]', '[/quote]', $cpp[0][$i]);
$pos = strpos($string, "|%|CPP|%|");
$string = substr($string, 0, $pos). cpp2thwb($cpp[0][$i]) .substr($string, $pos + 9);
}
}
}
return $string;
}
function ags_parse( $string )
{
// make sure we don't run this in PHP < 4.0
if( floor(phpversion()) > 3 )
{
// somewhat hacky code to extract the [ags]-block, parse it and reinsert it.
// might be better/faster to mess around with strpos and substr instead of
// all this preg stuff
if( preg_match_all( "/\[ags\](.*)\[\/ags\]/Us", $string, $ags ) )
{
$string = preg_replace("/\[ags\].*\[\/ags\]/Us", "|%|AGS|%|", $string );
for( $i = 0; $i < count($ags[0]); $i++ )
{
$ags[0][$i] = str_replace('[ags]', '[quote]', $ags[0][$i]);
$ags[0][$i] = str_replace('[/ags]', '[/quote]', $ags[0][$i]);
$pos = strpos($string, "|%|AGS|%|");
$string = substr($string, 0, $pos). ags2thwb($ags[0][$i]) .substr($string, $pos + 9);
}
}
}
return $string;
}
//-- end Code-Highlight-Hack by theDon