]> git.donarmstrong.com Git - ikiwiki_plugins.git/blob - qrcode.pm
start the R process first
[ikiwiki_plugins.git] / qrcode.pm
1 #!/usr/bin/perl
2 # Ikiwiki qrcode plugin encodes things into qrcode and is released
3 # under the terms of the GPL version 2, or any later version at your
4 # option.
5 # Copyright 2012 by Don Armstrong <don@donarmstrong.com>.
6
7
8 package IkiWiki::Plugin::qrcode;
9
10 =head1 NAME
11
12 qrcode -- embed qr codes in IkiWiki
13
14 =head1 SYNOPSIS
15
16 qrcode embed qr codes in IkiWIki
17
18
19 [[!qrcode code="http://www.donarmstrong.com"]]
20
21
22 =head1 DESCRIPTION
23
24 =head2 Available options
25
26 =over
27
28 =item code
29
30 QR code to generate
31
32 =back
33
34
35 =head1 BUGS
36
37 None currently known
38
39
40 =cut
41
42 use warnings;
43 use strict;
44
45 use Imager::QRCode qw(plot_qrcode);
46
47 use IkiWiki '3.00';
48
49 use Encode qw(decode);
50 use Digest::MD5 qw(md5_hex);
51
52 my $id = "qrcode";
53 sub import {
54     hook(type => "getsetup", id => $id, call => \&getsetup);
55     hook(type => "preprocess", id => $id, call => \&preprocess);
56 }
57
58 my $link = "http://git.donarmstrong.com/?p=ikiwiki_plugins.git;a=blob;f=qrcode.pm;hb=HEAD";
59 sub getsetup {
60     return(plugin => {safe => 1,
61                       rebuild => 1,
62                       section => "misc",
63                       link => $link,
64                       description => "qrcode plugin",
65                      },
66           );
67 }
68
69 sub code_md5 {
70     return(md5_hex(map {decode('utf8',$_)} @_));
71 }
72
73 sub preprocess {
74     my %param = @_;
75
76     if (not exists $param{code}
77         or not defined $param{code}
78         or not length $param{code}) {
79         # default to the link to qrcode for the time being
80         $param{code} = $link;
81     }
82
83     my $image_loc = '';
84     if (exists $param{fig}) {
85         $param{size} = '4' unless exists $param{width} and defined $param{width};
86         $param{margin} = '3' unless exists $param{height} and defined $param{height};
87         for (qw(size margin)) {
88             if ($param{$_} !~ /^\d+$/) {
89                 error("invalid $_; must be an integer: $param{$_}");
90             }
91         }
92         my $md5 = code_md5($param{code},$param{size},$param{margin});
93         $image_loc = "$param{page}/${md5}.png";
94         will_render($param{page},$image_loc);
95         my $url = qq(\n\n<img class="qrcode" src=").urlto($image_loc,$param{destpage}).qq(" alt=").html_escape($param{code}).qq("/>\n);
96         if (-e "$config{destdir}/$image_loc") {
97             return $url;
98         } else {
99             # this makes sure that we can write to the file result
100             writefile($image_loc, $config{destdir}, "");
101             my $img = plot_qrcode(decode('utf8',$param{code}),
102                                   {size => $param{size},
103                                    margin => $param{margin},
104                                    mode  => '8-bit'});
105             $img->write(file => "$config{destdir}/$image_loc");
106         }
107     }
108 }
109
110 1;
111
112
113 __END__
114
115
116
117
118
119
120
121