From a8e72e500800f8493fb317cd0f00e5150e4f1950 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 13 Aug 2016 10:19:34 +0200 Subject: Add support for custom entry sorting --- API.md | 12 ++++++++---- init.lua | 22 +++++++++++++++++++--- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/API.md b/API.md index 4dea73a..38d3bfd 100644 --- a/API.md +++ b/API.md @@ -54,10 +54,14 @@ data. First letter capitalized, no puncation at end of sentence, max. 100 characters * `sorting`: (optional) Sorting method of entries - * `"abc"`: Alphabetical (default) - * `"nosort": Entries appear in no particular order - * A table: Entries appear in exactly the same order as specified in the - table of entry IDs + * `"abc"`: Alphabetical (default) + * `"nosort"`: Entries appear in no particular order + * `"custom"`: Manually define the order of entries + * `sorting_data`: Additional data for special sorting methods. + If `sorting=="custom"`, this field must contain a table (list form) in which + the entry IDs are specified in the order they are supposed to appear in the + entry list. All entries which are missing in this table will appear in no + particular order below the final specified one. * `build_formspec`: The template function. Takes entry data as its only parameter (has the data type of the entry data) and must return a formspec which is inserted in the Entry tab. diff --git a/init.lua b/init.lua index 6d27a81..6c12494 100644 --- a/init.lua +++ b/init.lua @@ -257,20 +257,36 @@ function doc.get_sorted_entry_names(cid) local sort_table = {} local entry_table = {} local cat = doc.data.categories[cid] + local used_eids = {} + -- Predefined sorting + if cat.def.sorting == "custom" then + for i=1,#cat.def.sorting_data do + local new_entry = table.copy(cat.entries[cat.def.sorting_data[i]]) + new_entry.eid = cat.def.sorting_data[i] + table.insert(entry_table, new_entry) + used_eids[cat.def.sorting_data[i]] = true + end + end for eid,entry in pairs(cat.entries) do local new_entry = table.copy(entry) new_entry.eid = eid - table.insert(entry_table, new_entry) + if not used_eids[eid] then + table.insert(entry_table, new_entry) + end table.insert(sort_table, entry.name) end - table.sort(sort_table) + if cat.def.sorting == "custom" then + return entry_table + else + table.sort(sort_table) + end local reverse_sort_table = table.copy(sort_table) for i=1, #sort_table do reverse_sort_table[sort_table[i]] = i end - -- Sorting algorithm local comp if cat.def.sorting ~= "nosort" then + -- Alphabetic sorting if cat.def.sorting == "abc" or cat.def.sorting == nil then comp = function(e1, e2) if reverse_sort_table[e1.name] < reverse_sort_table[e2.name] then return true else return false end -- cgit v1.2.3