Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: Feature Request: Adding Colors to Source Code

by Vautrin (Hermit)
on Mar 07, 2004 at 03:11 UTC ( #334570=note: print w/ replies, xml ) Need Help??


in reply to Re: Feature Request: Adding Colors to Source Code
in thread Feature Request: Adding Colors to Source Code

Well if CPU usage is a problem, what about a javascript solution? That would take care of the problem of CPU usage. Add in a button to "colorify" the code on the current page and it might work well.


Want to support the EFF and FSF by buying cool stuff? Click here.


Comment on Re: Re: Feature Request: Adding Colors to Source Code
Re: Re: Re: Feature Request: Adding Colors to Source Code
by Jaap (Curate) on Mar 07, 2004 at 15:07 UTC
    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.)

      Bad solution. It would colour some Perl by accident, but what you're doing is nowhere near parsing Perl.

      Only perl can really parse Perl correctly (and not even that), but what Syntax::Highlight::Perl and Perl::Tidy do comes much, much closer. (The latter is better, but the former is much faster/lighter and would because of that probably be better for this site. (Benchmarked a year or so ago, maybe things have changed since.)) Supporting Perl's quoting operators is very important; try doing that with Javascript :)

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://334570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-10-20 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls