Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Feature Request: Adding Colors to Source Code

by theorbtwo (Prior)
on Mar 06, 2004 at 21:57 UTC ( [id://334540]=note: print w/replies, xml ) Need Help??


in reply to Feature Request: Adding Colors to Source Code

We have more free web server CPU then we did before, but not that much more. Also, as more uses come to the site, that free CPU will become less free.

Anyway, as castaway just pointed out to me, a lot of stuff gets put in <code> tags that is not /perl/ code.


Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

  • Comment on Re: Feature Request: Adding Colors to Source Code

Replies are listed 'Best First'.
Re: Re: Feature Request: Adding Colors to Source Code
by BrowserUk (Patriarch) on Mar 06, 2004 at 23:17 UTC

    If it was done once on the way into the DB (when a poster hits save) rather than everytime it is displayed it probably wouldn't be too much of a hit on the cpu, but that would still leave the problem of determining perl code blocks from other types of fixed pitch data,


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      How difficult would it be to add <code type=perl> or <code type=html> and parse them differently? Or if it's only going to parse Perl then just have a flag to tell it that it is Perl.

      But I think it would have to be a one time parse as it's added to the DB because it would be just too taxing to parse every time code is displayed.

Re: Re: Feature Request: Adding Colors to Source Code
by ehdonhon (Curate) on Mar 07, 2004 at 17:39 UTC

    Yes, CPU usage is a minor concern. But there are ways to make it not such a big deal.

    The specific case that Vautrin seems to be talking about is when somebody has a big block of Perl code that they have pasted into code blocks. In those cases, it can be hard to follow for those of us who have been spoiled with syntax highlighting. I don't think that means that all code blocks need to be highlighted all the time.

    As multiple others have already suggested, if there was a link, similar to the download code link, that was basically a "View code with context markup", then the monks could use that link whenever they wanted to, but otherwise it would take up no more CPU than normal. This also solves the "not all code blocks are Perl" issue.

Re: Re: Feature Request: Adding Colors to Source Code
by Vautrin (Hermit) on Mar 07, 2004 at 03:11 UTC
    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.
      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' }

Re: Re: Feature Request: Adding Colors to Source Code
by Juerd (Abbot) on Mar 07, 2004 at 20:27 UTC

    We have more free web server CPU then we did before, but not that much more. Also, as more uses come to the site, that free CPU will become less free.

    On my system, rendering the snippet I posted in this thread can be done 575 times per CPU second on my loaded system. I think nodes get updated much less often and am assuming the PM webserver(s) is/are better than mine.

    Should it *become* a problem, you can always turn it off later. It's not as if adding or removing this feature is a lot of work, given that code that implements it is already available.

    Anyway, as castaway just pointed out to me, a lot of stuff gets put in <code> tags that is not /perl/ code.

    So? There will be some coloured words and characters. That looks funny, but in my opinion is not at all a problem.

    If it were a problem, the introduction of a <perl> tag would solve it instantly. But I don't think it is.

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://334540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found