#!/usr/bin/perl -w
#
# PLies Copyright 2001 Dobrica Pavlinusic <dpavlin@rot13.org>
#
# this tool is based on SlideMaker and XLSies tool
# split a all.htm into slide*.htm
#
# For more information please see presentation about this tool on
# http://www.rot13.org/~dpavlin/presentations/XLSies_to_PLies/
#
# Copyright 1999 World Wide Web Consortium,
# (Massachusetts Institute of Technology, Institut
# National de Recherche en Informatique et en
# Automatique, Keio University). All Rights Reserved.
# This program is distributed under the W3C's
# Intellectual Property License. This program is
# distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See W3C License
# http://www.w3.org/Consortium/Legal/ for more details.
#
#
##############################################################################
#
# slidemaker credits:
#
# Stephan Montigaud - stephan@w3.org
# created 970601
# modified by Pierre Fillault
# check the documentation at http://www.w3.org/Talks/YYMMsub/
#
# modified 19990505 Bert Bos: ALT text of prev/next arrows is now
# "prev"/"next" rather than the title of the prev/next slide; looks better
# in lynx.
#
# version: 4.14 - 19990719
# $Id: w3cburst.pl,v 1.41 1999/11/02 17:25:50 charles Exp $
#
#
# XLSies credits:
#
# Sami Lempinen - lempinen@iki.fi
# http://www.snowman.sgic.fi/ssl/xslies/
#
# Text::FastTemplate:
# Robert Lehr - bozzio@the-lehrs.com

use Text::FastTemplate 0.91;
use strict;

##############################################################################
## default values of variables
##

# save base dir
my $base_dir = $0;
$base_dir =~ s,/[^/]+$,,;	# remove name of script

## show debug output
my $debug=0;

## default DOCTYPE added on the slides
my $doctype = '<html xmlns="http://www.w3.org/TR/REC-html40">';

## name of raw HTML file containing the slides
my $all = 'all.htm';

## table of content built from all.htm - also first page of the presentation
## this is only the basename as we need to generate one toc for each style sheets
## the main toc will not bear any more so the server can understand a request for '/'
## the next ones will bear a number corresponding to the slide index
#$overview = 'Overview';
my $overview = 'index';

## name of the file containing the parameters of the presentation
my $infos = 'infos.txt';

## link to the logo printed on all the slides
my $logoLink = '';

## default location of the logo - works when slidemaker is used as a package
my $logoFile = '';

### localization
my $loc_toc = "Table of contents";
my $loc_by = "by";
my $loc_amp = "&amp;";	# author separator
my $loc_slide = "Slide";
my $loc_of = "of";		# $loc_slide nr $loc_of total

## alternate representation of the logo
my $logoAlt = '';

## default values set to none
my $logoLink2 = '';     # link to a potential second reference
my $logoFile2 = '';     # location of a second logo
my $logoAlt2  = '';     # alternate representation of the second logo

## default accesskeys for navigation icons used in the slides
my $prevKey  = 'P';	# accesskey for previous slide
my $nextKey  = 'N';	# accesskey for next slide
my $tocKey   = 'C';	# accesskey for table of contents
my $styleKey = 'S';	# accesskey for changing style sheets

## default author(s) name and URL
my $author = 'Staff';
my $authorUrl;
my $author2;
my $author2Url;

## default presentation title
my $talkTitle = 'Talk';
my $talkSubTitle;

## standard style sheets 
my $cssStandard = "$base_dir/css/default.css";

## template name
my $template = "$base_dir/default";

## default charset use in meta tag http-equiv (undef to skip)
#my $charset = 'ISO-8859-1';
my $charset;

## default <body> tag
my $body = '<body>';

## number of entries on each TOC page
my $toc_on_page = 10;

## use progress bar
my $progress_bar = 1;

## content hight for each slide
my $content_hight = "70%";

## some other vars which can be setuped in infos.txt
my $date;


## end of default values for the presentation
##############################################################################

## globals
my $last_toc_title = "";
my %page_data;
my %overview_data;

my $pack = 0;
my @pack_additional;	# additional files to pack (pictures, logos...)
my %nesttag = init_nesttag();

my $http_equiv;
my @toc_entries;	# entries in TOC

my $js_code = "$base_dir/pylize.js";

# add perl_include to @INC path
push @INC, "$base_dir/perl_include" if (-e "$base_dir/perl_include");

##############################################################################
## reading user input from $infos
##
my @file;

##############################################################################
sub parse_infos {
	my @file=@_;
	do {
		if ($file[0] && $file[0] =~ /^[^#\n\r]/) {
			$file[0] =~ s/\n//;    # remove UNIX \n 
			$file[0] =~ s/\r//;    # remove WINDOWS \r    
			my ($var,$value) = split(/ *= */,$file[0],2);
			$value=~s/'/\\'/g;
			my $cmd;
			if ($value ne "") {
				$cmd="\$$var = \'$value\';";
			} else {
				$cmd="\$var = \'$value\' || 1;";
			}
			if (defined($value)) {
				eval($cmd) || warn "problem with eval of: $cmd";
			} else {
				die "no value defined for $var";
			}
			print STDERR "$file[0]\n";
		}
	} while (shift(@file));
}
##############################################################################

parse_infos(@ARGV);	# backward compatibility and for pack

# read parameters from infos.txt and put them in @PARAM
if (open(INFOS, $infos)) {
	print STDERR "--- Reading parameters file $infos ---\n";
	@file = <INFOS>;
	parse_infos(@file);
}

# try to read local infos.txt for template
if (-f "$template/$infos" && open(INFOS,"$template/$infos")) {
	print STDERR "--- Reading template parameters file $template/$infos ---\n";
	@file = <INFOS>;
	parse_infos(@file);
	close(INFOS);
}

## @PARAM is now a table with the user preferences for his presentation

## use charset

if ($charset) {
	$http_equiv='<meta http-equiv="Content-type" content="text/html; charset='.$charset.'">';
} else {
	$http_equiv='';
}


##############################################################################
## read the raw html presentation
##

## copy file in memory
my $sep = $/;
$/ = undef;
open(ALL, $all) || die "Error: Cannot open file: $all";
my $buf = <ALL>;
$buf =~ s/\r//g;	# remove WINDOWS \r    
close(ALL);
$/ = $sep;

## Remove comments from the raw presentation
## they do not need to show up on the slides
$buf =~ s/<!--.*?-->//sgo;

## if $pack is set, output name of css (for inclusion in archive), but
## reset $cssStandard only to filename (without path)

die "Error: Can't open css file: $cssStandard" if (!-r $cssStandard);
die "Error: Can't open js file: $js_code" if (!-r $js_code);

if ($pack) {
	push @pack_additional,$cssStandard;
	$cssStandard =~ s/^.*\/([^\/]+)$/$1/g;
	push @pack_additional,$js_code;
	$js_code =~ s/^.*\/([^\/]+)$/$1/g;
}

## the slidemaker tool assumes that each slide is self contained between 2 sets of h1 tags
## if not it will generate a rather weird output
## split using <h1...> and </h1...> as separator (ignores attributes!)
## h1 or H1 can be used
my @table = split(/<\/?[hH]1[^>]*>/, $buf);

## compute the total number of slides
my $total = $#table / 2;
if ($#table % 2 != 0) {
	$total = ($#table +1)/2;
}

##
## raw presentation has been read successfully
##############################################################################

##############################################################################
## processing the slides
 
print STDERR "\n--- Processing $total slides ---\n"; 

## generate the header table of content of the presentation
## which is also the first page of the talk
&openOverview($overview);

## start the slide count so we can number them
my $slideCount = 1;

## pre-load template slides using $template dir
Text::FastTemplate->defaults(
		path => [ $template ]
	);

Text::FastTemplate->preload( [
		{ file => 'slide.html', key => 'slide' },
		{ file => 'overview.html', key => 'overview' },
		{ file => 'title.html', key => 'title' } ]
	);

## unroll relative anchors (#something) into links with slides

my %anchor_on_slide;

## step 1: record anchors
for(my $i=0; $i<=$total; $i++) {
	my $tmp = $table[($i*2)];
	$tmp .= $table[($i*2)+1] if ($table[($i*2)+1]);
	while ($tmp =~ s,<a\s+name="*([^>"]+)"*>,,i) {
		$anchor_on_slide{$1}=$i;
		print STDERR "\tslide ",($i+1)," anchor: $1\n" if ($debug);
	}
}

## step 2: fix links
for(my $i=0; $i<$total; $i++) {
	foreach (keys %anchor_on_slide) {
		$table[($i*2)] =~ s,href="*#$_"*>,href="slide$anchor_on_slide{$_}\.html#$_">,gi;
	}
}

## @table is the array containing each slide with its title
## for each slide to be generated
## we delete each slide and its title when generated
## so that the current slide and its title are always at $table[0] (for the title)
## and $table[1] (for the slide content)

#undef $prev_title;
#undef $next_title;
my $prev_title;
my $next_title;

do {

	## get rid of the first element contained by the raw presentation array
	shift(@table);
	## then $table[0] is the title of the slide to be generated
	if ($table[0]) {
		$table[0] =~ s/[\n\r\s]+/ /g;	# compress whitespace
		$table[0] =~ s/^ +//;
		$table[0] =~ s/ +$//;
	}

	## $slideTitle preserves link(s) in the title
	my $slideTitle = $table[0];
	## need to check if the title contains any anchor
	## if so it needs to be removed
	## because the title is being used in the table of content to link to the corresponding slide
	$table[0] = remove_anchor($table[0]);

	## grab next slide title $table[2] (if there's a next slide)
	## to be able to use in the 'next' navigation button
	## keep in mind that $table[1] contains the slide corresponding to the title $table[0]
	if ($table[2]) {
		$next_title= remove_anchor($table[2]);
	}

	## the current slide content is stored $table[1]
	## there is an attempt to make sure it's clean HTML
	## Pierre Fillault's note: use same piece of as used in http://www.w3.org/Web/Tools/CvsCommitScripting
	## to make use of the validation service
	my $slideContent = &clean_html($table[1]) ;

	## extract slide Sub Title <h2>
	my $slideSubTitle;
	if ($slideContent && $slideContent =~ s/<h2[^>]*>(.+?)<\/h2[^>]*>//ism) {
		$slideSubTitle=remove_anchor($1);
	}

	## add the title of the current slide to the table of content
	&addTitle($slideTitle,$slideSubTitle,$slideCount);

	## generate the current slide
	## parameters are:
	## title of the slide, its content, the slide number, the title of the previous slide and the title of the next slide
	&createSlide($slideTitle,$slideSubTitle,$slideContent ,$slideCount++,$prev_title,$next_title);

	## save the title of the previous slide to be displayed in the 'previous' navigation button
	$prev_title=remove_anchor($table[0]);
}
## process the next slide 
while (shift(@table));

## close the table of content
&closeOverview;

## print additional files to pack
print STDOUT join("\n",@pack_additional) if ($pack);

print STDERR "--- Finished ---\n";
exit 0;
##
## end of the slidemaker main program
##############################################################################


##############################################################################
## generate the header of the table of content

sub openOverview 
{

	my ($file) = @_;

	## open the file to write to
	open(FOO, ">${file}.html") || die "can't open ${file}.html: $!";
	push @pack_additional,"${file}.html" if ($pack);

	## the style sheet used in the table of content is
	my $stylelink = "";
	## here is the standard style sheet
	$stylelink .= "<link href=\"$cssStandard\" rel=\"stylesheet\" type=\"text/css\" title=\"Talk\" media=\"screen\">";

	# add javascript anim
	$stylelink .= head_additional(undef, "slide1.html");

	%overview_data = (
		doctype => $doctype,
		title => $talkTitle,
		http_equiv => $http_equiv,
		stylelink => $stylelink,
		body => $body,

		logoLink => $logoLink,
		logoFile => $logoFile,
		logoAlt => $logoAlt,
		logoLink2 => $logoLink2,
		logoFile2 => $logoFile2,
		logoAlt2 => $logoAlt2,

		talkTitle => $talkTitle,
		talkSubTitle => $talkSubTitle,

		content_hight => $content_hight,

		author => $author,
		authorUrl => $authorUrl,
		author2 => $author2,
		author2Url => $author2Url,

		date => $date,

		toc_title => $loc_toc,
		template_dir => "$template/",
	);

}
##
## the beginning of the table of content has been generated and saved
##############################################################################

##############################################################################
## generate the footer of the table of content

sub closeOverview
{
	$overview_data{slide_html} = make_progress_bar(0,$total);
	$overview_data{toc_entries} = [ @toc_entries ];

	my $page= new Text::FastTemplate key => 'overview';

	my $html = add_onLoad( $page->output( \%overview_data ) );
	if ($pack) {
		print FOO extract_files($html, $overview_data{template_dir});
	} else {
		print FOO $html;
	}

	close(FOO); 
}
##
## the toc has been completed and saved
##############################################################################

##############################################################################
## add an item in the toc

sub addTitle 
{
	my $title = remove_tags(shift @_);
	my $subtitle = remove_tags(shift @_);
	my $nr = shift @_;

	if (! $title) {
		return 1;
	}

	# split TOC entries to multiple pages

	my $pre_ul;
	my $post_ul;

	if ($nr % $toc_on_page == 0) {
		my $toc_nr=int($nr/$toc_on_page);

		my $item = {
			pre_html => $pre_ul,
			accesskey => " ",	# space
			href => "index-toc$toc_nr.html",
			title => "...",
			post_html => $post_ul,
			more => 1,	# use style for more pages link (...)
		};
		push @toc_entries, $item;

		&closeOverview;
		undef @toc_entries;
		&openOverview("${overview}-toc${toc_nr}");
		$last_toc_title='';
	}

	$pre_ul=$post_ul='';
	if ($last_toc_title eq $title && $subtitle) {
		if ($subtitle !~ m,^\s*$,) {
			$title = $subtitle;
		} else {
			$title .= " (...)";
		}
		$pre_ul='<ul>';
		$post_ul='</ul>';
	} else {
		$last_toc_title=$title;
	}

	# add accesskey for first 9 slides (`1' - `9') or just for first
	# TOC page, and tabindex for all slides
	if ($nr < 10 && $nr < $toc_on_page) {
		my $item = {
			pre_html => $pre_ul,
			accesskey => "$nr",
			tabindex => "$nr",
			href => "slide$nr.html",
			title => $title,
			post_html => $post_ul,
			more => 0,
		};
		push @toc_entries,$item;
	} else {
		my $item = {
			pre_html => $pre_ul,
			tabindex => "$nr",
			href => "slide$nr.html",
			title => $title,
			post_html => $post_ul,
		};
		push @toc_entries,$item;
	}
}
##
##############################################################################

##############################################################################
## generate the current slide

sub createSlide 
{

	# parameters are respectively the slide title, its content,
	# its number, the next slide title and the previous slide title

	my $title = remove_tags(shift @_) || return 1;
	my $subtitle = remove_tags(shift @_);
	my $content = shift @_ || return 1;
	my $nr = shift @_;
	my $prev_title = remove_tags(shift @_);
	my $next_title = remove_tags(shift @_);

	# work the slide title, the previous and next titles
	chomp $title;

	# work the slide content
	$content =~ s/<\/body>//i; # remove if any
	$content =~ s/<\/html>//i; # remove if any

	print STDERR remove_tags(sprintf("Slide %2d: %s %s", $nr, $title, $subtitle || "")),"\n";

	&verify_html($content);		# check the html
	&check_tags($content);		# check open and closed tags

	## write to the slide
	open(SLIDE, ">slide$nr.html") || die "can't save slide$nr.html: $!";
	push @pack_additional,"slide$nr.html" if ($pack);

	my $toc_link = "$overview\.html";

	## initialization of the navigation links
	my $next_link = "";
	my $prev_link = "";

	if ($nr>1) {
		$prev_link = "slide".($nr-1).".html";
#	} else {
#	## add a link back to the toc for the first slide --CMN 19991102
#		$prev_link = "$overview\.html";
	}

	if ($nr != $total) {
		$next_link = "slide".($nr+1).".html";
	}

	my $stylelink = "";
	# here is the standard style sheet
	$stylelink .= "<link href=\"$cssStandard\" rel=\"stylesheet\" type=\"text/css\" title=\"Talk\">\n";

	$stylelink .= head_additional($prev_link, $next_link);

	# add javascript anim
 	my $slide_html=make_progress_bar($nr,$total);

	# undefine body if no content is found (so that template can show
	# only title and sub-title
	if ($content !~ m/\S/g) {
		undef $content;
	}

	%page_data = (
		doctype => $doctype,
		talkTitle => $talkTitle,
		title => $title,
		subtitle => $subtitle,
		http_equiv => $http_equiv,
		stylelink => $stylelink,
		body => $body,

		logoLink => $logoLink,
		logoFile => $logoFile,
		logoAlt => $logoAlt,
		logoLink2 => $logoLink2,
		logoFile2 => $logoFile2,
		logoAlt2 => $logoAlt2,


		content_hight => $content_hight,
		content => $content,

		prev_link => $prev_link,
		toc_link => $toc_link,
		next_link => $next_link,
		prev_title => $prev_title,
		toc_title => $loc_toc,
		next_title => $next_title,

		author => $author,
		authorUrl => $authorUrl,
		author2 => $author2,
		author2Url => $author2Url,

		date => $date,

		slide_html => $slide_html,

		template_dir => "$template/",
	);

	my $page;
	if ($content) {
		$page= new Text::FastTemplate key => 'slide';
	} else {
		$page= new Text::FastTemplate key => 'title';
	}

	my $html = add_onLoad( $page->output( \%page_data ) );
	if ($pack) {
		print SLIDE extract_files($html, $page_data{template_dir});
	} else {
		print SLIDE $html;
	}

	close(SLIDE);
	return 0;
}

##############################################################################
# check that the html of the slide 
# is correct (ALT tags, ...)
# This procedure produces only warning
sub verify_html {

    if ($_[0] =~ /<img([^>]*)>/im) {
	if (!($1 =~ /ALT=/im)) {
	    print STDERR "WARNING: <IMG> without ALT\n";
	    print STDERR "         <IMG$1>\n" ;
	}
    }
}

##############################################################################
# clean the html of the slide
# remove all <div class="comment">blabla</div>
sub clean_html {
	my $tmp=$_[0] || return;
	$tmp =~ s/<div\s+class\s*=\s*(?:comment[\s>]|\"comment\").*?<\/div>//igs;
	$tmp =~ s,</*font[^>]*>,,igms;

	# execute perl code inside <perl>...</perl> tags
	# warning: this create one fork per <perl> tag!
	sub eval_perl {
		my $code = shift @_;
		my $pid = open(FH, "-|");
		my $output;

		if ($pid) {   # parent
			while (<FH>) {
				$output .= $_;
			}
			close(FH);
		} else {
			# child
			eval($code);
			die "<perl> eval failed: $@ " if ($@);
			exit 0;
		}

		return $output;
	}

	$tmp =~ s,<perl>(.*?)</perl>,eval_perl($1),igmse;
	return $tmp;
}

##############################################################################
# make transparent 1x1 gif (for layout tricks)
sub make_dotgif {
	my @dot_gif=(71,73,70,56,57,97,1,0,1,0,128,0,0,192,192,192,0,0,0,33,
		249,4,1,0,0,0,0,44,0,0,0,0,1,0,1,0,0,2,2,68,1,0,59);
	open(GIF,"> $_[0]") || die "can't write gif $_[0]: $!";
	my $dotgif;
	foreach (@dot_gif) {
		$dotgif.=chr($_);
	}
	print GIF $dotgif;
	close(GIF);
}

##############################################################################
# make slide progress bar
sub make_progress_bar {
	my ($nr,$total) = @_;

	my $pcnt_done=int($nr*100/$total);
	my $pcnt_left=100-$pcnt_done;

	my $html;

	if ($progress_bar && uc($progress_bar) ne "NO") {
		my ($l,$r);
		$l = $r = "&nbsp;";
		my $t="$nr of $total";
		if ($pcnt_done > 50) {
			$l=$t;
		} else {
			$r=$t;
		}
		$html='<table border="0" width="50%" cellpadding="0" cellspacing="0" align="right"><tr>';
		if ($pcnt_done != 0) {
			$html.='<td width="'.$pcnt_done.'%" class="pcnt-done">'.$l.'</td>';
		}
		if ($pcnt_left != 0) {
			$html.='<td width="'.$pcnt_left.'%" class="pcnt-left">'.$r.'</td>';
		}
		$html.='</tr></table>';
	} else {
		$html="$loc_slide $nr $loc_of $total";
	}

	return $html;
}

##############################################################################
# remove anchors <a href...> from html (for titles)
sub remove_anchor {
	my $tmp = $_[0] || return;
	$tmp =~ s/(.*)<A[^>]*>(.*)<\/A>(.*)/$1$2$3/ig;
	return $tmp;
}

##############################################################################
# remove html tags
sub remove_tags {
	my $tmp = shift @_ || return;
	$tmp =~ s/<[^>]+>//g;
	$tmp =~ s/\r//g;
	$tmp =~ s/"/&#34;/g;	# quote "
	return $tmp;
}

##############################################################################
# extract files referenced in presentation and remove dirs from slide html

sub extract_files {
	my $slide = shift @_;
	my $template_dir = shift @_;

	my $tmp = $slide;

	while ($tmp =~ s/href="*([^"\s]+)"*//ism ||
		$tmp =~ s/src="*([^"\s]+)"*//ism) {
		if ("$1" !~ m/[hf]t?tp:/ && -f "$1" && !grep(/$1/,@pack_additional)) {
			my $file=$1;
			push @pack_additional,$file;
			if ($file =~ m,^(.+)/([^/]+),) {
				my ($d,$f) = ($1,$2);
				$slide =~ s,${d}/${f},${f},g;
			}
		}
	}
	$slide =~ s,$template_dir,,smg;
	return $slide;
}

##############################################################################
# check tags in slide
# based on code from hindent 1.1.2 by Paul Balyoz <pab@domtools.com>

sub init_nesttag {
# Tags that require their own end tag <TAG>...</TAG> we will nest them
# properly:   (WARNING, you must use lower-case here)
# All other tags (not on this list) will be ignored for indenting purposes.
return (
	'html' => 1,
	'head' => 1,
	'body' => 1,
	'title' => 1,

	'a' => 1,

	'table' => 1,
	'tr' => 1,
	'th' => 1,
	'td' => 1,

	'form' => 1,
	'select' => 1,
	'textarea' => 1,

#	'p' => 1,      Don't do this one because many people use <P> but not </P>
	'ul' => 1,
	'ol' => 1,
	'dl' => 1,
	'blockquote' => 1,
	'center' => 1,
	'div' => 1,

	'font' => 1,
	'pre' => 1,
	'tt' => 1,
	'i' => 1,
	'b' => 1,
	'u' => 1,
	'strike' => 1,
	'big' => 1,
	'small' => 1,
	'sub' => 1,
	'sup' => 1,
	'em' => 1,
	'strong' => 1,
	'dfn' => 1,
	'code' => 1,
	'samp' => 1,
	'kbd' => 1,
	'var' => 1,
	'cite' => 1,

	'h1' => 1,
	'h2' => 1,
	'h3' => 1,
	'h4' => 1,
	'h5' => 1,
	'h6' => 1,

	'applet' => 1,

	'map' => 1,

	'frameset' => 1,
	'noframes' => 1,
);
}

sub check_tags {
	my $tmp = $_[0];
	my @tagstack;
	my $level=0;

	while ($tmp =~ /<([^>]*?)>/gms) {
		my $tag=$1; $tag=~s/\s.*$//g;
		# if regular tag, push it on stack; if end-tag, pop it off stack.
		# but don't do any of this if it's not a special "nesting" tag!
		if ($tag !~ m,^/,) {
			if ($nesttag{lc($tag)}) {
				push @tagstack,$tag;
				$level++;		# remember how much for later
			}
		} else {
			$tag =~ s,^/,,;		# convert this end-tag to a begin-tag
			$tag = lc($tag);
			if ($nesttag{lc($tag)}) {
				# throw away tags until we find a match
				if ($#tagstack > -1) {
					while ($tag ne lc(pop @tagstack)) {
						$level--;	# we threw away extra tags
						last if $#tagstack <= 0;
					}
					$level--;	# we threw away extra tags
					if ($level < 0) {
						print STDERR "WARNING: more end than begin tags around </$tag> !\n";
					}
				}
			}
		}
	}

	if ($level > 0) {
		print STDERR "WARNING: level=$level, ", $#tagstack+1," tags left on stack after done parsing!  Specifically:\n<",join("> <",@tagstack),">\n";
	}

}

##############################################################################
#
# add javascript support from pylize by
# Christopher Arndt - chris.arndt@web.de
# http://www.chrisarndt.de/en/software/pylize/

sub head_additional {
	my $prev_link = shift @_;
	my $next_link = shift @_;

	my $html;
	$html .= "<link rel=\"contents\" href=\"index.html\" title=\"Contents\">\n";
	$html .= "<link rel=\"previous\" href=\"$prev_link\" title=\"Previous\">\n" if ($prev_link);
	$html .= "<link rel=\"next\" href=\"$next_link\" title=\"Next\">\n" if ($next_link);

	# add javascript support from pylize by
	# Christopher Arndt - chris.arndt@web.de
	# http://www.chrisarndt.de/en/software/pylize/
	$html .= '<script type="text/javascript" language="JavaScript1.2" src="'.$js_code.'">
</script>
<noscript>
<style type="text/css">
<!--
.delayed {
visibility: visible;
}
-->
</style>
</noscript>';

	return $html;
}

sub add_onLoad {
	my $html = shift @_;
	$html =~ s/(<body)/$1 onLoad="initPage()"/i;
	return $html;
}

