#!/usr/bin/perl

# $Id: make-book,v 1.2 2004/09/23 20:52:11 tomj Exp $

# Create a linear HTML book from a source file that contains an
# ordered list of filenames/page numbers and page titles.

# The two portions must be separated with a TAB.

# Files must have the name of the page number, and .tif format;
# eg. page 1 is 1.tif, 4-6 is 4-6.tif. Following the
# page number/filename by a tab is an optional page
# header or title. This title is applied to the browser
# window. If one is not specified then the most-recent page
# header is used (eg. you only need to specify it when
# it changes; you can title all the pages in a chapter by:
#
# 11	Chapter Foo
# 12
# 13
# ...

# "meta page numbers" fill in certain useful things:
#
# !T	/tab/	Title of document (becomes window title)
#
# There is no real list of metas, you have to
# look at the code below.


# Filename issues.
$htmlext= "html";		# HTML file type
$imageext= "png";		# !X image file type
$index= "index.$htmlext";	# index filename
$htmlpath= "Book";		# !P path to the HTML pages,
$imagepath= "../png";		# !F path from HTML to images,

# Defaults for items that are expected to be in the document.
$title= "No !T title given";	# !T
$desc= "No !D description given";# !D
$css= "No !C stylesheet given";	# !C
$home= "No !H home given";	# !H

$header= "";			# optional page header text
$last_header= "XX";		# so we can detect changes

# Each line of the input source is processed, those that are not
# metas each generate an HTML page.  We need to read one-line-ahead
# so that we can make the next/previous hrefs.

my ($prev, $this, $next)= ("", "");		# the queue of pages

	open (I, ">$index") or die ("Can't create $index?!");

	foreach (<>) {
		chomp;
		s/^\s*$//;
		next if /^$/;
		next if /^#/;

# Extract the metas here.

		if (/^!/) {
			($page, $txt)= split (/\t/);
			$htmlpath= $txt if $page eq "!P"; # path to HTML,
			$imagepath= $txt if $page eq "!F"; # path to images
			$imageext= $txt if $page eq "!X"; # image file extention
			$title= $txt if $page eq "!T";	# document title,
			$css= $txt if $page eq "!C";	# CSS stylesheet file
			$desc= $txt if $page eq "!D";	# document description,
			$home= $txt if $page eq "!H";	# document home,
			next;
		}

# We keep a 3-deep queue of page entries, so that we can
# make PREV, NEXT links.

		$prev= $this;				# shift the queue,
		$this= $next;
		$next= $_;				# add to the end,

# This hack gets us ahead one line so that we have a "next". 
# It complicates outputting the last line when we reach
# the end.
		next if $this eq "";			# await a page,

# This kludge causes the index header junk to be output only
# once.
		&start_index()if $prev eq "";

# Not every page reference has a title/header, eg. sequential
# pages in a chapter. This remembers the title until it changes.
# last_header is used to determine when to make a new table of
# contents entry.

		($page, $txt)= split (/\t/, $this);	# remember
		$header= $txt if $txt ne "";		# new header

# Now generate output.

		&print_toc ($page, $header) if $header ne $last_header;
		&print_page ($page, $header, $prev, $next);

		$last_header= $header;
	}

# Now output the remaining page.

	($page, $txt)= split (/\t/, $next);
	if ($txt ne "") {
		$last_header= $header;
		$header= $txt;
	}
	&print_toc ($page, $header) if $header ne $last_header;
	&print_page ($page, $header, $prev, "");
	&end_index();
	close I;




#############################################################
#
# Given a page number and header text, print one line
# into the Table of Contents.

sub print_toc {
my $page= shift;
my $hdr= shift;

	print I << "IDX";
<li class="l"><a href="$htmlpath/$page.$htmlext">Page $page, $hdr</a></li>
IDX
}


#############################################################
#
# Given a page number and header, and previous and next page
# numbers, output the page with the page-image.

sub print_page {
my $page= shift;
my $hdr= shift;
my $prev= shift;
my $next= shift;

	($prev, $txt)= split (/\t/, $prev);		# we only want
	($next, $txt)= split (/\t/, $next);		# page numbers

	open (F, ">$htmlpath/$page.$htmlext")
	    or die ("Can't create file $htmlpath/$page.$htmlext\n");

	print F << "P1";
<html>
<head>
<meta name="robots" content="index,nofollow">
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta name="copyright" content="Copyright Tom Jennings 2004">
<meta name="Author" content="A Perl script run by Tom Jennings">
<META NAME="description" CONTENT="$desc">
<link href="$stylesheet" rel="stylesheet" type="text/css">
<title>$title</title>
</head>

<body>
<span class="shead">$title$hdr</span>
<br>

<p class="p">
<a href="$home">[Back to document index]</a>&nbsp;&nbsp;&nbsp;
P1

	print F << "PREV" if $prev ne "";
<a href="$prev.$htmlext">[Previous page $prev]</a>&nbsp;&nbsp;&nbsp;
PREV

	print F << "NEXT" if $next ne "";
<a href="$next.$htmlext">[Next page $next]</a>
NEXT

	print F << "P2";
</p>

<img src="$imagepath/$page.$imageext">
<br>


</body>
</html>

P2

	close F;
}

#############################################################
#
# Output the header section of the index page.
#
sub start_index {

	print I << "H1";

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="description" CONTENT="$desc">
<meta name="copyright" content="Copyright Tom Jennings 2004">
<meta name="Author" content="A Perl script run by Tom Jennings">
<link href="$stylesheet" rel="stylesheet" type="text/css">
<title>$title</title>
</head>

<META NAME="keywords" CONTENT="lgp 21 30 lgp21 lgp30 lgp-21
lgp-30 royal mcbee general precision librascope serial drum
disk memory programs subroutines manual tube valve electronic
computer mel programmer flexo flexowriter friden ">

<LINK href="../LGP-21.css" rel="stylesheet" type="text/css">
</HEAD>

<body>
<font face="Arial, sans-serif" size="3">
<span class="head">$desc</span>

<p class="p">This is the LGP-21 Hardware Maintenance and Training
Manual supplied with the machine, page-scanned at 150 dpi,
11.5&quot; x 8.5&quot; (U.S. A size). It contains operation,
theory of operation (including a complete algebraic definition
of all the machine logic), memory alignment procedures
and a complete set of logic diagrams, photographs and
schematics.</p>

<p class="p">The original manual, borrowed from the Computer
Museum of America, was copied, then scanned on an ordinary
flatbed scanner.  Each page image was saved to a disk file,
named with the original document's page number (eg. page 1-3
became disk file 1-3.tiff). A simple <a href="manual.txt">text
file</a> contains these page-numbers, ordered, and notable pages
have a title or description separated from the page number by
a tab character.</p>

<p class="p">These pages were then created with a 
<a href="make-book">Perl script</a>
that takes a simple text-file list of page number/filenames and
descriptions, and creates all of the HTML. This allows updating
the entire dataset easily and portably.

<ul class="l">
H1
}


#############################################################
#
# Output the trailer section of the index page.
#
sub end_index {

	print I << "H2";
</ul>

</body>
</html>
H2
}

