#!/usr/bin/perl -w
# rss2html - converts an RSS file to HTML
# It take one argument, either a file on the local system,
# or an HTTP URL like http://slashdot.org/slashdot.rdf
# by Jonathan Eisenzopf. v1.0 19990901
# Copyright (c) 1999 internet.com Corp. All Rights Reserved.
# See http://www.webreference.com/perl for more information
#
# 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.

# INCLUDES
use strict;
use XML::RSS;
use LWP::Simple;

# Declare variables
my $content;
my $file;

# MAIN
# check for command-line argument
die "Usage: rss2html.pl (<RSS file> | <URL>)\n" unless @ARGV == 1;

# get the command-line argument
my $arg = shift;

# create new instance of XML::RSS
my $rss = new XML::RSS;

# argument is a URL
if ($arg=~ /http:/i) {
    $content = get($arg);
    die "Could not retrieve $arg" unless $content;
    # parse the RSS content
    $rss->parse($content);

# argument is a file
} else {
    $file = $arg;
    die "File \"$file\" does't exist.\n" unless -e $file;
    # parse the RSS file
    $rss->parsefile($file);
}

# print the HTML channel
print_html($rss);

# SUBROUTINES
sub print_html {
    my $rss = shift;
    print <<HTML;
<TABLE BGCOLOR="#000000" BORDER="0" WIDTH="200"><TR><TD>
<TABLE CELLSPACING="1" CELLPADDING="4" BGCOLOR="#FFFFFF"
 BORDER=0 WIDTH="100%">
  <TR>
  <TD VALIGN="middle" ALIGN="center" BGCOLOR="#EEEEEE"><FONT
   COLOR="#000000" FACE="Arial,Helvetica"><B><A
   HREF="$rss->{'channel'}->{'link'}">$rss->{'channel'}->{'title'}</A>
</B></FONT></TD></TR>
<TR><TD>
HTML

    # print channel image
    if ($rss->{'image'}->{'link'}) {
        print <<HTML;
<CENTER>
<P><A HREF="$rss->{'image'}->{'link'}"><IMG
 SRC="$rss->{'image'}->{'url'}" ALT="$rss->{'image'}->{'title'}"
 BORDER="0"
HTML
        print " WIDTH=\"$rss->{'image'}->{'width'}\""
            if $rss->{'image'}->{'width'};
        print " HEIGHT=\"$rss->{'image'}->{'height'}\""
            if $rss->{'image'}->{'height'};
        print "></A></CENTER><P>\n";
    }

    # print the channel items
    foreach my $item (@{$rss->{'items'}}) {
        next unless defined($item->{'title'})
                    && defined($item->{'link'});

        print "<LI><A HREF=\"$item->{'link'}\">".
            "$item->{'title'}</A><BR>\n";
    }

    # if there's a textinput element
    if ($rss->{'textinput'}->{'title'}) {
        print <<HTML;
<FORM METHOD="GET" ACTION="$rss->{'textinput'}->{'link'}">
$rss->{'textinput'}->{'description'}<BR> 
<INPUT TYPE="text" NAME="$rss->{'textinput'}->{'name'}"><BR>
<INPUT TYPE="submit" VALUE="$rss->{'textinput'}->{'title'}">
</FORM>
HTML
    }

    # if there's a copyright element
    if ($rss->{'channel'}->{'copyright'}) {
        print <<HTML;
<P><SUB>$rss->{'channel'}->{'copyright'}</SUB></P>
HTML
    }

    print <<HTML;
</TD>
</TR>
</TABLE>
</TD></TR></TABLE>
HTML
}