1#!/usr/bin/perl -w
2#
3# A plain-text documentation processor.
4# Copyright Chris Cannam 1998
5
6use strict;
7
8my $hbase = 2; # main heading is h$hbase, next is h($hbase+1) etc
9
10my @p;
11my $laststyle = "none";
12
13my $title = $ARGV[0];
14if (defined $title) { $title =~ s/\.txt//; }
15else { $title = "Untitled"; }
16
17my $filename = $ARGV[0];
18if (!defined $filename) { $filename = "(stdin)"; }
19
20print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">';
21print "\n<html><head>\n";
22print '<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">';
23print "\n<title>$title</title></head><body>\n<!-- Do not edit! Automatically generated from $filename by $0 -->\n";
24#print "<contents>\n"; # for automatic TOC generator
25
26my $hadtoc = 0;
27
28# Returned strings here are symbolic -- they only resemble HTML.
29# Don't change them, change the interpretation of them in write_para
30
31sub style_para
32{
33    # heading?
34    if ($#p == 1) {
35	$p[1] =~ s/^=+$// and return "h1";
36	$p[1] =~ s/^-+$// and return "h2";
37	$p[1] =~ s/^~+$// and return "h3";
38    }
39
40    # something wholly indented?
41    my @nni = grep { /^\s/ } @p;
42    if (@p and $#nni == $#p) {
43	$p[0] =~ s/^\s+[^\w\s\$\%\#]\s+([\w\"&])/$1/ and return "uli";
44	$p[0] =~ s/^\s+\d+[^\w\s]\s+([\w\"&])/$1/ and return "oli";
45	$p[0] =~ s/^\s+[a-z][^\w\s]\s+([\w\"&])/$1/ and return "cli";
46	$p[0] =~ s/^(\s+[^\w\s\d])/$1/ and return "pre";
47	return "cont";
48    }
49
50    # anything else
51    return "p";
52}
53
54sub write_head
55{
56    my $n = shift;
57    my $content = shift;
58    chop $content;
59    my $m = $hbase + $n - 1;
60    print "<h$m>$content</h$m>\n";
61}
62
63sub write_para
64{
65    my $style = shift;
66
67    if ($style eq "cont" and
68	!($laststyle eq "uli" or $laststyle eq "oli" or $laststyle eq "cli")) {
69	$style = "pre";
70    }
71
72    if (!($style eq $laststyle or $style eq "cont")) {
73
74	if ($laststyle eq "uli") {
75	    print "</ul>";
76	} elsif ($laststyle eq "oli") {
77	    print "</ol>";
78	} elsif ($laststyle eq "cli") {
79	    print "</ol>";
80	} elsif ($laststyle eq "pre") {
81	    print "</pre>";
82	}
83
84	if ($style eq "uli") {
85	    print "<ul>";
86	} elsif ($style eq "oli") {
87	    print "<ol>";
88	} elsif ($style eq "cli") {
89	    print "<ol type='a'>";
90	} elsif ($style eq "pre") {
91	    print "<pre>";
92	}
93    }
94
95    print "\n";
96
97    if (!$hadtoc and $style ne "h1") {
98        print "<CONTENTS>\n";
99        $hadtoc = 1;
100    }
101
102    if ($style eq "h1") {
103        write_head 1, $p[0];
104    } elsif ($style eq "h2") {
105        write_head 2, $p[0];
106    } elsif ($style eq "h3") {
107        write_head 3, $p[0];
108    } elsif ($style eq "uli" or $style eq "oli" or $style eq "cli") {
109	print "<br/><br/></li>" if ($laststyle eq $style);
110	$p[0] =~ s/^\s+(?:\d+)?[^\w\s]\s+//;
111	print "<li>@p";
112    } elsif ($style eq "cont") {
113	$style = $laststyle;
114	if ($style eq "uli" or $style eq "oli" or $style eq "cli") {
115	  print "<br/><br/>@p";
116	} else {
117	  print "<p>@p";
118	}
119    } elsif ($style eq "p") {
120	print "<p>@p</p>";
121    } else {
122	print @p;
123    }
124
125    if (!$hadtoc and $style eq "h1") {
126        print "<CONTENTS>\n";
127        $hadtoc = 1;
128    }
129
130    $laststyle = $style;
131}
132
133while (<>)
134{
135    s/&/&amp;/g;
136    s/</&lt;/g;
137    s/>/&gt;/g;
138
139    s, &lt; URL: ( [^&:\"]* ) .txt &gt; ,<A HREF="$1.html">$1.html</A>,xg;
140    s, &lt; URL: ( [^&\"]*  )      &gt; ,<A HREF="$1">$1</A>,xg;
141
142    s,    \*( \w[\w\s0-9-]* )\*    ,<B>$1</B>,xg;
143    s, \b  _( \w[\w\s0-9-]* )_  \b ,<I>$1</I>,xg;
144
145    s, \\([\[\]]) ,$1,xg or s, \[( .*? )\] ,<CODE>$1</CODE>,xg;
146
147    if (/^\s*$/) {
148	write_para style_para if $#p >= 0;
149	undef @p;
150    } else {
151	push @p, $_;
152	if (/^[=~-]+$/ && $#p == 1) {
153	    write_para style_para;
154	    undef @p;
155	}
156    }
157}
158
159#write_para style_para if defined @p;
160write_para style_para;
161print "\n</CONTENTS>\n</body>\n</html>\n";
162
163
164