diff options
author | Joey Hess <joey@gnu.kitenet.net> | 2010-04-06 23:15:33 -0400 |
---|---|---|
committer | Joey Hess <joey@gnu.kitenet.net> | 2010-04-06 23:15:33 -0400 |
commit | bab8fec52468b780485bdf96a37a593bd033c7e3 (patch) | |
tree | 5f292e4e6205bd4fcde1be249c1fe646dc75a26c /IkiWiki/Plugin | |
parent | 9dc220711dff12fd45274c4e2fd2645a33d3de5c (diff) | |
parent | a2dc8c9373b36f7cc8da239b823b5839788a743d (diff) |
Merge remote branch 'smcv/ready/sort-package'
Conflicts:
debian/NEWS
Diffstat (limited to 'IkiWiki/Plugin')
-rw-r--r-- | IkiWiki/Plugin/meta.pm | 69 | ||||
-rw-r--r-- | IkiWiki/Plugin/sortnaturally.pm | 32 |
2 files changed, 100 insertions, 1 deletions
diff --git a/IkiWiki/Plugin/meta.pm b/IkiWiki/Plugin/meta.pm index 5f046cb2a..34e902bec 100644 --- a/IkiWiki/Plugin/meta.pm +++ b/IkiWiki/Plugin/meta.pm @@ -88,7 +88,18 @@ 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); + my $encoded = HTML::Entities::encode_numeric($value); + $pagestate{$page}{meta}{title} = $encoded; + + if (exists $params{sortas}) { + $pagestate{$page}{meta}{titlesort}=$params{sortas}; + } + elsif ($encoded ne $value) { + $pagestate{$page}{meta}{titlesort}=$value; + } + else { + delete $pagestate{$page}{meta}{titlesort}; + } return ""; } elsif ($key eq 'description') { @@ -116,6 +127,12 @@ sub preprocess (@) { } 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') { @@ -282,6 +299,33 @@ sub pagetemplate (@) { } } +sub get_sort_key { + my $page = $_[0]; + my $meta = $_[1]; + + # e.g. titlesort (also makes sense for author) + my $key = $pagestate{$page}{meta}{$meta . "sort"}; + return $key if defined $key; + + # 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 { my $field=shift; my $page=shift; @@ -332,4 +376,27 @@ sub match_copyright ($$;@) { IkiWiki::Plugin::meta::match("copyright", @_); } +package IkiWiki::SortSpec; + +sub cmp_meta { + my $meta = $_[0]; + 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 { + $_[0] = 'title'; + return cmp_meta(@_); +} + 1 diff --git a/IkiWiki/Plugin/sortnaturally.pm b/IkiWiki/Plugin/sortnaturally.pm new file mode 100644 index 000000000..92453749d --- /dev/null +++ b/IkiWiki/Plugin/sortnaturally.pm @@ -0,0 +1,32 @@ +#!/usr/bin/perl +# Sort::Naturally-powered title_natural sort order for IkiWiki +package IkiWiki::Plugin::sortnaturally; + +use IkiWiki 3.00; +no warnings; + +sub import { + hook(type => "getsetup", id => "sortnaturally", call => \&getsetup); +} + +sub getsetup { + return + plugin => { + safe => 1, + rebuild => 1, + }, +} + +sub checkconfig () { + eval q{use Sort::Naturally}; + error $@ if $@; +} + +package IkiWiki::SortSpec; + +sub cmp_title_natural { + Sort::Naturally::ncmp(IkiWiki::pagetitle(IkiWiki::basename($a)), + IkiWiki::pagetitle(IkiWiki::basename($b))) +} + +1; |