1if not modules then modules = { } end modules [ ' node-ser ' ] = {
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
10
11
12local type , tostring = type , tostring
13local concat , tohash , sortedkeys , printtable , serialize = table . concat , table . tohash , table . sortedkeys , table . print , table . serialize
14local formatters , format , rep = string . formatters , string . format , string . rep
15
16local allocate = utilities . storage . allocate
17
18local context = context
19local nodes = nodes
20local node = node
21
22local traverse = nodes . traverse
23local is_node = nodes . is_node
24
25local nodecodes = nodes . nodecodes
26local subtcodes = nodes . codes
27local getfields = nodes . fields
28
29local tonode = nodes . tonode
30local tonut = nodes . tonut
31
32local hlist_code = nodecodes . hlist
33local vlist_code = nodecodes . vlist
34
35
36local f_char = formatters [ " %U " ]
37local f_attr = formatters [ " <%i> " ]
38
39
40
41
42
43
44local expand = allocate ( tohash {
45
46 " list " ,
47 " pre " ,
48 " post " ,
49 " replace " ,
50 " top_skip " ,
51 " attr " ,
52 " components " ,
53 " box_left " ,
54 " box_right " ,
55 " glyph " ,
56 " leader " ,
57 " action " ,
58 " value " ,
59 " head " ,
60
61 " nucleus " ,
62 " sup " ,
63 " sub " ,
64 " list " ,
65 " num " ,
66 " denom " ,
67 " left " ,
68 " right " ,
69 " display " ,
70 " text " ,
71 " script " ,
72 " scriptscript " ,
73 " delim " ,
74 " degree " ,
75 " accent " ,
76 " bot_accent " ,
77} )
78
79
80
81
82local ignore = allocate ( tohash {
83 " page_insert " ,
84 " split_insert " ,
85 " ref_count " ,
86} )
87
88local dimension = allocate ( tohash {
89 " width " , " height " , " depth " , " shift " ,
90 " stretch " , " shrink " ,
91 " xoffset " , " yoffset " ,
92 " surround " ,
93 " kern " ,
94 " box_left_width " , " box_right_width "
95} )
96
97
98
99
100
101nodes . dimensionfields = dimension
102nodes . listablefields = expand
103nodes . ignorablefields = ignore
104
105
106
107local function astable ( n , sparse )
108 n = tonode ( n )
109 local f = getfields ( n )
110 local t = { }
111 for i = 1 , # f do
112 local v = f [ i ]
113 local d = n [ v ]
114 if d then
115 if ignore [ v ] or v = = " id " then
116
117 elseif expand [ v ] then
118 t [ v ] = " <list> "
119 elseif sparse then
120 if ( type ( d ) = = " number " and d ~ = 0 ) or ( type ( d ) = = " string " and d ~ = " " ) then
121 t [ v ] = d
122 end
123 else
124 t [ v ] = d
125 end
126 end
127 end
128 t . type = nodecodes [ n . id ]
129 return t
130end
131
132nodes . astable = astable
133
134setinspector ( " node " , function ( v ) if is_node ( v ) then printtable ( astable ( v ) , tostring ( v ) ) return true end end )
135
136
137
138local function totable ( n , flat , verbose , noattributes )
139 local function to_table ( n , flat , verbose , noattributes )
140 local f = getfields ( n )
141 local tt = { }
142 for k = 1 , # f do
143 local v = f [ k ]
144 local nv = v and n [ v ]
145 if nv then
146 if ignore [ v ] then
147
148 elseif noattributes and v = = " attr " then
149 tt [ v ] = f_attr ( tonut ( nv ) )
150
151 elseif v = = " prev " then
152 tt [ v ] = " <node> "
153 elseif expand [ v ] then
154 if type ( nv ) = = " number " or type ( nv ) = = " string " then
155 tt [ v ] = nv
156 else
157 tt [ v ] = totable ( nv , flat , verbose , noattributes )
158 end
159 elseif type ( nv ) = = " table " then
160 tt [ v ] = nv
161 else
162 tt [ v ] = nv
163 end
164 end
165 end
166 if verbose then
167 local subtype = tt . subtype
168 local id = tt . id
169 local nodename = nodecodes [ id ]
170 tt . id = nodename
171 local subtypes = subtcodes [ nodename ]
172 if subtypes then
173 tt . subtype = subtypes [ subtype ]
174 elseif subtype = = 0 then
175 tt . subtype = nil
176 else
177
178 end
179 if tt . char then
180 tt . char = f_char ( tt . char )
181 end
182 if tt . small_char then
183 tt . small_char = f_char ( tt . small_char )
184 end
185 if tt . large_char then
186 tt . large_char = f_char ( tt . large_char )
187 end
188 end
189 return tt
190 end
191 if n then
192 if flat then
193 local t , tn = { } , 0
194 while n do
195 tn = tn + 1
196 local nt = to_table ( n , flat , verbose , noattributes )
197 t [ tn ] = nt
198 nt . next = nil
199 nt . prev = nil
200 n = n . next
201 end
202 return t
203 else
204 local t = to_table ( n , flat , verbose , noattributes )
205 local n = n . next
206 if n then
207 t . next = totable ( n , flat , verbose , noattributes )
208 end
209 return t
210 end
211 else
212 return { }
213 end
214end
215
216nodes . totable = function ( n , ... ) return totable ( tonode ( n ) , ... ) end
217nodes . totree = function ( n ) return totable ( tonode ( n ) , true , true , true ) end
218
219local function key ( k )
220 return ( ( type ( k ) = = " number " ) and " [ " . . k . . " ] " ) or k
221end
222
223function nodes . serialize ( root , flat , verbose , noattributes , name )
224 return serialize ( totable ( tonode ( root ) , flat , verbose , noattributes ) , name )
225end
226
227function nodes . serializebox ( n , flat , verbose , noattributes , name )
228 return serialize ( totable ( tex . box [ n ] , flat , verbose , noattributes ) , name )
229end
230
231function nodes . visualizebox ( n , flat , verbose , noattributes , name )
232 context . tocontext ( totable ( tex . box [ n ] , flat , verbose , noattributes ) , name )
233end
234
235function nodes . list ( head , n )
236 head = tonode ( head )
237 if not n then
238 context . starttyping ( true )
239 end
240 while head do
241 local id = head . id
242 context ( rep ( " " , n or 0 ) . . tostring ( head ) . . " \n " )
243 if id = = hlist_code or id = = vlist_code then
244 nodes . list ( head . list , ( n or 0 ) + 1 )
245 end
246 head = head . next
247 end
248 if not n then
249 context . stoptyping ( true )
250 end
251end
252
253function nodes . print ( head , n )
254 head = tonode ( head )
255 while head do
256 local id = head . id
257 logs . writer ( string . formatters [ " %w%S " ] , n or 0 , head )
258 if id = = hlist_code or id = = vlist_code then
259 nodes . print ( head . list , ( n or 0 ) + 1 )
260 end
261 head = head . next
262 end
263end
264
265
266
267
268local function apply ( n , action )
269 while n do
270 action ( n )
271 local id = n . id
272 if id = = hlist_code or id = = vlist_code then
273 apply ( n . list , action )
274 end
275 n = n . next
276 end
277end
278
279nodes . apply = apply
280
281local nuts = nodes . nuts
282local getid = nuts . getid
283local getlist = nuts . getlist
284local getnext = nuts . getnext
285
286local function apply ( n , action )
287 while n do
288 action ( n )
289 local id = getid ( n )
290 if id = = hlist_code or id = = vlist_code then
291 local list = getlist ( n , action )
292 if list then
293 apply ( list , action )
294 end
295 end
296 n = getnext ( n )
297 end
298end
299
300nuts . apply = apply
301 |