#webliberty::Rss.pm (2006/02/18)
#Copyright(C) 2002-2006 Knight, All rights reserved.

package webliberty::Rss;

use strict;

### コンストラクタ
sub new {
	my $class = shift;

	my $self = {
		rss_data    => undef,
		rdf_about   => undef,
		link        => undef,
		description => undef,
		items       => undef
	};
	bless $self, $class;

	return $self;
}

### RSSデータ作成
sub create_rss {
	my $self = shift;
	my %args = @_;

	$self->{rdf_about}   = $args{'rdf_about'};
	$self->{title}       = $args{'title'};
	$self->{link}        = $args{'link'};
	$self->{description} = $args{'description'};
	$self->{date}        = $args{'date'};
	$self->{items}       = $args{'items'};

	$self->{rdf_about} =~ s/~/%7E/g;
	$self->{link}      =~ s/~/%7E/g;

	if ($self->{date} =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
		$self->{date} = "$1-$2-$3" . "T$4:$5+09:00";
	}

	$self->{rss_data} = <<"_RSS_";
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xml:lang="ja">
	<channel rdf:about="$self->{rdf_about}">
		<title>$self->{title}</title>
		<link>$self->{link}</link>
		<description>$self->{description}</description>
		<dc:language>ja</dc:language>
		<dc:date>$self->{date}</dc:date>
		<items>
			<rdf:Seq>
_RSS_

	foreach (split(/\n/, $self->{items})) {
		my($title, $link, $description, $date) = split(/\t/);

		$link =~ s/~/%7E/g;

		$self->{rss_data} .= <<"_RSS_";
			<rdf:li rdf:resource="$link" />
_RSS_
	}

	$self->{rss_data} .= <<"_RSS_";
			</rdf:Seq>
		</items>
	</channel>
_RSS_

	foreach (split(/\n/, $self->{items})) {
		my($title, $link, $description, $date) = split(/\t/);

		$link =~ s/~/%7E/g;

		$description = (split(/<br \/>/, $description))[0];
		$description =~ s/<[^>]*>//g;
		$description =~ s/&lt;[^(&gt;)]*&gt;//g;
		$description =~ s/&/&amp;/g;

		if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
			$date = "$1-$2-$3" . "T$4:$5+09:00";
		}

		$self->{rss_data} .= <<"_RSS_";
	<item rdf:about="$link">
		<title>$title</title>
		<link>$link</link>
		<description>$description</description>
		<dc:date>$date</dc:date>
	</item>
_RSS_
	}

	$self->{rss_data} .= <<"_RSS_";
</rdf:RDF>
_RSS_

	return $self->{rss_data};
}

1;
