changelog-to-rss 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #! /usr/bin/perl
  2. # Copyright (C) 2005 Alex Schroeder <alex@emacswiki.org>
  3. #
  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.
  8. #
  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.
  13. #
  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
  19. use CGI;
  20. use CGI::Carp qw(fatalsToBrowser);
  21. use LWP::UserAgent;
  22. use encoding 'utf8';
  23. use POSIX;
  24. my $q = new CGI;
  25. my $url = $q->param('url');
  26. my $pattern = $q->param('pattern');
  27. if (not $url) {
  28. print $q->header(),
  29. $q->start_html('ChangeLog to RSS'),
  30. $q->h1('ChangeLog to RSS'),
  31. $q->p('Translates ChangeLog output to RSS 2.0.'),
  32. $q->p(q{$Id: changelog-to-rss,v 1.17 2005/01/07 13:09:27 as Exp $}),
  33. $q->start_form(-method=>'GET'),
  34. $q->p('ChangeLog URL: ',
  35. $q->textfield('url', '', 70)),
  36. $q->p('Link pattern if available, use %s for the filename: ',
  37. $q->textfield('pattern', '', 70)),
  38. $q->p('Limit number of entries returned: ',
  39. $q->textfield('limit', '15', 5)),
  40. $q->p($q->submit()),
  41. $q->end_form(),
  42. $q->end_html();
  43. exit;
  44. }
  45. print $q->header(-type=>'application/rss+xml; charset=UTF-8');
  46. my $rss = qq{<?xml version="1.0" encoding="UTF-8"?>
  47. <rss version="2.0">
  48. <channel>
  49. <title>ChangeLog</title>
  50. <description>RSS feed automatically extracted from a ChangeLog file.</description>
  51. <link>$url</link>
  52. };
  53. my $ua = new LWP::UserAgent;
  54. my $response = $ua->get($url);
  55. die $response->status_line unless $response->is_success;
  56. my $data = $response->content;
  57. my $limit = $q->param('limit') || 15;
  58. my ($date, $author, $file, $log, $count);
  59. foreach my $line (split(/\n/, $data)) {
  60. # print "----\n$line\n----\n";
  61. if ($line =~ m/^(\d\d\d\d-\d\d-\d\d)\s*(.*)/) {
  62. output($date, $author, $file, $log);
  63. $date = $1;
  64. $author = $2;
  65. $file = '';
  66. $log = '';
  67. } elsif ($line =~ m|^\t\* ([a-zA-Z0-9./-]+)(.*)|) {
  68. last if ++$count > $limit;
  69. output($date, $author, $file, $log);
  70. $file = $1;
  71. $log = $2;
  72. } else {
  73. $log .= "\n" . $line;
  74. }
  75. }
  76. output($date, $author, $file, $log) if $file or $log;
  77. $rss .= q{
  78. </channel>
  79. </rss>
  80. };
  81. print $rss;
  82. sub output {
  83. my ($date, $author, $file, $log) = @_;
  84. return unless $file;
  85. $date = to_date($date);
  86. $author = quote_html($author);
  87. $log =~ s|^\t||mg; # strip leading tabs on every line
  88. $log =~ s|\)\n\(|, |g; # fix weird continuation groups
  89. # add linebreaks and highlighting for parentheses
  90. $log =~ s|\((.*?)\):|</span><span class="chunk"><br />(<strong>$1</strong>):|g;
  91. $log =~ s|^ *<br />||; # strip first linebreak, if there is one
  92. $log = quote_html($q->span({-class=>"chunk"}, $log));
  93. my $link = $pattern;
  94. $link =~ s/\%s/$file/g or $link .= $file;
  95. $rss .= "<item>\n";
  96. $rss .= "<author>$author</author>\n" if $author;
  97. $rss .= "<pubDate>$date</pubDate>\n" if $date;
  98. $rss .= "<title>$file</title>\n" if $file;
  99. $rss .= "<link>$link</link>\n" if $link;
  100. $rss .= "<description>$log</description>\n" if $log;
  101. $rss .= "</item>\n\n";
  102. }
  103. sub to_date {
  104. $_ = shift;
  105. my ($year, $month, $day) = split(/-/);
  106. # Wed, 02 Oct 2002 00:00:00 GMT
  107. return strftime("%a, %d %b %Y 00:00:00 GMT",
  108. 0, 0, 0, $day, $month - 1, $year - 1900);
  109. }
  110. sub quote_html {
  111. $_ = shift;
  112. s/&/&amp;/g;
  113. s/</&lt;/g;
  114. s/>/&gt;/g;
  115. s/&amp;([#a-zA-Z0-9]+);/&$1;/g; # Allow character references
  116. return $_;
  117. }