Add interface for writing MathML into a file

This commit is contained in:
Marcel Fabian Krüger 2021-05-18 19:27:54 +02:00
parent c4c2b701b5
commit 2974676fab
3 changed files with 55 additions and 5 deletions

View File

@ -7,6 +7,8 @@ local mappings = require'luamml-legacy-mappings'
local write_xml = require'luamml-xmlwriter' local write_xml = require'luamml-xmlwriter'
local write_struct = require'luamml-structelemwriter' local write_struct = require'luamml-structelemwriter'
local filename_token = token.create'l__luamml_filename_tl'
local properties = node.get_properties_table() local properties = node.get_properties_table()
local funcid = luatexbase.new_luafunction'RegisterFamilyMapping' local funcid = luatexbase.new_luafunction'RegisterFamilyMapping'
@ -39,8 +41,18 @@ end
local mlist_buffer local mlist_buffer
local mlist_result, mlist_display local mlist_result, mlist_display
local undefined_cmd = token.command_id'undefined_cs'
local call_cmd = token.command_id'call'
local function save_result(xml, display) local function save_result(xml, display)
mlist_result, mlist_display = xml, display mlist_result, mlist_display = xml, display
if filename_token.command ~= undefined_cmd then
assert(filename_token.command == call_cmd)
token.put_next(filename_token)
assert(io.open(token.scan_string(), 'w'))
:write(write_xml(make_root({[0] = 'mrow', xml}, display and 0 or 2)) .. '\n')
:close()
end
if tex.count.tracingmathml > 1 then if tex.count.tracingmathml > 1 then
-- Here xml gets wrapped in an mrow to avoid modifying it. -- Here xml gets wrapped in an mrow to avoid modifying it.
texio.write_nl(write_xml(make_root({[0] = 'mrow', xml}, display and 0 or 2)) .. '\n') texio.write_nl(write_xml(make_root({[0] = 'mrow', xml}, display and 0 or 2)) .. '\n')

View File

@ -2,24 +2,28 @@
{Feel free to add a description here} {Feel free to add a description here}
\int_new:N \l__luamml_flag_int \int_new:N \l__luamml_flag_int
\tl_new:N \l__luamml_filename_tl
\int_new:N \tracingmathml \int_new:N \tracingmathml
\lua_now:n { require'luamml-tex' } \lua_now:n { require'luamml-tex' }
\cs_new:Nn \luamml_flag_save: { \cs_new:Npn \luamml_flag_save: {
\int_set:Nn \l__luamml_flag_int { 2 } \int_set:Nn \l__luamml_flag_int { 2 }
} }
\cs_new:Nn \luamml_flag_ignore: { \cs_new:Npn \luamml_flag_ignore: {
\int_set:Nn \l__luamml_flag_int { 3 } \int_set:Nn \l__luamml_flag_int { 3 }
} }
\cs_new:Nn \luamml_flag_alignment_left: { \cs_new:Npn \luamml_flag_alignment_left: {
\int_set:Nn \l__luamml_flag_int { 7 } \int_set:Nn \l__luamml_flag_int { 7 }
} }
\cs_new:Nn \luamml_flag_alignment_right: { \cs_new:Npn \luamml_flag_alignment_right: {
\int_set:Nn \l__luamml_flag_int { 6 } \int_set:Nn \l__luamml_flag_int { 6 }
} }
\cs_new:Nn \luamml_flag_structelem: { \cs_new:Npn \luamml_flag_structelem: {
\int_set:Nn \l__luamml_flag_int { 8 } \int_set:Nn \l__luamml_flag_int { 8 }
} }
\cs_new:Npn \luamml_set_filename:n {
\tl_set:Nn \l__luamml_filename_tl
}
\cs_new:Npn \__luamml_patch_package:nn #1 #2 { \cs_new:Npn \__luamml_patch_package:nn #1 #2 {
\@ifpackageloaded {#1} {#2} { \@ifpackageloaded {#1} {#2} {

34
test_structelem.tex Normal file
View File

@ -0,0 +1,34 @@
% !Mode:: "TeX:DE:UTF-8:Main"
\PassOptionsToPackage{enable-debug,check-declarations}{expl3}
\RequirePackage{pdfmanagement-testphase}
\DeclareDocumentMetadata{uncompress,pdfversion=2.0}
\RequirePackage{expl3}
\documentclass{article}
\usepackage{unicode-math}
\usepackage{tagpdf}
\tagpdfsetup{activate-all,interwordspace=true,}
\usepackage{amsmath}
\usepackage{luamml}
% \RegisterFamilyMapping\symsymbols{oms}
% \RegisterFamilyMapping\symletters{oml}
% \RegisterFamilyMapping\symlargesymbols{omx}
\ExplSyntaxOn
\luamml_flag_structelem:
\int_new:N \g__my_mathml_int
\luamml_set_filename:n {
\immediateassignment \int_gincr:N \g__my_mathml_int
\jobname -formula- \int_use:N \g__my_mathml_int .xml
}
\ExplSyntaxOff
\begin{document}
\tagstructbegin{tag=Document}
\tagstructbegin{tag=Formula}
\[
ax^2+bx+c=0,\quad
\int_1^n f(x) = \sum_{i=1}^m x^4 dx
\]
\tagstructend
\tagstructend
\end{document}