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