From b9105dcc4a98e4b1f1b2767e6921c9dca9d75855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Wed, 28 Apr 2021 22:03:40 +0200 Subject: [PATCH] Extract table functionality from amsmath module --- luamml-amsmath.lua | 63 ++++------------------------- luamml-patches-amsmath.sty | 1 - luamml-table.lua | 83 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 luamml-table.lua diff --git a/luamml-amsmath.lua b/luamml-amsmath.lua index 4a49f93..bf516b8 100644 --- a/luamml-amsmath.lua +++ b/luamml-amsmath.lua @@ -1,6 +1,9 @@ local write_xml = require'luamml-xmlwriter' local make_root = require'luamml-convert'.make_root 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() @@ -12,62 +15,16 @@ lua.get_functions_table()[funcid] = function() local boxnum = 0 local startmath = tex.box[boxnum].list assert(startmath.id == node.id"math") - local props = assert(properties[startmath]) - 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) + store_column(startmath, true) end funcid = luatexbase.new_luafunction'__luamml_amsmath_finalize_table:' token.set_lua('__luamml_amsmath_finalize_table:', funcid) lua.get_functions_table()[funcid] = function() -- 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 + local mml_table = get_table() if not mml_table then return end + mml_table.displaystyle = true local columns = node.count(node.id'align_record', tex.lists.align_head)//2 mml_table.columnalign = string.rep('right left', columns, ' ') local spacing = {} @@ -109,12 +66,6 @@ lua.get_functions_table()[funcid] = function() texio.write_nl'WARNING: Tag extraction failed' return end - 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 - mml_row[0] = 'mlabeledtr' - table.insert(mml_row, 1, {[0] = 'mtd', {[0] = 'mtext', last_tag}}) + store_tag({[0] = 'mtd', {[0] = 'mtext', last_tag}}) last_tag = nil end diff --git a/luamml-patches-amsmath.sty b/luamml-patches-amsmath.sty index 81d6f12..421f47d 100644 --- a/luamml-patches-amsmath.sty +++ b/luamml-patches-amsmath.sty @@ -75,7 +75,6 @@ \place@tag \__luamml_amsmath_set_tag: \fi - \__luamml_amsmath_finalize_row: \ifst@rred \else \global \@eqnswtrue diff --git a/luamml-table.lua b/luamml-table.lua new file mode 100644 index 0000000..f4321c4 --- /dev/null +++ b/luamml-table.lua @@ -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, +}