Hey guys,

I made a page which accepts "natural text", and outputs text that is more compatible with perlmonks.org forum posts.

Specifically, it removes line breaks and enters perlmonks breaks. It also ignores text within code blocks.

Updated: It does the same thing, but it turns them into paragraphs now: see replies below. (original code is still in this post, but the URL points to the new code)

One caveat is that you shouldn't have the string that it's searching for (the result of "code" . ">", but "code" . ">" is fine) in your source code, or it'll screw up.

The URL is here.


#!/usr/bin/perl # Perl Monks Forum Post Formatter, by David Caughell, grasp_the_sun@ya +hoo.co.uk # Changes end-of-line chars to <br>'s, and avoids "code" . ">" section +s use strict; use warnings; use CGI qw(:standard -debug); # output in text format to allow cutting and pasting print "Content-type: text/plain\n\n\n"; my $post = param('FPost'); my $match = "code" . ">"; my $target = 0; my $numoccur = 0; # while there are matches from the target onwards while (index(lc($post),$match,$target) != -1) { ++$numoccur; # if they're an opening match if ($numoccur % 2 == 1) { # make substitutions from previous target to new target substr($post,$target,index(lc($post),$match,$target) - $target) =~ s/ +\n/<br>/g; } $target = index(lc($post),$match,$target) + length($match) - 1; # +the "<" } # either close the code block or do the final substitution if ($numoccur % 2 == 1) { $post = $post . $match; } else { substr($post,$target) =~ s/\n/<br>/g; } print "$post\n\n\n\n\n"; # for all you other-language programmers out there, here are some stat +istics: # lines of source: 39 # empty lines: 9 # commenting (shebang to be considered code): 7 # lines containing only "}": 4 # lines of code: 19
--Dave.

P.S. I'd like to send a special shout-out to dws, who understandably wouldn't tell me how to make a post to the forum (I have done some replies already, but never started a thread).

Update: P.P.S. Though I'd like to take that remark back, but since it was replied to, I'm going to have to leave it there.


In reply to PM Poster (util for ease of posting / reply) by David Caughell

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.