#!/usr/bin/perl -w
#
# seminar-abstract -- copyright (c) David Richerby, 2003.
# Produce web page templates for seminar abstracts.
#
# --------------------------------------------------------------------------- #
#
# The templates produced by this script are for the Computer Lab's web
# page templating system, which is documented at
#
#    http://www.cl.cam.ac.uk/UoCCL/template/README
#
# This script takes as input a template called <something>.data and
# produces output in a file <something>.tmpl.  Any number of input
# files may be specified on the command line; a bug/feature is that
# specifying the file `-' will case input to be read from stdin and
# output to be sent to stdout.  Input files should be of the following
# form:
#
# ------ example template begins ------
# SPEAKER&David Richerby
# URL&http://www.cl.cam.ac.uk/~dmr25/
# INSTITUTION&Computer Laboratory
# TITLE&How to Use This Abstract Producing System
# DATE&Sunday, 2nd November, 2003
# TIME&14:00
# VENUE&FW11
# WEDNESDAY&
#
# In this talk, I will explain how to use this script to generate web
# pages for seminar abstracts.
#
# This is the second paragraph.
# ------- example template ends -------
#
# The header block must contain no blank lines, though the fields may
# be in any order.  After a blank line comes the text of the abstract,
# with paragraphs separated by completely blank lines.  The abstract
# may contain HTML tags such as links and images.
#
# The header fields are as follows, each consisting of a single word
# in upper case, followed by an ampersand and the field text
#
# SPEAKER      The speaker's name.  HTML character entities such as
#              &auml; are acceptable here.
#
# URL          URL of the speaker's web page.
#
# INSTITUTION  The speaker's institution.
#
# TITLE        The title of the talk.  If necessary, explicit line-
#              breaks can be introduced with <BR>
#
# DATE         The date of the talk.  Should be of the form indicated
#              in the example, though the day name is optional and, if
#              present, will be stripped from the schedule page
#              produced by the seminar-schedule script.
#
# TIME         The time of the talk, for preference in the 24-hour
#              clock.
#
# VENUE        The room in which the talk will be held.  Magic in the
#              script converts `FWnn', where nn is a two-digit number,
#              to `William Gates Building, room FWnn' with a link to
#              the department map; other venues will appear unaltered
#              in the output template -- they're probably in the old
#              buildings on the New Museums Site.  This magic will
#              need updating if other rooms are ever used within the
#              WGB.
#
# WEDNESDAY    If this line appears, the seminar was a Computer Lab
#              Wednesday seminar, not a Logic and Semantics Seminar,
#              and will be marked as such in the abstract and the
#              schedule page.
#
# --------------------------------------------------------------------------- #
#
# $Log: seminar-abstract,v $
# Revision 1.3  2003-11-18 17:28:55+00  dmr25
# Extended FW11 magic to any room FWnn.
#
# Revision 1.2  2003-11-03 14:02:30+00  dmr25
# Added link to Computer Lab template system documentation.
# Fixed HTML closing tag typo.
# Fixed HTML to put abstract in the table.
#
# Revision 1.1  2003-11-03 00:31:07+00  dmr25
# Initial revision
#
# --------------------------------------------------------------------------- #

use strict;         # Never leave $HOME without it!

#
# Seminar data
#
my ($speaker);      # Speaker's name
my ($url);          # URL of speaker's web page
my ($inst);         # Speaker's institution
my ($title);        # Title of seminar
my ($date, $time);  # Date and time of seminar
my ($venue);        # Venue of seminar

#
# Variables
#
my ($inabstract);   # Nonzero if currently looking at the abstract text
                    # rather than the seminar data
my ($ifname);       # Input file name
my ($ofname);       # Output file name

# --------------------------------------------------------------------------- #

foreach $ifname (@ARGV)
{
    $speaker = $url = $inst = $title = $date = $time = $venue = "";
    $inabstract = 0;

    #
    # Check that the input file name is either `-' or ends in `.data'.
    # Otherwise, output will be to the same file, which will result in
    # its being overwritten with garbage.
    #
    if ($ifname ne "-" && !($ifname=~/\.data/))
    {
	warn "Invalid input filename: $ifname (must be `-' or end in `.data')";
	next;
    }


    if (!open (IF, "<".$ifname))
    {
	warn "Could not open $ifname: $!";
	next;
    }

    #
    # Produce the output file name by replacing .data from the input
    # file name with .tmpl .  If the input is `-', the output will be
    # too.
    #
    $ofname = $ifname; $ofname=~s/\.data$/\.tmpl/;
    if (!open (OF, ">".$ofname))
    {
	warn "Could not open $ofname: $!";
	close (IF);
	next;
    }

    #
    # Parse input template
    #
    while (<IF>)
    {
	chomp;

	#
	# The abstract text is just copied to the output file, except
	# that blank lines are replaced by '</P><P>'.  Skip to the
	# next line of input after copying.
	#
	if ($inabstract)
	{
	    print OF $_ ? "    $_\n" : "  </P><P>\n";
	    next;
	}

	#
	# Parse header lines.
	#
	if    (/^SPEAKER\&(.*)/)     { $speaker = $1; }
	elsif (/^URL\&(.*)/)         { $url     = $1; }
	elsif (/^INSTITUTION\&(.*)/) { $inst    = $1; }
	elsif (/^TITLE\&(.*)/)       { $title   = $1; }
	elsif (/^DATE\&(.*)/)        { $date    = $1; }
	elsif (/^TIME\&(.*)/)        { $time    = $1; }
	elsif (/^VENUE\&(.*)/)       { $venue   = $1; }

	#
	# Since we're not in the abstract text, a blank line indicates
	# the end of the header lines.  Print the output template
	# header and the HTML table containing the speaker's name,
	# etc.  When this has finished, we're ready to slot in the
	# abstract text.
	#
	elsif ($_ eq "")
	{
	    $inabstract = 1;
	    print OF <<__END_OF_HEADER;
$date: $speaker
SECTION\&Logic and Semantics Seminar
COMMENTS\&<A href=\"mailto:tjr22\@cl.cam.ac.uk\">tjr22\@cl.cam.ac.uk</A>
BREADCRUMB\&Research\&http://www.cl.cam.ac.uk/UoCCL/research/
BREADCRUMB\&TSG\&http://www.cl.cam.ac.uk/Research/TSG/
BREADCRUMB\&Logic and Semantics Seminar\&http://www.cl.cam.ac.uk/Research/LS/

<TABLE border=0 cellpadding=10 cellspacing=0>
<TR>
  <TH align=\"left\" valign=\"top\">Speaker:</TH>
__END_OF_HEADER

            if ($url)
	    {
		print OF "  <TD><A href=\"$url\">$speaker</A>";
	    }
	    else
	    {
		print OF "  <TD>$speaker";
	    }
	    print OF ", $inst" if ($inst);
	    print OF "</TD>\n</TR><TR>\n";
	    print OF "  <TH align=\"left\" valign=\"top\">Title:</TH>\n";
	    print OF "  <TD>$title</TD>\n</TR><TR>\n";
	    print OF "  <TH align=\"left\" valign=\"top\">Time:</TH>\n";
	    print OF "  <TD>$date";
	    print OF ", $time" if ($time);
	    print OF "</TD>\n</TR><TR>\n";

	    #
	    # Magic for supplying link to the department maps if the
	    # venue is FWnn.
	    #
	    if ($venue)
	    {
		print OF "  <TH align=\"left\" valign=\"top\">Venue:</TH>\n".
		         "  <TD>";
		if ($venue =~ /FW\d\d/)
		{
		    print OF "William Gates Building, room <A href=\"http://www.cl.cam.ac.uk/site-maps/gates2.html\">$venue</A>";
		}
		else
		{
		    print OF $venue;
		}
		print OF "</TD>\n</TR><TR>\n";
	    }
	    print OF "  <TH align=\"left\" valign=\"top\">Abstract:</TH>\n";
	    print OF "  <TD>\n";
	    print OF "  <P>\n";
	}
    }

    #
    # Finish off the HTML and close the files.
    #
    print OF "  </P>\n  </TD>\n</TR>\n</TABLE>\n";

    close (IF);
    close (OF);
}

# --------------------------------------------------------------------------- #
