From 174cad050b1f6a4be1ff93f4e7e25363c4e84fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Wed, 20 Dec 2023 19:22:21 +0100 Subject: [PATCH] Add LaTeX hook to intercept converted MathML --- luamml-tex.lua | 25 +++++++++++++++++++++++-- luamml-xmlwriter.lua | 2 +- luamml.dtx | 19 +++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/luamml-tex.lua b/luamml-tex.lua index c391a0a..7c069c8 100644 --- a/luamml-tex.lua +++ b/luamml-tex.lua @@ -10,6 +10,10 @@ local write_struct = require'luamml-structelemwriter' local filename_token = token.create'l__luamml_filename_tl' local label_token = token.create'l__luamml_label_tl' +local left_brace = token.new(string.byte'{', 1) +local right_brace = token.new(string.byte'}', 2) + +local output_hook_token local properties = node.get_properties_table() local mmode, hmode, vmode do @@ -92,6 +96,11 @@ local function save_result(xml, display, structelem) if tracing then texio.write_nl(write_xml(mlist_result) .. '\n') end + if output_hook_token then + tex.runtoks(function() + tex.sprint(-2, output_hook_token, left_brace, write_xml(mlist_result), right_brace) + end) + end if tex.count.l__luamml_flag_int & 8 == 8 then write_struct(mlist_result) end @@ -166,7 +175,7 @@ lua.get_functions_table()[funcid] = function() end funcid = luatexbase.new_luafunction'luamml_begin_single_file:' -token.set_lua('luamml_begin_single_file:', funcid, protected) +token.set_lua('luamml_begin_single_file:', funcid, 'protected') lua.get_functions_table()[funcid] = function() token.put_next(filename_token) local filename = token.scan_argument() @@ -176,7 +185,7 @@ lua.get_functions_table()[funcid] = function() end funcid = luatexbase.new_luafunction'luamml_end_single_file:' -token.set_lua('luamml_end_single_file:', funcid, protected) +token.set_lua('luamml_end_single_file:', funcid, 'protected') lua.get_functions_table()[funcid] = function() if out_file then out_file:close() @@ -184,6 +193,18 @@ lua.get_functions_table()[funcid] = function() end end +funcid = luatexbase.new_luafunction'luamml_register_output_hook:N' +token.set_lua('__luamml_register_output_hook:N', funcid, 'protected') +lua.get_functions_table()[funcid] = function() + output_hook_token = token.get_next() +end + +funcid = luatexbase.new_luafunction'luamml_disable_output_hook:' +token.set_lua('__luamml_disable_output_hook:', funcid, 'protected') +lua.get_functions_table()[funcid] = function() + output_hook_token = nil +end + local annotate_context = require'luamml-tex-annotate' annotate_context.data.mathml = labelled_mathml diff --git a/luamml-xmlwriter.lua b/luamml-xmlwriter.lua index 47d4385..c68b79a 100644 --- a/luamml-xmlwriter.lua +++ b/luamml-xmlwriter.lua @@ -10,7 +10,7 @@ local escapes = { ['&'] = "&", } local function escape_text(text) - return string.gsub(string.gsub(tostring(text), '["<>&]', escapes), '[\x00-\x08\x0B\x0C\x0E-\x20]', function(x) + return string.gsub(string.gsub(tostring(text), '["<>&]', escapes), '[\x00-\x08\x0B\x0C\x0E-\x1F]', function(x) return string.format('^^%02x', string.byte(x)) end) end diff --git a/luamml.dtx b/luamml.dtx index 72b9561..5c18f49 100644 --- a/luamml.dtx +++ b/luamml.dtx @@ -99,6 +99,21 @@ % \begin{macrocode} %\lua_now:n { require'luamml-tex' } % \end{macrocode} +% +% \subsection{Hook} +% We also call a hook with arguments at the end of every MathML conversion with the result. +% Currently only implemented in Lua\TeX{} since it immediately provides the output. +% \begin{macrocode} +%<*luatex> +\hook_new_with_args:nn { luamml / converted } { 1 } + +\cs_new_protected:Npn \__luamml_output_hook:n { + \hook_use:nnw { luamml / converted } { 1 } +} +\__luamml_register_output_hook:N \__luamml_output_hook:n +% +% \end{macrocode} + % % \subsection{Flags} % The most important interface is for setting the flag which controls how the @@ -259,7 +274,7 @@ \cs_new_protected:Npn \luamml_annotate:nen #1#2#3 { \__luamml_annotate_begin: #3 - \__luamml_annotate_end:we \tex_numexpr:D #1 \scan_stop: {#2} + \__luamml_annotate_end:we \tex_numexpr:D #1 \scan_stop: {#2} } \cs_new_protected:Npn \luamml_annotate:en #1#2 { @@ -434,7 +449,7 @@ % \begin{macrocode} \cs_new_protected:Npn \__luamml_patch_package:nn #1 #2 { \@ifpackageloaded {#1} {#2} { - \hook_gput_code:nnn {package/after/#1} {luamml} {#2} + \hook_gput_code:nnn {package/#1/after} {luamml} {#2} } } \cs_new_protected:Npn \__luamml_patch_package:n #1 {