From 903480a3f95d05f985b6df1b8114f57112979ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Sat, 26 Jun 2021 19:49:28 +0200 Subject: [PATCH] Prepare for better pdfTeX support --- luamml-pdf.sty | 1 + pdfmml-emulate-node.lua | 3 +++ pdfmml-logreader.lua | 6 ++++-- pdfmml.lua | 45 ++++++++++++++++++++++++++++++++--------- test_pdf.tex | 4 ++-- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/luamml-pdf.sty b/luamml-pdf.sty index 50ba948..83ebda8 100644 --- a/luamml-pdf.sty +++ b/luamml-pdf.sty @@ -33,6 +33,7 @@ \iow_now:Nx \l__luamml_pdf_stream { LUAMML_FORMULA_BEGIN: \int_use:N \g__luamml_formula_id_int + :3:mrow } \__luamml_pdf_showlists: \iow_now:Nx \l__luamml_pdf_stream { diff --git a/pdfmml-emulate-node.lua b/pdfmml-emulate-node.lua index c7f6135..e2209ca 100644 --- a/pdfmml-emulate-node.lua +++ b/pdfmml-emulate-node.lua @@ -32,5 +32,8 @@ node = { traverse = function(head) return traverse_iter, head, nil end, + direct = { + todirect = function(n) return n end, + }, } tex.nulldelimiterspace = tex.nulldelimiterspace or 78643 -- 1.2pt diff --git a/pdfmml-logreader.lua b/pdfmml-logreader.lua index 8aa7bdd..a17f604 100644 --- a/pdfmml-logreader.lua +++ b/pdfmml-logreader.lua @@ -7,8 +7,10 @@ local non_final_list_block = (l.C((1-l.P'\n')^1) * '\n' - '### ' + '\n')^0 local math_lists_block = l.Ct('### ' * l.Cg(l.C'display' * ' ', 'display')^-1 * 'math mode entered at line ' * l.Cg(l.R'09'^1 / tonumber, 'line') * '\n' * non_final_list_block)^1 local generic_list_block = '### ' * (line - 'current page:') * non_final_list_block -local luamml_block = l.Cg('LUAMML_FORMULA_BEGIN:' * id * l.P'\n'^1 * l.Ct( - (math_lists_block + generic_list_block/0)^0 +local luamml_block = l.Cg('LUAMML_FORMULA_BEGIN:' * id * ':' * l.Ct( + l.Cg(id, 'flag') * ':' * l.Cg((1-l.P'\n')^0, 'tag') * l.P'\n'^1 + + * (math_lists_block + generic_list_block/0)^0 * (line - 'LUAMML_FORMULA_END\n')^0 * 'LUAMML_FORMULA_END\n') * l.Cc'groups') diff --git a/pdfmml.lua b/pdfmml.lua index 08d5d0b..116a617 100755 --- a/pdfmml.lua +++ b/pdfmml.lua @@ -52,13 +52,40 @@ else out_stream = assert(io.open(arg[2], 'w')) end end -for i, block in ipairs(parsed.groups) do - local stream = out_stream or assert(io.open(out_prefix .. tostring(i) .. out_suffix, 'w')) - block = block[1] - local parsed = parse_showlists(block, nil, nil, parsed) - local style = block.display and 0 or 2 - stream:write( - to_xml(convert.make_root(convert.process(parsed, style), style)), '\n' - ) - if not out_stream then stream:close() end +parsed.mathml = {} + +local function shallow_copy(t) + local new = {} + for k, v in next, t do + new[k] = v + end + return new +end + +-- Currently only 3 flag values are supported: +-- 0: Ignore (Doesn't make a lot of sense here) +-- 1: Only save +-- 3: Generate normally +for i, block in ipairs(parsed.groups) do + local flag, tag = block.flag, block.tag + block = block[1] + if flag & 3 ~= 0 then + local style = block.display and 0 or 2 + local xml = convert.process(parse_showlists(block, nil, nil, parsed), style) + if flag & 2 == 2 then + local stream = out_stream or assert(io.open(out_prefix .. tostring(i) .. out_suffix, 'w')) + stream:write(to_xml(convert.make_root(shallow_copy(xml), style)), '\n') + if not out_stream then stream:close() end + end + if tag ~= 'mrow' then + if xml[0] == 'mrow' then + xml[0] = tag + else + xml = {[0] = tag, xml} + end + end + if style == 2 and flag & 1 == 1 then + parsed.mathml[i] = xml + end + end end diff --git a/test_pdf.tex b/test_pdf.tex index be803a7..be80b1c 100644 --- a/test_pdf.tex +++ b/test_pdf.tex @@ -9,9 +9,9 @@ \cs_set_eq:NN \WriteoutFormula \luamml_pdf_write: \NewDocumentCommand\AnnotateFormula{ o m m }{% \IfValueTF{#1}{% - \luamml_annotate:nnn{#1}% + \luamml_annotate:nen{#1}% }{ - \luamml_annotate:nn + \luamml_annotate:en }{#2}{#3} } \ExplSyntaxOff