1if not modules then modules = { } end modules [ ' toks-ini ' ] = {
2 version = 1 . 001 ,
3 author = " Hans Hagen, PRAGMA-ADE, Hasselt NL " ,
4 copyright = " PRAGMA ADE / ConTeXt Development Team " ,
5 license = " see context related readme files "
6}
7
8
9
10local utfbyte , utfchar , utfvalues = utf . byte , utf . char , utf . values
11local format , gsub = string . format , string . gsub
12local tostring = tostring
13
14local tokens = tokens
15local token = token
16local tex = tex
17local context = context
18local commands = commands
19
20tokens . collectors = tokens . collectors or { }
21local collectors = tokens . collectors
22
23collectors . data = collectors . data or { }
24local collectordata = collectors . data
25
26collectors . registered = collectors . registered or { }
27local registered = collectors . registered
28
29local report = logs . reporter ( " tokens " , " collectors " )
30
31
32
33
34
35
36
37local get_next = token . get_next
38local create_token = token . create
39
40function collectors . install ( tag , end_cs )
41 local data , d = { } , 0
42 collectordata [ tag ] = data
43 end_cs = gsub ( end_cs , " ^\\ " , " " )
44 while true do
45 local t = get_next ( )
46 if t . csname = = end_cs then
47 context [ end_cs ] ( )
48 return
49 else
50 d = d + 1
51 data [ d ] = t
52 end
53 end
54end
55
56local simple = { letter = " letter " , other_char = " other " }
57
58function collectors . show ( data )
59
60
61
62 if type ( data ) = = " string " then
63 data = collectordata [ data ]
64 end
65 if not data then
66 return
67 end
68 local ctx_NC = context . NC
69 local ctx_NR = context . NR
70 local ctx_bold = context . bold
71 local ctx_verbatim = context . verbatim
72 context . starttabulate { " |Tl|Tc|Tl| " }
73 ctx_NC ( ) ctx_bold ( " cmd " )
74 ctx_NC ( ) ctx_bold ( " meaning " )
75 ctx_NC ( ) ctx_bold ( " properties " )
76 ctx_NC ( ) ctx_NR ( )
77 context . HL ( )
78 for i = 1 , # data do
79 local token = data [ i ]
80 local cmdname = token . cmdname
81 local simple = simple [ cmdname ]
82 ctx_NC ( )
83 ctx_verbatim ( simple or cmdname )
84 ctx_NC ( )
85 ctx_verbatim ( simple and utfchar ( token . mode ) or token . csname )
86 ctx_NC ( )
87 if token . active then context ( " active " ) end
88 if token . expandable then context ( " expandable " ) end
89 if token . protected then context ( " protected " ) end
90 ctx_NC ( )
91 ctx_NR ( )
92 end
93 context . stoptabulate ( )
94end
95
96local function printlist ( data )
97 if data and # data > 0 then
98 report ( " not supported (yet): printing back to tex " )
99 end
100end
101
102tokens . printlist = printlist
103
104function collectors . flush ( tag )
105 printlist ( collectordata [ tag ] )
106end
107
108function collectors . test ( tag , handle )
109 report ( " not supported (yet): testing " )
110end
111
112function collectors . register ( name )
113 report ( " not supported (yet): registering " )
114end
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
247commands . collecttokens = collectors . install
248commands . showtokens = collectors . show
249commands . flushtokens = collectors . flush
250commands . testtokens = collectors . test
251commands . registertoken = collectors . register
252
253
254
255
256
257
258
259
260
261collectors . dowithwords = collectors . test
262
263
264
265tokens . vbox = create_token ( " vbox " )
266tokens . hbox = create_token ( " hbox " )
267tokens . vtop = create_token ( " vtop " )
268tokens . bgroup = create_token ( utfbyte ( " { " ) , 1 )
269tokens . egroup = create_token ( utfbyte ( " } " ) , 2 )
270
271tokens . letter = function ( chr ) return create_token ( utfbyte ( chr ) , 11 ) end
272tokens . other = function ( chr ) return create_token ( utfbyte ( chr ) , 12 ) end
273
274tokens . letters = function ( str )
275 local t , n = { } , 0
276 for chr in utfvalues ( str ) do
277 n = n + 1
278 t [ n ] = create_token ( chr , 11 )
279 end
280 return t
281end
282
283function collectors . defaultwords ( t , str )
284 if t then
285 local n = # t
286 n = n + 1 ; t [ n ] = tokens . bgroup
287 n = n + 1 ; t [ n ] = create_token ( " red " )
288 for i = 1 , # str do
289 n = n + 1 ; t [ n ] = tokens . other ( ' * ' )
290 end
291 n = n + 1 ; t [ n ] = tokens . egroup
292 end
293end
294 |