From: Richard Kettlewell Date: Sun, 29 Nov 2009 15:55:32 +0000 (+0000) Subject: More sensible ordering of search results in web interface. X-Git-Tag: 5.0~30 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/commitdiff_plain/d53ef367fb4f4aed2e2ff9d8ef4c187a61c9497e More sensible ordering of search results in web interface. Fixes issue #46. --- diff --git a/CHANGES.html b/CHANGES.html index 81e2a86..5050343 100644 --- a/CHANGES.html +++ b/CHANGES.html @@ -213,6 +213,11 @@ span.command { disobedience doesn't configure its back end + + #46 + Sort search results in web interface + + #48 build-time dependency on oggdec removed diff --git a/cgi/macros-disorder.c b/cgi/macros-disorder.c index 30614c1..29835bb 100644 --- a/cgi/macros-disorder.c +++ b/cgi/macros-disorder.c @@ -859,24 +859,36 @@ static int exp__files_dirs(int nargs, /* Get the list */ if(fn(dcgi_client, dir, re, &tracks, &ntracks)) return 0; - /* Sort it. NB trackname_transform() does not go to the server. */ - tsd = tracksort_init(ntracks, tracks, type); - /* Expand the subsiduary templates. We chuck in @sort and @display because - * it is particularly easy to do so. */ - for(n = 0; n < ntracks; ++n) - if((rc = mx_expand(mx_rewritel(m, - "index", make_index(n), - "parity", n % 2 ? "odd" : "even", - "track", tsd[n].track, - "first", n == 0 ? "true" : "false", - "last", n + 1 == ntracks ? "false" : "true", - "sort", tsd[n].sort, - "display", tsd[n].display, - (char *)0), - output, u))) - return rc; + if(type) { + /* Sort it. NB trackname_transform() does not go to the server. */ + tsd = tracksort_init(ntracks, tracks, type); + /* Expand the subsiduary templates. We chuck in @sort and @display because + * it is particularly easy to do so. */ + for(n = 0; n < ntracks; ++n) + if((rc = mx_expand(mx_rewritel(m, + "index", make_index(n), + "parity", n % 2 ? "odd" : "even", + "track", tsd[n].track, + "first", n == 0 ? "true" : "false", + "last", n + 1 == ntracks ? "false" : "true", + "sort", tsd[n].sort, + "display", tsd[n].display, + (char *)0), + output, u))) + return rc; + } else { + for(n = 0; n < ntracks; ++n) + if((rc = mx_expand(mx_rewritel(m, + "index", make_index(n), + "parity", n % 2 ? "odd" : "even", + "track", tracks[n], + "first", n == 0 ? "true" : "false", + "last", n + 1 == ntracks ? "false" : "true", + (char *)0), + output, u))) + return rc; + } return 0; - } /*$ @tracks{DIR}{RE}{TEMPLATE} @@ -936,14 +948,12 @@ static int exp__search_shim(disorder_client *c, const char *terms, * - @parity: "even" or "odd" alternately * - @first: "true" on the first directory and "false" otherwise * - @last: "true" on the last directory and "false" otherwise - * - @sort: the sort key for this track - * - @display: the UNQUOTED display string for this track */ static int exp_search(int nargs, const struct mx_node **args, struct sink *output, void *u) { - return exp__files_dirs(nargs, args, output, u, "track", exp__search_shim); + return exp__files_dirs(nargs, args, output, u, NULL, exp__search_shim); } /*$ @label{NAME} diff --git a/lib/trackname.h b/lib/trackname.h index 63b881b..56f933e 100644 --- a/lib/trackname.h +++ b/lib/trackname.h @@ -51,7 +51,14 @@ int compare_path_raw(const unsigned char *ap, size_t an, /* Comparison function for path names that groups all entries in a directory * together */ -/* Convenient wrapper for compare_path_raw */ +/** @brief Compare two paths + * @param ap First path + * @param bp Second path + * @return -ve, 0 or +ve for ap <, = or > bp + * + * Sorts files within a directory together. + * A wrapper around compare_path_raw(). + */ static inline int compare_path(const char *ap, const char *bp) { return compare_path_raw((const unsigned char *)ap, strlen(ap), (const unsigned char *)bp, strlen(bp)); diff --git a/lib/trackorder.c b/lib/trackorder.c index d0ae448..94d2bd5 100644 --- a/lib/trackorder.c +++ b/lib/trackorder.c @@ -27,6 +27,22 @@ #include "log.h" #include "unicode.h" +/** @brief Compare two tracks + * @param sa First sort key + * @param sb Second sort key + * @param da First display string + * @param db Second display string + * @param ta First raw track + * @param tb Second raw track + * @return -ve, 0 or +ve for a <, = or > b + * + * Tries the following comparisons until a difference is found: + * - case-independent comparison of sort keys + * - case-dependent comparison of sort keys + * - case-independent comparison of display strings + * - case-dependent comparison of display strings + * - case-dependent comparison of paths (see compare_path()) + */ int compare_tracks(const char *sa, const char *sb, const char *da, const char *db, const char *ta, const char *tb) { @@ -43,6 +59,17 @@ int compare_tracks(const char *sa, const char *sb, return compare_path(ta, tb); } +/** @brief Compare two paths + * @param ap First path + * @param an Length of @p ap + * @param bp Second path + * @param bn Length @p bp + * @return -ve, 0 or +ve for ap <, = or > bp + * + * Sorts files within a directory together. + * + * See also compare_path(). + */ int compare_path_raw(const unsigned char *ap, size_t an, const unsigned char *bp, size_t bn) { /* Don't change this function! The database sort order depends on it */ diff --git a/lib/tracksort.c b/lib/tracksort.c index 2f81739..b1fcc3a 100644 --- a/lib/tracksort.c +++ b/lib/tracksort.c @@ -32,6 +32,16 @@ static int tracksort_compare(const void *a, const void *b) { ea->track, eb->track); } +/** @brief Sort tracks + * @param ntracks Number of tracks to sort + * @param tracks List of tracks + * @param type Comparison type + * @return Sorted track data + * + * Tracks are compared using compare_tracks(), with the sort key and display + * string set according to @p type, which should be "track" if the tracks are + * really tracks and "dir" if they are directories. + */ struct tracksort_data *tracksort_init(int ntracks, char **tracks, const char *type) {