Hello monks!

I need a way to apply syntax highlighting to Perl code. (Mostly short snippets.) I do not require support for any other languages, although some rudimentary checking to differentiate Perl from non-Perl plain text would be a bonus. I realize nothing can parse Perl but perl, and this will not be an exact science, but it doesn't need to be; I just need it to 1) parse the snippets pretty well, most of the time (or fail somewhat gracefully if things get out of hand), and 2) not modify the code itself.

After some false starts with others, I spent a few hours with Perl::Tidy's formatter/write_line API with limited success. It seems to entirely discard lines of tokens if it gets even slightly confused, so my only recourse is to abandon the highlighting and render the source as plain text when that happens. I had less success with other mods.

I do have a slight preference for a server-side (Perl) implementation so I can pre-compile and cache all the XHTML to reduce page render times, thus the reason I'm asking here on perlmonks instead of a JS forum.


In reply to Perl HTML syntax highlighting by wanna_code_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.