| 1 |
--[[ |
| 2 |
This file defines the luafunctions |
| 3 |
* \__luamml_array_init_col: |
| 4 |
* \__luamml_array_finalize_col:w |
| 5 |
* \__luamml_array_save_array: |
| 6 |
* \__luamml_array_finalize_array: |
| 7 |
|
| 8 |
The functions are used in tagging sockets, that are used in array.sty and |
| 9 |
handle the math tagging of the array environment. |
| 10 |
|
| 11 |
--]] |
| 12 |
|
| 13 |
-- TODO why are 4 these loaded? |
| 14 |
local write_xml = require'luamml-xmlwriter' |
| 15 |
local make_root = require'luamml-convert'.make_root |
| 16 |
local save_result = require'luamml-tex'.save_result |
| 17 |
local store_tag = require'luamml-table'.store_tag |
| 18 |
|
| 19 |
local store_column = require'luamml-table'.store_column |
| 20 |
local store_column_xml = require'luamml-table'.store_column_xml |
| 21 |
local get_table = require'luamml-table'.get_table |
| 22 |
local to_text = require'luamml-lr' |
| 23 |
|
| 24 |
local properties = node.get_properties_table() |
| 25 |
|
| 26 |
--[[ Documentation of luafunctions \__luamml_array_init_col: |
| 27 |
Initialization code for a cell, used in \@classz |
| 28 |
TODO: error handling |
| 29 |
--]] |
| 30 |
|
| 31 |
local funcid = luatexbase.new_luafunction'__luamml_array_init_col:' |
| 32 |
token.set_lua('__luamml_array_init_col:', funcid, 'protected') |
| 33 |
lua.get_functions_table()[funcid] = function() |
| 34 |
-- TODO: Error handling etc |
| 35 |
local nest = tex.nest[tex.nest.ptr-1] |
| 36 |
-- The special will be deleted again, it just marks the right math list since the start math node is not there yet |
| 37 |
local special = node.new('whatsit', 'special') |
| 38 |
node.insert_after(nest.tail, nest.tail, special) |
| 39 |
nest.tail = special |
| 40 |
local temp = nest.head |
| 41 |
local props = properties[temp] |
| 42 |
if not props then |
| 43 |
props = {} |
| 44 |
properties[temp] = props |
| 45 |
end |
| 46 |
props.luamml_array_startmath = special |
| 47 |
end |
| 48 |
|
| 49 |
--[[ Documentation of luafunctions \__luamml_array_finalize_col:w |
| 50 |
finalize a cell, used in \@classz |
| 51 |
the argument, a number, describes the alignment. |
| 52 |
TODO: error handling |
| 53 |
--]] |
| 54 |
|
| 55 |
local funcid = luatexbase.new_luafunction'__luamml_array_finalize_col:w' |
| 56 |
token.set_lua('__luamml_array_finalize_col:w', funcid, 'protected') |
| 57 |
lua.get_functions_table()[funcid] = function() |
| 58 |
local alignment = token.scan_int() -- Do it first to consume number even if we end early |
| 59 |
-- TODO: Error handling etc |
| 60 |
local temp = tex.nest.top.head |
| 61 |
local props = properties[temp] |
| 62 |
local special = props and props.luamml_array_startmath |
| 63 |
if not special then return end |
| 64 |
node.remove(tex.nest.top.head, special) |
| 65 |
local startmath = node.free(special) |
| 66 |
props.luamml_array_startmath = nil |
| 67 |
|
| 68 |
alignment = alignment == 1 and 'left' or alignment == 2 and 'right' or nil |
| 69 |
|
| 70 |
if node.end_of_math(startmath) == tex.nest.top.tail then |
| 71 |
if startmath.next == tex.nest.top.tail then return end |
| 72 |
store_column(startmath).columnalign = alignment |
| 73 |
else |
| 74 |
-- Oh no, we got text. Let's complain to the user, it's probably their fault |
| 75 |
print'We are mathematicians, don\'t bother us with text' |
| 76 |
store_column_xml(to_text(startmath, tex.nest.top.tail)).columnalign = alignment |
| 77 |
end |
| 78 |
end |
| 79 |
|
| 80 |
local saved_array |
| 81 |
|
| 82 |
--[[ Documentation of luafunctions \__luamml_array_save_array: |
| 83 |
save the array as a table. |
| 84 |
Used in \endarray. |
| 85 |
TODO: error handling |
| 86 |
--]] |
| 87 |
|
| 88 |
funcid = luatexbase.new_luafunction'__luamml_array_save_array:' |
| 89 |
token.set_lua('__luamml_array_save_array:', funcid) |
| 90 |
lua.get_functions_table()[funcid] = function() |
| 91 |
-- TODO: Error handling etc. |
| 92 |
local colsep = tex.dimen['col@sep'] |
| 93 |
saved_array = get_table() |
| 94 |
if colsep ~= 0 then |
| 95 |
saved_array = {[0] = 'mrow', |
| 96 |
{[0] = 'mspace', width = string.format('%.3fpt', colsep/65781.76)}, |
| 97 |
saved_array, |
| 98 |
{[0] = 'mspace', width = string.format('%.3fpt', colsep/65781.76)}, |
| 99 |
} |
| 100 |
end |
| 101 |
end |
| 102 |
|
| 103 |
--[[ Documentation of luafunctions \__luamml_array_finalize_array: |
| 104 |
finalize the array |
| 105 |
Used in \endarray. |
| 106 |
TODO: error handling |
| 107 |
--]] |
| 108 |
|
| 109 |
funcid = luatexbase.new_luafunction'__luamml_array_finalize_array:' |
| 110 |
token.set_lua('__luamml_array_finalize_array:', funcid) |
| 111 |
lua.get_functions_table()[funcid] = function() |
| 112 |
local nucl = tex.nest.top.tail.nucleus |
| 113 |
local array = saved_array |
| 114 |
saved_array = nil |
| 115 |
if not array then |
| 116 |
tex.error( |
| 117 |
"Socket tagsupport/math/luamml/array/finalize without an array to be finalized. \z |
| 118 |
This is a bug", |
| 119 |
{ |
| 120 |
"Please report this to the maintainer of the package defining\n\z |
| 121 |
the table command you just used.\n\z |
| 122 |
The socket tagsupport/math/luamml/array/finalize can only be used\n\z |
| 123 |
after saving a table with tagsupport/math/luamml/array/save, but\n\z |
| 124 |
no saved table could be found." |
| 125 |
} |
| 126 |
) |
| 127 |
return |
| 128 |
end |
| 129 |
if not nucl then |
| 130 |
tex.error( |
| 131 |
"Socket tagsupport/math/luamml/array/finalize executed in \z |
| 132 |
unexpected location. This is a bug", |
| 133 |
{ |
| 134 |
string.format( |
| 135 |
"Please report this to the maintainer of the package defining\n\z |
| 136 |
the table command you just used.\n\z |
| 137 |
The socket tagsupport/math/luamml/array/finalize should always\n\z |
| 138 |
come directly after the math noad which contains the finalized\n\z |
| 139 |
array, but it appeared after a %s instead.", |
| 140 |
node.type(tex.nest.top.tail) |
| 141 |
) |
| 142 |
} |
| 143 |
) |
| 144 |
return |
| 145 |
end |
| 146 |
local props = nucl and properties[nucl] |
| 147 |
if not props then |
| 148 |
props = {} |
| 149 |
properties[nucl] = props |
| 150 |
end |
| 151 |
props.mathml_table = array |
| 152 |
end |