page-ini.lua /size: 7121 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
page-ini
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to page-ini.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
local
tonumber
,
rawget
,
rawset
,
type
,
next
=
tonumber
,
rawget
,
rawset
,
type
,
next
10
local
match
=
string
.
match
11
local
sort
,
tohash
,
insert
,
remove
=
table
.
sort
,
table
.
tohash
,
table
.
insert
,
table
.
remove
12
local
settings_to_array
,
settings_to_hash
=
utilities
.
parsers
.
settings_to_array
,
utilities
.
parsers
.
settings_to_hash
13 14
local
texgetcount
=
tex
.
getcount
15 16
local
context
=
context
17
local
ctx_doifelse
=
commands
.
doifelse
18 19
local
implement
=
interfaces
.
implement
20 21
local
data
=
table
.
setmetatableindex
(
"
table
"
)
22
local
last
=
0
23
local
pages
=
structures
.
pages
24
local
autolist
=
{
}
25
local
report
=
logs
.
reporter
(
"
pages
"
,
"
mark
"
)
26 27
local
trace
=
false
trackers
.
register
(
"
pages.mark
"
,
function
(
v
)
trace
=
v
end
)
28 29
function
pages
.
mark
(
name
,
list
)
30
local
realpage
=
texgetcount
(
"
realpageno
"
)
31
if
not
list
or
list
=
=
"
"
then
32
if
trace
then
33
report
(
"
marking current page %i as %a
"
,
realpage
,
name
)
34
end
35
data
[
realpage
]
[
name
]
=
true
36
return
37
end
38
if
type
(
list
)
=
=
"
string
"
then
39
list
=
settings_to_array
(
list
)
40
end
41
if
type
(
list
)
=
=
"
table
"
then
42
for
i
=
1
,
#
list
do
43
local
page
=
list
[
i
]
44
local
sign
=
false
45
if
type
(
page
)
=
=
"
string
"
then
46
local
f
,
t
=
match
(
page
,
"
(%d+)[:%-](%d+)
"
)
47
if
f
and
t
then
48
f
,
t
=
tonumber
(
f
)
,
tonumber
(
t
)
49
if
f
and
t
and
f
<
=
t
then
50
if
trace
then
51
report
(
"
marking page %i upto %i as %a
"
,
f
,
t
,
name
)
52
end
53
for
page
=
f
,
t
do
54
data
[
page
]
[
name
]
=
true
55
end
56
end
57
page
=
false
58
else
59
local
s
,
p
=
match
(
page
,
"
([%+%-])(%d+)
"
)
60
if
s
then
61
sign
,
page
=
s
,
p
62
end
63
end
64
end
65
if
page
then
66
page
=
tonumber
(
page
)
67
if
page
then
68
if
sign
=
=
"
+
"
then
69
page
=
realpage
+
page
70
end
71
if
sign
=
=
"
-
"
then
72
report
(
"
negative page numbers are not supported
"
)
73
else
74
if
trace
then
75
report
(
"
marking page %i as %a
"
,
page
,
name
)
76
end
77
data
[
page
]
[
name
]
=
true
78
end
79
end
80
end
81
end
82
else
83
if
trace
then
84
report
(
"
marking current page %i as %a
"
,
realpage
,
name
)
85
end
86
data
[
realpage
]
[
name
]
=
true
87
end
88
end
89 90
local
function
marked
(
name
)
91
local
realpage
=
texgetcount
(
"
realpageno
"
)
92
for
i
=
last
,
realpage
-1
do
93
rawset
(
data
,
i
,
nil
)
94
end
95
local
pagedata
=
rawget
(
data
,
realpage
)
96
return
pagedata
and
pagedata
[
name
]
and
true
or
false
97
end
98 99
local
function
toranges
(
marked
)
100
local
list
=
{
}
101
local
size
=
#
marked
102
if
size
>
0
then
103
local
first
=
marked
[
1
]
104
local
last
=
first
105
for
i
=
2
,
size
do
106
local
page
=
marked
[
i
]
107
if
page
>
last
+
1
then
108
list
[
#
list
+
1
]
=
{
first
,
last
}
109
first
=
page
110
end
111
last
=
page
112
end
113
list
[
#
list
+
1
]
=
{
first
,
last
}
114
end
115
return
list
116
end
117 118
local
function
allmarked
(
list
)
119
if
list
then
120
local
collected
=
pages
.
collected
121
if
collected
then
122
if
type
(
list
)
=
=
"
string
"
then
123
list
=
settings_to_hash
(
list
)
124
elseif
type
(
list
)
=
=
"
table
"
and
#
list
>
0
then
125
list
=
tohash
(
list
)
126
end
127
if
type
(
list
)
=
=
"
table
"
then
128
local
found
=
{
}
129
for
name
in
next
,
list
do
130
for
page
,
list
in
next
,
data
do
131
if
list
[
name
]
and
collected
[
page
]
then
132
found
[
#
found
+
1
]
=
page
133
end
134
end
135
end
136
if
#
found
>
0
then
137
sort
(
found
)
138
if
trace
then
139
local
ranges
=
toranges
(
found
)
140
for
i
=
1
,
#
ranges
do
141
local
range
=
ranges
[
i
]
142
local
first
=
range
[
1
]
143
local
last
=
range
[
2
]
144
if
first
=
=
last
then
145
report
(
"
marked page : %i
"
,
first
)
146
else
147
report
(
"
marked range: %i upto %i
"
,
first
,
last
)
148
end
149
end
150
end
151
return
found
152
end
153
end
154
end
155
end
156
end
157 158
pages
.
marked
=
marked
159
pages
.
toranges
=
toranges
160
pages
.
allmarked
=
allmarked
161 162
-- An alternative is to use an attribute and identify the state by parsing the node
163
-- list but that's a bit overkill for a hardly used feature like this.
164 165
luatex
.
registerpageactions
(
function
(
)
166
local
nofauto
=
#
autolist
167
if
nofauto
>
0
then
168
local
realpage
=
texgetcount
(
"
realpageno
"
)
169
for
i
=
1
,
nofauto
do
170
local
names
=
autolist
[
i
]
171
for
j
=
1
,
#
names
do
172
local
name
=
names
[
j
]
173
data
[
realpage
]
[
name
]
=
true
174
if
trace
then
175
report
(
"
automatically marking page %i as %a
"
,
realpage
,
name
)
176
end
177
end
178
end
179
end
180
end
)
181 182
implement
{
183
name
=
"
markpage
"
,
184
arguments
=
"
2 strings
"
,
185
actions
=
pages
.
mark
186
}
187 188
implement
{
189
name
=
"
doifelsemarkedpage
"
,
190
arguments
=
"
string
"
,
191
actions
=
{
marked
,
ctx_doifelse
}
192
}
193 194
implement
{
195
name
=
"
markedpages
"
,
196
arguments
=
"
string
"
,
197
actions
=
function
(
name
)
198
local
t
=
allmarked
(
name
)
199
if
t
then
200
context
(
"
%,t
"
,
t
)
201
end
202
end
203
}
204 205
implement
{
206
name
=
"
startmarkpages
"
,
207
arguments
=
"
string
"
,
208
actions
=
function
(
name
)
209
insert
(
autolist
,
settings_to_array
(
name
)
)
210
end
211
}
212 213
implement
{
214
name
=
"
stopmarkpages
"
,
215
arguments
=
"
string
"
,
216
actions
=
function
(
name
)
217
if
#
autolist
>
0
then
218
remove
(
autolist
)
219
end
220
end
221
}
222 223
local
tonut
=
nodes
.
tonut
224
local
nextlist
=
nodes
.
nuts
.
traversers
.
list
225
local
texlists
=
tex
.
lists
226 227
implement
{
228
name
=
"
doifelsependingpagecontent
"
,
229
actions
=
function
(
)
230
local
h
=
texlists
.
contrib_head
231
-- local t = texlists.contrib_tail
232
local
p
=
false
233
if
h
then
234
for
n
in
nextlist
,
tonut
(
h
)
do
235
p
=
true
236
break
237
end
238
end
239
ctx_doifelse
(
p
)
240
end
,
241
}
242