X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/b86276ffed7ee001b35cd610e5d56e5afb4088cf..ee72b1e03294b0efbfbf107de6046a68947293a8:/IkiWiki/Plugin/meta.pm?ds=sidebyside diff --git a/IkiWiki/Plugin/meta.pm b/IkiWiki/Plugin/meta.pm index e8cc1e392..e7b96bdf1 100644 --- a/IkiWiki/Plugin/meta.pm +++ b/IkiWiki/Plugin/meta.pm @@ -37,12 +37,13 @@ sub needsbuild (@) { } } } + return $needsbuild; } -sub scrub ($$) { +sub scrub ($$$) { if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) { return IkiWiki::Plugin::htmlscrubber::sanitize( - content => shift, destpage => shift); + content => shift, page => shift, destpage => shift); } else { return shift; @@ -88,30 +89,30 @@ sub preprocess (@) { # Metadata collection that needs to happen during the scan pass. if ($key eq 'title') { - $pagestate{$page}{meta}{title}=HTML::Entities::encode_numeric($value); - if (exists $params{sort}) { - $pagestate{$page}{meta}{titlesort}=$params{sort}; + $pagestate{$page}{meta}{title}=$value; + if (exists $params{sortas}) { + $pagestate{$page}{meta}{titlesort}=$params{sortas}; } else { - $pagestate{$page}{meta}{titlesort}=$value; + delete $pagestate{$page}{meta}{titlesort}; } return ""; } elsif ($key eq 'description') { - $pagestate{$page}{meta}{description}=HTML::Entities::encode_numeric($value); + $pagestate{$page}{meta}{description}=$value; # fallthrough } elsif ($key eq 'guid') { - $pagestate{$page}{meta}{guid}=HTML::Entities::encode_numeric($value); + $pagestate{$page}{meta}{guid}=$value; # fallthrough } elsif ($key eq 'license') { - push @{$metaheaders{$page}}, ''; + push @{$metaheaders{$page}}, ''; $pagestate{$page}{meta}{license}=$value; return ""; } elsif ($key eq 'copyright') { - push @{$metaheaders{$page}}, ''; + push @{$metaheaders{$page}}, ''; $pagestate{$page}{meta}{copyright}=$value; return ""; } @@ -120,8 +121,26 @@ sub preprocess (@) { add_link($page, $value); return ""; } + elsif ($key eq 'enclosure') { + my $link=bestlink($page, $value); + if (! length $link) { + error gettext("enclosure not found") + } + add_depends($page, $link, deptype("presence")); + + $value=urlto($link, $page, 1); + $pagestate{$page}{meta}{enclosure}=$value; + $pagestate{$page}{meta}{enclosurefile}=$link; + # fallthrough + } elsif ($key eq 'author') { $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') { @@ -155,7 +174,7 @@ sub preprocess (@) { # Metadata handling that happens only during preprocessing pass. if ($key eq 'permalink') { if (safeurl($value)) { - push @{$metaheaders{$page}}, scrub('', $destpage); + push @{$metaheaders{$page}}, scrub('', $page, $destpage); } } elsif ($key eq 'stylesheet') { @@ -167,10 +186,21 @@ sub preprocess (@) { if (! length $stylesheet) { error gettext("stylesheet not found") } - push @{$metaheaders{$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('', + $page, $destpage); } elsif ($key eq 'openid') { my $delegate=0; # both by default @@ -191,8 +221,19 @@ sub preprocess (@) { '" rel="openid2.local_id" />' if $delegate ne 1; } if (exists $params{"xrds-location"} && safeurl($params{"xrds-location"})) { - push @{$metaheaders{$page}}, ''; + # force url absolute + eval q{use URI}; + error($@) if $@; + my $url=URI->new_abs($params{"xrds-location"}, $config{url}); + push @{$metaheaders{$page}}, ''; + } + } + elsif ($key eq 'foaf') { + if (safeurl($value)) { + push @{$metaheaders{$page}}, ''; } } elsif ($key eq 'redir') { @@ -229,7 +270,7 @@ sub preprocess (@) { my $delay=int(exists $params{delay} ? $params{delay} : 0); my $redir=""; if (! $safe) { - $redir=scrub($redir, $destpage); + $redir=scrub($redir, $page, $destpage); } push @{$metaheaders{$page}}, $redir; } @@ -239,20 +280,34 @@ sub preprocess (@) { join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params). - " />\n", $destpage); + " />\n", $page, $destpage); } } elsif ($key eq 'robots') { push @{$metaheaders{$page}}, ''; } - elsif ($key eq 'description') { - push @{$metaheaders{$page}}, ''; } + elsif ($key eq 'name') { + push @{$metaheaders{$page}}, scrub('', $page, $destpage); + } + elsif ($key eq 'keywords') { + # Make sure the keyword string is safe: only allow alphanumeric + # characters, space and comma and strip the rest. + $value =~ s/[^[:alnum:], ]+//g; + push @{$metaheaders{$page}}, ''; + } else { - push @{$metaheaders{$page}}, scrub('', $destpage); + push @{$metaheaders{$page}}, scrub('', $page, $destpage); } return ""; @@ -270,12 +325,31 @@ sub pagetemplate (@) { $template->param(meta => join("\n", grep { (! $seen{$_}) && ($seen{$_}=1) } @{$metaheaders{$page}})); } if (exists $pagestate{$page}{meta}{title} && $template->query(name => "title")) { - $template->param(title => $pagestate{$page}{meta}{title}); + eval q{use HTML::Entities}; + $template->param(title => HTML::Entities::encode_numeric($pagestate{$page}{meta}{title})); $template->param(title_overridden => 1); } - foreach my $field (qw{author authorurl description permalink}) { - $template->param($field => $pagestate{$page}{meta}{$field}) + if (exists $pagestate{$page}{meta}{enclosure}) { + $template->param(enclosure => HTML::Entities::encode_entities(IkiWiki::urlabs($pagestate{$page}{meta}{enclosure}, $config{url}))); + } + + foreach my $field (qw{authorurl}) { + eval q{use HTML::Entities}; + $template->param($field => HTML::Entities::encode_entities($pagestate{$page}{meta}{$field})) + if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field); + } + + foreach my $field (qw{permalink}) { + if (exists $pagestate{$page}{meta}{$field} && $template->query(name => $field)) { + eval q{use HTML::Entities}; + $template->param($field => HTML::Entities::encode_entities(IkiWiki::urlabs($pagestate{$page}{meta}{$field}, $config{url}))); + } + } + + foreach my $field (qw{description author}) { + eval q{use HTML::Entities}; + $template->param($field => HTML::Entities::encode_numeric($pagestate{$page}{meta}{$field})) if exists $pagestate{$page}{meta}{$field} && $template->query(name => $field); } @@ -288,14 +362,31 @@ sub pagetemplate (@) { } } -sub titlesort { - my $key = $pagestate{$_[0]}{meta}{titlesort}; +sub get_sort_key { + my $page = shift; + my $meta = shift; - if (defined $key) { - return $key; - } + # e.g. titlesort (also makes sense for author) + my $key = $pagestate{$page}{meta}{$meta . "sort"}; + return $key if defined $key; - return pagetitle(IkiWiki::basename($_[0])); + # e.g. title + $key = $pagestate{$page}{meta}{$meta}; + return $key if defined $key; + + # fall back to closer-to-core things + if ($meta eq 'title') { + return pagetitle(IkiWiki::basename($page)); + } + elsif ($meta eq 'date') { + return $IkiWiki::pagectime{$page}; + } + elsif ($meta eq 'updated') { + return $IkiWiki::pagemtime{$page}; + } + else { + return ''; + } } sub match { @@ -314,15 +405,15 @@ sub match { } if (defined $val) { - if ($val=~/^$re$/i) { + if ($val=~$re) { return IkiWiki::SuccessReason->new("$re matches $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1); } else { - return IkiWiki::FailReason->new("$re does not match $field of $page", "" => 1); + return IkiWiki::FailReason->new("$re does not match $field of $page", $page => $IkiWiki::DEPEND_CONTENT, "" => 1); } } else { - return IkiWiki::FailReason->new("$page does not have a $field", "" => 1); + return IkiWiki::FailReason->new("$page does not have a $field", $page => $IkiWiki::DEPEND_CONTENT); } } @@ -348,10 +439,31 @@ sub match_copyright ($$;@) { IkiWiki::Plugin::meta::match("copyright", @_); } +sub match_guid ($$;@) { + IkiWiki::Plugin::meta::match("guid", @_); +} + +package IkiWiki::SortSpec; + +sub cmp_meta { + my $meta = shift; + error(gettext("sort=meta requires a parameter")) unless defined $meta; + + if ($meta eq 'updated' || $meta eq 'date') { + return IkiWiki::Plugin::meta::get_sort_key($a, $meta) + <=> + IkiWiki::Plugin::meta::get_sort_key($b, $meta); + } + + return IkiWiki::Plugin::meta::get_sort_key($a, $meta) + cmp + IkiWiki::Plugin::meta::get_sort_key($b, $meta); +} + +# A prototype of how sort=title could behave in 4.0 or something sub cmp_meta_title { - IkiWiki::Plugin::meta::titlesort($_[0]) - cmp - IkiWiki::Plugin::meta::titlesort($_[1]) + $_[0] = 'title'; + return cmp_meta(@_); } 1