strc-lev.lua /size: 2575 b    last modification: 2020-07-01 14:35
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        ctx_nostarthead { f_two_colon(category,level) }
56    else
57        local lcl = lc[level]
58        if n > #lcl then
59            n = #lcl
60        end
61        ctx_dostarthead { lc[level][n] }
62    end
63    insert(categories,{ category, n })
64end
65
66local function stopsectionlevel()
67    local top = remove(categories)
68    if top then
69        local category = top[1]
70        local n = top[2]
71        local lc = levels[category]
72        if not lc or level > #lc then
73            ctx_nostophead { f_two_colon(category,level) }
74        else
75            ctx_dostophead { lc[level][n] }
76        end
77        level = level - 1
78    else
79        -- error
80    end
81end
82
83implement {
84    name      = "definesectionlevels",
85    actions   = definesectionlevels,
86    arguments = "2 strings",
87}
88
89implement {
90    name      = "startsectionlevel",
91    actions   = startsectionlevel,
92    arguments = { "integer", "string", "string" },
93}
94
95implement {
96    name      = "stopsectionlevel",
97    actions   = stopsectionlevel,
98}
99