#! /usr/bin/perl
#
# recipelist - creates an html index of "recipes" from La Espiral
#
# Copyright (C) Jaime Villate <villate@gnu.org>, 2001
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# $Id: recipelist,v 1.1 2001/12/05 21:37:19 villate Exp $

use XML::DOM;
while ($file = pop(@ARGV))
{
    ($id) = ($file =~ /receta(\d+)\.html/);
    ($src = $file) =~ s/\.html$/.xml/;
    $parser = new XML::DOM::Parser;
    $doc = $parser->parsefile ($src);
    $title{$id} = &extract($doc->getElementsByTagName ("titulo"));
    $author{$id} = &extract($doc->getElementsByTagName ("autor"));
    $date{$id} = &extract($doc->getElementsByTagName ("fecha"));
    $page{$id} = $file;
}
print <<'Header';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Recetas de La Espiral</title>
</head>
<body>
<h2>Recetas de <a href="http://www.laespiral.org">La Espiral</a></h2>
<dl>
Header

foreach $key (sort {$a <=> $b} (keys %title))
{
    print qq{<dt><b>Receta $key</b></dt>\n};
    print qq{<dd><a href="$page{$key}">$title{$key}</a>,\n};
    print qq{por $author{$key}, $date{$key}.</dd>\n};
}

print <<'Footer';
</dl>
Copyright 2001, <a href="http://www.laespiral.org">La Espiral</a>.
</body>
<html>
Footer

exit;

sub extract
{
    (@elements) = @_;
    $element = $elements[0]->toString;
    $element =~ s/^[^>]*>\n?//;
    $element =~ s/\n?\s*<[^<]*$//;
    return $element;
}
