I have to output text from a db to a web browser. The text gets input by anyone and his dog, so the rules for what they can do are simple:
(1) they can enter line breaks, which will be translated into
<BR> tags;
(2) they can precede one or more successive lines with a '-' or a '·' and those lines will be formatted in HTML as a bulleted list.
All tags they try to enter get stripped out, and any other formatting they attempt is ignored.
So I have to come up with a way to do the above simple formatting to any block of text. I have done this. BUT it looks way clunky to me. I wd love to crib your code if you felt like sharing a sharper way to do it; and I am sure I would learn a lot by seeing how somebody better than I am does this.
Here's my version:
$text =~ s/\n|^/<br>/gs;
$text =~ s/<br>\s*[-·]/<ul><li> /gs;
$text =~ s/<ul><li>([^<]*)<ul>/<ul><li>$1/gs;
$text =~ s/<li>([^<]*)(<br>\n*\s*)+/<li>$1<\/ul>/gs;
I'd be very grateful for any suggestions how to improve this.
§
George Sherston
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.