]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - doc/plugins/contrib/bibtex2html.mdwn
oops, forgot some changes
[git.ikiwiki.info.git] / doc / plugins / contrib / bibtex2html.mdwn
1 [[!template id=plugin name=bibtex2html author="[[anarcat]]"]]
3 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.
5 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.
7 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.
9 [[!format perl """
10 #!/usr/bin/perl
11 package IkiWiki::Plugin::bibtex2html;
12 use warnings;
13 use strict;
14 use IkiWiki 3.00;
15 use open qw{:utf8 :std};
17 sub import {
18         hook(type => "getsetup", id => "bibtex2html",  call => \&getsetup);
19         hook(type => "preprocess", id => "bibtex2html", call => \&bibtex2html);
20 }
22 sub getsetup () {
23         return
24                 plugin => {
25                         safe => 0,
26                         rebuild => undef,
27                         section => "core",
28                 },
29 }
31 sub bibtex2html {
32     my %params=@_;
34     # check the files exist
35     my $file=shift;
36     if (! defined $file) {
37         error sprintf(gettext('file parameter is required'));
38     }
39     my $near = bestlink($params{page}, $file);
40     if (! $near) {
41         error sprintf(gettext('cannot find bestlink for "%s"'), $file);
42     }
43     if (! exists $pagesources{$near}) {
44         error sprintf(gettext('cannot find file "%s"'), $near);
45     }
46     add_depends($params{page}, $near);
47     $near = srcfile($near);
48     my @bibtex_cmd = (qw[bibtex2html -noheader -nofooter -nobibsource -nodoc -q -o -], $near);
49     
50     open(PIPE, "-|", @bibtex_cmd)
51         || error "can't open pipe to @bibtex_cmd: $!";
52     my $html = join("", <PIPE>);
53     close PIPE;
54     debug "ran @bibtex_cmd: $html";
55     return "<div class=\"bibtex2html\">$html<!-- The above was generated with @bibtex_cmd--></div>";
56 }
58 1;
59 """]]
61 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]]