summaryrefslogtreecommitdiff
path: root/catalogue.lua
diff options
context:
space:
mode:
Diffstat (limited to 'catalogue.lua')
-rw-r--r--catalogue.lua73
1 files changed, 57 insertions, 16 deletions
diff --git a/catalogue.lua b/catalogue.lua
index 0eca2f3..5444d17 100644
--- a/catalogue.lua
+++ b/catalogue.lua
@@ -29,6 +29,24 @@ function mt:get(id)
return self.data[id]
end
+function mt:_ensure_catchall(section_id)
+ local id = section_id .. ':category:catchall'
+ local entry = self.data[id]
+ if entry then
+ return entry
+ end
+
+ return self:add({
+ section = section_id,
+ type = 'group',
+ group_type = 'category',
+ id = id,
+ parent_id = section_id .. ':category:0',
+ -- non-ascii symbol to sort near end
+ name = '∗CATCHALL∗',
+ })
+end
+
function mt:add(entry)
self.data[entry.id] = entry
@@ -37,28 +55,25 @@ function mt:add(entry)
end
if not entry.parent_id then
- return
+ return entry
end
+ local parent = self.data[entry.parent_id]
+
-- dump any entries referencing nonexistent categories into a single
-- catchall category
- if not self.data[entry.parent_id] then
- entry.parent_id = entry.section .. ':category:catchall'
- if not self.data[entry.parent_id] then
- self:add({
- section = entry.section,
- type = 'group',
- group_type = 'category',
- id = entry.parent_id,
- parent_id = entry.section .. ':category:0',
- -- non-ascii symbol to sort near end
- name = '∗CATCHALL∗',
- })
- end
+ if not parent then
+ parent = self:_ensure_catchall(entry.section)
+ entry.parent_id = parent.id
+ end
+
+ if parent.id == entry.id then
+ entry.parent_id = nil
+ else
+ parent.children[#parent.children+1] = entry
end
- local parent_children = self.data[entry.parent_id].children
- parent_children[#parent_children+1] = entry
+ return entry
end
function mt:load_xc_section(section)
@@ -111,4 +126,30 @@ function mt:load_xc_section(section)
end
end
+function mt:path_to_root(entry)
+ local path = {}
+
+ local curr = entry
+ while curr.parent_id and curr.parent_id ~= 'root' do
+ curr = self:get(curr.parent_id)
+ if not curr then
+ return
+ end
+ path[#path+1] = curr
+ end
+ if curr.parent_id ~= 'root' then
+ return
+ end
+
+ return path
+end
+
+function mt:path_from_root(entry)
+ local path = self:path_to_root(entry)
+ if path then
+ util.reverse(path)
+ end
+ return path
+end
+
return catalogue