aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/hnb.pm
blob: bd2177a06a1217de57700c40de641e9794133b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl
# hnb markup
# Licensed under the GPL v2 or greater
# Copyright (C) 2008 by Axel Beckert <abe@deuxchevaux.org>
# 
# TODO: Make a switch to allow both HTML export routines of hnb 
# (`export_html` and `export_htmlcss`) to be used.

package IkiWiki::Plugin::hnb;

use warnings;
use strict;
use IkiWiki 3.00;
use File::Temp qw(:mktemp);

sub import {
	hook(type => "getsetup", id => "hnb", call => \&getsetup);
	hook(type => "htmlize", id => "hnb", call => \&htmlize);
}

sub getsetup () {
	return
		plugin => {
			safe => 1,
			rebuild => 1, # format plugin
		},
}

sub htmlize (@) {
	my %params = @_;

	# hnb outputs version number etc. every time to STDOUT, so
	# using files makes it easier to seprarate.

	my $tmpin  = mkstemp( "/tmp/ikiwiki-hnbin.XXXXXXXXXX"  );
	my $tmpout = mkstemp( "/tmp/ikiwiki-hnbout.XXXXXXXXXX" );

	open(TMP, '>', $tmpin) or die "Can't write to $tmpin: $!";
	print TMP $params{content};
	close TMP;

	system("hnb '$tmpin' 'go root' 'export_html $tmpout' > /dev/null");
	unlink $tmpin;

	open(TMP, '<', $tmpout) or die "Can't read from $tmpout: $!";
	local $/;
	my $ret = <TMP>;
	close TMP;
	unlink $tmpout;

	$ret =~ s/.*<body>//si;
	$ret =~ s/<body>.*//si;

	return $ret;
}

1;