page-pst.lua /size: 2765 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['page-pst'] = {
2    version   = 1.001,
3    comment   = "companion to page-pst.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
9-- todo: adapt message
10
11local tonumber, next, type = tonumber, next, type
12local find, validstring = string.find, string.valid
13
14local context     = context
15local implement   = interfaces.implement
16
17local texgetcount = tex.getcount
18local texsetcount = tex.setcount
19
20local sortedkeys   = table.sortedkeys
21local formatters   = string.formatters
22
23local cache = { }
24
25local function flush(page)
26    local c = cache[page]
27    if c then
28        for i=1,#c do
29         -- characters.showstring(c[i])
30            context.viafile(c[i],formatters["page.%s"](validstring(page,"nopage")))
31        end
32        cache[page] = nil
33    end
34end
35
36local function setnextpage()
37    local n = next(cache) and sortedkeys(cache)[1]
38    if not n then
39        n = 0           -- nothing in the cache
40    elseif n == 0 then
41        n = -1          -- generic buffer (0)
42    elseif n > 0 then
43                        -- upcoming page (realpageno)
44    end
45    texsetcount("global","c_page_postponed_blocks_next_page",n)
46end
47
48local function flushpostponedblocks(specification)
49    -- we need to flush previously pending pages as well and the zero
50    -- slot is the generic one so that one is always flushed
51    local t = sortedkeys(cache)
52    local p = tonumber(specification.page) or texgetcount("realpageno") or 0
53    for i=1,#t do
54        local ti = t[i]
55        if ti <= p then
56            flush(ti)
57        else
58            break
59        end
60    end
61    setnextpage()
62end
63
64implement {
65    name      = "flushpostponedblocks",
66    actions   = flushpostponedblocks,
67    arguments = {
68        {
69            { "page" }
70        }
71    }
72}
73
74local function registerpostponedblock(page)
75    if type(page) == "string" then
76        if find(page,"^+") then
77            page = texgetcount("realpageno") + (tonumber(page) or 1) -- future delta page
78        else
79            page = tonumber(page) or 0 -- preferred page or otherwise first possible occasion
80        end
81    end
82    if not page then
83        page = 0
84    end
85    local c = cache[page]
86    if not c then
87        c = { }
88        cache[page] = c
89    end
90    c[#c+1] = buffers.raw("postponedblock")
91    buffers.erase("postponedblock")
92    if page == 0 then
93        interfaces.showmessage("layouts",3,#c)
94    else
95        interfaces.showmessage("layouts",3,formatters["%s (realpage: %s)"](#c,page))
96    end
97    setnextpage()
98end
99
100implement {
101    name      = "registerpostponedblock",
102    actions   = registerpostponedblock,
103    arguments = "string"
104}
105
106