[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] XML::RSS as a Perl Filter (finished code)



Time to put the problem to bed:

Thanks to Brad Hanson for getting to the heart of the matter.  Although 
he wasn't exactly on the mark, he was close enough so that I could find 
it after he pointed the way.

Note: 

$rss->parsefile(shift @ARGV);

Does exactly what Brad suggested.

Jim Correria suggested that I cross-post my results to BBEdit-Talk. 
Because of this, I'm going to have to summarize a bit (my apologies to 
the MacPerl list for the re-hash).

My problem was to find a more convienient way to use a pair of Perl 
scripts written under Unix, which expected arguments from a command line. 
 Running the scripts as droplets requires I LEAVE BBEdit (Horrors!), when 
my intent is to use BBEdit to upload the resulting files to the webserver 
when I'm done.

Perl Filters was the answer, but how to pass the data to the programs.  
The first of the two ran without modification, but the second needed 
coaxing.  The complete tutorial for the XML::RSS perl module is at:

http://www.webreference.com/perl/tutorial/8/

The original "rss2html.pl" is intended to be called as a server-side 
include.  I needed a version that gives me a preview of what that program 
generates.  Hence the Perl Filter below.

Now all you Mac webmasters out there have no excuses for not making your 
site a channel on "My Netscape" :-)  Er, that really isn't my intent 
here, but go for it anyway.

--B

#!perl -w
# my_rss2html - converts an RSS file to HTML
# Modified for use as BBEdit Filter
# Brian McNett 10/24/1999
# orig. by Jonathan Eisenzopf. v1.0 19990901
# See http://www.webreference.com/perl for more information
# modified slightly for Mycoinfo.

# INCLUDES
use strict;
use XML::RSS;

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

# parse the RSS file
$rss->parsefile(shift @ARGV);

# print the HTML channel
&print_html($rss);


# SUBROUTINES
sub print_html {
    my $rss = shift;
       if ($rss->{'image'}->{'link'}) {
print <<HTML;
<table bgcolor="#333366" border="0" width="184"><tr><td>
<table cellspacing="1" cellpadding="2" bgcolor="#FFFFFF" border=0 
width="100%">
<tr>
<td valign="middle" align="center" bgcolor="#3366CC">
<center><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>\n";
    }

print <<'HTML';
</td>
</tr>
<tr>
<td>
HTML

    # print the channel items
    foreach my $item (@{$rss->{'items'}}) {
	next unless defined($item->{'title'}) && defined($item->{'link'});
	print "<font size='-1'><li><a 
href=\"$item->{'link'}\">$item->{'title'}</a></font><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 align='right'><sub>$rss->{'channel'}->{'copyright'}</sub></p>
HTML
    }

    print <<HTML;
</td>
</tr>
</table>
</td>
</tr>
</table>
HTML
}

__END__

# Brian McNett                                      Fungal Parataxonomy
# Webmaster, Staff Writer               Mycology Information (Mycoinfo)
#                              **The World's First Mycology E-Journal**   
# <mailto:webmaster@mycoinfo.com>            <http://www.mycoinfo.com/>
# First they ignore you. Then they laugh at you. Then they fight you.
# Then you win.                                     --Mohandas Gandhi


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org