diff options
author | intrigeri <intrigeri@boum.org> | 2010-08-22 11:05:03 +0200 |
---|---|---|
committer | intrigeri <intrigeri@boum.org> | 2010-08-22 11:05:03 +0200 |
commit | 474b6524e0fe4b4bb3402bf6551bf97be7f3ee1f (patch) | |
tree | f303c5391cb168793bdd8d08265d37900eb96330 /IkiWiki/Plugin | |
parent | 0055354e3cce1d1fb8ea2ac0d5c67a4c16a2b0b0 (diff) | |
parent | 20ecef4d024f751481645db3aa68d098e1251b19 (diff) | |
download | ikiwiki-474b6524e0fe4b4bb3402bf6551bf97be7f3ee1f.tar ikiwiki-474b6524e0fe4b4bb3402bf6551bf97be7f3ee1f.tar.gz |
Merge remote branch 'upstream/master' into prv/po
Diffstat (limited to 'IkiWiki/Plugin')
-rw-r--r-- | IkiWiki/Plugin/filecheck.pm | 25 | ||||
-rw-r--r-- | IkiWiki/Plugin/flattr.pm | 97 | ||||
-rw-r--r-- | IkiWiki/Plugin/relativedate.pm | 2 | ||||
-rw-r--r-- | IkiWiki/Plugin/smiley.pm | 9 | ||||
-rw-r--r-- | IkiWiki/Plugin/toggle.pm | 2 |
5 files changed, 126 insertions, 9 deletions
diff --git a/IkiWiki/Plugin/filecheck.pm b/IkiWiki/Plugin/filecheck.pm index d00b6dfd3..a78058ffe 100644 --- a/IkiWiki/Plugin/filecheck.pm +++ b/IkiWiki/Plugin/filecheck.pm @@ -132,15 +132,28 @@ sub match_mimetype ($$;@) { return IkiWiki::ErrorReason->new("file does not exist"); } - # Use ::magic to get the mime type, the idea is to only trust - # data obtained by examining the actual file contents. + # Get the mime type. + # + # First, try File::Mimeinfo. This is fast, but doesn't recognise + # all files. eval q{use File::MimeInfo::Magic}; - if ($@) { - return IkiWiki::ErrorReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type"); + my $mimeinfo_ok=! $@; + my $mimetype; + if ($mimeinfo_ok) { + my $mimetype=File::MimeInfo::Magic::magic($file); } - my $mimetype=File::MimeInfo::Magic::magic($file); + + # Fall back to using file, which has a more complete + # magic database. if (! defined $mimetype) { - $mimetype=File::MimeInfo::Magic::default($file); + open(my $file_h, "-|", "file", "-bi", $file); + $mimetype=<$file_h>; + close $file_h; + } + if (! defined $mimetype || $mimetype !~s /;.*//) { + # Fall back to default value. + $mimetype=File::MimeInfo::Magic::default($file) + if $mimeinfo_ok; if (! defined $mimetype) { $mimetype="unknown"; } diff --git a/IkiWiki/Plugin/flattr.pm b/IkiWiki/Plugin/flattr.pm new file mode 100644 index 000000000..3aee1eb93 --- /dev/null +++ b/IkiWiki/Plugin/flattr.pm @@ -0,0 +1,97 @@ +#!/usr/bin/perl +package IkiWiki::Plugin::flattr; + +use warnings; +use strict; +use IkiWiki 3.00; + +sub import { + hook(type => "getsetup", id => "flattr", call => \&getsetup); + hook(type => "preprocess", id => "flattr", call => \&preprocess); + hook(type => "format", id => "flattr", call => \&format); +} + +sub getsetup () { + return + plugin => { + safe => 1, + rebuild => undef, + }, + flattr_userid => { + type => "string", + example => 'joeyh', + description => "userid or user name to use by default for Flattr buttons", + advanced => 0, + safe => 1, + rebuild => undef, + }, +} + +my %flattr_pages; + +sub preprocess (@) { + my %params=@_; + + $flattr_pages{$params{destpage}}=1; + + my $url=$params{url}; + if (! defined $url) { + $url=urlto($params{page}, "", 1); + } + + my @fields; + foreach my $field (qw{language uid button hidden category tags}) { + if (exists $params{$field}) { + push @fields, "$field:$params{$field}"; + } + } + + return '<a class="FlattrButton" href="'.$url.'"'. + (exists $params{title} ? ' title="'.$params{title}.'"' : ''). + ' rev="flattr;'.join(';', @fields).';"'. + '>'. + (exists $params{description} ? $params{description} : ''). + '</a>'; +} + +sub format (@) { + my %params=@_; + + # Add flattr's javascript to pages with flattr buttons. + if ($flattr_pages{$params{page}}) { + if (! ($params{content}=~s!^(<body[^>]*>)!$1.flattrjs()!em)) { + # no <body> tag, probably in preview mode + $params{content}=flattrjs().$params{content}; + } + } + return $params{content}; +} + +my $js_cached; +sub flattrjs { + return $js_cached if defined $js_cached; + + my $js_url='https://api.flattr.com/js/0.5.0/load.js?mode=auto'; + if (defined $config{flattr_userid}) { + my $userid=$config{flattr_userid}; + $userid=~s/[^-A-Za-z0-9_]//g; # sanitize for inclusion in javascript + $js_url.="&uid=$userid"; + } + + # This is Flattr's standard javascript snippet to include their + # external javascript file, asynchronously. + return $js_cached=<<"EOF"; +<script type="text/javascript"> +<!--//--><![CDATA[//><!-- +(function() { + var s = document.createElement('script'), t = document.getElementsByTagName('script')[0]; + s.type = 'text/javascript'; + s.async = true; + s.src = '$js_url'; + t.parentNode.insertBefore(s, t); +})();//--><!]]> +</script> +EOF +} + +1 diff --git a/IkiWiki/Plugin/relativedate.pm b/IkiWiki/Plugin/relativedate.pm index c9280ef14..7296889ab 100644 --- a/IkiWiki/Plugin/relativedate.pm +++ b/IkiWiki/Plugin/relativedate.pm @@ -27,7 +27,7 @@ sub format (@) { my %params=@_; if (! ($params{content}=~s!^(<body[^>]*>)!$1.include_javascript($params{page})!em)) { - # no </body> tag, probably in preview mode + # no <body> tag, probably in preview mode $params{content}=include_javascript($params{page}, 1).$params{content}; } return $params{content}; diff --git a/IkiWiki/Plugin/smiley.pm b/IkiWiki/Plugin/smiley.pm index 0d77916d0..6f4f49d18 100644 --- a/IkiWiki/Plugin/smiley.pm +++ b/IkiWiki/Plugin/smiley.pm @@ -25,7 +25,14 @@ sub getsetup () { } sub build_regexp () { - my $list=readfile(srcfile("smileys.mdwn")); + my $srcfile = srcfile("smileys.mdwn", 1); + if (! defined $srcfile) { + print STDERR sprintf(gettext("smiley plugin will not work without %s"), + "smileys.mdwn")."\n"; + $smiley_regexp=''; + return; + } + my $list=readfile($srcfile); while ($list =~ m/^\s*\*\s+\\\\([^\s]+)\s+\[\[([^]]+)\]\]/mg) { my $smiley=$1; my $file=$2; diff --git a/IkiWiki/Plugin/toggle.pm b/IkiWiki/Plugin/toggle.pm index 20967d3fd..1f93f87fe 100644 --- a/IkiWiki/Plugin/toggle.pm +++ b/IkiWiki/Plugin/toggle.pm @@ -69,7 +69,7 @@ sub format (@) { if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">\s*)</div>!$1!g) { $params{content}=~s/<div class="toggleableend">//g; if (! ($params{content}=~s!^(<body[^>]*>)!$1.include_javascript($params{page})!em)) { - # no </body> tag, probably in preview mode + # no <body> tag, probably in preview mode $params{content}=include_javascript($params{page}, 1).$params{content}; } } |