5 use Style qw(html_header html_footer);
8 # Perl version written by Kamion; based on a sed script by Aquarius
13 Usage: $0 index-filename [up-URL] < faq-document
15 URLs may be absolute or relative.
24 return unless defined $$line;
25 $$line =~ s/\&/&/g;
28 $$line =~ s/"/"/g;
29 $$line =~ s/æ/æ/g;
33 #my $sts_link = "http://riva.ucam.org/~kamion/archive-bin/" .
34 # "article.pl?msgid=5n0qq5\$d9p\@mercury.dur.ac.uk";
35 my $sts_link = 'http://groups.google.com/groups?' .
36 'q=Easter+Egg&ic=1&selm=5n0qq5%24d9p%40mercury.dur.ac.uk';
38 # Paragraph to format and undefine, followed optionally by open and close tags.
39 sub print_paragraph (\$;$$)
42 return unless defined $$para && $$para !~ /^\s*$/;
43 my $open_tags = shift;
44 my $close_tags = shift;
47 $$para =~ s#(http:(?:[^\ \&<]|\&(?!gt;))*)
48 #<A TARGET="_top" HREF="$1">$1</A>#gx;
50 # Make all text within pairs of underscores outside URLs emphasized
51 $$para =~ s[_((?:[^_.-])+\.?)_
52 (?![^"]*">|[^<]*</A>)][<EM>$1</EM>]gx;
54 # Similarly, strengthen bold text within asterisks
55 $$para =~ s[\*((?:[^*.])+\.?)\*
56 (?![^"]*">|[^<]*</A>)][<STRONG>$1</STRONG>]gx;
59 $$para =~ s|(Sephrenia the Styric)|<A HREF="$sts_link">$1</A>|g;
61 $$para = "$open_tags$$para" if defined $open_tags;
62 $$para .= $close_tags if defined $close_tags;
68 my $index_file = shift or usage;
71 open CONTROL, ">control.html" or die "Can't write to control.html: $!";
74 html_header "Revision information", $index_file;
82 $changes_done = 1 if (/^Archive-name:/);
83 next unless $changes_done;
87 unless (/^([^:]+:)\s*(.+)$/)
89 warn "Non-header line found in control section: $_";
96 html_footer $index_file;
98 open INDEX, ">$index_file" or die "Can't write to $index_file: $!";
101 html_header "Contents", $up_url;
104 print "<H1 ALIGN=\"center\">\n";
113 print "<P><A HREF=\"control.html\">Revision information</A></P>\n";
125 # Stop at TOC-body separator
128 print_paragraph $paragraph, "", $listentry ? "</A>" : "";
131 # Convert headings to links
133 if ($line =~ /([0-9]+)((?:.[0-9]+)*)[\)\s]/)
136 my $heading = "$1$2";
137 my @components = split /\./, $heading;
140 $line =~ /\s+(.*)$/g;
142 print_paragraph $paragraph, "", $listentry ? "</A>" : "";
144 # Change level of ordered list if necessary
145 if (@components > $level)
147 print "<OL>\n" x (@components - $level);
149 elsif (@components < $level)
151 print "</OL>\n" x ($level - @components);
153 #print "</P>\n\n" if @components == 1 && $level > 0;
154 print "\n<BR>\n\n" if @components == 1 && $level > 0;
155 $level = @components;
156 $#lastvalues = $level;
157 #print "<P>\n" if $level == 1;
159 # Reformat the current line
160 my $value = $components[$#components];
161 if (defined $lastvalues[$level] &&
162 $lastvalues[$level] + 1 != $value)
164 warn "Missing item in contents before $heading";
165 print "<LI VALUE=\"$value\">";
169 print "<LI VALUE=\"$value\">";
175 $lastvalues[$level] = $value;
176 print "<A HREF=\"part$firstcomp.html#sec$heading\">\n";
177 $paragraph = $linetail;
182 elsif (/^[A-Z][^a-z]+$/)
184 print_paragraph $paragraph, "", $listentry ? "</A>" : "";
187 print "\n<H2>$_</H2>\n\n";
192 print "</OL>\n\n" x ($level - 1);
193 print "\n" if $level == 1;
194 print "<H3>$_</H3>\n\n";
195 print "<OL>\n" x ($level - 1);
204 print_paragraph $paragraph, "", $listentry ? "</A>" : "";
208 print_paragraph $paragraph;
212 # Do all the necessary end tags
213 #print "</P>\n" if $level > 0;
214 print "\n<BR>\n" if $level > 0;
216 print "</OL>\n\n" x $level;
225 my $section_open = 0;
226 my $num_sections = $lastvalues[1];
232 my $heading_pat = qr/^\s*([0-9]+(?:.[0-9]+)*)\)\s+/;
239 if (/^\s*([0-9]+)\)\s/)
241 chomp $paragraph if defined $paragraph;
246 print_paragraph $paragraph, "", "</STRONG></A>";
250 print_paragraph $paragraph, "<P>\n", "\n</P>";
253 $in_heading = $in_h2 = 0;
254 $oldsection = $section;
256 print "\n" if $section_open;
257 print "</OL>\n\n" x $level if $section_open;
258 html_footer $index_file,
260 ("part" . ($oldsection - 1) . ".html") :
262 "part" . ($oldsection + 1) . ".html"
264 open SECTION, ">part$section.html" or
265 die "Couldn't write to part$section.html: $!";
267 html_header "section $section",
269 "part" . ($section - 1) . ".html",
270 ($section < $num_sections) ?
271 ("part" . ($section + 1) . ".html") :
278 # Convert headings to anchors in list items
282 my @components = split /\./, $heading;
283 chomp $paragraph if defined $paragraph;
286 print_paragraph $paragraph, "", "</STRONG></A>\n";
290 print_paragraph $paragraph, "<P>\n", "\n</P>\n";
294 # Change level of ordered list if necessary
295 if (@components > $level)
297 print "<OL>\n\n" x (@components - $level);
299 elsif (@components < $level)
301 print "</OL>\n\n" x ($level - @components);
303 $level = @components;
304 $#lastvalues = $level;
306 my $value = $components[$#components];
307 if (@components == 1)
310 s|$heading_pat|<LI><H2><A NAME="sec$1">|;
313 elsif (defined $lastvalues[$level] &&
314 $lastvalues[$level] + 1 != $value)
316 warn "Missing item in main document before $heading";
317 s|$heading_pat|<LI VALUE="$value"><A NAME="sec$1"><STRONG>|;
321 s|$heading_pat|<LI VALUE="$value"><A NAME="sec$1"><STRONG>|;
325 s|$heading_pat|<LI><A NAME="sec$1"><STRONG>|;
327 $lastvalues[$level] = $value;
329 warn "Heading $heading encountered outside a section"
330 unless defined $section;
331 warn "Heading $heading encountered in section $section"
332 if defined $section && $heading !~ /^$section(?:\.|$)/;
337 # Mark unnumbered headings
338 if (/^([A-Z][^a-z]*[A-Z])\n/)
343 print "\n</OL>\n" x ($level - 1);
344 print "\n" if $level != 1;
345 print_paragraph $paragraph, "<H3>", "</H3>";
346 $in_heading = $in_h2 = 0;
347 print "\n<OL>\n" x ($level - 1);
353 # If this is a paragraph delimiter of some kind (usually a blank line),
354 # print the preceding paragraph, handling things we recognize as tables.
355 if (defined $paragraph &&
356 (/^\n/ || ($in_heading && $paragraph =~ /<A NAME[^>]*>.{0,40}$/)))
358 if (($paragraph =~ /ISBN +[0-9]-/ &&
359 $paragraph =~ /\((?:hc|pb|trade pb)\)/)
360 || $paragraph =~ /c\/o Del Rey Books/
361 || $paragraph =~ /esper\.net$/m)
363 $paragraph =~ s/\n/<BR>\n/g;
367 # We have to split up the paragraph and work line-by-line here;
368 # multi-line regexps are really hard to get right.
369 my @parlines = split /\n/, $paragraph;
371 foreach my $parline (@parlines)
373 unless (defined $prevline)
375 $prevline = \$parline;
378 if ($parline =~ /^(\s*)((?:\w\)|
380 \w+(?:\s+\w+)?:\s*http).*|
381 \<http(?:[^&]|\&(?!gt;))*\>)$/x)
383 $parline = "$1$2<BR>";
384 $$prevline .= "<BR>" if $$prevline !~ /<BR>$/;
386 $prevline = \$parline;
388 $paragraph = (join "\n", @parlines) . "\n";
389 $paragraph =~ s/:$/:<BR>/gm;
392 $paragraph =~ s/(\S) +/$1 /g unless /<BR>/;
395 print_paragraph $paragraph, "", "</A></H2>\n";
402 print_paragraph $paragraph, "", "</STRONG></A>";
406 print_paragraph $paragraph, "<P>\n", "\n</P>";
410 $paragraph = "$_" unless /^\n?$/;
412 elsif (defined $paragraph)
418 $paragraph = "$_" unless /^\n?$/;
424 chomp $paragraph if defined $paragraph;
427 print_paragraph $paragraph, "", "</STRONG></A>";
431 print_paragraph $paragraph, "<P>\n", "\n</P>";
434 print "</OL>\n\n" x $level;
436 html_footer $index_file,
437 ($section > 1) ? ("part" . ($section - 1) . ".html") : undef,
439 close SECTION if $section_open;
443 warn "No sections encountered";