Administrator
Регистрация: 26.08.2005
Адрес: Москва
|
kashperuk
Ну вот примерно так. Тупо в лоб. Со всеми некрасивостями.
PHP код:
// remove htmlspecialchars'd bits and excess spacing
$code = rtrim($code);
$code = preg_replace('#^\W*\n#', '', $code); //Вырезаем ведущие переводы строки
$operators = array('abstract', 'anytype', 'asc', 'at', 'avg', 'break', 'breakpoint', 'by', 'byref', 'case', 'catch', 'changecompany', 'class', 'classnum', 'classstr', 'client', 'configurationkeynum', 'configurationkeystr', 'container', 'continue', 'count', 'crosscompany', 'date', 'default', 'delete_from', 'desc', 'display', 'div', 'do', 'edit', 'else', 'enumnum', 'enumstr', 'exists', 'extendedtypenum', 'extendedtypestr', 'extends', 'false', 'featurekeynum', 'featurekeystr', 'fieldnum', 'fieldpname', 'fieldstr', 'final', 'firstfast', 'firstonly', 'firstonly10', 'firstonly100', 'firstonly1000', 'flush', 'for', 'forceliterals', 'forcenestedloop', 'forceplaceholders', 'forceselectorder', 'formstr', 'forupdate', 'from', 'group', 'helpapplstr', 'helpdevstr', 'hint', 'htmlpdahorizontalpagestr', 'htmlpdaverticalpagestr', 'htmlstandardpagestr', 'identifierstr', 'if', 'implements', 'index', 'indexnum', 'indexstr', 'insert_recordset', 'int', 'int64', 'interface', 'join', 'left', 'licensecodenum', 'licensecodestr', 'like', 'literalstr', 'maxof', 'menuitemactionstr', 'menuitemdisplaystr', 'menuitemoutputstr', 'menustr', 'methodstr', 'minof', 'mod', 'nbofetch', 'new', 'next', 'nofetch', 'not', 'notexists', 'null', 'optimisticlock', 'order', 'outer', 'pause', 'pessimisticlock', 'print', 'private', 'protected', 'public', 'querystr', 'real', 'repeatableread', 'resourcestr', 'retry', 'return', 'reverse', 'right', 'securitykeynum', 'securitykeystr', 'select', 'server', 'setting', 'static', 'staticmethodstr', 'str', 'sum', 'super', 'switch', 'tablecollectionstr', 'tablefieldgroupstr', 'tablelock', 'tablemethodstr', 'tablenum', 'tablepname', 'tablestaticmethodstr', 'tablestr', 'throw', 'true', 'try', 'ttsabort', 'ttsbegin', 'ttscommit', 'typeid', 'update_recordset', 'varstr', 'void', 'webclientfilestr', 'webformstr', 'webmenustr', 'webreportstr', 'webserverfilestr', 'where', 'while', 'window');
$string_1 = false; // '
$string_2 = false; // "
$comment = 0; // 1 - было /
// 2 - было //
// 3 - было /*
$slash = false; // было \
$word = '';
$result = '';
for ($i = 0; $i < strlen($code); $i++)
{
$char = $code{$i};
//сбрасываем признак начала возможного комментария
if ($comment == 1 && $char != '/' && $char != '*')
{
$comment = 0;
$word .= '/';
}
switch ($char) {
case "'":
if ($comment<2 && !$string_2 && !$slash)
{
if ($string_1)
{
$result .= '<span style="color: red">'.htmlspecialchars($word.$char).'</span>';
$string_1 = false;
$string_2 = false;
$comment = 0;
$word = '';
}else{
$result .= htmlspecialchars($word);
$word = $char;
$string_1 = true;
}
}else{
$word .= "'";
}
$slash = false;
break;
case "\"":
if ($comment<2 && !$string_1 && !$slash)
{
if ($string_2)
{
$result .= '<span style="color: red">'.htmlspecialchars($word.$char).'</span>';
$string_1 = false;
$string_2 = false;
$comment = 0;
$word = '';
}else{
$result .= htmlspecialchars($word);
$word = $char;
$string_2 = true;
}
}else{
$word .= '"';
}
$slash = false;
break;
case "/":
switch ($comment) {
case 0:
if (!$string_1 && !$string_2)
{
//$word .= $char; добавим после
$comment = 1;
}else{
$word .= $char;
}
break;
case 1:
$result .= htmlspecialchars($word);
$word = $char;
$string_1 = false;
$string_2 = false;
$comment = 2;
break;
case 2:
$word .= $char;
break;
case 3:
if ($comment == 3 && $code{($i==0)?1:$i-1}=='*')
{
$result .= '<span style="color: green">/'.htmlspecialchars($word.$char).'</span>';
$string_1 = false;
$string_2 = false;
$comment = 0;
$word = '';
}else{
$word .= $char;
}
}
$slash = false;
break;
case "\\":
$slash = !$slash;
$word .= $char;
break;
case "\n":
if ($comment == 2)
{
$result .= '<span style="color: green">/'.htmlspecialchars($word.$char).'</span>';
$string_1 = false;
$string_2 = false;
$comment = 0;
$word = '';
}else{
$word .= $char;
}
$slash = false;
break;
case "*":
if ($comment == 1)
{
$result .= htmlspecialchars($word);
$word = $char;
$string_1 = false;
$string_2 = false;
$comment = 3;
}else{
$word .= $char;
}
$slash = false;
break;
default:
if (!$string_1 && !$string_2 && $comment<2)
{
if (in_array(strtolower($char),array('0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')))
{
$word .= $char;
}elseif (in_array(strtolower($word), $operators))
{
$result .= '<span style="color: blue">'.htmlspecialchars($word).'</span>'.$char;
$word = '';
}else{
$result .= htmlspecialchars($word.$char);
$word = '';
}
}else{
$word .= $char;
}
$slash = false;
}
}
//Подсвечиваем незакрытое
if ($string_1 OR $string_2)
{
$result .= '<span style="color: red">'.htmlspecialchars($word).'</span>';
$string_1 = false;
$string_2 = false;
$comment = 0;
$word = '';
}
elseif ($comment > 1)
{
$result .= '<span style="color: green">/'.htmlspecialchars($word).'</span>';
$string_1 = false;
$string_2 = false;
$comment = 0;
$word = '';
}
$code = $result . htmlspecialchars($word);
__________________
Был грязный плащ на нем одет,
Цилиндр черный смят в гармошку...
|