2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00

count only leaf nodes of toc tree when level is zero

This commit is contained in:
chrox 2014-10-11 21:10:07 +08:00
parent 2b26e599d9
commit de38ea1199
2 changed files with 31 additions and 14 deletions

View File

@ -101,7 +101,8 @@ end
--[[
TOC ticks is a list of page number in ascending order of TOC nodes at certain level
positive level counts nodes of the depth level (level 1 for depth 1)
non-positive level counts nodes of reversed depth level (level -1 for max_depth-1)
negative level counts nodes of reversed depth level (level -1 for max_depth)
zero level counts leaf nodes of the toc tree
--]]
function ReaderToc:getTocTicks(level)
if self.ticks[level] then return self.ticks[level] end
@ -110,15 +111,26 @@ function ReaderToc:getTocTicks(level)
local ticks = {}
if #self.toc > 0 then
local depth = nil
if level > 0 then
depth = level
if level == 0 then
local depth = 0
for i = #self.toc, 1, -1 do
local v = self.toc[i]
if v.depth >= depth then
table.insert(ticks, v.page)
end
depth = v.depth
end
else
depth = self:getMaxDepth() + level
end
for _, v in ipairs(self.toc) do
if v.depth == depth then
table.insert(ticks, v.page)
local depth = nil
if level > 0 then
depth = level
else
depth = self:getMaxDepth() + level + 1
end
for _, v in ipairs(self.toc) do
if v.depth == depth then
table.insert(ticks, v.page)
end
end
end
-- normally the ticks are sorted already but in rare cases

View File

@ -25,21 +25,26 @@ describe("Readertoc module", function()
it("should get ticks of level 0", function()
ticks_level_0 = toc:getTocTicks(0)
DEBUG("ticks", ticks_level_0)
assert.are.same(26, #ticks_level_0)
assert.are.same(28, #ticks_level_0)
end)
local ticks_level_1 = nil
it("should get ticks of level 1", function()
ticks_level_1 = toc:getTocTicks(1)
assert.are.same(7, #ticks_level_1)
end)
local ticks_level_2 = nil
it("should get ticks of level 2", function()
ticks_level_2 = toc:getTocTicks(2)
assert.are.same(26, #ticks_level_2)
end)
local ticks_level_m1 = nil
it("should get ticks of level -1", function()
ticks_level_m1 = toc:getTocTicks(1)
assert.are.same(7, #ticks_level_m1)
ticks_level_m1 = toc:getTocTicks(-1)
assert.are.same(26, #ticks_level_m1)
end)
it("should get the same ticks of level -1 and level 1", function()
it("should get the same ticks of level -1 and level 2", function()
if toc_max_depth == 2 then
assert.are.same(ticks_level_1, ticks_level_m1)
assert.are.same(ticks_level_2, ticks_level_m1)
end
end)
end)