]> sipb.mit.edu Git - ikiwiki.git/blob - doc/plugins/contrib/mandoc.mdwn
t3h c0d35
[ikiwiki.git] / doc / plugins / contrib / mandoc.mdwn
1 [[!template id=plugin name=mandoc author="[[schmonz]]"]]
2 [[!tag type/format]]
3
4 This plugin lets ikiwiki convert Unix man pages to HTML. It uses
5 [mdocml](http://mdocml.bsd.lv/) for the conversion, and postprocesses
6 xrefs into hyperlinks.
7
8 Sample output: <http://wiki.netbsd.org/users/schmonz/tunefs.8/>
9
10 -----
11
12
13     #!/usr/bin/perl
14     package IkiWiki::Plugin::mandoc;
15     
16     use warnings;
17     use strict;
18     use IkiWiki 3.00;
19     use Encode;
20     use IPC::Open2;
21     
22     sub import {
23         hook(type => "getsetup", id => "mandoc", call => \&getsetup);
24         hook(type => "htmlize", id => $_, call => \&htmlize, keepextension => 1)
25                 foreach ('man', 1..9);
26     }
27     
28     sub getsetup () {
29         return
30                 plugin => {
31                         safe => 1,
32                         rebuild => 1, # format plugin
33                         section => "format",
34                 },
35     }
36     
37     sub htmlize (@) {
38         my %params=@_;
39         my $content = decode_utf8(encode_utf8($params{content}));
40     
41         return $content if $@;
42     
43         my $pid = open2(*MANDOCOUT, *MANDOCIN, 'mandoc', '-Thtml');
44         binmode($_, ':utf8') foreach (*MANDOCOUT, *MANDOCIN);
45     
46         print MANDOCIN $content;
47         close MANDOCIN;
48         my @html_output = <MANDOCOUT>;
49         close MANDOCOUT;
50         waitpid $pid, 0;
51     
52         my $html = join('', @html_output);
53         my $link_prefix = $config{usedirs} ? '../' : '';
54         my $link_suffix = $config{usedirs} ? '/' : '';
55         $html =~ s|<a class="link-man">(.+?)\((.)\)</a>|<a class="link-man" href="$link_prefix$1.$2$link_suffix">$1($2)</a>|g;
56     
57         return $html;
58     }
59     
60     1