]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/inline:_numerical_ordering_by_title.mdwn
Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / doc / todo / inline:_numerical_ordering_by_title.mdwn
1 Could you please add numerical ordering by title to [[inline|plugins/inline]]
2 plugin? Now I can do only alphabetical order by title, but sometime it's not enough.
3
4 BTW, it seems that ordering by title is rather ordering by filename of page.
5 For me "title" means title of page I can set using `title` parameter
6 of [[meta|plugins/meta]] plugin :)
7
8 Why do I need that feature? I've just been migrating an info site of our university
9 [mail system](http://poczta.uw.edu.pl/) to Ikiwiki from very static, console handling
10 Makefile+[WML](http://thewml.org/)+XML+XSL=HTML solution. I have many news files
11 (`1.mdwn`, `2.mdwn`, etc.) and unfortunately I did very stupid thing. I've commited
12 all of them in the same revision of our Subversion repo...
13
14 Now I have a problem with sorting these files using inline plugin. I can't do
15 sorting by age, because both old and young news files have the same age. I can't
16 sort by title too. For example, when I sort them by title, then `9.mdwn` page is
17 between `90.mdwn` and `89.mdwn` pages... It sucks, of course. Sorting by mtime
18 also is not a solution for me, because it means that I can't touch/fix old news
19 anymore.
20
21 Do you have any idea how to workaround that issue? --[[Paweł|ptecza]]
22
23 > Maybe you can rename `9.mdwn` to `09.mdwn`? See `rename(1)`, it renames multiple files
24 > in one go. --[[buo]]
25
26 >> Thanks for your suggestion! But what about if number of my news files grows to 100+?
27
28 >>     $ ls
29 >>     09.mdwn  100.mdwn  101.mdwn  102.mdwn  89.mdwn  90.mdwn
30
31 >> I don't want to rename all previous files to add `0` prefix. --[[Paweł|ptecza]]
32
33 >>> Rather than adding 0's or or a 'sorttype' parameter, I'd just fix the sort order.
34 >>> Both MacOS and Windows use a smarter sort order than just lexical in their
35 >>> file browsers (e.g. <http://support.microsoft.com/default.aspx?kbid=319827>,
36 >>> <http://docs.info.apple.com/article.html?artnum=300989>).
37 >>>
38 >>> The [Unicode Collation algorithm](http://en.wikipedia.org/wiki/Unicode_collation_algorithm)
39 >>> would seem to be a reasonable sort order.  (See also <http://www.unicode.org/unicode/reports/tr10/>.)
40 >>> Unfortunately the standard perl implementation, [Unicode::Collate](http://perldoc.perl.org/Unicode/Collate.html)
41 >>> doesn't handle the optional [numbers](http://www.unicode.org/unicode/reports/tr10/#Customization)
42 >>> extension which is what you want.  --[[Will]]
43
44 ---
45
46 Below is my simple patch. Feel free to use it or comment!
47
48 I have also 2 considerations for inline sorting:
49
50 1. Maybe changing name of `sort` parameter to `sortby` or `sortkey` will
51    be good idea?
52 1. Maybe you should use `title` sort key for title from meta plugin and `name`, 
53    `filename`, `page` or `pagename` for page names? In the future you can also
54    sort by meta author, license or another key.
55
56 --[[Paweł|ptecza]]
57
58     --- inline.pm-orig  2008-09-02 09:53:20.000000000 +0200
59     +++ inline.pm       2008-09-02 10:09:02.000000000 +0200
60     @@ -186,7 +186,15 @@
61         }
62
63         if (exists $params{sort} && $params{sort} eq 'title') {
64     -           @list=sort { pagetitle(basename($a)) cmp pagetitle(basename($b)) } @list;
65     +           if (! $params{sorttype} || $params{sorttype} eq 'lexical') {
66     +                   @list=sort { pagetitle(basename($a)) cmp pagetitle(basename($b)) } @list;
67     +           }
68     +           elsif ($params{sorttype} eq 'numeric') {
69     +                   @list=sort { pagetitle(basename($a)) <=> pagetitle(basename($b)) } @list;
70     +           }
71     +           else {
72     +                   return sprintf(gettext("unknown sort type %s"), $params{sorttype});
73     +           }
74         }
75         elsif (exists $params{sort} && $params{sort} eq 'mtime') {
76                 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
77     @@ -195,7 +203,7 @@
78                 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
79         }
80         else {
81     -           return sprintf(gettext("unknown sort type %s"), $params{sort});
82     +           return sprintf(gettext("unknown sort key %s"), $params{sort});
83         }
84
85         if (yesno($params{reverse})) {
86
87 ---
88
89 Joey, have you forgotten about that request? ;) --[[Paweł|ptecza]]
90
91 > Okie.  Here is a different [[patch]] based on my comment above.  It doesn't introduce
92 > a new key, but rather changes the title sorting order. Two caveats:
93
94  * I've only tested this in `inline`, not the other places I changed the sort order.
95  * I'm unsure if the regexp used in the split should be `/(-?\d+)/` instead of `/(\d+)/`.
96     As written, '-' is interpreted as a hyphen rather than a minus sign.
97
98 > --[[Will]]
99
100 >> Hi Will! Your idea looks interesting for me, but I'm affraid that it's too big
101 >> change in Ikiwiki... Maybe I'm wrong? ;) What do you think, Joey? --[[Paweł|ptecza]]
102
103 >>> It isn't that big a change.  It is just supplying a sort order to the sort.  The
104 >>> patch is a little larger because I then went through and made that sort
105 >>> order available in other places where it makes sense.  (Looking at the
106 >>> patch again briefly, I should have also used it in the `map` plugin.)
107 >>>
108 >>> If you wanted a simple patch, you could just move the `titlecmp` function
109 >>> into the inline plugin and only use it there.  The problem with that is that
110 >>> it only fixes the inline plugin. -- [[Will]]
111
112 >>>> Will, I agree with you that it's improved way of sort order. But on the other
113 >>>> hand I prefer to be careful when I change something in a several places,
114 >>>> because I don't want to break any working things when I fix one thing.
115 >>>> I hope that Joey agree with us too and all Ikiwiki users will be happy
116 >>>> after applying your patch ;) --[[Paweł|ptecza]]
117
118 ----
119
120     diff --git a/IkiWiki.pm b/IkiWiki.pm
121     index c0f5dea..d001f8d 100644
122     --- a/IkiWiki.pm
123     +++ b/IkiWiki.pm
124     @@ -20,7 +20,7 @@ use Exporter q{import};
125      our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
126                       bestlink htmllink readfile writefile pagetype srcfile pagename
127                       displaytime will_render gettext urlto targetpage
128     -            add_underlay
129     +            add_underlay titlecmp
130                       %config %links %pagestate %renderedfiles
131                       %pagesources %destsources);
132      our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
133     @@ -835,6 +835,42 @@ sub titlepage ($) { #{{{
134         return $title;
135      } #}}}
136      
137     +sub titlecmp ($$) { #{{{
138     +   my $titleA=shift;
139     +   my $titleB=shift;
140     +   
141     +   my @listA=split(/(\d+)/,$titleA);
142     +   my @listB=split(/(\d+)/,$titleB);
143     +   
144     +   while (@listA && @listB) {
145     +           # compare bits of text
146     +           my $a = shift @listA;
147     +           my $b = shift @listB;
148     +           my $c = ($a cmp $b);
149     +           return $c if ($c);
150     +
151     +           if (@listA && @listB) {
152     +                   # compare numbers
153     +                   $a = shift @listA;
154     +                   $b = shift @listB;
155     +                   $c = $a <=> $b;
156     +                   return $c if ($c);
157     +                   
158     +                   # 01 is different to 1
159     +                   $c = (length($a) <=> length($b));
160     +                   return $c if ($c);
161     +
162     +                   $c = ($a cmp $b);
163     +                   return $c if ($c);
164     +           }
165     +   }
166     +   
167     +   return 1 if (@listA);
168     +   return -1 if (@listB);
169     +   
170     +   return 0;
171     +} #}}}
172     +
173      sub linkpage ($) { #{{{
174         my $link=shift;
175         my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
176     diff --git a/IkiWiki/Plugin/brokenlinks.pm b/IkiWiki/Plugin/brokenlinks.pm
177     index 37752dd..ccaa399 100644
178     --- a/IkiWiki/Plugin/brokenlinks.pm
179     +++ b/IkiWiki/Plugin/brokenlinks.pm
180     @@ -59,7 +59,7 @@ sub preprocess (@) { #{{{
181                         map {
182                                 "<li>$_</li>"
183                         }
184     -                   sort @broken)
185     +                   sort titlecmp @broken)
186                 ."</ul>\n";
187      } # }}}
188      
189     diff --git a/IkiWiki/Plugin/inline.pm b/IkiWiki/Plugin/inline.pm
190     index 8efef3f..263e7a6 100644
191     --- a/IkiWiki/Plugin/inline.pm
192     +++ b/IkiWiki/Plugin/inline.pm
193     @@ -192,7 +192,7 @@ sub preprocess_inline (@) { #{{{
194         }
195      
196         if (exists $params{sort} && $params{sort} eq 'title') {
197     -           @list=sort { pagetitle(basename($a)) cmp pagetitle(basename($b)) } @list;
198     +           @list=sort { titlecmp(pagetitle(basename($a)),pagetitle(basename($b))) } @list;
199         }
200         elsif (exists $params{sort} && $params{sort} eq 'mtime') {
201                 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
202     diff --git a/IkiWiki/Plugin/orphans.pm b/IkiWiki/Plugin/orphans.pm
203     index b910758..10a1d87 100644
204     --- a/IkiWiki/Plugin/orphans.pm
205     +++ b/IkiWiki/Plugin/orphans.pm
206     @@ -56,7 +56,7 @@ sub preprocess (@) { #{{{
207                                 htmllink($params{page}, $params{destpage}, $_,
208                                          noimageinline => 1).
209                                 "</li>"
210     -                   } sort @orphans).
211     +                   } sort titlecmp @orphans).
212                 "</ul>\n";
213      } # }}}
214      
215     diff --git a/IkiWiki/Render.pm b/IkiWiki/Render.pm
216     index ceb7c84..00798e1 100644
217     --- a/IkiWiki/Render.pm
218     +++ b/IkiWiki/Render.pm
219     @@ -89,7 +89,7 @@ sub genpage ($$) { #{{{
220                 $template->param(have_actions => 1);
221         }
222      
223     -   my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
224     +   my @backlinks=sort { titlecmp($a->{page}, $b->{page}) } backlinks($page);
225         my ($backlinks, $more_backlinks);
226         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
227                 $backlinks=\@backlinks;