#! /usr/bin/perl -w use strict; require 5.000; use lib qw(.); use Style qw(html_header html_footer); # The AFE FAQ HTMLizer # Perl version written by Kamion; based on a sed script by Aquarius sub usage () { print STDERR <<"EOF"; Usage: $0 index-filename [up-URL] < faq-document URLs may be absolute or relative. EOF exit 1; } sub html_escape (\$) { my $line = shift; return unless defined $$line; $$line =~ s/\&/&/g; $$line =~ s/</g; $$line =~ s/>/>/g; $$line =~ s/"/"/g; $$line =~ s/æ/æ/g; return $$line; } #my $sts_link = "http://riva.ucam.org/~kamion/archive-bin/" . # "article.pl?msgid=5n0qq5\$d9p\@mercury.dur.ac.uk"; my $sts_link = 'http://groups.google.com/groups?' . 'q=Easter+Egg&ic=1&selm=5n0qq5%24d9p%40mercury.dur.ac.uk'; # Paragraph to format and undefine, followed optionally by open and close tags. sub print_paragraph (\$;$$) { my $para = shift; return unless defined $$para && $$para !~ /^\s*$/; my $open_tags = shift; my $close_tags = shift; # Make URLs links $$para =~ s#(http:(?:[^\ \&<]|\&(?!gt;))*) #$1#gx; # Make all text within pairs of underscores outside URLs emphasized $$para =~ s[_((?:[^_.-])+\.?)_ (?![^"]*">|[^<]*)][$1]gx; # Similarly, strengthen bold text within asterisks $$para =~ s[\*((?:[^*.])+\.?)\* (?![^"]*">|[^<]*)][$1]gx; # Easter egg $$para =~ s|(Sephrenia the Styric)|$1|g; $$para = "$open_tags$$para" if defined $open_tags; $$para .= $close_tags if defined $close_tags; print "$$para\n"; undef $$para; } my $index_file = shift or usage; my $up_url = shift; open CONTROL, ">control.html" or die "Can't write to control.html: $!"; select CONTROL; html_header "Revision information", $index_file; print "\n"; my $changes_done = 0; print "
\n"; while (<>) { $changes_done = 1 if (/^Archive-name:/); next unless $changes_done; chomp; html_escape $_; last if (/^\s*$/); unless (/^([^:]+:)\s*(.+)$/) { warn "Non-header line found in control section: $_"; next; } print "$_\n"; } print "\n\n"; html_footer $index_file; open INDEX, ">$index_file" or die "Can't write to $index_file: $!"; select INDEX; html_header "Contents", $up_url; print "\n"; print "
\n" if $level == 1; # Reformat the current line my $value = $components[$#components]; if (defined $lastvalues[$level] && $lastvalues[$level] + 1 != $value) { warn "Missing item in contents before $heading"; print "
\n"; $#lastvalues = 0; } $listentry = 0; next; } elsif (/^$/) { print_paragraph $paragraph, "", $listentry ? "" : ""; $listentry = 0; next; } print_paragraph $paragraph; $paragraph = $_; } # Do all the necessary end tags #print "
\n" if $level > 0; print "\n\n", "\n
"; } } $in_heading = $in_h2 = 0; $oldsection = $section; $section = $1; print "\n" if $section_open; print "\n\n" x $level if $section_open; html_footer $index_file, ($oldsection > 1) ? ("part" . ($oldsection - 1) . ".html") : undef, "part" . ($oldsection + 1) . ".html" if $section_open; open SECTION, ">part$section.html" or die "Couldn't write to part$section.html: $!"; select SECTION; html_header "section $section", $index_file, "part" . ($section - 1) . ".html", ($section < $num_sections) ? ("part" . ($section + 1) . ".html") : undef; $section_open = 1; $level = 0; $#lastvalues = -1; } # Convert headings to anchors in list items if (/$heading_pat/) { my $heading = $1; my @components = split /\./, $heading; chomp $paragraph if defined $paragraph; if ($in_heading) { print_paragraph $paragraph, "", "\n"; } else { print_paragraph $paragraph, "\n", "\n
\n"; } $in_h2 = 0; # Change level of ordered list if necessary if (@components > $level) { print "\n", "\n
"; } } $in_heading = 0; $paragraph = "$_" unless /^\n?$/; } elsif (defined $paragraph) { $paragraph .= "$_"; } else { $paragraph = "$_" unless /^\n?$/; } } if ($section_open) { chomp $paragraph if defined $paragraph; if ($in_heading) { print_paragraph $paragraph, "", ""; } else { print_paragraph $paragraph, "\n", "\n
"; } print "\n"; print "\n\n" x $level; html_footer $index_file, ($section > 1) ? ("part" . ($section - 1) . ".html") : undef, undef; close SECTION if $section_open; } else { warn "No sections encountered"; }