strc-lev.lua /size: 2863 b    last modification: 2025-02-21 11:03
1if not modules then modules = { } end modules ['strc-lev'] = {
2    version   = 1.001,
3    comment   = "companion to strc-lev.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local insert, remove = table.insert, table.remove
10local settings_to_array = utilities.parsers.settings_to_array
11
12local context     = context
13local interfaces  = interfaces
14
15local sections    = structures.sections
16local implement   = interfaces.implement
17
18local v_default   = interfaces.variables.default
19
20sections.levels   = sections.levels or { }
21
22local level       = 0
23local levels      = sections.levels
24local categories  = { }
25
26local f_two_colon = string.formatters["%s:%s"]
27
28storage.register("structures/sections/levels", levels, "structures.sections.levels")
29
30local function definesectionlevels(category,list)
31    list = settings_to_array(list)
32    for i=1,#list do
33        list[i] = settings_to_array(list[i])
34    end
35    levels[category] = list
36end
37
38local ctx_nostarthead = context.nostarthead
39local ctx_dostarthead = context.dostarthead
40local ctx_nostophead  = context.nostophead
41local ctx_dostophead  = context.dostophead
42
43local function startsectionlevel(n,category,current)
44    category = category ~= "" and category or v_default
45    local lc = levels[category]
46    for i=1,#lc do
47        local lcl = lc[i]
48        if (lcl[n] or lcl[1]) == current then
49            level = i
50            break
51        end
52    end
53    level = level + 1
54    if not lc or level > #lc then
55    level = level - 1
56        local what = f_two_colon(category,level)
57        ctx_nostarthead { what }
58        insert(categories,{ category, n, level, what })
59    else
60        local lcl = lc[level]
61        if n > #lcl then
62            n = #lcl
63        end
64        ctx_dostarthead { lcl[n] }
65        insert(categories,{ category, n })
66    end
67end
68
69local function stopsectionlevel()
70    local top = remove(categories)
71    if top then
72        local category = top[1]
73        local n        = top[2]
74        local bad      = top[3]
75        local what     = top[4]
76        local lc       = levels[category]
77        if not lc or bad then
78            ctx_nostophead { what }
79            level = bad
80        else
81            local lcl = lc[level]
82            if n > #lcl then
83                n = #lcl
84            end
85            ctx_dostophead { lcl[n] }
86            level = level - 1
87        end
88    else
89        ctx_nostophead { "?" }
90    end
91end
92
93implement {
94    name      = "definesectionlevels",
95    actions   = definesectionlevels,
96    arguments = "2 strings",
97}
98
99implement {
100    name      = "startsectionlevel",
101    actions   = startsectionlevel,
102    arguments = { "integer", "string", "string" },
103}
104
105implement {
106    name      = "stopsectionlevel",
107    actions   = stopsectionlevel,
108}
109