X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/adb1bc8761efd7ff7c45abc8c5c503c95c09b402..d22489299adb04fdce241a9365b47799ba7f66d6:/IkiWiki/Plugin/meta.pm?ds=inline diff --git a/IkiWiki/Plugin/meta.pm b/IkiWiki/Plugin/meta.pm index 42fddb0bb..434fde527 100644 --- a/IkiWiki/Plugin/meta.pm +++ b/IkiWiki/Plugin/meta.pm @@ -4,27 +4,32 @@ package IkiWiki::Plugin::meta; use warnings; use strict; -use IkiWiki 2.00; +use IkiWiki 3.00; -my %meta; -my %title; -my %permalink; -my %author; -my %authorurl; -my %license; -my %copyright; +my %metaheaders; -sub import { #{{{ +sub import { + hook(type => "getsetup", id => "meta", call => \&getsetup); hook(type => "needsbuild", id => "meta", call => \&needsbuild); hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1); hook(type => "pagetemplate", id => "meta", call => \&pagetemplate); -} # }}} +} + +sub getsetup () { + return + plugin => { + safe => 1, + rebuild => undef, + section => "core", + }, +} -sub needsbuild (@) { #{{{ +sub needsbuild (@) { my $needsbuild=shift; foreach my $page (keys %pagestate) { if (exists $pagestate{$page}{meta}) { - if (grep { $_ eq $pagesources{$page} } @$needsbuild) { + if (exists $pagesources{$page} && + grep { $_ eq $pagesources{$page} } @$needsbuild) { # remove state, it will be re-added # if the preprocessor directive is still # there during the rebuild @@ -32,35 +37,40 @@ sub needsbuild (@) { #{{{ } } } + return $needsbuild; } -sub scrub ($) { #{{{ +sub scrub ($$$) { if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) { - return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift); + return IkiWiki::Plugin::htmlscrubber::sanitize( + content => shift, page => shift, destpage => shift); } else { return shift; } -} #}}} +} -sub htmlize ($$$) { #{{{ +sub safeurl ($) { + my $url=shift; + if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} && + defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) { + return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/; + } + else { + return 1; + } +} + +sub htmlize ($$$) { my $page = shift; my $destpage = shift; - my $text = shift; - $text=IkiWiki::htmlize($page, pagetype($pagesources{$page}), + return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}), IkiWiki::linkify($page, $destpage, - IkiWiki::preprocess($page, $destpage, $text))); - - # hack to get rid of enclosing junk added by markdown - $text=~s!^

!!; - $text=~s!

$!!; - chomp $text; - - return $text; + IkiWiki::preprocess($page, $destpage, shift))); } -sub preprocess (@) { #{{{ +sub preprocess (@) { return "" unless @_; my %params=@_; my $key=shift; @@ -79,48 +89,81 @@ sub preprocess (@) { #{{{ # Metadata collection that needs to happen during the scan pass. if ($key eq 'title') { - $title{$page}=HTML::Entities::encode_numeric($value); + $pagestate{$page}{meta}{title}=$value; + if (exists $params{sortas}) { + $pagestate{$page}{meta}{titlesort}=$params{sortas}; + } + else { + delete $pagestate{$page}{meta}{titlesort}; + } + return ""; + } + elsif ($key eq 'description') { + $pagestate{$page}{meta}{description}=$value; + # fallthrough + } + elsif ($key eq 'guid') { + $pagestate{$page}{meta}{guid}=$value; + # fallthrough } elsif ($key eq 'license') { - push @{$meta{$page}}, ''; - $license{$page}=$value; + push @{$metaheaders{$page}}, ''; + $pagestate{$page}{meta}{license}=$value; return ""; } elsif ($key eq 'copyright') { - push @{$meta{$page}}, ''; - $copyright{$page}=$value; + push @{$metaheaders{$page}}, ''; + $pagestate{$page}{meta}{copyright}=$value; return ""; } elsif ($key eq 'link' && ! %params) { # hidden WikiLink - push @{$links{$page}}, $value; + add_link($page, $value); return ""; } elsif ($key eq 'author') { - $author{$page}=$value; + $pagestate{$page}{meta}{author}=$value; + if (exists $params{sortas}) { + $pagestate{$page}{meta}{authorsort}=$params{sortas}; + } + else { + delete $pagestate{$page}{meta}{authorsort}; + } # fallthorough } elsif ($key eq 'authorurl') { - $authorurl{$page}=$value; + $pagestate{$page}{meta}{authorurl}=$value if safeurl($value); # fallthrough } - - if (! defined wantarray) { - # avoid collecting duplicate data during scan pass - return; + elsif ($key eq 'permalink') { + $pagestate{$page}{meta}{permalink}=$value if safeurl($value); + # fallthrough } - - # Metadata collection that happens only during preprocessing pass. - if ($key eq 'date') { + elsif ($key eq 'date') { eval q{use Date::Parse}; if (! $@) { my $time = str2time($value); $IkiWiki::pagectime{$page}=$time if defined $time; } } - elsif ($key eq 'permalink') { - $permalink{$page}=$value; - push @{$meta{$page}}, scrub(''); + elsif ($key eq 'updated') { + eval q{use Date::Parse}; + if (! $@) { + my $time = str2time($value); + $pagestate{$page}{meta}{updated}=$time if defined $time; + } + } + + if (! defined wantarray) { + # avoid collecting duplicate data during scan pass + return; + } + + # Metadata handling that happens only during preprocessing pass. + if ($key eq 'permalink') { + if (safeurl($value)) { + push @{$metaheaders{$page}}, scrub('', $page, $destpage); + } } elsif ($key eq 'stylesheet') { my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet"; @@ -129,20 +172,50 @@ sub preprocess (@) { #{{{ # editable page as a stylesheet my $stylesheet=bestlink($page, $value.".css"); if (! length $stylesheet) { - return "[[meta ".gettext("stylesheet not found")."]]"; + error gettext("stylesheet not found") } - push @{$meta{$page}}, '"; + "\" type=\"text/css\" />", $page, $destpage); + } + elsif ($key eq 'script') { + my $defer=exists $params{defer} ? ' defer="defer"' : ''; + my $async=exists $params{async} ? ' async="async"' : ''; + my $js=bestlink($page, $value.".js"); + if (! length $js) { + error gettext("script not found"); + } + push @{$metaheaders{$page}}, scrub('