use strict;
use warnings;
BEGIN {
unshift @INC, qq{/home/dotan/cgi/mt/extlib};
}
use XML::Smart;

while (my $subfile = shift @ARGV)
{
    my $subs = XML::Smart->new($subfile , 'XML::Smart::Parser');
    # visitItems($subs->{'opml'}{'body'}, \&printSubscriptions);
    visitItems($subs->{'opml'}{'body'}, \&printSubsAsText);
}

sub visitItems
{
my ($ref, $subRef) = @_;
for my $item (@{$ref->{'outline'}})
{
    &$subRef($item);
    if ($item->{'outline'})
    {
        visitItems($item, $subRef);
    }
}
}

sub printSubscriptions
{
my ($item) = @_;
# This is a less verbose (but more confusing?) way to do the next 5 declarations.
#    my ($html, $title, $rss, $type, $description) =
#    map { $item->{$_} } (qw(htmlUrl title xmlUrl type description));
my $html = $item->{'htmlUrl'};
my $title = $item->{'title'};
my $rss = $item->{'xmlUrl'};
my $type = $item->{'type'};
my $description = $item->{'description'};
print qq{<p>};
if ($html)
{
print qq{<a href="$html"};
print qq{ title="$description" } if ($description);
print qq{>};
}

print (($title) ? $title : $html || $rss);
print qq{</a> } if ($html);

print qq{ <a href="$rss"><img
             src="http://corky.net/dotan/log/images/xml.gif"
             width="36" height="14" border="0"
             alt="RSS"></a>} if $rss;
print qq{</p>\n };
} 

sub printSubsAsText {
my ($item) = @_;
my $rss = $item->{'xmlUrl'};
print "$rss\n";
}
