aboutsummaryrefslogtreecommitdiff
path: root/doc/plugins/contrib/bibtex2html.mdwn
blob: 5432ea3cc79b85b04d5ac52e4920bb72d15c8ceb (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
61
[[!template id=plugin name=bibtex2html author="[[anarcat]]"]]

Trivial plugin to implement [[todo/BibTeX]] support simply using [bibtex2html](https://www.lri.fr/~filliatr/bibtex2html/). It only takes a `bib` file as an argument and dumps whatever bibtex2html returns for it, so it shows *all* the entries, something that is not really possible with the existing [[bibtex]] plugin, as that one requires you to explicitly state every citation you want to show.

It is hopefully secure enough, but I have still marked it as unsafe because I am worried about parameter expansion in bibtex calls from bibtex2html that wouldn't escape those characters properly. The pipeline is called safely, but certain `-flags` could be maliciously added to the filenames somehow.

The plugin is generic enough that I wonder if there's a level of abstraction that exists here that I have missed. If not it would be interesting to add. Update: that tool is the [[compile]] plugin, darn it. I guess the next step here is to review that plugin and figure out how to do exactly this with just the `compile` configuration. Yet this works for me now so I'm unlikely to do that in the short term.

[[!format perl """
#!/usr/bin/perl
package IkiWiki::Plugin::bibtex2html;
use warnings;
use strict;
use IkiWiki 3.00;
use open qw{:utf8 :std};

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

sub getsetup () {
	return
		plugin => {
			safe => 0,
			rebuild => undef,
                        section => "core",
		},
}

sub bibtex2html {
    my %params=@_;

    # check the files exist
    my $file=shift;
    if (! defined $file) {
        error sprintf(gettext('file parameter is required'));
    }
    my $near = bestlink($params{page}, $file);
    if (! $near) {
        error sprintf(gettext('cannot find bestlink for "%s"'), $file);
    }
    if (! exists $pagesources{$near}) {
        error sprintf(gettext('cannot find file "%s"'), $near);
    }
    add_depends($params{page}, $near);
    $near = srcfile($near);
    my @bibtex_cmd = (qw[bibtex2html -noheader -nofooter -nobibsource -nodoc -q -o -], $near);
    
    open(PIPE, "-|", @bibtex_cmd)
        || error "can't open pipe to @bibtex_cmd: $!";
    my $html = join("", <PIPE>);
    close PIPE;
    debug "ran @bibtex_cmd: $html";
    return "<div class=\"bibtex2html\">$html<!-- The above was generated with @bibtex_cmd--></div>";
}

1;
"""]]

Obviously, this should be implemented through Text::Bibtex as forking is expensive. Yet I haven't found a way to do what this plugin does with the existing [[bibtex]] module. [[bibtex]] could of course be extended and then render this plugin obsolete, but I have found it simpler to just reuse an existing working rendered than rewrite my own in Perl. --[[anarcat]]