summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/strc-lev.lua
blob: ec5dcf6f0a56c67e2e9876793f486ff247075c83 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
if not modules then modules = { } end modules ['strc-lev'] = {
    version   = 1.001,
    comment   = "companion to strc-lev.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local insert, remove = table.insert, table.remove
local settings_to_array = utilities.parsers.settings_to_array

local context     = context
local interfaces  = interfaces

local sections    = structures.sections
local implement   = interfaces.implement

local v_default   = interfaces.variables.default
local v_auto      = interfaces.variables.auto

sections.levels   = sections.levels or { }

local level       = 0
local levels      = sections.levels
local categories  = { }

local f_two_colon = string.formatters["%s:%s"]

storage.register("structures/sections/levels", levels, "structures.sections.levels")

local function definesectionlevels(category,list)
    list = settings_to_array(list)
    for i=1,#list do
        list[i] = settings_to_array(list[i])
    end
    levels[category] = list
end

local function startsectionlevel(n,category,current)
    category = category ~= "" and category or v_default
    local lc = levels[category]
    for i=1,#lc do
        local lcl = lc[i]
        if (lcl[n] or lcl[1]) == current then
            level = i
            break
        end
    end
    level = level + 1
    if not lc or level > #lc then
        context.nostarthead { f_two_colon(category,level) }
    else
        local lcl = lc[level]
        if n > #lcl then
            n = #lcl
        end
        context.dostarthead { lc[level][n] }
    end
    insert(categories,{ category, n })
end

local function stopsectionlevel()
    local top = remove(categories)
    if top then
        local category = top[1]
        local n = top[2]
        local lc = levels[category]
        if not lc or level > #lc then
            context.nostophead { f_two_colon(category,level) }
        else
            context.dostophead { lc[level][n] }
        end
        level = level - 1
    else
        -- error
    end
end

implement {
    name      = "definesectionlevels",
    actions   = definesectionlevels,
    arguments = { "string", "string" },
}

implement {
    name      = "startsectionlevel",
    actions   = startsectionlevel,
    arguments = { "integer", "string", "string" },
}

implement {
    name      = "stopsectionlevel",
    actions   = stopsectionlevel,
}