Debian Codegraph @

texmf-dist/tex/lualatex/luamml/luamml-tex-annotate.lua

raw
1 --[[
2 This file defines the luafunctions
3 * \__luamml_annotate_begin:
4 * \__luamml_annotate_end:we
5 * \__luamml_annotate_end:e
6
7 It defines the global function
8 * consume_label
9
10 It returns
11 * mark_environment: a table with annotation data used as _ENV
12
13 Core function is
14 * annotate (used in the luafunctions)
15
16 TODO: should annotate() error if there are no noads in the argument?
17 --]]
18
19 local nest = tex.nest
20
21 local properties = node.get_properties_table()
22 local simple_noad = node.id'noad'
23
24 local mark_environment = {
25 data = {
26 },
27 }
28 do
29 local _ENV = mark_environment
30 function consume_label(label, fn)
31 local mathml, core = data.mathml[label], data.mathml_core[label]
32 data.mathml[label], data.mathml_core[label] = nil, nil
33 if fn then
34 mathml, core = fn(mathml, core)
35 end
36 return mathml
37 end
38 end
39
40 -- If mathml_table and/or mathml_core is set while we also have filters
41 -- apply the filter directly.
42 local function flatten_mathml_properties(props)
43 local mathml, core = props.mathml_table, props.mathml_core
44 if mathml == nil and core == nil then return end
45 local filter = props.mathml_filter
46 if filter == nil then return end
47 props.mathml_table, props.mathml_core = filter(mathml or core, core)
48 props.mathml_filter = nil
49 end
50
51 --[[
52 The function annotate receives a "key-val"-list as arguments.
53 It stores a table (annotation) in the properties of a node.
54 And knows the following "keys"
55 * nucleus, boolean: decides if the annotation is stored
56 in a noad or the nucleus of a noad.
57 The noad is a wrapper around the inner kernel noad which contains the class
58 \mathbin, \mathord, etc., the superscript, subscript, ....
59 So by e.g. replacing the noad with an annotation all of this gets replaced, while
60 replacing the nucleus only replaces the main math expression
61 (which is often what you want).
62 For more complicated math structures (e.g. square roots) on the other hand
63 annotations should usually affect the whole noad and not just
64 the nucleus (which would be e.g. 2 in \sqrt{2}.
65 * offset, 0< int <= noad count: decides on which noad the annotation is stored.
66 * mathml, table: contains the mathml representation.
67 See luamml-algorithm.tex for examples how the table should look like.
68 Stored as `mathml_table` in the annotation.
69 * core, table or false: contains the core operator of the mathml representation.
70 When core is set it also gets set for `mathml` by default. If both mathml and core are
71 set you want core to be set to `false` or `space_like`.
72 See luamml-algorithm.tex for examples how the core table should look like.
73 Stored as `mathml_core` in the annotation.
74 * struct, string: a label referencing a stashed, labeled structure.
75 Stored as a function mathml_filter, that adds the field :struct to the mathml.
76 * structnum, number: a structure number referencing a stashed structure.
77 Stored as a function mathml_filter, that adds the field :structnum to the mathml.
78 TODO: are they (and mathml_filter) actually used somewhere?
79 See luamml-convert.lua for the processing.
80 --]]
81
82 local function annotate()
83 local annotation, err = load( 'return {'
84 .. token.scan_argument()
85 .. '}', nil, 't', mark_environment)
86 if not annotation then
87 tex.error('Error while parsing LuaMML annotation', {err})
88 return 0
89 end
90 annotation = annotation()
91 local nesting = nest.top
92 local props = properties[nesting.head]
93 -- `luamml__annotate_context` is initialized below in `__luamml_annotate_begin:`
94 -- and has two (optional) fields:
95 -- `head` contains a pointer to the tail of current list.
96 -- `prev` contains data of previous annotate commands in the same list.
97 local current = props and props.luamml__annotate_context
98 if current then
99 current, props.luamml__annotate_context = current.head, current.prev
100 else
101 tex.error('Mismatched LuaMML annotation',
102 {'Something odd happened. Maybe you forgot braces around an annotated symbol in a subscript or superscript?'})
103 return 0
104 end
105 local after = nesting.tail
106 local count, offset = 0, annotation.offset
107 local marked
108 -- TODO should this error or silently return?
109 if current == after then
110 tex.error'Empty LuaMML annotation'
111 else
112 repeat
113 current = current.next
114 count = count + 1
115 if count == offset then
116 marked = current
117 elseif offset or current ~= after then
118 local props = properties[current]
119 if not props then
120 props = {}
121 properties[current] = props
122 end
123 props.mathml_table, props.mathml_core = nil, false
124 end
125 until current == after
126 if offset and not marked then
127 tex.error'Invalid offset in LuaMML annotation'
128 end
129 marked = marked or current
130 if annotation.nucleus then
131 marked = marked.nucleus
132 end
133 if marked then
134 local props = properties[marked]
135 if not props then
136 props = {}
137 properties[marked] = props
138 end
139 if annotation.mathml ~= nil or annotation.core ~= nil then
140 props.mathml_table, props.mathml_core, props.mathml_filter = annotation.mathml, annotation.core, nil
141 end
142 if annotation.struct ~= nil then
143 local saved = props.mathml_filter
144 local struct = annotation.struct
145 function props.mathml_filter(mml, core)
146 mml[':struct'] = struct
147 if saved then
148 return saved(mml, core)
149 else
150 return mml, core
151 end
152 end
153 end
154 if annotation.structnum ~= nil then
155 local saved = props.mathml_filter
156 local structnum = annotation.structnum
157 function props.mathml_filter(mml, core)
158 mml[':structnum'] = structnum
159 if saved then
160 return saved(mml, core)
161 else
162 return mml, core
163 end
164 end
165 end
166 flatten_mathml_properties(props)
167 else
168 tex.error'Unable to annotate nucleus of node without nucleus'
169 end
170 end
171 return count
172 end
173
174 local function annotate_attribute(key, value, change_core)
175 local nesting = nest.top
176 local props = properties[nesting.head]
177 -- See above for `luamml__annotate_context`, the mechanism is shared with regular annotations.
178 local current = props and props.luamml__annotate_context
179 if current then
180 current, props.luamml__annotate_context = current.head, current.prev
181 else
182 tex.error('Mismatched LuaMML annotation',
183 {'Something odd happened. Maybe you forgot braces around an annotated symbol in a subscript or superscript?'})
184 return 0
185 end
186 local after = nesting.tail
187 if current == after then
188 tex.error'Empty LuaMML attribute list'
189 end
190 local marked = current.next
191 if marked ~= after then
192 tex.error("Multiple math nodes in LuaMML attribute list. The attribute will be ignored.",
193 {"There was an attempt to add an attribute to a list of multiple nodes. Since the list will usually not result \z
194 in a single MathML node, this is not allowed. Maybe try adding braces around the argument?"})
195 return
196 end
197 if marked.id == simple_noad and not (marked.sub or marked.sup) then
198 marked = marked.nucleus
199 end
200 local props = properties[marked]
201 if not props then
202 props = {}
203 properties[marked] = props
204 end
205 local saved = props.mathml_filter
206 function props.mathml_filter(mml, core)
207 if core and change_core then
208 core[key] = value
209 else
210 mml[key] = value
211 end
212 if saved then
213 return saved(mml, core)
214 else
215 return mml, core
216 end
217 end
218 flatten_mathml_properties(props)
219 end
220
221 --[[ Documentation for luafunction \__luamml_annotate_begin:
222 This function initialize an annotation.
223 It stores in the properties of head of the current list
224 the current tail node and previous annotate context of previous
225 annotations in the same list.
226 --]]
227
228 local funcid = luatexbase.new_luafunction'__luamml_annotate_begin:'
229 token.set_lua('__luamml_annotate_begin:', funcid, 'protected')
230 lua.get_functions_table()[funcid] = function()
231 local top = nest.top
232 local temp = top.head
233 local props = properties[temp]
234 if not props then
235 props = {}
236 properties[temp] = props
237 end
238 props.luamml__annotate_context = {
239 prev = props.luamml__annotate_context,
240 head = top.tail,
241 }
242 end
243
244 --[[ Documentation for luafunction \__luamml_annotate_end:we
245 This function calls annotate(). Additionally it compares
246 its first argument (a number) with the count of noads done by
247 annotate(). With luatex this is not really needed and only a source
248 of errors, so the next function should be preferred.
249 --]]
250
251 funcid = luatexbase.new_luafunction'__luamml_annotate_end:we'
252 token.set_lua('__luamml_annotate_end:we', funcid, 'protected')
253 lua.get_functions_table()[funcid] = function()
254 local count = token.scan_int()
255 local real_count = annotate()
256 if count ~= real_count then
257 tex.error('Incorrect count in LuaMML annotation', {
258 'A LuaMML annotation was discovered with an explicit count \z
259 which was not the same as the number of top-level nodes annotated.',
260 string.format('This can be fixed by changing the supplied count from %i to %i \z
261 or by omitting the count value entirely.', count, real_count)
262 })
263 end
264 end
265
266 --[[ Documentation for luafunction \__luamml_annotate_end:e
267 This function calls annotate().
268 --]]
269
270 funcid = luatexbase.new_luafunction'__luamml_annotate_end:e'
271 token.set_lua('__luamml_annotate_end:e', funcid, 'protected')
272 lua.get_functions_table()[funcid] = annotate
273
274 --[[ Documentation for luafunction \__luamml_annotate_end:e
275 This function calls annotate_attribute().
276 --]]
277
278 funcid = luatexbase.new_luafunction'__luamml_annotate_attribute_end:e'
279 token.set_lua('__luamml_annotate_attribute_end:wee', funcid, 'protected')
280 lua.get_functions_table()[funcid] = function()
281 local core = token.scan_keyword'core'
282 local key = token.scan_argument()
283 local value = token.scan_argument()
284 return annotate_attribute(key, value, core)
285 end
286
287
288 return mark_environment