summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/page-ini.lmt
blob: c2acd7f215f375eb09b28269ab13dc16b4396e73 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
if not modules then modules = { } end modules ['page-ini'] = {
    version   = 1.001,
    comment   = "companion to page-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- Some day I need to make this more efficient.

local tonumber, rawget, rawset, type, next = tonumber, rawget, rawset, type, next
local match = string.match
local sort, tohash, insert, remove, sortedkeys = table.sort, table.tohash, table.insert, table.remove, table.sortedkeys
local settings_to_array, settings_to_hash = utilities.parsers.settings_to_array, utilities.parsers.settings_to_hash

local texgetcount  = tex.getcount

local tonut        = nodes.tonut
local nextlist     = nodes.nuts.traversers.list
local texlists     = tex.lists

local context      = context
local ctx_doif     = commands.doif
local ctx_doifelse = commands.doifelse

local implement    = interfaces.implement

local data         = table.setmetatableindex("table")
local last         = 0
local pages        = structures.pages
local autolist     = { }
local report       = logs.reporter("pages","mark")
local active       = false

local trace        = false  trackers.register("pages.mark",function(v) trace = v end)

local c_realpageno = tex.iscount("realpageno")

function pages.mark(name,list,settings)
    active = true
    --
    local realpage = texgetcount(c_realpageno)
    if type(settings) == "string" then
        settings = settings_to_hash(settings)
    end
    if not list or list == "" then
        if trace then
            report("marking current page %i as %a",realpage,name)
        end
        data[realpage][name] = settings or true
        return
    end
    if type(list) == "string" then
        list = settings_to_array(list)
    end
    if type(list) == "table" then
        for i=1,#list do
            local page = list[i]
            local sign = false
            if type(page) == "string" then
                local f, t = match(page,"(%d+)[:%-](%d+)")
                if f and t then
                    f, t = tonumber(f), tonumber(t)
                    if f and t and f <= t then
                        if trace then
                            report("marking page %i upto %i as %a",f,t,name)
                        end
                        for page=f,t do
                            data[page][name] = settings or true
                        end
                    end
                    page = false
                else
                    local s, p = match(page,"([%+%-])(%d+)")
                    if s then
                        sign, page = s, p
                    end
                end
            end
            if page then
                page = tonumber(page)
                if page then
                    if sign == "+" then
                        page = realpage + page
                    end
                    if sign == "-" then
                        report("negative page numbers are not supported")
                    else
                        if trace then
                            report("marking page %i as %a",page,name)
                        end
                        data[page][name] = settings or true
                    end
                end
            end
        end
    else
        if trace then
            report("marking current page %i as %a",realpage,name)
        end
        data[realpage][name] = settings or true
    end
end

local tobemarked = { }

function pages.markedlist(realpage)
    if active and realpage then
        local m = rawget(tobemarked,realpage) or rawget(data,realpage)
        return m and next(m) and sortedkeys(m)
    end
end

local function marked(name)
    if active then
        local realpage = texgetcount(c_realpageno)
        if last ~= 0 then
            for i=last,realpage-1 do
    --     print(last)
                local di = data[i]
                if di then
                    tobemarked[i] = di
                    rawset(data,i,nil)
                end
            end
            last = 0 -- needs checking
        end
        local pagedata = rawget(data,realpage)
        return pagedata and pagedata[name] and true or false
    else
        return false
    end
end

local function markedparameter(name,key)
    if active then
        local pagedata = rawget(data,texgetcount(c_realpageno))
        if pagedata then
            pagedata = pagedata[name]
            if pagedata then
                pagedata = pagedata[key]
            end
        end
        return pagedata
    end
end

local function toranges(marked)
    local list = { }
    local size = #marked
    if size > 0 then
        local first = marked[1]
        local last  = first
        for i=2,size do
            local page = marked[i]
            if page > last + 1 then
                list[#list+1] = { first, last }
                first = page
            end
            last = page
        end
        list[#list+1] = { first, last }
        --
        active = true
    end
    return list
end

local function allmarked(list)
    if active and list then
        local collected = pages.collected
        if collected then
            if type(list) == "string" then
                list = settings_to_hash(list)
            elseif type(list) == "table" and #list > 0 then
                list = tohash(list)
            end
            if type(list) == "table" then
                local found = { }
                for name in next, list do
                    for page, list in next, data do
                        if list[name] and collected[page] then
                            found[#found+1] = page
                        end
                    end
                end
                if #found > 0 then
                    sort(found)
                    if trace then
                        local ranges = toranges(found)
                        for i=1,#ranges do
                            local range = ranges[i]
                            local first = range[1]
                            local last  = range[2]
                            if first == last then
                                report("marked page : %i",first)
                            else
                                report("marked range: %i upto %i",first,last)
                            end
                        end
                    end
                    return found
                end
            end
        end
    end
end

pages.marked    = marked
pages.toranges  = toranges
pages.allmarked = allmarked

-- An alternative is to use an attribute and identify the state by parsing the node
-- list but that's a bit overkill for a hardly used feature like this.

-- Page actions are bound to a real page. When we set one, we need to bind to the
-- current page unless we just flushed. So we also need to check the current page.

-- \page ... \start : sync realpage
-- \page     \start : sync realpage
-- \page     \stop  : reset synced because no content yet
-- \page ... \stop  : keep sync

local function autopageaction()
    if active then
        local nofauto = #autolist
        if nofauto > 0 then
            local realpage = texgetcount(c_realpageno)
            for i=1,nofauto do
                local entry    = autolist[i]
                local names    = entry[1]
                local settings = entry[2]
                for j=1,#names do
                    local name = names[j]
                    local list = data[realpage]
                    if not list[name] then
                        if trace then
                            report("automatically marking page %i as %a",realpage,name)
                        end
                        list[name] = settings or true
                    end
                end
            end
        end
    end
end

local function startmarked(name,settings)
    active = true
    --
    insert(autolist, { settings_to_array(name), settings_to_hash(settings) })
    autopageaction(true)
end

local function stopmarked()
    local nofauto = #autolist
    if nofauto > 0 then
        if not texlists.pagehead then
            local realpage = texgetcount(c_realpageno)
            for i=1,nofauto do
                local entry = autolist[i]
                local names = entry[1]
                for j=1,#names do
                    local name = names[j]
                    local list = data[realpage]
                    if list[name] then
                        if trace then
                            report("automatically unmarking page %i as %a",realpage,name)
                        end
                        list[name] = nil
                    end
                end
            end
        end
        remove(autolist)
    end
end

implement {
    name      = "checkmarkedpages",
    protected = true,
    public    = true,
    actions   = autopageaction,
}

implement {
    name      = "markpage",
    arguments = "3 strings",
    actions   = pages.mark
}

implement {
    name      = "doifelsemarkedpage",
    arguments = "argument",
    protected = true,
    public    = true,
    actions   = { marked, ctx_doifelse }
}

implement {
    name      = "doifmarkedpage",
    arguments = "argument",
    protected = true,
    public    = true,
    actions   = { marked, ctx_doif }
}

implement {
    name      = "markedpageparameter",
    arguments = "2 arguments",
    public    = true,
    actions   = function(name,key)
        local value = markedparameter(name,key)
        if value then
            context(value)
        end
    end
}

implement {
    name      = "markedpages",
    arguments = "string",
    actions   = function(name)
        local t = allmarked(name)
        if t then
            context("%,t",t)
        end
    end
}

implement {
    name      = "startmarkpages",
    arguments = "2 strings",
    actions   = startmarked,
}

implement {
    name      = "stopmarkpages",
    protected = true,
    public    = true,
    actions   = stopmarked,
}

implement {
    name      = "doifelsependingpagecontent",
    actions   = function()
        local h = texlists.contrib_head
     -- local t = texlists.contrib_tail
        local p = false
        if h then
            for n in nextlist, tonut(h) do
                p = true
                break
            end
        end
        ctx_doifelse(p)
    end,
}

-- implement {
--     name    = "pagestate",
--     public  = true,
--     usage   = "value",
--     actions = function()
--         local result = 0
--         if nodes.nuts.getspeciallist("contributehead") then
--             result = result | 1
--         end
--         if nodes.nuts.getspeciallist("pagehead") then
--             result = result | 2
--         end
--         return tokens.values.integer, result
--     end
-- }