summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/page-sel.lmt
blob: 2ca93c039d084a6d081b6a1419417005bfa4cb79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
if not modules then modules = { } end modules ['page-sel'] = {
    version   = 1.001,
    comment   = "companion to page-sel.mkxl",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

--- maybe more will end up here

local addsuffix = file.addsuffix
local context = context

do

    local sections = { }

    local function sectionpage(filename,reference,what)
        local filedata = sections[filename]
        if not filedata then
            filedata = {
                data = job.loadother(filename,"tuc") or false,
                list = { }
            }
            sections[filename] = filedata
        end
        local list  = filedata.list
        local entry = list[reference]
        if not entry then
            local first = 0
            local last  = 0
            local data  = filedata.data
            if data then
                local lists = data.structures.lists.collected
                local pages = data.structures.pages.collected
                if lists and pages then
                    for i=1,#lists do
                        local li = lists[i]
                        if li.references.reference == reference then
                            local level = li.metadata.level
                            first = li.references.realpage
                            last  = pages[#pages].number
                            for j=i+1,#lists do
                                local lj = lists[j]
                                if lj.metadata.level == level then
                                    last = lj.references.realpage - 1
                                    break
                                end
                            end
                        end
                    end
                end
            end
            entry = {
                first = first,
                last  = last,
            }
            list[reference] = entry
        end
        context(what and entry.first or entry.last)
    end

    interfaces.implement {
        name      = "firstsectionpage",
        public    = true,
        arguments = { "optional", "optional", true },
        actions   = sectionpage
    }

    interfaces.implement {
        name      = "lastsectionpage",
        public    = true,
        arguments = { "optional", "optional", false },
        actions   = sectionpage
    }

end