PNG inlining in html files exported from org

Sitemap

---> misc
| ---> 2016
| ---> 2015
| ---> 2014
| ---> 2013
| ---> 2012
`--> Agenda

I use both Rstudio and org. Rstudio is really neat for teaching students in particular because of the easy integration with knitr and rpubs. In particular something really neat is that when exporting to rpubs, all images generated by knitr are inlined in the html file (with base64 encoding). Hence there is not the burden of file management as I have when use with org. Although, it is not a problem when publishing on my webpage, if I ever want to send a document to a colleague through email, then it becomes extremely painful. So here is the solution. A simple perl filter that post-process the html generated by org-mode to inline all png files. The resulting html file is larger but is self-contained.

#!/usr/bin/perl -w
use strict;

my($line);
while(defined($line=<STDIN>)) {
     if($line =~ /^(.*)<img *src="([^"]*)"(.*)$/g) {
        my($pre,$image,$post) = ($1,$2,$3);
        print "$image\n";
        my $base64=`base64 -w 0 $image`;
        my $format=$image;
        $format =~ s/.*\.//g;
        print $pre."<img src=\"data:image/$format;base64,".$base64.'"'.$post;
     } else {
        print $line;
     }
}