#!/usr/bin/perl
# Perl Monks Forum Post Formatter, by David Caughell, grasp_the_sun@yahoo.co.uk
# Changes end-of-line chars to
's, and avoids "code" . ">" sections
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/
/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/
/g;
}
print "$post\n\n\n\n\n";
# for all you other-language programmers out there, here are some statistics:
# lines of source: 39
# empty lines: 9
# commenting (shebang to be considered code): 7
# lines containing only "}": 4
# lines of code: 19