I've taken the liberty of making an example:
<html>
<head>
<title>test</title>
<style>
code
{
white-space: pre;
}
.functionName
{
color: red;
}
.reservedWord
{
color: green;
}
.stringValue
{
color: blue;
}
.comment
{
color: grey;
}
</style>
<script>
function HighlightSyntax(object)
{
var codeContent = object.innerHTML;
//stringValue
codeContent = codeContent.replace(/(\"|\')(.*?)\1/g, "$1<span clas
+s=\"stringValue\">$2</span>$1");
//functionName
codeContent = codeContent.replace(/(sub\s+)([a-zA-Z]\w+)/g, "$1<sp
+an class=\"functionName\">$2</span>");
//reserverdWord
codeContent = codeContent.replace(/(\s+)(close|else|if|for|foreach
+|my|open|print|printf|return|sprintf|sub|unless|use|while)(\s+)/g, "$
+1<span
class=\"reservedWord\">$2</span>$3");
//comment
codeContent = codeContent.replace(/(\#.*?\r|\n)/g, "<span class=\"
+comment\">$1</span>");
object.innerHTML = codeContent;
}
</script>
</head>
<body>
<code onClick="HighlightSyntax(this);">
#!/usr/bin/perl -w
sub JavascriptEncodeText($)
{
my $string = shift;
unless (defined ($string))
{
$string = '';
}
$string =~ s/([\x{80}-\x{ffff}])/sprintf('&#x%04X;', ord($1))/ge;
return "$string";
}
#apples
print "test\n";
<\/code>
</body>
</html>
To try it, remove the backslash in the last <\/code> thing. This could easily be expandedn (i'll work some more on it and post it too.)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|