Sigh. I was hoping you wouldn't say that. ;)

Personally, i think you should use said module with the 'paras' arg instead of 'lines'. The reason is because the browser does an excellent job with text placement. If you are worried about width, just embed the resulting writeup in a <table>. Besides, 'paras' _does_ eliminate that unwanted white space.

thraxil's solution is nice, by the way. Note how both \n and \r is accounted for. thraxil++

If you are still hell bent on using <br> tags then here is a hack i came up with, borrowing a little from thraxil and accounting for extra whitespace:

$comment =~ s/(?:\s*[\n\r]\s*){2,}/\n<p>/g; $comment =~ s/[\n\r](?!<p>)/<br>\n/g; $comment =~ s/<p>/<p>\n/g;
The first regex replaces two or more newlines surrounding by possible other whitespace with a <p> on it's own line (and if you think that the two \s* thingies are unecessary, try this without em). I left out the trailing new line in the substitution because i just couldn't get a negative lookahead to work in the next regex. Hence, the third regex. I am sure that there is a way to use a negative lookahead to deprecate having to resort to the third regex, but I would just use HTML::FromText anyway!

The second regex replaces all newlines that are not followed by a <p> tag with a <br> tag and newline. I would have rather liked for this to work:

$comment =~ s/(?:\s*[\n\r]\s*){2,}/\n<p>\n/g; $content =~ s/(?!<p>)[\n\r](?!<p>)/<br>\n/g;
but as i said, this just didn't work. :( .o0(?)

UPDATE:
Looks like you have your solution, but consider how much time it takes (barring educational purposes of course) for you to figure out these little details instead of finding a CPAN module - especially when puting together a site. Granted, this one didn't do exactly what you need - but, do you really need 'exactly' what you need? (ask that question to the great film makers)

jeffa


In reply to (jeffa) 2Re: HTML String Parsing by jeffa
in thread HTML String Parsing by Ionizor

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.