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