node-pro.lua /size: 6860 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
node-pro
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to node-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
trace_callbacks
=
false
trackers
.
register
(
"
nodes.callbacks
"
,
function
(
v
)
trace_callbacks
=
v
end
)
10
local
force_processors
=
false
directives
.
register
(
"
nodes.processors.force
"
,
function
(
v
)
force_processors
=
v
end
)
11 12
local
report_nodes
=
logs
.
reporter
(
"
nodes
"
,
"
processors
"
)
13 14
local
nodes
=
nodes
15
local
tasks
=
nodes
.
tasks
16
local
nuts
=
nodes
.
nuts
17
local
tonut
=
nodes
.
tonut
18 19
nodes
.
processors
=
nodes
.
processors
or
{
}
20
local
processors
=
nodes
.
processors
21 22
-- vbox: grouptype: vbox vtop output split_off split_keep | box_type: exactly|aditional
23
-- hbox: grouptype: hbox adjusted_hbox(=hbox_in_vmode) | box_type: exactly|aditional
24 25
local
actions
=
tasks
.
actions
(
"
processors
"
)
26 27
do
28 29
local
isglyph
=
nuts
.
isglyph
30
local
getnext
=
nuts
.
getnext
31 32
local
utfchar
=
utf
.
char
33
local
concat
=
table
.
concat
34 35
local
n
=
0
36 37
local
function
reconstruct
(
head
)
-- we probably have a better one
38
local
t
,
n
,
h
=
{
}
,
0
,
head
39
while
h
do
40
n
=
n
+
1
41
local
char
,
id
=
isglyph
(
h
)
42
if
char
then
-- todo: disc etc
43
t
[
n
]
=
utfchar
(
char
)
44
else
45
t
[
n
]
=
"
[]
"
46
end
47
h
=
getnext
(
h
)
48
end
49
return
concat
(
t
)
50
end
51 52
function
processors
.
tracer
(
what
,
head
,
groupcode
,
before
,
after
,
show
)
53
if
not
groupcode
then
54
groupcode
=
"
unknown
"
55
elseif
groupcode
=
=
"
"
then
56
groupcode
=
"
mvl
"
57
end
58
n
=
n
+
1
59
if
show
then
60
report_nodes
(
"
%s: location %a, group %a, # before %a, # after %s, stream: %s
"
,
what
,
n
,
groupcode
,
before
,
after
,
reconstruct
(
head
)
)
61
else
62
report_nodes
(
"
%s: location %a, group %a, # before %a, # after %s
"
,
what
,
n
,
groupcode
,
before
,
after
)
63
end
64
end
65 66
end
67 68
processors
.
enabled
=
true
-- this will become a proper state (like trackers)
69 70
do
71 72
local
has_glyph
=
nodes
.
has_glyph
73
local
count_nodes
=
nodes
.
countall
74 75
local
texget
=
tex
.
get
76 77
local
tracer
=
processors
.
tracer
78 79
local
function
pre_linebreak_filter
(
head
,
groupcode
)
80
local
found
=
force_processors
or
has_glyph
(
head
)
81
if
found
then
82
if
trace_callbacks
then
83
local
before
=
count_nodes
(
head
,
true
)
84
head
=
actions
(
head
,
groupcode
)
85
local
after
=
count_nodes
(
head
,
true
)
86
tracer
(
"
pre_linebreak
"
,
head
,
groupcode
,
before
,
after
,
true
)
87
else
88
head
=
actions
(
head
,
groupcode
)
89
end
90
elseif
trace_callbacks
then
91
local
n
=
count_nodes
(
head
,
false
)
92
tracer
(
"
pre_linebreak
"
,
head
,
groupcode
,
n
,
n
)
93
end
94
return
head
95
end
96 97
local
function
hpack_filter
(
head
,
groupcode
,
size
,
packtype
,
direction
,
attributes
)
98
local
found
=
force_processors
or
has_glyph
(
head
)
99
if
found
then
100
--
101
-- yes or no or maybe an option
102
--
103
if
not
direction
then
104
direction
=
texget
(
"
textdir
"
)
105
end
106
--
107
if
trace_callbacks
then
108
local
before
=
count_nodes
(
head
,
true
)
109
head
=
actions
(
head
,
groupcode
,
size
,
packtype
,
direction
,
attributes
)
110
local
after
=
count_nodes
(
head
,
true
)
111
tracer
(
"
hpack
"
,
head
,
groupcode
,
before
,
after
,
true
)
112
else
113
head
=
actions
(
head
,
groupcode
,
size
,
packtype
,
direction
,
attributes
)
114
end
115
elseif
trace_callbacks
then
116
local
n
=
count_nodes
(
head
,
false
)
117
tracer
(
"
hpack
"
,
head
,
groupcode
,
n
,
n
)
118
end
119
return
head
120
end
121 122
processors
.
pre_linebreak_filter
=
pre_linebreak_filter
123
processors
.
hpack_filter
=
hpack_filter
124 125
do
126 127
local
hpack
=
nodes
.
hpack
128 129
function
nodes
.
fullhpack
(
head
,
...
)
130
return
hpack
(
(
hpack_filter
(
head
)
)
,
...
)
131
end
132 133
end
134 135
do
136 137
local
hpack
=
nuts
.
hpack
138 139
function
nuts
.
fullhpack
(
head
,
...
)
140
return
hpack
(
tonut
(
hpack_filter
(
tonode
(
head
)
)
)
,
...
)
141
end
142 143
end
144 145
callbacks
.
register
(
'
pre_linebreak_filter
'
,
pre_linebreak_filter
,
"
horizontal manipulations (before par break)
"
)
146
callbacks
.
register
(
'
hpack_filter
'
,
hpack_filter
,
"
horizontal manipulations (before hbox creation)
"
)
147 148
end
149 150
do
151
-- Beware, these are packaged boxes so no first_glyph test needed. Maybe some day I'll add a hash
152
-- with valid groupcodes. Watch out, much can pass twice, for instance vadjust passes two times,
153 154
local
actions
=
tasks
.
actions
(
"
finalizers
"
)
-- head, where
155
local
count_nodes
=
nodes
.
countall
156 157
local
tracer
=
processors
.
tracer
158 159
local
function
post_linebreak_filter
(
head
,
groupcode
)
160
if
trace_callbacks
then
161
local
before
=
count_nodes
(
head
,
true
)
162
head
=
actions
(
head
,
groupcode
)
163
local
after
=
count_nodes
(
head
,
true
)
164
tracer
(
"
post_linebreak
"
,
head
,
groupcode
,
before
,
after
,
true
)
165
else
166
head
=
actions
(
head
,
groupcode
)
167
end
168
return
head
169
end
170 171
processors
.
post_linebreak_filter
=
post_linebreak_filter
172 173
callbacks
.
register
(
"
post_linebreak_filter
"
,
post_linebreak_filter
,
"
horizontal manipulations (after par break)
"
)
174 175
end
176 177
do
178 179
----- texnest = tex.nest
180
local
getnest
=
tex
.
getnest
181 182
local
getlist
=
nuts
.
getlist
183
local
setlist
=
nuts
.
setlist
184
local
getsubtype
=
nuts
.
getsubtype
185 186
local
linelist_code
=
nodes
.
listcodes
.
line
187 188
local
actions
=
tasks
.
actions
(
"
contributers
"
)
189 190
function
processors
.
contribute_filter
(
groupcode
)
191
if
groupcode
=
=
"
box
"
then
-- "pre_box"
192
local
whatever
=
getnest
(
)
193
if
whatever
then
194
local
line
=
whatever
.
tail
195
if
line
then
196
line
=
tonut
(
line
)
197
if
getsubtype
(
line
)
=
=
linelist_code
then
198
local
head
=
getlist
(
line
)
199
if
head
then
200
local
result
=
actions
(
head
,
groupcode
,
line
)
201
if
result
and
result
~
=
head
then
202
setlist
(
line
,
result
)
203
end
204
end
205
end
206
end
207
end
208
end
209
end
210 211
callbacks
.
register
(
"
contribute_filter
"
,
processors
.
contribute_filter
,
"
things done with lines
"
)
212 213
end
214 215
statistics
.
register
(
"
h-node processing time
"
,
function
(
)
216
return
statistics
.
elapsedseconds
(
nodes
,
"
including kernel
"
)
-- hm, ok here?
217
end
)
218