|
-
Mar 23rd, 2004, 10:14 PM
#1
Thread Starter
Addicted Member
C# Syntax Highlighter
I am having some problems with multiline comments stripping the color from other text.
PHP Code:
function cshilite($code)
{
$code = str_replace("<br>", "", $code);
$code = str_replace("<br />", "", $code);
$code = str_replace(">", ">", $code);
$code = str_replace("<", "<", $code);
$code = str_replace(""", "\\"", $code);
$code = str_replace("&", "&", $code);
$code = str_replace('$', '\\$', $code);
$code = str_replace('\\n', '\\\\\\\\n', $code);
$code = str_replace('\\r', '\\\\\\\\r', $code);
$code = str_replace('\\t', '\\\\\\\\t', $code);
$code = stripslashes($code);
$code = htmlentities($code);
$keywords = array('abstract', 'event', 'new', 'struct', 'as', 'explicit', 'null', 'switch', 'base', 'extern', 'object', 'this', 'bool', 'false', 'operator', 'throw', 'break', 'finally', 'out', 'true', 'byte', 'fixed', 'override', 'try', 'case', 'float', 'params', 'typeof', 'catch', 'for', 'private', 'uint', 'char', 'foreach', 'protected', 'ulong', 'checked', 'goto', 'public', 'unchecked', 'class', 'if', 'readonly', 'unsafe', 'const', 'implicit', 'ref', 'ushort', 'continue', 'in', 'return', 'using', 'decimal', 'int', 'sbyte', 'virtual', 'default', 'interface', 'sealed', 'volatile', 'delegate', 'internal', 'short', 'void', 'do', 'is', 'sizeof', 'while', 'double', 'lock', 'stackalloc', 'else', 'long', 'static', 'enum', 'namespace', 'string');
for ($x = 0; $x < count($keywords); $x++) {
$pattern = '/(^|[^a-zA-Z0-9#@_\\'\\"])(' . $keywords[$x] . ')([^a-zA-Z0-9#_\\'\\"]|$)/';
$code = preg_replace($pattern, '\\\\1<font color="blue">\\\\2</font>\\\\3', $code);
}
$single = '/((^|[^(https?|ftp|gopher|irc|news|telnet):])\\/\\/(.*?))([\\r\\n]|$)/is';
if (preg_match_all($single, $code, $match))
{
$return = preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]);
$return = preg_replace($single,'<font color="green">\\\\1</font>\\\\4',$return);
$code = str_replace($match[0],$return,$code);
}
$multi = '/(\\/\\*(.*?)(\\*\\/|$))/is';
if (preg_match_all($multi, $code, $match))
{
$return = preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]);
$return = preg_replace($multi,'<font color="green">\\\\1</font>',$return);
$code = str_replace($match[0],$return,$code);
}
$spattern = '/(\\"(.*?)[^\\\\\\]")/is';
if (preg_match_all($spattern, $code, $match))
{
$return = preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]);
$return = preg_replace($spattern,'\\\\1',$return);
$code = str_replace($match[0],$return,$code);
}
$code = str_replace(""", "\\"", $code);
$code = str_replace("<br>", "", str_replace("<br />", "", $code));
return "<blockquote><pre><smallfont>CSharp Code:</smallfont><hr size=1><br />" . $code . "<hr size=1></pre></blockquote>";
}
Last edited by Tewl; Mar 25th, 2004 at 12:57 PM.
-
Mar 24th, 2004, 02:03 PM
#2
care to provide any C# code so that i can see what it looks like when you are submitting your script? i don't know C#..
-
Mar 24th, 2004, 02:23 PM
#3
Stuck in the 80s
Originally posted by kows
care to provide any C# code so that i can see what it looks like when you are submitting your script? i don't know C#..
I'm giving his script a wirl right now to see if I can figure it out, but all you need is a block comment, same as C++ and PHP:
PHP Code:
/*
multiline block comment
*/
Only the /* will be green, instead of the whole thing.
-
Mar 24th, 2004, 03:42 PM
#4
Thread Starter
Addicted Member
Example:
Code:
private void test(object obj)
{
// single line comment 1
string[] mystring = new string[10];
/* multiline
comment */
// another single line comment
}
The pattern in the comment regexp needs to be rewritten. I am at work atm but I will start on it again when I get home.
Last edited by Tewl; Mar 24th, 2004 at 03:51 PM.
-
Mar 24th, 2004, 06:48 PM
#5
Thread Starter
Addicted Member
Ok I have 2 patterns made for commenting multiline and single line comments an they appear to work fine apart from each other. Problem is I need to have one expression to do this otherwise it will not be following standards if someone adds comment code inside a comment. Heres what I have now.
Patterns:
PHP Code:
$cpattern[0] = '/(\\/\\*(.*?)(\\*\\/|$))/is';
$cpattern[1] = '/((^|[^(https?|ftp|gopher|irc|news|telnet):])\\/\\/(.*?)([\\r\\n]|$))/is';
for ($x = 0; $x < 2; $x++)
{
if (preg_match_all($cpattern[$x], $code, $match))
{
$return = preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]);
$return = preg_replace($cpattern[$x],'<font color="green">\\\\1</font>',$return);
$code = str_replace($match[0],$return,$code);
}
}
}
Last edited by Tewl; Mar 24th, 2004 at 08:27 PM.
-
Mar 24th, 2004, 09:48 PM
#6
Thread Starter
Addicted Member
I forgot to parse strings.
Last edited by Tewl; Mar 24th, 2004 at 10:05 PM.
-
Mar 25th, 2004, 12:59 PM
#7
Thread Starter
Addicted Member
So far I know of 2 errors.
1. /* in a single line comment will be read as multiline until it hits */ or end of code
2. // or /* in a string causes color disfunction
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|