#!/usr/bin/perl
#
# ========================================================================
#  @perl-file{
#     author              = "Alan Jeffrey",
#     version             = "0.19",
#     date                = "15 January 1996",
#     time                = "12:26:55 GMT",
#     filename            = "texfaq2html",
#     address             = "School of Cognitive and Computing Sciences
#                            University of Sussex
#                            Brighton BN1 9QH
#                            UK",
#     FAX                 = "+44 1273 671320"
#     email               = "alanje@cogs.sussex.ac.uk",
#     codetable           = "ISO/ASCII",
#     keywords            = "LaTeX FAQ HTML",
#     supported           = "yes",
#     abstract            = "This perl script converts the UKTUG TeX FAQ
#                            LaTeX source document into HTML, on the fly."
#     package             = "stands alone",
#     dependencies        = "faqbody.tex, newfaq.aux, dirctan.tex,
#                            filectan,tex", 
#  }
# ========================================================================

# A script to provide a searchable WWW interface to the the UKTUG TeX
# FAQ file.
#
# The script takes parameters in the form of the QUERY_STRING environment 
# variable. 

# Copyright 1994 Alan Jeffrey
# Maintenance, since 1997, Robin Fairbairns

require "sanitize.pl";

# The site-specific stuff:

$default_web = "www.tex.ac.uk";
$href_script="http://$default_web/cgi-bin/texfaq2html";
$home = "$ENV{FAQ_HOME}" || "/home/rf/tex/faq";
$texfaq = "faqbody.tex";
$auxfaq = "newfaq.aux";
$ctandir = "dirctan.tex";
$ctanfiles = "filectan.tex";
$archive_list = "archive.list";

# derived:

$faqhtml = $home . "/html";

# defaults; these have to be allowed by the $archive_list file

$default_archive = "cam.ctan.org";
$fmt_1 = ".tar.gz";
$fmt_2 = ".zip";
$fmt_2_name = "zip";
$fmt_3 = "/";
$fmt_3_name = "browse";

# protocols for transferring files, browsing directories
$proto_f = "ftp";
$host_f = $default_archive;
$proto_d = "http";
$host_d = $default_web;

$proto_1 = $proto_2 = $proto_f;
$host_1 = $host_2 = $host_f;
$proto_3 = $proto_d;
$host_3 = $host_d;

# table of symbols we believe in
%SymbolChar = (
	        92 => "\\",
	       123 => "\{",
	       125 => "\}"
);

# This script produces HTML, but we're sending the stuff to file
# so we needn't tell anyone
#
# print ("Content-type: text/html\n\n");

# By default, we convert LaTeX to HTML.

$converting = 1;
$ignoring = 0;
$sectioning = 0;

# two things used in conversion of \item[ ]

$itemset = "";
$enditemset = "";

# Get the list of CTAN directories:

open (CTANDIR, "$home/$ctandir")
	|| &oh_dear ("Couldn't open $ctandir");

while (<CTANDIR>) {
    if ( /\\CTANdirectory\{([^\}]*)\}\{([^\}]*)\}/ ) {
	$ctanref{$1} = "$2";
	$ctanref_plus{$1} = 1;
    } elsif ( /\\CTANdirectory\*\{([^\}]*)\}\{([^\}]*)\}/ ) {
	$ctanref{$1} = "$2/";
	$ctanref_plus{$1} = 0;
    }
}

# Get the list of CTAN files

open (CTANFILES, "$home/$ctanfiles") 
	|| &oh_dear ("Couldn't open $ctanfiles");

while (<CTANFILES>) {

    if ( /\\CTANfile\{([^\}]*)\}\{([^\}]*)\}/ ) {
	$ctanref{$1} = "$2";
	$ctanref_plus{$1} = -1;
    }
}

# Get the list of allowable archives

open (ARCHIVE_LIST, "$home/$archive_list")
    || &oh_dear ("Couldn't open $archive_list");

while (<ARCHIVE_LIST>) {
    chop;
    ($archive, $root_dir) = split(/\s+/, $_, 2);
    $archive_root{$archive} = $root_dir;
}
unless ( $this_root = $archive_root{$default_archive} ) {
    &oh_dear("Archive $default_archive isn't in my list") }
$arch_root = "$default_archive/$this_root/";

# Get the Qrefs:

open (AUXFAQ, "$home/$auxfaq") 
	|| &oh_dear ("Couldn't open $auxfaq");


while (<AUXFAQ>) {
    if (/\\newlabel\{([^\}]*)\}\{\{([^\}]*)\}/) {
	next if ( !( ($this_Qlabel = $1) =~ /^Q-/ ) );
	$this_label = substr $this_Qlabel, 1;
	$qref{$this_Qlabel} = "FAQ$this_label.html";
    }
}

# Open the FAQ file:

open (TEXFAQ, "$home/$texfaq")
	|| &oh_dear ("Couldn't open $texfaq");

# Run through to the introduction, grabbing useful info.

while (<TEXFAQ>) {
    last if /\\section\{Introduction\}/;
    $fileversion=$1 if /\\def\\faqfileversion\{([^\}]*)\}/;
    $filedate=$1 if /\\def\\faqfiledate\{([^\}]*)\}/;
}

# open main html file

open (FAQ, ">$faqhtml/index.html") ||
    oh_dear ("couldn't open output file $faqhtml/index.html");

printf FAQ ("
<html><head>
<title>TeX Frequently Asked Questions</title>
</head><body>
<h1 align=\"center\">Welcome to the UK List of<br>
                TeX Frequently Asked Questions</h1>
");

# Blast out the introduction until we get to the first section.

print FAQ "<h2>Introduction</h2>\n";
while (<TEXFAQ>) {
    last if /\\section\{([^\}]*)\}/;
    &sanitize_line;
    print FAQ;
}

# start the index of questions ($_ still left over from last read from file)
/\\section\{([^\}]*)\}/;
$_ = $1;
&sanitize_line;
print FAQ "<h3>$_</h3><ul>\n";

# first pass, we have a file open by default
$faq_file_open = 1;

while ( $faq_file_open ) {

    # no question yet open
    $qfile_open = 0;

    while (<TEXFAQ>) {
	if ($converting) {
	    if ( /^\s*\\section\{(.*)\}\s*$/ ) {
		$_=$1;
		&sanitize_line;
		print FAQ "</ul><h3>$_</h3><ul>\n";
		$_="";
	    } elsif ( /^\s*\\subsection\{(.*)\}\s*$/ ) {
		$_=$1;
		&sanitize_line;
		print FAQ "</ul><h4>$_</h4><ul>\n";
		$_="";
	    }
	}

	&sanitize_line;
	if (/^\s*\\Question\[([^]]*)\]\{(.*)\}\s*$/) {
	    $qnum++;
	    $next_qlabel = substr $1, 2;
	    $entry = "<li><a href=\"FAQ-$next_qlabel.html\">$2</a>\n";
	    print FAQ $entry;
	    if ($qfile_open) {
		print QFILE "<p>This question on the Web: ",
		  "<a href=\"http://www.tex.ac.uk/cgi-bin/texfaq2html?label=",
		  $qlabel,"\">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=",
		  $qlabel,"</a>\n";
		print QFILE "</body>\n";
		close QFILE;
	    }
	    $qlabel = $next_qlabel;
	    open (QFILE, ">$faqhtml/FAQ-$qlabel.html") ||
		oh_dear ("couldn't open file for question $qnum");
	    $qfile_open=1;
	    printf QFILE ("<head>
<title>UK TeX FAQ -- question label $qlabel</title>
</head><body>
<h3>$2</h3>
");
	} else {
	    if ($qfile_open) { print QFILE; }
	}
    }

    if ($qfile_open) {
	print QFILE "</body>\n";
	close QFILE;
    }

    # now: are there any other file names in ARGV?
    if ( $#ARGV >= 0 ) {
	close TEXFAQ;
	$next_file = $ARGV[0];
	open (TEXFAQ, "$home/$next_file") ||
	    oh_dear ("Couldn't open $home/$next_file");
	shift;
    } else {
	$faq_file_open = 0;
    }
}

# finish off the index.html file listing all questions
print FAQ "</ul>\n";

# report number of questions processed
    print "$qnum questions processed\n";

# An error report:

sub oh_dear {
    print $_[0], "\n";
    die $_;
}


# Print a tail.
    
printf FAQ ("
<hr><address>
   Maintenance of the
   <a href=\"http://www.tex.ac.uk/faq\">TeX FAQ</a>
   is coordinated by Robin Fairbairns.<p>
");

$qref_noans=$qref{"Q-noans"};
$qref_newans=$qref{"Q-newans"};

printf FAQ ("
   Comments, suggestions, or error reports? -- see
   \"<a href=\"%s\">Improving the FAQ</a>\" or
   \"<a href=\"%s\">Extending the FAQ</a>\". 
<p>
   This is FAQ version $fileversion, last modified on $filedate.
</address>
</body></html>
", $qref_noans, $qref_newans);

# That's it!
