#!/usr/bin/perl -w
#
# Copyright (C) 2003 Julian Fong
block handling
use POSIX;
use strict;
my (
$blocktype, # 1 for paragraph, 2 for UL list, 3 for OL list, 4 for HTML block level, 5 for preformatted chunks
$indentlevel,
@indentlengths
);
$blocktype = 0;
$indentlevel = 0;
print << "HEADER";
untitled
HEADER
sub endofblock {
my ($blocktype, $indentlevel) = @_;
my $i;
my $return = '';
print "\n";
if ($blocktype == 1) {
$return .= "\n";
} elsif ($blocktype == 2) {
$return .= "\n";
for ($i = 0; $i < $indentlevel; $i++) {
$return .= "\n";
}
} elsif ($blocktype == 3) {
$return .= "\n";
for ($i = 0; $i < $indentlevel; $i++) {
$return .= "\n";
}
}
$return;
}
while (<>) {
# Handle preformatted blocks. First case: in the middle of a pre block
if ($blocktype == 5) {
if (/^\s*<\/pre/) {
print "\n";
print;
$indentlevel = 0;
$blocktype = 0;
} else {
print;
}
}
elsif (/^\s*\n";
print;
}
# Ignore empty lines, but keep track of them to ensure
# that we correctly start the next paragraph
elsif (/^\s*$/) {
# Handle end of block transitions
if ($blocktype) {
print endofblock($blocktype, $indentlevel);
$indentlevel = 0;
$blocktype = 0;
}
}
else {
# Determine whether this is a special HTML block which should
# not be wrapped in P tags. Currently this includes lists,
# tables, blockquotes
if (/^\s*<(?:table|blockquote|ul|ol|nl|dl)/i) {
# End the block
if ($blocktype) {
print endofblock($blocktype, $indentlevel);
}
$indentlevel = 0;
$blocktype = 4;
print;
}
# Looks like it might be a list? Then set one up.
elsif (/^(\s*)-(.*)/) {
if ($blocktype != 2) {
print endofblock($blocktype, $indentlevel);
print "\n\n\n"; print; } # Just maintain the current blocktype, do nothing else { print; } } } # Handle end of block transitions if ($blocktype) { print endofblock($blocktype, $indentlevel); } print << "FOOTER"; FOOTER