From 165c6bd9ad2af28a25e285cddd7bf63d02a29031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Mon, 19 Apr 2021 13:30:54 +0200 Subject: [PATCH] Handle many TODOs --- mlist_to_mml.lua | 157 +++++++++++++++++++++++++++++++++-------------- test_tex.lua | 7 ++- test_tex.tex | 2 +- write_xml.lua | 12 +++- 4 files changed, 125 insertions(+), 53 deletions(-) diff --git a/mlist_to_mml.lua b/mlist_to_mml.lua index 0ab19dd..3eed3b8 100644 --- a/mlist_to_mml.lua +++ b/mlist_to_mml.lua @@ -9,6 +9,9 @@ local fence_sub = node.subtypes'fence' local nodes_to_table +local function sub_style(s) return s//4*2+5 end +local function sup_style(s) return s//4*2+4+s%2 end + -- We ignore large_... since they aren't used for modern fonts local function delim_to_table(delim) if not delim then return end @@ -16,7 +19,7 @@ local function delim_to_table(delim) return {[0] = 'mo', utf8.char(delim.small_char), ['tex:family'] = fam ~= 0 and fam or nil } end -local function kernel_to_table(kernel) +local function kernel_to_table(kernel, cur_style) if not kernel then return end local id = kernel.id if id == math_char_t then @@ -27,15 +30,15 @@ local function kernel_to_table(kernel) elseif id == sub_box_t then return {[0] = 'mi', {[0] = 'mglyph', ['tex:box'] = kernel.list}} elseif id == sub_mlist_t then - return nodes_to_table(kernel.list) + return nodes_to_table(kernel.list, cur_style) else error'confusion' end end -local function do_sub_sup(t, n) - sub = kernel_to_table(n.sub) - sup = kernel_to_table(n.sup) +local function do_sub_sup(t, n, cur_style) + local sub = kernel_to_table(n.sub, sub_style(cur_style)) + local sup = kernel_to_table(n.sup, sup_style(cur_style)) if sub then if sup then return {[0] = 'msubsup', t, sub, sup} @@ -49,14 +52,11 @@ local function do_sub_sup(t, n) end end -local function noad_to_table(noad, sub) +local function noad_to_table(noad, sub, cur_style) local class = noad_sub[sub] - local nucleus = kernel_to_table(noad.nucleus) + local nucleus = kernel_to_table(noad.nucleus, class == 'over' and cur_style//2*2+1 or cur_style) if class == 'ord' then - -- elseif class == 'opdisplaylimits' then - -- elseif class == 'oplimits' then - -- elseif class == 'opnolimits' then - elseif class == 'bin' or class == 'rel' or class == 'open' + elseif class == 'opdisplaylimits' or class == 'oplimits' or class == 'opnolimits' or class == 'bin' or class == 'rel' or class == 'open' or class == 'close' or class == 'punct' or class == 'inner' then if nucleus[0] == 'mrow' then -- TODO @@ -64,19 +64,73 @@ local function noad_to_table(noad, sub) nucleus[0] = 'mo' end nucleus['tex:class'] = class - -- elseif class == 'under' then - -- elseif class == 'over' then - -- elseif class == 'vcenter' then - else - -- error[[confusion]] + + if (noad.sup or noad.sub) and (class == 'opdisplaylimits' or class == 'oplimits') then + nucleus.movablelimits = class == 'opdisplaylimits' + local sub = kernel_to_table(noad.sub, sub_style(cur_style)) + local sup = kernel_to_table(noad.sup, sup_style(cur_style)) + return {[0] = sup and (sub and 'munderover' or 'mover') or 'munder', + nucleus, + sub or sup, + sub and sup, + } + end + elseif class == 'under' then + return {[0] = 'munder', + nucleus, + {[0] = 'mo', '_',}, + } + elseif class == 'over' then + return {[0] = 'mover', + nucleus, + {[0] = 'mo', '\u{203E}',}, + } + elseif class == 'vcenter' then nucleus['tex:TODO'] = class + else + error[[confusion]] end - return do_sub_sup(nucleus, noad) + return do_sub_sup(nucleus, noad, cur_style) end -local function radical_to_table(radical, sub) +local function accent_to_table(accent, sub, cur_style) + local nucleus = kernel_to_table(accent.nucleus, cur_style//2*2+1) + local top_acc = kernel_to_table(accent.accent, cur_style) + local bot_acc = kernel_to_table(accent.bot_accent, cur_style) + if top_acc then + top_acc[0] = 'mo' + if sub & 1 == 1 then + top_acc.stretchy = 'false' + end + end + if bot_acc then + bot_acc[0] = 'mo' + if sub & 2 == 2 then + bot_acc.stretchy = 'false' + end + end + return {[0] = top_acc and (bot_acc and 'munderover' or 'mover') or 'munder', + nucleus, + bot_acc or top_acc, + bot_acc and top_acc, + } +end + +local style_table = { + display = {displaystyle = "true", scriptlevel = "0"}, + text = {displaystyle = "false", scriptlevel = "0"}, + script = {displaystyle = "false", scriptlevel = "1"}, + scriptscript = {displaystyle = "false", scriptlevel = "2"}, +} + +style_table.crampeddisplay, style_table.crampedtext, +style_table.crampedscript, style_table.crampedscriptscript = + style_table.display, style_table.text, + style_table.script, style_table.scriptscript + +local function radical_to_table(radical, sub, cur_style) local kind = radical_sub[sub] - local nucleus = kernel_to_table(radical.nucleus) + local nucleus = kernel_to_table(radical.nucleus, cur_style//2*2+1) local left = delim_to_table(radical.left) local elem if kind == 'radical' or kind == 'uradical' then @@ -96,14 +150,13 @@ local function radical_to_table(radical, sub) else error[[confusion]] end - return do_sub_sup(elem, radical) + return do_sub_sup(elem, radical, cur_style) end -local function fraction_to_table(fraction, sub) - local num = kernel_to_table(fraction.num) - local denom = kernel_to_table(fraction.denom) +local function fraction_to_table(fraction, sub, cur_style) + local num = kernel_to_table(fraction.num, sup_style(cur_style)) + local denom = kernel_to_table(fraction.denom, sub_style(cur_style)) local left = delim_to_table(fraction.left) - -- local middle = delim_to_table(fraction.middle) local right = delim_to_table(fraction.right) local mfrac = {[0] = 'mfrac', linethickness = fraction.width and fraction.width == 0 and 0 or nil, @@ -127,46 +180,58 @@ local function fraction_to_table(fraction, sub) end end -local function fence_to_table(fraction, sub) - error[[TODO]] - return { - kind = fence_sub[sub], - } +local function fence_to_table(fence, sub, cur_style) + local delim = delim_to_table(fence.delimiter) + delim.stretchy = 'true' + delim.fence = 'true' + return delim end -function nodes_to_table(head) +function nodes_to_table(head, cur_style) local t = {[0] = "mrow"} + local result = t for n, id, sub in node.traverse(head) do if id == noad_t then - t[#t+1] = noad_to_table(n, sub) + t[#t+1] = noad_to_table(n, sub, cur_style) elseif id == accent_t then - print(n) - t[#t+1] = {[0] = 'TODO', accent = n} + t[#t+1] = accent_to_table(n, sub, cur_style) elseif id == style_t then - print(n) - t[#t+1] = {[0] = 'TODO', style = n} + if #t ~= 0 then + local new_t = {[0] = 'mstyle'} + t[#t+1] = new_t + t = new_t + end + if sub < 2 then + t.displaystyle, t.scriptlevel = true, 0 + else + t.displaystyle, t.scriptlevel = false, sub//2 - 1 + end + cur_style = sub elseif id == choice_t then - print(n) - t[#t+1] = {[0] = 'TODO', choice = n} + local size = cur_style//2 + t[#t+1] = nodes_to_table(n[size == 0 and 'display' or size == 1 and 'text' + or size == 2 and 'script' + or size == 3 and 'scriptscript' or assert(false)], 2*size) elseif id == radical_t then - t[#t+1] = radical_to_table(n, sub) + t[#t+1] = radical_to_table(n, sub, cur_style) elseif id == fraction_t then - t[#t+1] = fraction_to_table(n, sub) + t[#t+1] = fraction_to_table(n, sub, cur_style) elseif id == fence_t then - print(n) - t[#t+1] = {[0] = 'TODO', fence = n} + t[#t+1] = fence_to_table(n, sub, cur_style) else - print(n) - t[#t+1] = n + t[#t+1] = {[0] = 'tex:TODO', other = n} end end - return t + return result end -return function(head) - local result = nodes_to_table(head) +return function(head, style) + local result = nodes_to_table(head, style or 0) result[0] = 'math' result.xmlns = 'http://www.w3.org/1998/Math/MathML' result['xmlns:tex'] = 'http://typesetting.eu/2021/LuaMathML' + if style == 2 then + result.display = 'block' + end return result end diff --git a/test_tex.lua b/test_tex.lua index a1b56c0..50b1b1a 100644 --- a/test_tex.lua +++ b/test_tex.lua @@ -4,10 +4,11 @@ local function show(t) return print(inspect(t)) end local mlist_to_table = require'mlist_to_mml' local write_xml = require'write_xml' -luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(mlist) +luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(mlist, style) print'\n\n' - local xml = mlist_to_table(mlist) - show(write_xml(xml)) + local xml = mlist_to_table(mlist, style == 'display' and 2 or 0) + print(write_xml(xml)) + -- print(write_xml(xml, '\n')) print'\n' return true end, 'dump_list') diff --git a/test_tex.tex b/test_tex.tex index 16c5ae9..37c78bc 100644 --- a/test_tex.tex +++ b/test_tex.tex @@ -9,6 +9,6 @@ x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}. \] \[ - x = \longdivision{5} + \sum_a\underline c\dot bc' \] \end{document} diff --git a/write_xml.lua b/write_xml.lua index aa842b3..f67db4e 100644 --- a/write_xml.lua +++ b/write_xml.lua @@ -11,13 +11,14 @@ local escapes = { ['&'] = "&", } local function escape_text(text) - return text:gsub('("<>&)', escapes) + return string.gsub(tostring(text), '("<>&)', escapes) end -local function write_elem(tree) +local function write_elem(tree, indent) if not tree[0] then print('ERR', require'inspect'(tree)) end local escaped_name = escape_name(assert(tree[0])) local out = "<" .. escaped_name + if indent then out = indent .. out end for attr, val in next, tree do if type(attr) == 'string' then out = out .. ' ' .. escape_name(attr) .. '="' .. escape_text(val) .. '"' end end @@ -25,13 +26,18 @@ local function write_elem(tree) return out .. '/>' end out = out .. '>' + local inner_indent = indent and indent .. ' ' for _, elem in ipairs(tree) do if type(elem) == 'string' then + if inner_indent then + out = out .. inner_indent + end out = out .. escape_text(elem) else - out = out .. write_elem(elem) + out = out .. write_elem(elem, inner_indent) end end + if indent then out = out .. indent end return out .. '' end