#!/usr/bin/perl -w # # Copyright (C) 2003 Julian Fong . All rights reserved. # # Permission to use, copy, modify, distribute, and sell this software # and its documentation for any purpose is hereby granted without fee, # provided that the above copyright notice appear in all copies and # that both that copyright notice and this permission notice appear in # supporting documentation. No representations are made about the # suitability of this software for any purpose. It is provided "as # is" without express or implied warranty. # # Converts a minimally formatted text file into minimally formatted # HTML. Handles newline separated paragraphs, embedded top level tags # (ie pre or table), and nested unordered and ordered lists # 09/26/03: added
 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"; $blocktype = 2; $indentlevel = 1; $indentlengths[$indentlevel] = $1; } else { # If the indentation level has changed, push or pop # the indentation stack if (length($1) > length($indentlengths[$indentlevel])) { $indentlevel += 1; $indentlengths[$indentlevel] = $1; print "\n
      \n"; } else { # Pop back as far as we need to while (length($1) < length($indentlengths[$indentlevel])) { $indentlevel -= 1; print "\n
    \n"; } } print "\n"; } print "
  • \n"; print "$2\n"; } # Ditto, but for ordered lists. elsif (/^(\s*)\#(.*)/) { if ($blocktype != 3) { print endofblock($blocktype, $indentlevel); print "\n\n
      \n"; $blocktype = 3; $indentlevel = 1; $indentlengths[$indentlevel] = $1; } else { if (length($1) > length($indentlengths[$indentlevel])) { $indentlevel += 1; $indentlengths[$indentlevel] = $1; print "\n
        \n"; } else { # Pop back as far as we need to while (length($1) < length($indentlengths[$indentlevel])) { $indentlevel -= 1; print "\n
      \n"; } } print "\n"; } print "
    1. \n"; print "$2\n"; } # Transitioning from newline - start a new paragraph elsif (!$blocktype) { $blocktype = 1; 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