Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Code highlighting?

by radiantmatrix (Parson)
on Oct 06, 2004 at 12:03 UTC ( #397051=monkdiscuss: print w/replies, xml ) Need Help??

Update 2004-10.Oct-19: Due to many concerns, both public and private, this proposal has been withdrawn.

I've forgotten where, now, but I visited a site last week with some example Perl code listings. The code was highlighted much as it might be in Komodo, Emacs, Vi, &c..

Our <code> tags are used mostly for Perl code, but sometimes for other forms of text. So, if the Monestary added code highlighting support, we'd need to have a way of specifying what counts as "Perl code" and what is just a block of preformatted text (without using the <pre> tags. The two that pop into my head are an attribute:

# I've needed to escape slashes in HTML tags... <code type='perl'>print("This is perl code");<\/code> # or <code type='noperl'>This is output, or some non-perl code<\/code>
Or a special tag:
<pcode>Perl code </pcode> #or <notcode>Not code </notcode>

Only one of either pair would need to be implemented: e.g. if <pcode> tags were used, the default <code> tags would be non-perl code. And vice-versa.

I am willing to apply my less-than-saintly coding skills to do the work, but I'm curious before I start: is this something the Monestary would like? If so, how should it work? Restrictions, ideas?

Replies are listed 'Best First'.
Re: Code highlighting?
by demerphq (Chancellor) on Oct 06, 2004 at 12:15 UTC

    Personally i am in favour of something like this. BUT its not so straightforward, and there are legitimate issues such as load, and theme sensitive color schemes that need to be addressed for such an endeavour. Presumably we could use the HTML output form of Perltidy, but Perltidy is a known resource hog.

    You should have a look through the PMD archives as this subject comes up fairly regularly and the comments made before will still be relevent now.


      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


      theme sensitive color schemes that need to be addressed for such an endeavour...

      This can be addressed by doing the coloring in CSS; post processing (using the Syntax::Highlight::Perl keywords for classes):

      <code class="perl"><span class="Directive">print</span> <span class="Quote">"</span><span class="String">Hello world...

      Then everything is still black unless you have a style sheet with code.perl > .Directive { ... } etc. and each style could have its own code styles companion.

      As much as I'd like to see it, it does seem like a big headache, it would mostly only improve readability of longer code, and even using the faster methods available, it would slow things down a bit.

      Oh, and this node, already listed below by cchampion doesn't mention that Perl::Tidy is customizable with its "formatter" arg to perltidy() though it's difficult to use.

Re: Code highlighting?
by kutsu (Priest) on Oct 06, 2004 at 12:23 UTC
Re: Code highlighting?
by PodMaster (Abbot) on Oct 06, 2004 at 12:25 UTC
Re: Code highlighting?
by cchampion (Curate) on Oct 06, 2004 at 13:22 UTC

      Right that the pmdev staff should not be saddled with it. But, it seemed that there were a few people who would enjoy it. Also, it would be very helpful for new SoPW to be able to understand some of the "quickie code" laid down by the Monks.

      That's why I'm offering to do the code -- I couldn't promise that it would be done quickly, but it would be a great exercise. As long as there isn't significant opposition to the idea, and as long as users could turn it off in their preferences, it is something I'd like to do. That makes this particular discussion slightly different than previous nodes.

      My primary questions, then, are:

      • Are there any objections to implementing this?
      • Would the pmdev team roll the patches in, assuming they were done well, etc, etc.?
      • If it were done, how should it be done? IOW, what would make the monestary comfortable with the idea?
      require General::Disclaimer;

        The problem is that this isn't something you can realy opt-out of. For instance if you post code, the server should realy do the HTML markup for highlighting then (no reason to do it over and over). If the next person doesn't want highlighting then they don't use the CSS to highlight it. However even though that person didn't want the highlighting the server still went to the work to implement the code highlighting HTML.

        As I type this I had an idea though. Why doesn't PerlMonks cache a node after formating all the links and formating? As far as I could tell now it reapplies links and formating everytime the node is displayed. Why not add a feild that is identical to the content but holds the already html formated output? When edits are done it presents the original post, and when submited saves the unformated AND formated copies? This could be a huge bonus even if it saves only microseconds per post. Just thinking out loud so try not to shoot me. Anyway I'm all for CSS based syntax highlighting if it can be in a way that doesn't add huge additional load.

        Eric Hodges

        Doing this on the server would be too much load (sounds like it'd be way too much load even using existing C code implementations which don't appear to do it in a flexible enough way). And we can't do the highlighting at submit time because people use different code wrapping settings and it'd be a lot to implement caching of the highlighted code (all of that isn't impossible, but it is a whole lot of work).

        If you really want it on the server, you should probably buy a third web server first (we are pushing the current two pretty hard right now).

        There is already a client-side implementation. If you want it, use that. If it isn't good enough, improve it.

        - tye        

Re: Code highlighting?
by gaal (Parson) on Oct 06, 2004 at 12:17 UTC
    Not directly answering your question, but here's one way to produce colored syntax:

    vim has a convert-to-html (or better: convert-to-xhtml) command that can be used to generate htmlified colorized perl. Here's a shell snippet from the vim docs that with a little love could be a lot of help.

       for f in *.[ch]; do gvim -f +"syn on" +"run! syntax/2html.vim" +"wq" +"q" $f; done

    This is pretty slow, so I don't know how happy the site maintainers would be happy about it. Plus, it's going to be a mess figuring out color schemes for this :)

Re: Code highlighting?
by allolex (Curate) on Oct 07, 2004 at 11:50 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: monkdiscuss [id://397051]
Approved by kutsu
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-12-02 21:58 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (19 votes). Check out past polls.