#!/usr/bin/perl
#
# htol2ss - script to convert HTML lecture outlines (coded as nested lists)
#    into HTML suitable for presentation as a slide-show
#    where each top-level point becomes a separate slide, in the file
#
$usage = "Usage:  htol2ss [-b val] [-c val] [-f] [-h] [-i dir] [-p pref] [-q]
		[-r fil|-R] [-s siz] [-t val] file1.html [file2.html ...]
	converts files of HTML nested list lecture outlines to HTML slide-shows
	-b file		name of background image file for BODY tag (bg.jpg)
	-b #rrggbb	OR the background color specified in BODY tag
	-c #rrggbb	text color used for H2 slide headings (default)
	-f		force update even if original is older
	-h		prints this usage message
	-i dir		name of directory the images are in (img/)
	-p prefix	prefix added to form output slide-show file names (ss-)
	-q		quiet mode, no status messages
	-r file		name of ruler image used below slide headings (hr.gif)
	-R		DONT use ruler image
	-s fontsize	base font size (1..7) to use for slide-show text (6)
	-t #rrggbb	text color specified in BODY tag (default)
	file1.html [file2.html ... ] list of files to convert";

# Version history:
#    v1.0 first release, Lawrie.Brown@adfa.edu.au, 25 Nov 98
#    v1.1 assorted cmdline args & customise, Lawrie.Brown@adfa.edu.au, 1 Dec 98
#    v1.2 more fixes, nav at page end also, Lawrie.Brown@adfa.edu.au, 16 Feb 99
$ver = 1.2;

# Copyright Notice:
$copy = "Copyright (C) 1998 by Lawrie.Brown\@adfa.edu.au
    Permission is granted to freely copy and use this script provided due
    acknowledgement is given to the author, and this notice remains intact.";

###### ----- assorted configuration defines ----- #####
$imgdir = "img";			# directory the following images are in
$back   = "left.gif";			# HREF for back arrow (prev slide)
$fwd    = "right.gif";			# HREF for fwd  arrow (next slide)
$bgimg  = "bg.jpg";			# HREF for background image
$hrimg  = "hr.gif";			# HREF for HR (line) under slide headers
$bfont  = 5;				# set (Netscape) base font size
$pref   = "ss-";			# prefix used on slide-show output file
################### End config section ################

##### Process cmdline args
require 'getopts.pl';			# use getopt module to process args
$@ && die "$0: require getopts.pl failed!\n";
&Getopts ('b:c:fhi:p:qr:Rs:t:');	# extract cmdline args into $opt_* vars

# check input flags for special handling
$bodyargs = "";						# body tag params 
$h2args   = "";						# slide H2 tag params 
if ($opt_c) { $h2args .= " color=\"$opt_c\""; };	# specify H2 color
if ($opt_h) { die "$usage\n\n$0 v$ver is $copy\n"; }	# help wanted
if ($opt_i) { $imgdir = $opt_i; };			# specify img dir
if ($opt_p) { $pref  = $opt_p; };			# specify file prefix
if ($opt_r) { $hrimg = $opt_r; };			# specify ruler image
if ($opt_s) { $bfont = $opt_s; };			# specify base fontsize
if ($opt_t) { $bodyargs .= " text=\"$opt_t\""; };	# specify text color
if ($opt_b) {				# specify background (AFTER check opt_i)
    if ($opt_b =~ /^#/) { $bodyargs .= " bgcolor=\"$opt_b\""; }
    else		{ $bodyargs .= " background=\"$imgdir/$opt_b\""; }
} else {
    if ($bgimg) { $bodyargs .= " background=\"$imgdir/$bgimg\""; }
}

# now update some values given cmdline args
$hfont = $bfont+1;				# set hdr font size basesize+1
$h2args .= " size=$hfont";			# and include in h2args
$back = "$imgdir/$back";			# prefix image names by imgdir
$fwd  = "$imgdir/$fwd" ;
$hrimg = "$imgdir/$hrimg";

##### start processing each input file in turn
foreach $i (@ARGV) {			# for each file on command-line
	($base = $i) =~ s:.html::;	# strip trailing .html off

					# skip if no update needed
	next if (!$opt_f && (-f "$base.html") && (-f "$pref$base.html") &&
			((-M "$base.html") > (-M "$pref$base.html")));

	print "$base.html -> " unless $opt_q;	# display name while processing

	# open input and output files
	open(IN, "<$base.html") || die "Unable to read file '$base.html'";
	open(OUT, ">$pref$base.html") ||
		die "Unable to create file '$pref$base.html'";

	# slurp entire file in and close it
	undef ($/);
	$inp = <IN>;
        close(IN);

	# now split it up at the start of each HTML tag so can scan them
	@htags = split(/</,$inp);

	$slide = 0;			# start with slide no 0
	$slidehdr = 0;			# and are not in slide hdr yet
	$listlev = 0;			# and with list nest level of 0

	##### scan each HTML tag in turn to see what changes are needed
        foreach $t (@htags) {		# scan each HTML tag in turn
	    # assorted processing for various tags seen

	    # BODY - so can write slide-show formatting options
	    if ($t =~ m|^body[^>]*>|i) {
		# display body tag with appropriate handling
		print OUT "<body$bodyargs>\n";
		# display initial navigation arrow (to slide 1)
		print OUT "<a href=\"#s1\"><img src=$fwd alt=\"->\" align=right></a>\n";
		# and lastly replace old body tag with basefont spec
	        $t =~ s|^body>|basefont size=$bfont>|;
	    }

	    # OL or UL - new list so increase list nest level
	    if ($t =~ m|^[ou]l[ >]|i) {
	        $listlev++;
		# and remove 1st level list (since using headers in slide show)
		if ($listlev == 1) { $t =~ s:^..>:a name=s0>:; }
	    }

	    # /OL or /UL - end list so down level
	    if ($t =~ m|^/[ou]l[ >]|i) {
	        $listlev--;
		# and remove end of 1st level list, leaving gap after last slide
                if ($listlev == 0) {
		    $slide++;
		    $t =~ s:^/..>:pre>\n\n\n\n\n\n</pre><a name=s$slide>:;
		}
	    }

	    # LI - see list-item, lots of work if list nest level is 1
	    if (($listlev == 1) && ($t =~ m|^li[ >]|i)) {	# level 1 list
	        # okay new slide time - lots of stuff to write
		$slide++;				# increment slide no
		$slidehdr = 1;				# processing slide hdr
		print OUT "\n<a href=\"#s" . ($slide) .
			"\"><img src=$fwd alt=\"->\" align=right></a>\n"
			    if ($slide > 1);		# end prev slide nav
		print OUT "\n<pre>\n\n\n\n\n\n</pre><hr>\n";	# start slide
		print OUT "<center><h2>\n";		# start heading
		print OUT "<a name=s$slide></a>\n";	# anchor for slide
							# navigation arrows next
		print OUT "<a href=\"#s" . ($slide-1) .
			"\"><img src=$back alt=\"<-\" align=left></a>\n";
		print OUT "<a href=\"#s" . ($slide+1) .
			"\"><img src=$fwd alt=\"->\" align=right></a>\n";
		# change to H2 hdr
		$t =~ s|li>|font$h2args>$slide. |;	# and finally do font
		print OUT "<$t";			# display NOW
		next;					# and continue
	    }

	    # some other tag, but check if must close slide hdr
	    if ( $slidehdr ) {				# try & spot end of item
		# assume tag flags end of list item unless its a formatting tag
	        unless ($t =~ m:^/?(a[ >]|b>|code>|em>|i>|strong>|sub|sup|tt>|u>):i) {
		    print OUT "</font></h2>\n"; 	# end hdr
		    # display rule under hdr
		    print OUT "<img src=\"$hrimg\">\n" unless ($opt_R);
		    print OUT "</center>\n"; 		# end cetering of hdr
		    $slidehdr = 0;			# and note all done
		}
	    }

	    # display the modified line (may have put extra stuff first)
	    print OUT "<$t" unless ($t eq "");	# and write jigged line to OUT
        }

        # close out put file
        close(OUT);

	print "$pref$base.html\n" unless $opt_q;	# finish status msg off
}

