This question is possibly a little off-topic in that it doesn't directly concern Perl, but the issue keeps cropping up for me in CGI/mod_perl work with Perl.

I usually have some means of displaying the full text of error messages in the web browser during development, often including pieces of code, e.g. stacktraces. Due to the presence of pieces of code, such messages are usually best displayed in a fixed-width font and benefit from whitespace being preserved rather than crunched as in HTML. The simplest way to achieve this is to use <pre> tags, after first running the message text through something like Apache::Util::escape_html() to escape unsafe HTML characters.

However, the problem with <pre> tags is that the text within them, being interpreted as "pre-formatted", doesn't get wrapped to the web browser window. Using <code> tags instead gives the desired wrapping effect, but then whitespace gets crunched.

I don't see any other HTML tags that would be of use (there is a (non-standard) <xmp> tag for "example" code which behaves like <pre> and does escaping of unsafe charcaters too, but still doesn't wrap to the window), so the text probably needs processing in Perl first, and then maybe displaying with <code> tags.

Currently I'm thinking of changing newlines to <br> tags and spaces to &nbsp; entities (after escaping unsafe characters first) and then using <code> tags, but I wondered if others had a more elegant way of doing this.

- Steve


In reply to Formatting CGI script error messages by shay

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.