I'm not sure if you want to home-brew something simple (which would give you exactly the results you wanted, rather than having to endlessly tweak someone else's output), but I'll describe how I would approach the problem.

I have, in the past, written a script or two to get some XML/XHTML/HTML formatted more acceptibly. The basic algorithm I use is pretty simple. First, just tokenize the text stream based on tags, and keep a simple counter of what level we're at. An opening tag increments the counter, a closing tag decrements it. Then you print that tag, prefixed by the appropriate amount of indentation. Of course, with plain HTML you must be careful of tags that do not commonly have a closing tag (such as a, p, img), but with well-formed XML/XHTML you do not have to worry as much (other than to watch out for single tags that open and close themselves, like <br/>).

Update: scooped by diotalevi. I guess that's what happens when you have to walk away for 5 minutes to talk to your boss. :) I should note that diotalevi's code pretty much does exactly what I describe here, so maybe my post will still be a useful plain-english explanation? (Grasping at straws here.)


In reply to Re: X(ht)ML Source Formatting by revdiablo
in thread X(ht)ML Source Formatting by ViceRaid

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.