summaryrefslogtreecommitdiff
path: root/tex/context/base/strc-blk.lua
blob: 5d6d90da9ed38e0e3e133d174fc2e744fddda694 (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
if not modules then modules = { } end modules ['strc--blk'] = {
    version   = 1.001,
    comment   = "companion to strc-blk.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- this one runs on top of buffers and structure

local texprint, format, gmatch, find = tex.print, string.format, string.gmatch, string.find
local lpegmatch = lpeg.match
local settings_to_set, settings_to_array = utilities.parsers.settings_to_set, utilities.parsers.settings_to_array

local ctxcatcodes = tex.ctxcatcodes

local structures  = structures

structures.blocks = structures.blocks or { }

local blocks   = structures.blocks
local sections = structures.sections
local lists    = structures.lists

blocks.collected = blocks.collected or { }
blocks.tobesaved = blocks.tobesaved or { }
blocks.states    = blocks.states    or { }

local tobesaved, collected, states = blocks.tobesaved, blocks.collected, blocks.states

local function initializer()
    collected, tobesaved = blocks.collected, blocks.tobesaved
end

job.register('structures.blocks.collected', blocks.tobesaved, initializer)

local printer = (lpeg.patterns.textline/texprint)^0 -- can be shared

function blocks.print(name,data,hide)
    if hide then
        texprint(ctxcatcodes,format("\\dostarthiddenblock{%s}",name))
    else
        texprint(ctxcatcodes,format("\\dostartnormalblock{%s}",name))
    end
    if type(data) == "table" then
        for i=1,#data do
            texprint(data[i])
        end
    else
        lpegmatch(printer,data)
    end
    if hide then
        texprint(ctxcatcodes,"\\dostophiddenblock")
    else
        texprint(ctxcatcodes,"\\dostopnormalblock")
    end
end

function blocks.define(name)
    states[name] = { all = "hide" }
end

function blocks.setstate(state,name,tag)
    local all = tag == ""
    local tags = not all and settings_to_array(tag)
    for n in gmatch(name,"%s*([^,]+)") do
        local sn = states[n]
        if not sn then
            -- error
        elseif all then
            sn.all = state
        else
            for _, tag in next, tags do
                sn[tag] = state
            end
        end
    end
end

function blocks.select(state,name,tag,criterium)
    criterium = criterium or "text"
    if find(tag,"=") then tag = "" end
    local names = settings_to_set(name)
    local all = tag == ""
    local tags = not all and settings_to_set(tag)
    local hide = state == "process"
    local n = sections.numberatdepth(criterium)
    local result = lists.filter_collected("all", criterium, n, collected, { })
    for i=1,#result do
        local ri = result[i]
        local metadata = ri.metadata
        if names[metadata.name] then
            if all then
                blocks.print(name,ri.data,hide)
            else
                local mtags = metadata.tags
                for tag, sta in next, tags do
                    if mtags[tag] then
                        blocks.print(name,ri.data,hide)
                        break
                    end
                end
            end
        end
    end
end

function blocks.save(name,tag,buffer) -- wrong, not yet adapted
    local data = buffers.data[buffer]
    local tags = settings_to_set(tag)
    local plus, minus = false, false
    if tags['+'] then plus  = true tags['+'] = nil end
    if tags['-'] then minus = true tags['-'] = nil end
    tobesaved[#tobesaved+1] = {
        metadata = {
            name = name,
            tags = tags,
            plus = plus,
            minus = minus,
        },
        references = {
            section  = sections.currentid(),
        },
        data = data or "error",
    }
    local allstate = states[name].all
    if not next(tags) then
        if allstate ~= "hide" then
            blocks.print(name,data)
        elseif plus then
            blocks.print(name,data,true)
        end
    else
        local sn = states[name]
        for tag, _ in next, tags do
            if sn[tag] == nil then
                if allstate ~= "hide" then
                    blocks.print(name,data)
                    break
                end
            elseif sn[tag] ~= "hide" then
                blocks.print(name,data)
                break
            end
        end
    end
    buffers.data[buffer] = nil
end