Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl Monk, Perl Meditation

syntax highlighting of code in perlmonks forums

by aquarium (Curate)
on Jun 10, 2003 at 02:06 UTC ( #264521=monkdiscuss: print w/ replies, xml ) Need Help??

not sure if this has been raised before...would other monks like to see the code written in questions and answers syntax highlighted for easier reading? e.g. gvim style. i pinched code from another post to show how it would look if colored by me with Vim.
#! perl -w

use strict;
use File::Copy;

my $infile = "C:/radtrans/doclisth.chr";

my ( $yr, $mo, $dy ) = (localtime)<font color="#00ffff">5</font>,<font color="#00ffff">4</font>,<font color="#00ffff">3</font>;
my $outfile = sprintf( "C:/radtrans/%04d%02d%02d.txt",$yr+1900,$mo+1,$
+dy );

my $staticdir = "C:\\radtrans\\";

open IN, "<$infile" or die "Couldn't open $infile, $!";
open OUT,">$outfile" or die "Couldn't open $outfile, $!";

while (<IN>) {
    my @fields   = split /|/;

    my $newfile = $fields<font color="#00ffff">0</font>;
    my $path_str = $fields<font color="#00ffff">12</font>;

    do { warn "Empty field 13"; next } unless $path_str;
    my @path = split /\\/, $path_str;
    my $dir = join "\\", @path <font color="#00ffff">0</font>, <font color="#00ffff">1</font>, <font color="#00ffff">2</font>, <font color="#00ffff">3</font>, <font color="#00ffff">4</font>, <font color="#00ffff">5</font>, <font color="#00ffff">6</font> ;

    $newfile =~ s/$/.rtf/;

    my $out = join ('|', @fields<font color="#00ffff">0.</font>.<font color="#00ffff">9</font>) . "@" . $staticdir . $newfile;

    print OUT "$out\n";


close IN;

sub process_dir {

    my ($dir, $newfile) = @_;

    do { warn "$dir does not exist!\n"; return } unless -e $dir;
    opendir DIR, $dir or do { warn "Could not open $dir $!\n" ; return
    while ( my $file = readdir DIR ) {

        print "dir: $dir file:$file newfile:$newfile\n";
        #before the next unless statements.

        next unless -f "$dir\\$file";

        next unless $file =~ m/\.rtf$/i;

        copy( "$dir\\$file", "C:\\temp\\$newfile" )
          or die "Failed to copy $file: $!\n";

Edit by tye, add READMORE tag

Comment on syntax highlighting of code in perlmonks forums
•Re: syntax highlighting of code in perlmonks forums
by merlyn (Sage) on Jun 10, 2003 at 02:12 UTC
    That looks really hard to read on my white background, and is probably also hard to read on a black background. Did you have some other color in mind for the background?

    Personally, I can easily do without syntax coloring. It just puts too much flash on the screen. Maybe it's because I'm not part of the MTV Road Rules generation, but I'd rather just see plain mono-spaced text, thank you.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      and is probably also hard to read on a black background.

      It's well readable on a black background, but still, these colours hurt :)

      Juerd # { site => '', plp_site => '', do_not_use => 'spamtrap' }

      vim has both "light background" and "dark background" settings. Looks like aquarium was using the dark background setting when he shouldn't have. On a standard black terminal, those colors are just fine. He should have modified his vim settings before converting the file to HTML.

      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

Re: syntax highlighting of code in perlmonks forums
by CukiMnstr (Deacon) on Jun 10, 2003 at 02:19 UTC
    Even if I use syntax highlighting and like it very much, I don't think it is appropriate for code posted here.
    First of all, some monks have said here that they do not like syntax highlight, so I guess you will find the first critics for your idea there.
    And then there is the big question: which colorscheme should the monastery use? For example, the code you just posted is unreadable on my white background, and there are many different looks (or color themes) one can choose from the user preferences. You can even use your own stylesheet to define your own colors.
    With so many choices for the colorscheme of the site, syntax highlight for the code posted seems like too much trouble to be worth. Besides, you can always d/l the code and open it in vim ;)

    hope this helps,

      Which now brings up the question - is there a vim mode for browsing PerlMonks that knows what to do with the d/l code links?

        You just have to tell your browser to open the particular content type (text/plain with gvim, or a g?vim? in a terminal emulator egxterm -e vim. Opera will all you to do all manner of silly things with content types it doesn't know internally, such as passing the uri/downloaded version/filenames to another program etc...

        I'm pretty sure the only thing you could do at PM's side to help this is by allowing the user to select the Content-Type header sent with code downloads, allowing the user to specify a header of text/x-vim, or perhaps a type perlish, so windows users get it going to their perl editor instead of their regular text editor (which most likely defaults to notepad)

        @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re: syntax highlighting of code in perlmonks forums
by pfaut (Priest) on Jun 10, 2003 at 02:32 UTC

    Just to add to what's already been mentioned: not everything inside <code> tags is perl code. Also, it would take a lot of horsepower that I think the site doesn't need to waste.

    90% of every Perl application is already written.
Re: syntax highlighting of code in perlmonks forums
by Popcorn Dave (Abbot) on Jun 10, 2003 at 03:25 UTC
    I have to agree with Merlyn that what you did is horribly hard to read on a white background. That said, perhaps the ability to highlight certain parts of code would be more to what you're after, but I'm not sure how you're going to balance the color scheme with people's ability to use CSS to control their own colors here.

    It's an interesting idea, but perhaps better left to your own code editor.

    There is no emoticon for what I'm feeling now.

      ..well on a black or dark blue it looks good. the site colorizes so much as you all mention, and there's css on top. If not all than at least some would benefit from syntax colors instead of all the other color jazz-me-ups. And it wouldn't be too hard to have a new tag for <perl_code> And, if it's possible to have your own color schemes for the forums, so it would be possible to have your own syntax highlight color scheme. Copying others code, then perhaps opening vim/jedit, pasting code, and then setting syntax color mode, is all a bit tedious. hey, i'm lazy. + to me it seems clearer to see color highlighted code together with the comments. have i conviced anyone ?:)
Re: syntax highlighting of code in perlmonks forums
by converter (Priest) on Jun 10, 2003 at 03:31 UTC
Re: syntax highlighting of code in perlmonks forums
by daeve (Deacon) on Jun 10, 2003 at 04:29 UTC
    If it were implemented I think it should be controlable in the user settings. Either the ability to select the colors to use for displaying the text or just: Use color with code? y/n.

    I for one cannot read text displayed in certain colors and it is getting worse as I am being dragged kicking and clawing toward the mid century mark so black and white is fine with me.

    Just my $0.02

Re: syntax highlighting of code in perlmonks forums
by theorbtwo (Prior) on Jun 10, 2003 at 04:47 UTC

    With a light background, that's unreadable, so schemes have to be customizable... But people can write their own schemes with CSS, which means that the syntax-hilighting output has to be easily CSS-manipulable to match... which means we have to either mess with the output of an existing colorizer a lot, or we have to write our own.

    It uses a lot of CPU.

    It'd be a lot of work to put up.

    It'd get a lot of code, such as many obfuscations, wrong, which would make them even more difficult. Those that it doesn't get wrong it would make too easy, or just make them look nacious.

    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).

      And, even worse, if it got obfuscations right, they wouldn't be obfuscations any more.


Re: syntax highlighting of code in perlmonks forums
by Arguile (Hermit) on Jun 10, 2003 at 05:44 UTC

    I’d strongly disagree with doing this within the monastery for a ton of reasons, most already hashed out in other posts. That said, I used to see the code sections syntax highlighted.

    Well before this site became nicer with XML and CSS support nashdj created pmproxy2 as a rewriting proxy to create his own themes.

    I stumbled across Perl::Syntax::Highlight and thought it would be cool to add syntax highlighting to his rewriting proxy. After a bit of work, it provided CSS control over each type of code element. It worked well for my needs at the time, but it has its issues.

    Update: Heh, I found some old screenshots from it. Here, here, and one from rewriting here.

    Now that the site is cleaner (XML, CSS, etc.), I’ve been meaning to rewrite it properly. I have a framework using XML::SAX and Apache + mod_perl started if you’d be interested in continuing it.

    Basically it rewrites the documents using event based rules; calling either XSLT or Perl filters. Using the value of the host header to rewrite URIs to point back to it instead of, adding syntax highlighting to stand alone code sections and javascript controls to turn the CSS behind them on and off for each section, etc.

    The framework and a bunch of the event handler are there, I just don’t really care enough about syntax highlighting anymore to finish it (the new custom CSS feature on the site is enough for me). So if you’re interested either check out the original pmproxy2 or I can email you the code (it’s way to many modules and snippets to post here).

    The old code may suit your needs with few changes to the regexps. Parsing HMTL with regexps isn’t great, but the old killed HTML::Parser more often than not ;).

Re: syntax highlighting of code in perlmonks forums
by parv (Priest) on Jun 10, 2003 at 06:34 UTC

    I rather not have highlighting by colors by default. If/When we do, it should be only via CSS, so that i can override the damn thing in my user agent as i want. OTOH i would not mind some light highlighting via bolding and italicizing... much like enscript(0) does w/ default level of language highlighting (or as the highlighted text appears in black-and-white on paper).


Re: syntax highlighting of code in perlmonks forums
by allolex (Curate) on Jun 10, 2003 at 07:15 UTC

    Nah, I had to highlight the code with my mouse to make it a legible white on blue, although this particular highlighting scheme looks really good in Links/Lynx. =)

    But seriously, I really haven't had much trouble reading the code on this site and I always have an editor open (with my own syntax highlighting), just in case I want to paste a snippet into it. So, for me, syntax highlighting at the Monastery is really not necessary.


Re: syntax highlighting of code in perlmonks forums
by hossman (Prior) on Jun 10, 2003 at 07:38 UTC

    I don't think this would be a very useful "general" feature for all <code> in the monestary, for many of the reasons listed, my biggest personal reason being that typically when i read code here, it's only "snippets" of code, that's short enough to be easier to read without a lot of markup.

    If there was a node that really had a LOT of code, that i wanted to read with syntax highlighting, i would download it, and open it in an editor (which would also allow me to re-indent the code, something that's more important to me for readability then color.)

    Which makes me think: perhaps having a user customizable mime-type sent by the server when a user clicks the "d/l code" link would be more usefull (and simplier to impliment) then syntax highlighting ... then you could either just use text/plain, or you could create your own "x-code/perl" mime type and register your favorite editor/syntax highlither as the handler in your www browser.

Re: syntax highlighting of code in perlmonks forums
by Juerd (Abbot) on Jun 10, 2003 at 10:08 UTC

    I like the idea, and even think it'd be good if this were enabled by default. Just not with the colours that you propose; they burn holes in my screen :)

    Not everything in code-blocks is Perl source, but I see no harm in trying to render it as such. After all, only colour changes, not the text itself. Does it matter if some non-code is coloured differently?

    Perhaps the Monastery could add <span> tags automatically so that people who want colours can put some colour definitions in their personal CSS. That wouldn't change anything for users that think colours are "gay".

    One problem could be server load, but I don't know how well Everything caches, so I can't say much about that. I do know that on my box, Syntax::Highlight::Perl performs better than Perl::Tidy. It isn't as good, though, but I can live with the results. My Scribble service uses S::H::P, and I copied your code so you can have a look at how S::H::P does it: (link dies in 30 days). Colours are not hard coded, but in an external CSS file. Disable CSS and the colours are gone. Ideal, IMO.

    Horrible code, by the way...

    Juerd # { site => '', plp_site => '', do_not_use => 'spamtrap' }

      I'm a massive fan of using the particular monk's CSS to determine the hilighting... and providing a seperate option to enable it. (disabled by default)

      Perhaps also providing a couple of default styles to get you started, a nice light-on-dark, a dark-on-light etc

      even if it was just based on css, without an off switch

      .codeblock { background-color: inherit!important; color:inherit!important; }
      and boom, the hilighting disappears... and it starts looking like normal text (black on white) Or you could even do something outragious and provide a real user stylesheet to your browser to over-ride these things...
      @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re: syntax highlighting of code in perlmonks forums
by YAFZ (Pilgrim) on Jun 10, 2003 at 11:58 UTC
    Looks nice on my black background (I always prefer black or dark backgrounds, big white areas are torturing my eyes as well as my soul, I just don't understand why people prefer big white web pages :). But I think the objections above must be taken into account. It'd better be left off or implemented as an optional functionality.
Re: syntax highlighting of code in perlmonks forums
by novitiate (Scribe) on Jun 10, 2003 at 15:21 UTC
    Aside from the colors, it's also turing the hash and array
    subscript operators into dead links...


    "...goodnight you princes of main(e)"  --The Cider House Rules
Re: syntax highlighting of code in perlmonks forums
by krujos (Curate) on Jun 10, 2003 at 22:26 UTC
    This would make life difficult for people using custom css that modified the code tag. Some of the above post is nearly invisible with my css.
Re: syntax highlighting of code in perlmonks forums
by hbo (Monk) on Jun 11, 2003 at 07:39 UTC
    Parsing Perl well enough to do a good job of syntax highlighting is hard. The ony program I've seen that seems to get it right all the time is perltidy. It's 20,000 lines of Perl, most of which aren't concerned with syntax highlighting. But that gives a sense of the scale of the problem.

    So, let's say you had a special tag, <perl_code> has been suggested here, that would run the tag contents through perltidy --html. The highlighting is implemented with a CSS stylesheet, which could be incorporated into the standard site CSS. To turn off the highlighting, or to implement your own style, just add a custom stylesheet.

    Of course, it would still be a significant load on the server(s).

Re: syntax highlighting of code in perlmonks forums
by staeryatz (Monk) on Jun 12, 2003 at 20:14 UTC
    I dont' know if you've ever heard of my program, webcpp. It converts source code into HTML and supports colour schemes, and over 30 languages, including PERL. It uses its own CSS namespace, so no interference. In recent versions I've implemented an option for creating snippets only. That combined with the ability to use STDIN/STDOUT, and it's cgi-readiness, it should be able to work on a webserver, with the help of a perl cgi interface. It's written in C++, but it works on windows, and pretty much compiles in all UNIX variants will Gcc 3.0 or higher. The PerlMonks may be able to get this going, just a thought. I'm not sure how it'll handle Japh (coz I'm not a Perl hacker), but maybe someone should give it a try.
Re: syntax highlighting of code in perlmonks forums
by jettero (Monsignor) on Jan 04, 2007 at 20:12 UTC

    I was about to try to introduce this topic as a new discussion, but I was happy to see it's come up before. Personally, I'd really like to see this but have it add spans with names so you could add your own colors iff you wanted them. I imagine there'd be a 50/50 split on people that would and would not like this.

    I'm under the impression that Perl::Tidy does this highlighting automagically. I've never tried to use it for syntax highlighting. But even if it can't do it, it's not much of a stretch to do the syntax markup.

    Anyway, take a look at the pure beauty of Evolution of a POE Server to see why I dig it. Gorgeous.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (14)
As of 2014-04-21 16:18 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (496 votes), past polls