1#!/usr/bin/perl
2
3# postconffix - add HTML paragraphs
4
5# Basic operation:
6#
7# - Process input as text blocks separated by one or more empty
8# (or all whitespace) lines.
9#
10# - Don't touch blocks that start with `<' in column zero.
11#
12# The only changes made are:
13#
14# - Put <p>..</p> around text blocks that start in column zero.
15#
16# - Put <pre>..</pre> around text blocks that start elsewhere.
17
18#use Getopt::Std;
19
20#$opt_h = undef;
21#$opt_v = undef;
22#getopts("hv");
23
24#die "Usage: $0 [-hv]\n" if ($opt_h);
25
26#push @ARGV, "/dev/null"; # XXX
27
28while(<>) {
29
30    # Pass through comments and blank linkes before a text block.
31    if (/^(#|\s*$)/) {
32	print;
33	next;
34    }
35
36    # Gobble up the next text block.
37    $block = "";
38    do {
39	$_ =~ s/\s+\n$/\n/;
40	$block .= $_;
41    } while(($_ = <>) && /\S/);
42
43    # Don't touch a text block starting with < in column zero.
44    if ($block =~ /^</) {
45	print "$block\n";
46    }
47
48    # Meta block.
49    elsif ($block =~ /^%/) {
50	print "$block\n";
51    }
52
53    # Example block.
54    elsif ($block =~ /^\S+\s=/) {
55	print "<pre>\n$block</pre>\n\n";
56    }
57
58    # Pre-formatted block.
59    elsif ($block =~ /^\s/) {
60	print "<pre>\n$block</pre>\n\n";
61    }
62
63    # Paragraph block.
64    elsif ($block =~ /^\S/) {
65	print "<p>\n$block</p>\n\n";
66    }
67
68    # Can't happen.
69    else {
70	die "Unrecognized text block:\n$block";
71    }
72}
73