#!/usr/bin/perl
# $Revision: 1.7 $
#
# Convert Delicious Library XML files to HTML files.
#
# Usage: Put this file in the directory
# /Users/<username>/Application Support/Delicious Library
# Create a directory called HTML, then run this script.
# Copy index.html, and the directories HTML and 'Images/Plain Covers' to
# your website. There's a lot of other stuff under Images that you don't
# want.
# Future versions will use some variety of templates, so that the output
# isn't so ugly.
# Bugs, patches, complaints and adulation to Rich Bowen
# <rbowen@rcbowen.com>

use XML::Simple;

my $ref = XMLin("Library Media Data.xml");
my @books = ( @{$ref->{items}->{book}}, 
              @{$ref->{items}->{movie}},
              @{$ref->{items}->{music}}
            );

open INDEX, ">index.html";
print INDEX "<ul>\n";

# Don't order by "A" and "The"
$_->{title} =~ s/(A|The) (.*)/$2, $1/ foreach @books;

# Schwartzian transform. Don't hurt yourself.
@books = 
  map { $_->[0] } 
    sort { $a->[1] cmp $b->[1] } 
      map { [$_, $_->{title}] } 
        @books;

foreach my $book (@books) {
    $book->{fullTitle} ||= $book->{title};
    print INDEX "<li><a href='HTML/$book->{uuid}.html'>$book->{fullTitle}</a></li>\n";

    $book->{author} =~ s/\n/ &amp; /g;
    open B, ">HTML/$book->{uuid}.html";
    print B qq!
        <h1>$book->{fullTitle}</h1>
        <img src="../Images/Plain%20Covers/$book->{uuid}.jpg"
        align="left">
        <b>$book->{author}</b><br />
        Publisher: $book->{publisher}<br />
        Pages: $book->{pages}<br />
        Published: $book->{published}<br />
        ISBN: $book->{asin}<br />
        Buy it: <a
         href="http://www.amazon.com/exec/obidos/asin/$book->{asin}/drbacchus/">Amazon</a> 
         | <a href="http://www.pricescan.com/books/BookDetail.asp?isbn=$book->{asin}">Pricescan</a><br />
        !;
    close B;
}

print INDEX "</ul>\n";
close INDEX;

