wiki.pl: Port some fixes from upstream
[Orgmuse.git] / simplify.pl
blob0e99fe945568d21677dc74d6f767c3446eca62b3
1 #! /usr/bin/perl
2 # Copyright (C) 2003, 2004 Alex Schroeder <alex@emacswiki.org>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the
16 # Free Software Foundation, Inc.
17 # 59 Temple Place, Suite 330
18 # Boston, MA 02111-1307 USA
20 use CGI qw/:standard/;
21 use CGI::Carp qw(fatalsToBrowser);
22 use XML::RSS;
23 use LWP::UserAgent;
24 use encoding 'utf8';
26 my $wikins = 'http://purl.org/rss/1.0/modules/wiki/';
27 my $rdfns = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
28 my $output = '0.91';
30 if (not param('url')) {
31 print header(),
32 start_html('RSS Simplification'),
33 h1('RSS Simplification'),
34 p('Translates any RSS feed to Really Simple Syndication ', $output,
35 'It understands ModWiki, and will use wiki:diff as the link,',
36 'and it will add dc:contributor to the description.'),
37 start_form(-method=>'GET'),
38 p('RSS feed: ', textfield('url', '', 70)),
39 p(submit()),
40 end_form(),
41 end_html();
42 exit;
45 print header(-type=>'text/plain; charset=UTF-8');
46 my $rss = new XML::RSS(output=>$output);
47 my $ua = new LWP::UserAgent;
48 my $request = HTTP::Request->new('GET', param('url'));
49 my $response = $ua->request($request);
50 my $data = $response->content;
51 eval {
52 local $SIG{__DIE__} = sub { parse_rss3(); }; # parsing errors -> try RSS 3.0!
53 $rss->parse($data);
54 munge_rss();
57 sub munge_rss {
58 foreach my $i (@{$rss->{items}}) {
59 if ($i->{dc}->{contributor}) {
60 if ($i->{description}) {
61 $i->{description} = $i->{description} . ' -- ' . $i->{dc}->{contributor};
62 } else {
63 $i->{description} = '-- ' .$i->{dc}->{contributor};
66 if ($i->{$wikins}->{diff}) {
67 $i->{link} = $i->{$wikins}->{diff};
70 print $rss->as_string();
73 # perl simplify.pl 'url=http://localhost/cgi-bin/wiki.pl?search=foo%3braw=1'
75 sub parse_rss3 {
76 $rss->add_module(
77 prefix => 'wiki',
78 uri => 'http://purl.org/rss/1.0/modules/wiki/'
80 my @entries = ();
81 foreach my $entry (split(/\n\n+/, $data)) {
82 my %entry = ();
83 while ($entry =~ /(\S+?): (.*?)(?=\n[^\t]|\Z)/sg) {
84 my ($key, $value) = ($1, $2);
85 $value =~ s/\n\t/\n/g;
86 $entry{$key} = $value if $value;
88 push(@entries, \%entry);
90 # the first entry is the channel
91 my %entry = %{shift(@entries)};
92 $rss->channel(%entry);
93 # the rest are items
94 while (@entries) {
95 my %entry = %{shift(@entries)};
96 my %dc = (date => $entry{'last-modified'},
97 contributor => $entry{generator},);
98 my %wiki = (size => $entry{size},);
99 $entry{dc} = %dc;
100 $entry{wiki} = %wiki;
101 for my $key qw(last-modified generator size) { delete $entry{$key}; }
102 $rss->add_item(%entry);
104 print $rss->as_string();