favlink.pm:#!/usr/bin/perl
package IkiWiki::Plugin::favlink;
#use warnings;
use strict;
use HTML::TokeParser;
use LWP::Simple;
use IkiWiki 3.00;
sub import {
hook(type => "getsetup", id => "favlink", call => \&getsetup);
hook(type => "checkconfig", id => "favlink", call => \&checkconfig);
hook(type => "preprocess", id => "favlink", call => \&preprocess);
}
sub getsetup () {
return
plugin => {
description => "favlink link with favicon",
safe => 1,
rebuild => undef,
section => "widget",
},
getFavicon => {
type => "string",
example => 'http://g.etfv.co',
safe => 1,
rebuild => 1,
},
use_getFav =>{
type => "boolean",
example => 1,
description => '0 defaults to the getFavicon Service, 1 uses direkt Links',
safe => 1,
rebuild => 1,
},
}
sub checkconfig () {
# setup default settings
if (! exists $config{getFavicon}) {
$config{getFavicon}='http://g.etfv.co';
}
if (! exists $config{use_getFav}) {
$config{use_getFav}=1;
}
}
sub preprocess (@) {
my $url=@_[0];
if ($url !~ m/^http/) {
$url="http://$url";
}
my %params=@_;
my $content = get $url;
if (exists $params{title} && length $params{title}) {
$params{title} = " $params{title}";
}
if (! exists $params{title}) {
my $p = HTML::TokeParser->new(\$content);
my $token = $p->get_tag("title");
$params{title} = $p->get_text;
}
if (! exists $params{getFav}) {
$params{getFav} = $config{use_getFav};
}
if ($params{getFav} eq 0 || exists $params{icon}) {
if (! exists $params{icon} || ! length $params{icon}) {
$params{icon}="/favicon.ico";
my $p = HTML::TokeParser->new(\$content);
while (my $token = $p->get_tag("link")) {
my $token_attribs = $token->[1];
if ($token_attribs->{rel} eq 'icon') {
$params{icon} = $token_attribs->{href};
last;
}
if ($token_attribs->{rel} eq 'shortcut icon') {
$params{icon} = $token_attribs->{href};
last;
}
}
}
$params{icon} = URI->new_abs($params{icon}, $url);
my @array = head($params{icon});
if (scalar @array == 0){
$params{icon}="/link.ico";
}
return "$params{title}";
}
else {
return "
$params{title}";
}
}
1