Add LaTeX hook to intercept converted MathML

This commit is contained in:
Marcel Fabian Krüger 2023-12-20 19:22:21 +01:00
parent 637dfbf90a
commit 174cad050b
3 changed files with 41 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -99,6 +99,21 @@
% \begin{macrocode}
%<luatex>\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
%</luatex>
% \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 {