Debian Codegraph @

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

raw
1 --[[
2 This file returns a number of helper functions used when tagging
3 table-like math.
4
5 The returned functions are
6 * store_column (startmath)
7 * store_column_xml (mml, display)
8 * store_tag (xml) (this adds the intent :equation-label')
9 * store_notag (xml) (this adds the intent :no-equation-label')
10 * set_row_attribute (name, value)
11 * get_table ()
12
13 It adds a function to the callback
14 * hpack_filter.
15 This function is executed if groupcode == fin_row, so at the end of alignment rows.
16
17 The file is loaded by luamml-amsmath and luamml-array.
18 --]]
19
20 --TODO: why are they loaded? None of the functions is used.
21 local write_xml = require'luamml-xmlwriter'
22 local make_root = require'luamml-convert'.make_root
23 local save_result = require'luamml-tex'.save_result
24
25 local properties = node.get_properties_table()
26
27 local glue_id = node.id'glue'
28 local tabskip_sub = 12
29 assert(node.subtypes'glue'[tabskip_sub] == 'tabskip')
30
31 local function store_get_row()
32 local row_temp
33 for i=tex.nest.ptr-1, 0, -1 do
34 local head = tex.nest[i].head
35 local glue = head.next
36 if glue and glue.id == glue_id and glue.subtype == tabskip_sub then
37 row_temp = head
38 break
39 end
40 end
41 if not row_temp then
42 error[[luamml_table's store function called outside of table]]
43 end
44 local props = properties[row_temp]
45 if not props then
46 props = {}
47 properties[row_temp] = props
48 end
49 local mml_row = props.mathml_row
50 if not mml_row then
51 mml_row = {[0] = 'mtr'}
52 props.mathml_row = mml_row
53 end
54 return mml_row
55 end
56
57 local function store_column_xml(mml, display)
58 if mml[0] ~= 'mtd' then
59 if display and mml[0] == 'mstyle' and mml.displaystyle == true then
60 mml[0], mml.displaystyle, mml.scriptlevel = 'mtd', nil, nil
61 else
62 if display and mml[0] ~= 'mstyle' then
63 mml = {[0] = 'mstyle', displaystyle = false, mml}
64 end
65 mml = {[0] = 'mtd', mml}
66 end
67 end
68 table.insert(store_get_row(), mml)
69 return mml
70 end
71
72 local function store_column(startmath)
73 local props = properties[startmath]
74 if not props then return end
75 local mml = props.saved_mathml_table or props.saved_mathml_core
76 if mml then return store_column_xml(mml) end
77 end
78
79 local function store_tag(xml)
80 local mml_row = store_get_row()
81 xml.intent = ':equation-label'
82 table.insert(mml_row, 1, xml)
83 last_tag = nil
84 end
85
86 local function store_notag(xml)
87 local mml_row = store_get_row()
88 xml.intent = ':no-equation-label'
89 table.insert(mml_row, 1, xml)
90 end
91
92 local function set_row_attribute(name, value)
93 local mml_row = store_get_row()
94 mml_row[name] = value
95 end
96
97 local function store_row(xml)
98 local props = properties[tex.lists.align_head]
99 if not props then
100 props = {}
101 properties[tex.lists.align_head] = props
102 end
103 local mml_table = props.mathml_table_node_table
104 if not mml_table then
105 mml_table = {[0] = 'mtable'}
106 props.mathml_table_node_table = mml_table
107 end
108 table.insert(mml_table, xml)
109 end
110
111 luatexbase.add_to_callback('hpack_filter', function(_, group)
112 if group ~= 'fin_row' then return true end
113
114 local temp = tex.nest.top.head
115 local props = properties[temp]
116 if not props then return true end
117 local mml_row = props.mathml_row
118 if not mml_row then return true end
119 props.mathml_row = nil
120
121 store_row(mml_row)
122 return true
123 end, 'mathml amsmath processing')
124
125 local function get_table()
126 -- TODO: Error handling etc
127 local props = properties[tex.lists.align_head]
128 if not props then return end
129 local mml_table = props.mathml_table_node_table
130 props.mathml_table_node_table = nil
131 return mml_table
132 end
133
134 return {
135 store_column = store_column,
136 store_column_xml = store_column_xml,
137 store_tag = store_tag,
138 store_notag = store_notag,
139 store_row = store_row,
140 set_row_attribute = set_row_attribute,
141 get_table = get_table,
142 }