#!/usr/local/bin/perl
#
# slidemaker tool
# split a all.htm into slide*.htm
#
# Stephan Montigaud - stephan@w3.org
# 970601
#

# default variables
$all = 'all.htm';
$overview = 'overview.htm';
$logoLink = 'http://www.w3.org/';
$logoFile = '../Icons/w3c_home.gif';
$left = '../Icons/left.gif';
$right = '../Icons/right.gif';
$top = '../Icons/up.gif';
$author = 'W3C Staff';
$talkTitle = 'W3C Talk';
$cssFile = '../Tools/w3ctalk.css';
$body = '<body bgcolor=#000060 text=#ffffff link=#ffffe8 vlink=#eeffee alink=#ff0000>';

# process arguments
foreach (@ARGV) {
    split(/=/);
    $cmd="\$$_[0] = \'$_[1]\';";
    if (length $_[1] != 0) { 
	eval($cmd);
    }
}


# misc initializations
if ($authorUrl) {
    $author = "<a href=\"$authorUrl\">$author</a>";
}
if ($author2 && $author2Url) {
    $author2 = "<a href=\"$authorUrl2\">$author2</a>";
}
if ($author2) {
    $author = $author." & ".$author2;
}


# copy file in memory
if (!open(ALL, $all)) {
    print "Error: Cannot open file: $all\n";
    exit 0;
}
@table = <ALL>;
close(ALL);


# split using <h1>xxxx</h1> as separator
@table = split(/<\/?[hH]1>/, "@table");
$total = $#table / 2;

print STDOUT "--- Processing $total slides ---\n"; 

$OVERVIEW = &openOverview($overview);
$slideCount = 1;

do {
    shift(@table);
    $table[0] =~ s/\n+/ /g;
    $table[0] =~ s/ +/ /g;
    $table[0] =~ s/^ //;
    $table[0] =~ s/ $//;
    &addTitle($table[0],$slideCount);
    &createSlide($table[0],$table[1],$slideCount++);
} 
while (shift(@table));

&closeOverview;

print STDOUT "--- Finished ---\n";
exit 0;


sub openOverview 
{
    open(OVERVIEW, ">$_[0]");

    print OVERVIEW <<END;
<html>
<head>
<title>$talkTitle - Slide list</title>
<link href="$cssFile" rel=stylesheet type="text/css" title="W3C Talk">
</head>
$body
<h1 align=center class=slideList>$talkTitle<br>
<small>by</small> <i>$author</i></h1>
<blockquote>
<h2>Table of contents</h2>
<ul>
END

    return OVERVIEW;
}

sub closeOverview
{
    print $OVERVIEW <<END;
</ul></blockquote>
<hr>
<table border=0 width=100%>
<tr valign=top>
<td align=right><a href=\"$logoLink\"><img src=\"$logoFile\" align=bottom border=0 alt=\"W3C\"></a></td>
</tr></table>
</body>
</html>
END

    close($OVERVIEW);
}

sub addTitle 
{
    if (length $_[0] == 0) {
	return 1;
    }
    print $OVERVIEW <<END;
<li><a href=\"slide$_[1].htm\">$_[0]</a></li>
END
    return 0;
}

sub createSlide 
{
    if (length $_[0] == 0) {
	return 1;
    }

    open(SLIDE, ">slide$_[2].htm");

    $_[0] =~ s/[^a-z ]//ig; # keep only printable characters
    $status = sprintf "Slide %2d: %s\n", $_[2], $_[0];
    print STDOUT $status;

    $_[1] =~ s/<\/body>//i; # remove if any
    $_[1] =~ s/<\/html>//i; # remove if any
    &verify_html($_[1]);    # check the html

    local($navBar);
    $navBar = '<td align=right nowrap>';

    if ($_[2]>1) {
	$navBar = $navBar."<a href=\"slide".eval($_[2]-1).".htm\"><img src=\"$left\" border=0 alt=\"previous\"></a>";
    }

    $navBar = $navBar."<a href=\"$overview\"><img src=\"$top\" border=0 alt=\"top\"></a>";

    if ($_[2] != $total) {
	$navBar = $navBar."<a href=\"slide".eval($_[2]+1).".htm\"><img src=\"$right\" border=0 alt=\"next\"></a>";
    }

    print SLIDE <<END;
<html>
<head>
<title>$talkTitle - slide $_[0]</title>
<link href="$cssFile" rel=stylesheet type="text/css" title="W3C Talk">
</head>
$body
<table border=0 width=100%>
<tr valign=top>
<td align=left><h1 class=slide>$_[0]</h1></td>
$navBar
</td></tr>
</table>
<hr class=top>
$_[1]
<hr class=bottom>
<table border=0 width=100% valign=bottom>
<tr><td><p class=author>$author</td>
<td align=right><a href=\"$logoLink\"><img src=\"$logoFile\" border=0 alt=\"W3C\"></a>
</td></tr>
</table>
</body>
</html>
END

    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 STDOUT "WARNING: <IMG> without ALT\n";
	    print STDOUT "         <IMG$1>\n" ;
	}
    }
}






