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

Re: [MacPerl] Using XML::RSS as a BBEdit Perl Filter



Larry F. Allen-Tonar, <larryat@cts.com> wrote:

>My $0.02:
>BBEdit filter gives the perl script the selected text as STDIN.
>@myinput = <>; -- if there is @ARGV is empty, will simply read from STDIN.
>So, perhaps you could open the file for editing, clover-A to select all
>the text of the file you wish to process, and invoke the filter from
>BBEdit -- which *should* then show up as @myinput (as long as the
>INPUT RECORD SEPARATOR is correct).

Well, that just about describes where I'm *at* at the moment.

Problem:  The method--

$rss->parse();

takes a STRING as its argument.  What I get from BBEdit is an ARRAY.

Maybe:
@myinput = <>;
my $i = 0;

while (@myinput) {
   $rss->parse($myinput[$i]);
   $i++
}

worth a try?  Nope, no dice.  But, I did find something that sorta works.

STRIP OUT ALL THE CARRIAGE RETURNS!

Thinking was like this:

The RSS file is just XML.  XML is enough like HTML that BBEdit will treat 
the XML tags like they're HTML tags.  So, before parsing the document, 
render the whole thing in "Compact" formatting, then run the filter:

#!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;

my $myinput = <>;

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

# parse the RSS file
$rss->parse($myinput);

# 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__

Not particularly elegant.  So, if anyone's still interested, is there a 
way to munge the input data into a string with no carriage returns WITHIN 
the filter?

--B

# 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