initial add
[ikiwiki] / IkiWiki / Plugin / hnb.pm
1 #!/usr/bin/perl
2 # hnb markup
3 package IkiWiki::Plugin::hnb;
4
5 # Copyright (C) 2008 by Axel Beckert <abe@deuxchevaux.org>
6 #
7 #  This program is free software; you can redistribute it and/or
8 #  modify it under the terms of the GNU General Public License
9 #  as published by the Free Software Foundation; either version 2
10 #  of the License, or (at your option) any later version.
11 #
12 #  This program is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with this program; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #
21 # You can reach the author by snail-mail at the following address:
22 #
23 #  Axel Beckert
24 #  Kuerbergstrasse 20
25 #  8049 Zurich, Switzerland
26 #
27 # Version History:
28 #
29 # 2008-03-10 / 0.01:   Initial release
30 # 2008-05-08 / 0.01.1: License, version and version history added
31 # 2008-05-26 / 0.02:   Using content instead of page, s/mktemp/File::Temp/
32
33 my $VERSION ='0.02';
34
35 use warnings;
36 use strict;
37 use IkiWiki 2.00;
38 use File::Temp qw(:mktemp);
39
40 sub import {
41     hook(type => "htmlize", id => "hnb", call => \&htmlize);
42 }
43
44 sub htmlize (@) {
45     my %params = @_;
46
47     # hnb does output version number etc. every time to STDOUT, so
48     # using files makes it easier to seprarate.
49
50     my ($fhi, $tmpin)  = mkstemp( "/tmp/ikiwiki-hnbin.XXXXXXXXXX"  );
51     my ($fho, $tmpout) = mkstemp( "/tmp/ikiwiki-hnbout.XXXXXXXXXX" );
52
53     open(TMP, '>', $tmpin) or die "Can't write to $tmpin: $!";
54     print TMP $params{content};
55     close TMP;
56
57     system("hnb '$tmpin' 'go root' 'export_html $tmpout' > /dev/null");
58     # Nicer, but too much output
59     #system('hnb', $tmpin, 'go root', "export_html $tmpout");
60     unlink $tmpin;
61
62     open(TMP, '<', $tmpout) or die "Can't read from $tmpout: $!";
63     local $/;
64     my $ret = <TMP>;
65     close TMP;
66     unlink $tmpout;
67
68     $ret =~ s/.*<body>//si;
69     $ret =~ s/<body>.*//si;
70
71     return $ret;
72 }
73
74 1;