| protozoa ( @ 2003-09-12 18:20:00 |
A Challenge
Who can make the shortest C++ program to syntax color and escape C++ for the web using only standard C++? I think it's a difficult problem because of the scanning. C++ needs regular expressions.
Updated: I added what I meant to in the original challenge; only standard C++ and syntax coloring.
Who can make the shortest C++ program to syntax color and escape C++ for the web using only standard C++? I think it's a difficult problem because of the scanning. C++ needs regular expressions.
Updated: I added what I meant to in the original challenge; only standard C++ and syntax coloring.
#!/usr/bin/perl
print "<pre><code>\n";
while(<>)
{
# C style comments
if(/\/\*/)
{
s|(/\*.*$)|<font color="#008000">$1|g;
print $_;
while(<>)
{
if(/\*\//)
{
break;
}
print $_;
}
s|(^.*?\*\/)|$1</font>|g;
}
# The & character
s/&/&/g;
# The < and > characters
s/</</g;
s/>/>/g;
# Double quoted strings
s|(".*?")|<font color="#800000">$1</font>|g;
# The # preprocessor statements
s|(^\s*#\w+\b)|<font color="#000080">$1</font>|g;
# C++ comments
s|(//.*?$)|<font color="#008000">$1</font>|g;
# Keywords
s/( const| public| template| class| typename)/<font color="#000080">$1<\/font>/g;
s/( if| switch| break| case| default| else)/<font color="#000080">$1<\/font>/g;
s/( int| double| for| while| return| using)/<font color="#000080">$1<\/font>/g;
s/( namespace| void| bool| operator| struct)/<font color="#000080">$1<\/font>/g;
print $_;
}
print "</code></pre>\n";