2010/03/20

Media RSS in blosxom

I often post photos on my blog. On the html page they are displayed big, and smaller in the RSS feed. Today I decided to also add them in the rss using the Media RSS extension. Here's how I did it using blosxom (maybe other people have better ways to do it, you can let me know in the comments).

On my server I store the images in different sizes :

When writting a blog post, I don't write the html code to display an image, instead I only write on a line IMG=[number]. The following blosxom plugin will replace those lines with the large size version in the html output, medium version in the rss output, and will add the original size versions using RSS Media extension in the $bigimages::medias variable:

$ cat plugins/bigimages
#!/usr/bin/perl

package bigimages;

sub start {
  1;
}

my $imgpath = "https://boklm.eu/im/";
our $medias;

sub story {
  my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
  if ($blosxom::flavour eq "html") {
      $$body_ref =~ s/^IMG=([0-9]+)$/<p><a href="$imgpath\1.html"><img src="$imgpath\1-l.jpg" alt="\1" \/><\/a><\/p>/msg;
  }

  if ($blosxom::flavour eq "rss") {
      $medias = '';
      my $mlist = '';
      while ($$body_ref =~ m/^IMG=([0-9]+)$/msg) {
	 $mlist .= "<media:content type=\"image/jpeg\" url=\"$imgpath$1-o.jpg\"/>\n";
      }
      if ($mlist) {
	  $medias = "<media:group>\n"
	  	. "<media:credit role=\"author\">Nicolas Vigier - https://boklm.eu/</media:credit>\n"
		. "<creativeCommons:license>http://creativecommons.org/licenses/by-sa/2.5/deed.en</creativeCommons:license>"
		. $mlist
		. "</media:group>\n";
      }
      $$body_ref =~ s/^IMG=([0-9]+)$/<a href="$imgpath\1.html"><img src="$imgpath\1-m.jpg" alt="\1" \/><\/a>/msg;
  }

  1;
}

1;

Of course if you want to use it, you'll have to update URLs, credits, license, etc ... And maybe add any other informations you want to add about the pictures such as title, keywords, categories, thumbnail.

The plugins/bigimages script will create the $bigimages::medias variable, now you need to use it in story.rss. Example :

$ cat story.rss
<item>
    <title>$title</title>
    <pubDate>$dw, $da $mo $yr $ti:00 $utc_offset</pubDate>
    <link>$url$path/$fn.html</link>
    <guid isPermaLink="false">$url$path/$fn.html</guid>
    <category>$path</category>
    <description>$body</description>
    $bigimages::medias
</item>

You'll also need to update head.rss to add xmlns:media (and xmlns:creativeCommons if you use it). Here's mine :

$ cat head.rss
<?xml version="1.0"?>
<!-- name="generator" content="blosxom/$version" -->

<rss version="2.0"
	xmlns:media="http://search.yahoo.com/mrss/"
	xmlns:creativeCommons="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html"
	>
  <channel>
    <title>$blog_title $path_info_da $path_info_mo $path_info_yr</title>
    <link>$url</link>
    <description>$blog_description</description>
    <language>$blog_language</language>

Blosxom doesn't support a lot of things by default, but it's easy to write plugins to do what you want.

[/log/tech/blosxom] permanent link