aboutsummaryrefslogtreecommitdiff
path: root/doc/plugins/contrib/mandoc.mdwn
blob: e4ff96d08d525ec51efd67b98a571bd151fbb418 (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
58
59
60
[[!template id=plugin name=mandoc author="[[schmonz]]"]]
[[!tag type/format]]

This plugin lets ikiwiki convert Unix man pages to HTML. It uses
[mdocml](http://mdocml.bsd.lv/) for the conversion, and postprocesses
xrefs into hyperlinks.

Sample output: <http://wiki.netbsd.org/users/schmonz/tunefs.8/>

-----


    #!/usr/bin/perl
    package IkiWiki::Plugin::mandoc;
    
    use warnings;
    use strict;
    use IkiWiki 3.00;
    use Encode;
    use IPC::Open2;
    
    sub import {
    	hook(type => "getsetup", id => "mandoc", call => \&getsetup);
    	hook(type => "htmlize", id => $_, call => \&htmlize, keepextension => 1)
    		foreach ('man', 1..9);
    }
    
    sub getsetup () {
    	return
    		plugin => {
    			safe => 1,
    			rebuild => 1, # format plugin
    			section => "format",
    		},
    }
    
    sub htmlize (@) {
    	my %params=@_;
    	my $content = decode_utf8(encode_utf8($params{content}));
    
    	return $content if $@;
    
    	my $pid = open2(*MANDOCOUT, *MANDOCIN, 'mandoc', '-Thtml');
    	binmode($_, ':utf8') foreach (*MANDOCOUT, *MANDOCIN);
    
    	print MANDOCIN $content;
    	close MANDOCIN;
    	my @html_output = <MANDOCOUT>;
    	close MANDOCOUT;
    	waitpid $pid, 0;
    
    	my $html = join('', @html_output);
    	my $link_prefix = $config{usedirs} ? '../' : '';
    	my $link_suffix = $config{usedirs} ? '/' : '';
    	$html =~ s|<a class="link-man">(.+?)\((.)\)</a>|<a class="link-man" href="$link_prefix$1.$2$link_suffix">$1($2)</a>|g;
    
    	return $html;
    }
    
    1