Extract table functionality from amsmath module

This commit is contained in:
Marcel Fabian Krüger 2021-04-28 22:03:40 +02:00
parent bfc4efe3e1
commit b9105dcc4a
3 changed files with 90 additions and 57 deletions

View File

@ -1,6 +1,9 @@
local write_xml = require'luamml-xmlwriter' local write_xml = require'luamml-xmlwriter'
local make_root = require'luamml-convert'.make_root local make_root = require'luamml-convert'.make_root
local save_result = require'luamml-tex'.save_result local save_result = require'luamml-tex'.save_result
local store_column = require'luamml-table'.store_column
local store_tag = require'luamml-table'.store_tag
local get_table = require'luamml-table'.get_table
local properties = node.get_properties_table() local properties = node.get_properties_table()
@ -12,62 +15,16 @@ lua.get_functions_table()[funcid] = function()
local boxnum = 0 local boxnum = 0
local startmath = tex.box[boxnum].list local startmath = tex.box[boxnum].list
assert(startmath.id == node.id"math") assert(startmath.id == node.id"math")
local props = assert(properties[startmath]) store_column(startmath, true)
local mml = assert(props.saved_mathml_table)
props.saved_mathml_table = nil
if mml[0] == 'mstyle' and mml.displaystyle == true then
mml[0], mml.displaystyle, mml.scriptlevel = 'mtd', nil, nil
else
if mml[0] ~= 'mstyle' then
mml = {[0] = 'mstyle', displaystyle = false, mml}
end
mml = {[0] = 'mtd', mml}
end
local row_temp = tex.nest[tex.nest.ptr-1]
props = properties[row_temp]
if not props then
props = {}
properties[row_temp] = props
end
if not props.mathml_row then
props.mathml_row = {[0] = 'mtr'}
end
mml_row = props.mathml_row
table.insert(mml_row, mml)
end
funcid = luatexbase.new_luafunction'__luamml_amsmath_finalize_row:'
token.set_lua('__luamml_amsmath_finalize_row:', funcid, 'protected')
lua.get_functions_table()[funcid] = function()
-- TODO: Error handling etc
local row_temp = tex.nest[tex.nest.ptr-1]
local props = properties[row_temp]
if not props then return end
if not props.mathml_row then return end
mml_row = props.mathml_row
props.mathml_row = nil
props = properties[tex.lists.align_head]
if not props then
props = {}
properties[tex.lists.align_head] = props
end
local mml_table = props.mathml_table_node_table
if not mml_table then
mml_table = {[0] = 'mtable', displaystyle = true}
props.mathml_table_node_table = mml_table
end
table.insert(mml_table, mml_row)
end end
funcid = luatexbase.new_luafunction'__luamml_amsmath_finalize_table:' funcid = luatexbase.new_luafunction'__luamml_amsmath_finalize_table:'
token.set_lua('__luamml_amsmath_finalize_table:', funcid) token.set_lua('__luamml_amsmath_finalize_table:', funcid)
lua.get_functions_table()[funcid] = function() lua.get_functions_table()[funcid] = function()
-- TODO: Error handling etc -- TODO: Error handling etc
local props = properties[tex.lists.align_head] local mml_table = get_table()
if not props then return end
local mml_table = props.mathml_table_node_table
props.mathml_table_node_table = nil
if not mml_table then return end if not mml_table then return end
mml_table.displaystyle = true
local columns = node.count(node.id'align_record', tex.lists.align_head)//2 local columns = node.count(node.id'align_record', tex.lists.align_head)//2
mml_table.columnalign = string.rep('right left', columns, ' ') mml_table.columnalign = string.rep('right left', columns, ' ')
local spacing = {} local spacing = {}
@ -109,12 +66,6 @@ lua.get_functions_table()[funcid] = function()
texio.write_nl'WARNING: Tag extraction failed' texio.write_nl'WARNING: Tag extraction failed'
return return
end end
local row_temp = tex.nest[tex.nest.ptr-1] store_tag({[0] = 'mtd', {[0] = 'mtext', last_tag}})
local props = properties[row_temp]
if not props then return end
if not props.mathml_row then return end
mml_row = props.mathml_row
mml_row[0] = 'mlabeledtr'
table.insert(mml_row, 1, {[0] = 'mtd', {[0] = 'mtext', last_tag}})
last_tag = nil last_tag = nil
end end

View File

@ -75,7 +75,6 @@
\place@tag \place@tag
\__luamml_amsmath_set_tag: \__luamml_amsmath_set_tag:
\fi \fi
\__luamml_amsmath_finalize_row:
\ifst@rred \ifst@rred
\else \else
\global \@eqnswtrue \global \@eqnswtrue

83
luamml-table.lua Normal file
View File

@ -0,0 +1,83 @@
local write_xml = require'luamml-xmlwriter'
local make_root = require'luamml-convert'.make_root
local save_result = require'luamml-tex'.save_result
local properties = node.get_properties_table()
local function store_get_row()
local row_temp = tex.nest[tex.nest.ptr-1].head
local props = properties[row_temp]
if not props then
props = {}
properties[row_temp] = props
end
local mml_row = props.mathml_row
if not mml_row then
mml_row = {[0] = 'mtr'}
props.mathml_row = mml_row
end
return mml_row
end
local function store_column(startmath, display)
local props = properties[startmath]
if not props then return end
local mml = props.saved_mathml_table
if not mml then return end
props.saved_mathml_table = nil
if display and mml[0] == 'mstyle' and mml.displaystyle == true then
mml[0], mml.displaystyle, mml.scriptlevel = 'mtd', nil, nil
else
if display and mml[0] ~= 'mstyle' then
mml = {[0] = 'mstyle', displaystyle = false, mml}
end
mml = {[0] = 'mtd', mml}
end
table.insert(store_get_row(), mml)
end
local function store_tag(xml)
local mml_row = store_get_row()
mml_row[0] = 'mlabeledtr'
table.insert(mml_row, 1, xml)
last_tag = nil
end
luatexbase.add_to_callback('hpack_filter', function(_, group)
if group ~= 'fin_row' then return true end
local temp = tex.nest.top.head
local props = properties[temp]
if not props then return true end
local mml_row = props.mathml_row
if not mml_row then return true end
props.mathml_row = nil
props = properties[tex.lists.align_head]
if not props then
props = {}
properties[tex.lists.align_head] = props
end
local mml_table = props.mathml_table_node_table
if not mml_table then
mml_table = {[0] = 'mtable'}
props.mathml_table_node_table = mml_table
end
table.insert(mml_table, mml_row)
return true
end, 'mathml amsmath processing')
local function get_table()
-- TODO: Error handling etc
local props = properties[tex.lists.align_head]
if not props then return end
local mml_table = props.mathml_table_node_table
props.mathml_table_node_table = nil
return mml_table
end
return {
store_column = store_column,
store_tag = store_tag,
get_table = get_table,
}