<!--
=head1 NAME

rssbox.wmk - include RSS feeds into your documents

=head1 LOADING

  < use plugin="xsl" />
  < use plugin="rssbox" />

=head1 HTML / XML TAGS

  In WebMake file, load the RSS file itself:

	< template name="perlnews.rss"
	    src="http://meerkat.oreillynet.com/?c=738&t=7DAY&_fl=rss"
	    format="text/xml">
	    < /template>

  In HTML output file, include the CSS code used inside the <style> block in
  the <head> area of your HTML page(s):

	< head>
	[ ... ]
	< style>
	[ ... ]
	${rss2html.css_text}
	[ ... ]
	< /style>
	[ ... ]
	< head>

  In HTML output file, use the C<rssbox> tag to insert the RSS box:

	< rssbox rss="perlnews.rdf" />

=head1 DESCRIPTION

This WebMake library provides an XSL stylesheet, which allows you to include
RSS feeds directly into your HTML documents.

It doesn't matter what version of RSS is used in the item named in the C<rss>
parameter, C<rssbox> supports RSS 0.9, 0.91 and 1.0.

Note that you also need to include a LINK or STYLE block which contains
the C<rss2html.stylesheet_text> content item, in order to set the CSS styles
used by the output HTML.

It doesn't matter what version of RSS is used in the C<rss> parameter,
C<rssbox> supports RSS 0.9, 0.91 and 1.0.

The XSL stylesheet used was originally written by Michael Claßen, for
WebReference.com.  It's been updated with some more XSL from Eric van der
Vlist's stylesheet on 4xt.org, to support RSS 1.0, and Eric's converter
stylesheets are used to support 0.9 and 0.91.

=head1 TEMPLATES USED

The following template items are predefined by this plugin, and can be
overridden to change the output.  The default setting is listed beside the
template's name.

=over 4

=item rss2html.box.foreground: black

=item rss2html.box.background: white

=item rss2html.box.border: 3

=item rss2html.title.foreground: black

=item rss2html.title.background: white

=item rss2html.title.foreground.mouseover: black

=item rss2html.title.background.mouseover: white

=item rss2html.title.font.family: Helvetica, Arial, sansserif

=item rss2html.title.font.size: 12

=item rss2html.title.font.style: bold

=item rss2html.item.foreground: black

=item rss2html.item.background: white

=item rss2html.item.foreground.mouseover: black

=item rss2html.item.background.mouseover: white

=item rss2html.item.font.family: Times, serif

=item rss2html.item.font.size: 10

=item rss2html.item.font.style: italic

=item rss2html.disable_text_escaping: yes

=back

=head1 REQUIRED MODULES

C<XML::Sablotron>

=head1 SEE ALSO

C<http://www.webreference.com/xml/column16/>

=head1 AUTHORS

Justin Mason <jm@jmason.org>

XSL stylesheet by Michael Claßen of WebReference.com, updated with RSS 1.0
support with help from Eric van der Vlist's stylesheet on 4xt.org.

The conversion stylesheets, to handle RSS 0.9 and 0.91 feeds, are Copyright (c)
2000 Eric van der Vlist and 4xt.org (http://4xt.org)

=cut

-->
<!-- --------------------------------------------------------------------- -->

<template name="rss2html.box.foreground">black</template> 
<template name="rss2html.box.background">white</template> 
<template name="rss2html.box.border">1</template> 
<template name="rss2html.title.foreground">black</template> 
<template name="rss2html.title.background">white</template> 
<template name="rss2html.title.foreground.mouseover">black</template> 
<template name="rss2html.title.background.mouseover">white</template> 
<template name="rss2html.title.font.family">Helvetica, Arial, sansserif</template> 
<template name="rss2html.title.font.size">12</template> 
<template name="rss2html.title.font.style">bold</template> 
<template name="rss2html.item.foreground">black</template> 
<template name="rss2html.item.background">white</template> 
<template name="rss2html.item.foreground.mouseover">black</template> 
<template name="rss2html.item.background.mouseover">white</template> 
<template name="rss2html.item.font.family">Times, serif</template> 
<template name="rss2html.item.font.size">10</template> 
<template name="rss2html.item.font.style">italic</template> 
<template name="rss2html.disable_text_escaping">yes</template> 

<!-- --------------------------------------------------------------------- -->

<template name="rss2html.css_text" format="text/html">
  .box { color: ${rss2html.box.foreground};
	 background-color: ${rss2html.box.background};
  }
  A.rsstitle:link { color: ${rss2html.title.foreground};
	background-color: ${rss2html.title.background};
	font-family: ${rss2html.title.font.family};
	font-size: ${rss2html.title.font.size};
	font-weight: ${rss2html.title.font.style};
  }
  A.rssitem:link { color: ${rss2html.item.foreground};
	background-color: ${rss2html.item.background};
	font-family: ${rss2html.item.font.family};
	font-size: ${rss2html.item.font.size};
	font-weight: ${rss2html.item.font.style};
  }
  A.rsstitle:hover { color: ${rss2html.title.foreground.mouseover}; }
  A.rssitem:hover { color: ${rss2html.item.foreground.mouseover}; }
</template>

<!-- --------------------------------------------------------------------- -->

<template name="rss2html.xsl" format="text/xml">
<?xml version="1.0" encoding='iso-8859-1'?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rss="http://purl.org/rss/1.0/"
	version="1.0">

  <xsl:output method="html" omit-xml-declaration="yes" />

  <xsl:param name="box.foreground">${rss2html.box.foreground}</xsl:param>
  <xsl:param name="box.background">${rss2html.box.background}</xsl:param>
  <xsl:param name="box.border">${rss2html.box.border}</xsl:param>
  <xsl:param name="title.foreground">${rss2html.title.foreground}</xsl:param>
  <xsl:param name="title.background">${rss2html.title.background}</xsl:param>
  <xsl:param name="title.foreground.mouseover">${rss2html.title.foreground.mouseover}</xsl:param>
  <xsl:param name="title.background.mouseover">${rss2html.title.background.mouseover}</xsl:param>
  <xsl:param name="title.font.family">${rss2html.title.font.family}</xsl:param>
  <xsl:param name="title.font.size">${rss2html.title.font.size}</xsl:param>
  <xsl:param name="title.font.style">${rss2html.title.font.style}</xsl:param>
  <xsl:param name="item.foreground">${rss2html.item.foreground}</xsl:param>
  <xsl:param name="item.background">${rss2html.item.background}</xsl:param>
  <xsl:param name="item.foreground.mouseover">${rss2html.item.foreground.mouseover}</xsl:param>
  <xsl:param name="item.background.mouseover">${rss2html.item.background.mouseover}</xsl:param>
  <xsl:param name="item.font.family">${rss2html.item.font.family}</xsl:param>
  <xsl:param name="item.font.size">${rss2html.item.font.size}</xsl:param>
  <xsl:param name="item.font.style">${rss2html.item.font.style}</xsl:param>
  
  <xsl:template match="/">
    <TABLE CLASS="box"> <xsl:attribute name="BORDER">
			  <xsl:value-of select="$box.border"/>
			</xsl:attribute>
      <xsl:apply-templates select="rdf:RDF/*" />
    </TABLE>
  </xsl:template>

  <xsl:template match="rss:channel">
    <TR><TH>
      <A CLASS="rsstitle" HREF="{rss:link}" target="_new">
	<xsl:value-of select="rss:title"
      				disable-output-escaping="${rss2html.disable_text_escaping}" />
      </A>
    </TH></TR>
  </xsl:template>

  <xsl:template match="rss:item">
    <TR><TD>
      <A CLASS="rssitem" HREF="{rss:link}" target="_new">
	<xsl:value-of select="rss:title"
      				disable-output-escaping="${rss2html.disable_text_escaping}" />
      </A>
      <P CLASS="description">
	<xsl:value-of select="rss:description"
      				disable-output-escaping="${rss2html.disable_text_escaping}" />
      </P>
    </TD></TR>
  </xsl:template>

  <xsl:template match="text()"/>

</xsl:stylesheet>
</template>

<!-- --------------------------------------------------------------------- -->

<template name="rss09-to-10.xsl" format="text/xml">
<?xml version="1.0" encoding='iso-8859-1'?>

<!--
Copyright (c) 2000 Eric van der Vlist
                   4xt.org (http://4xt.org)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

The name of the authors when specified in the source files shall be 
kept unmodified.

THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL 4XT.ORG BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:rss09="http://my.netscape.com/rdf/simple/0.9/"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
	xmlns="http://purl.org/rss/1.0/" 
	version="1.0">

  <xsl:output method="xml" omit-xml-declaration="no" />

<xsl:param name="channel" select="normalize-space(/rdf:RDF/rss09:channel/rss09:link)"/>

<xsl:template match="/rdf:RDF">
	<xsl:comment>
		Translated from RSS 0.9 by the RSS stylesheets release 0.01
	</xsl:comment>
	<rdf:RDF>
		<xsl:apply-templates select="rss09:channel"  mode="09"/>
		<xsl:apply-templates select="rss09:image"  mode="09"/>
		<xsl:apply-templates select="rss09:item" mode="09"/>
		<xsl:apply-templates select="rss09:textinput"  mode="09"/>
	</rdf:RDF>
</xsl:template>

<xsl:template match="rss09:channel" mode="09">
	<channel rdf:about="{$channel}">
		<xsl:apply-templates select="rss09:title" mode="09"/>
		<xsl:apply-templates select="rss09:link" mode="09"/>
		<xsl:apply-templates select="rss09:description" mode="09"/>
		<xsl:apply-templates select="../rss09:image" mode="09channel"/>
		<items>
			<rdf:Seq>
				<xsl:apply-templates select="../rss09:item" mode="09channel"/>
			</rdf:Seq>
		</items>
		<xsl:apply-templates select="../rss09:textinput" mode="09channel"/>
	</channel>
</xsl:template>

<xsl:template match="rss09:image" mode="09">
	<image rdf:about="{normalize-space(rss09:url)}">
		<xsl:apply-templates mode="09"/>
	</image>
</xsl:template>

<xsl:template match="rss09:textinput" mode="09">
	<textinput rdf:about="{normalize-space(rss09:link)}">
		<xsl:apply-templates mode="09"/>
	</textinput>
</xsl:template>

<xsl:template match="rss09:item" mode="09channel">
	<rdf:li	resource="{normalize-space(rss09:link)}"/>
</xsl:template>

<xsl:template match="rss09:image" mode="09channel">
	<image	rdf:resource="{normalize-space(rss09:url)}"/>
</xsl:template>

<xsl:template match="rss09:textinput" mode="09channel">
	<textinput	rdf:resource="{normalize-space(rss09:link)}"/>
</xsl:template>

<xsl:template match="rss09:item" mode="09">
	<item
		rdf:about="{normalize-space(rss09:link)}">
		<xsl:apply-templates mode="09"/>
	</item>
</xsl:template>

<xsl:template match="rss09:*" mode="09">
	<xsl:element name="{name()}">
		<xsl:apply-templates mode="09"/>
	</xsl:element>
</xsl:template>
</xsl:stylesheet>
</template>

<!-- --------------------------------------------------------------------- -->

<template name="rss091-to-10.xsl" format="text/xml">
<?xml version="1.0" encoding='iso-8859-1'?>

<!--
Copyright (c) 2000 Eric van der Vlist
                   4xt.org (http://4xt.org)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

The name of the authors when specified in the source files shall be 
kept unmodified.

THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL 4XT.ORG BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:rss091="http://purl.org/rss/1.0/modules/rss091#"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
	xmlns="http://purl.org/rss/1.0/" 
	version="1.0">
  <xsl:output method="xml" omit-xml-declaration="no" />

<xsl:param name="channel091" select="normalize-space(/rss/channel/link)"/>

<xsl:template match="/rss">
	<xsl:comment>
		Translated from RSS 0.91 by the RSS stylesheets release 0.01
	</xsl:comment>
	<rdf:RDF>
		<channel rdf:about="{$channel091}">
			<xsl:apply-templates select="channel/title"	mode="091"/>
			<xsl:apply-templates select="channel/link"	mode="091"/>
			<xsl:apply-templates select="channel/description"	mode="091"/>
			<xsl:apply-templates select="channel/image" mode="091channel"/>
			<items>
				<rdf:Seq>
					<xsl:apply-templates select="channel/item" mode="091channel"/>
				</rdf:Seq>
			</items>
			<xsl:apply-templates select="channel/textinput" mode="091channel"/>
			<xsl:apply-templates select="channel/language"	mode="091"/>
			<xsl:apply-templates select="channel/rating"	mode="091"/>
			<xsl:apply-templates select="channel/managingEditor"	mode="091"/>
			<xsl:apply-templates select="channel/webmaster"	mode="091"/>
			<xsl:apply-templates select="channel/pubDate"	mode="091"/>
			<xsl:apply-templates select="channel/lastBuildDate"	mode="091"/>
			<xsl:apply-templates select="channel/copyright"	mode="091"/>
			<xsl:apply-templates select="channel/skipHours"	mode="091"/>
			<xsl:apply-templates select="channel/skipDays"	mode="091"/>
		</channel>
		<xsl:apply-templates select="channel/image" mode="091"/>
		<xsl:apply-templates select="channel/item" mode="091"/>
		<xsl:apply-templates select="channel/textinput" mode="091"/>
	</rdf:RDF>
</xsl:template>

<xsl:template match="
 /rss/channel/title|
 /rss/channel/link|
 /rss/channel/description|
 /rss/channel/item/title|
 /rss/channel/item/link|
 /rss/channel/item/description|
 /rss/channel/image/title|
 /rss/channel/image/url|
 /rss/channel/image/link|
 /rss/channel/textinput/title|
 /rss/channel/textinput/description|
 /rss/channel/textinput/name|
 /rss/channel/textinput/link
 "
priority="1" mode="091">
	<xsl:element name="{name()}">
		<xsl:apply-templates mode="091"/>
	</xsl:element>
</xsl:template>

<xsl:template match="/rss/channel/item" mode="091channel">
	<rdf:li resource="{normalize-space(link)}"/>
</xsl:template>

<xsl:template match="/rss/channel/image" mode="091channel">
	<image rdf:resource="{normalize-space(url)}"/>
</xsl:template>

<xsl:template match="/rss/channel/textinput" mode="091channel">
	<textinput rdf:resource="{normalize-space(link)}"/>
</xsl:template>

<xsl:template match="/rss/channel/item" mode="091">
	<item
		rdf:about="{normalize-space(link)}">
		<xsl:apply-templates mode="091"/>
	</item>
</xsl:template>

<xsl:template match="/rss/channel/image" mode="091">
	<image rdf:about="{normalize-space(url)}">
		<xsl:apply-templates mode="091"/>
	</image>

</xsl:template>

<xsl:template match="/rss/channel/textinput" mode="091">
	<textinput rdf:about="{normalize-space(link)}">
		<xsl:apply-templates mode="091"/>
	</textinput>

</xsl:template>

<!-- The following template could be uncommented to include a rdf:parseType
attribute. -->

<!--
<xsl:template match="image/*|channel/*|item/*|textinput/*"  mode="091">
	<xsl:element name="rss091:{name()}">
		<xsl:attribute name="rdf:parseType">Literal</xsl:attribute>
		<xsl:apply-templates mode="091"/>
	</xsl:element>
</xsl:template>
-->

<xsl:template match="*" mode="091">
	<xsl:element name="rss091:{name()}">
		<xsl:apply-templates mode="091"/>
	</xsl:element>
</xsl:template>

</xsl:stylesheet>
</template>

<{perl

  $self->define_empty_preformat_tag ("rssbox", \&handle_rssbox_tag, qw(rss));

  sub handle_rssbox_tag {
    my ($tagname, $attrs, $text, $self) = @_;

    my $rssname = $attrs->{rss};
    my $rdf = $self->get_content ($rssname);

    # Convert older RSS to RSS 1.0. A total kludge.  I'd appreciate if someone
    # who knows how to do this in XSL, instead, could provide tips!! (TODO)
    #
    if ($rdf =~ m,http://my.netscape.com/publish/formats/rss-0.91.dtd,)
    {
      $rdf = $self->expand (qq{<xsl xslname="rss091-to-10.xsl"
      			xmlname="$rssname" /> });
      $rssname .= "_converted";
      $self->set_content ($rssname, $rdf);
    }
    elsif ($rdf =~ m,http://my.netscape.com/rdf/simple/0.9/,)
    {
      $rdf = $self->expand (qq{<xsl xslname="rss09-to-10.xsl"
      			xmlname="$rssname" /> });
      $rssname .= "_converted";
      $self->set_content ($rssname, $rdf);
    }

    return qq{<xsl xslname="rss2html.xsl" xmlname="$rssname" /> };
  }

  '';

}>
