New flag values

This commit is contained in:
Marcel Fabian Krüger 2021-06-16 15:19:26 +02:00
parent fba9e02b92
commit 702e974485
2 changed files with 61 additions and 29 deletions

View File

@ -41,51 +41,63 @@ local function shallow_copy(t)
end end
-- Possible flag values: -- Possible flag values:
-- 0: Normal (This is the only supported one in display mode) -- 0: Skip
-- 1: Like 0, result is display math -- 1: Generate MathML, but only save it for later usage in startmath node
-- 2: Generate MathML, but only save it for later usage in startmath node -- 3: Normal (This is the only supported one in display mode)
-- 3: Skip -- 11: Generate MathML structure elements
-- 8: Generate MathML structure elements
-- --
-- In other words: -- More generally, flags is a bitfield with the defined bits:
-- Bit 1: Suppress output -- Bit 5-7: See Bit 4
-- Bit 0: Force display if 1 isn't set, if it is then skip MathML generation -- Bit 4: Overwrite mathstyle with bit 9-11
-- Bit 3: Generate MathML structure elements
-- Bit 2: Reserved
-- Bit 1: Save MathML as a fully converted formula
-- Bit 0: Save MathML for later usage in startmath node. Ignored for display math.
local mlist_result, mlist_display local mlist_result
local undefined_cmd = token.command_id'undefined_cs' local undefined_cmd = token.command_id'undefined_cs'
local call_cmd = token.command_id'call' local call_cmd = token.command_id'call'
local function save_result(xml, display) local function save_result(xml, display, structelem)
mlist_result, mlist_display = xml, display mlist_result = make_root(xml, display and 0 or 2)
token.put_next(filename_token) token.put_next(filename_token)
local filename = token.scan_argument() local filename = token.scan_argument()
local tracing = tex.count.tracingmathml > 1 local tracing = tex.count.tracingmathml > 1
local xml_root = (filename ~= '' or tracing) and make_root(shallow_copy(xml), display and 0 or 2)
if filename ~= '' then if filename ~= '' then
assert(io.open(filename, 'w')) assert(io.open(filename, 'w'))
:write(write_xml(xml_root, true):sub(2) .. '\n') :write(write_xml(mlist_result, true):sub(2) .. '\n')
:close() :close()
end end
if tracing then if tracing then
-- Here xml gets wrapped in an mrow to avoid modifying it. texio.write_nl(write_xml(mlist_result) .. '\n')
texio.write_nl(write_xml(xml_root) .. '\n')
end end
return mlist_result
end end
luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(mlist, style) luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(mlist, style)
local flag = tex.count.l__luamml_flag_int local flag = tex.count.l__luamml_flag_int
if flag & 3 == 3 then if flag & 3 == 0 then
return true return true
end end
local xml, core = process_mlist(mlist, style == 'display' and 0 or 2) local display = style == 'display'
if flag & 2 == 0 then style = flag & 4 == 4 and flag>>5 & 0x7 or display and 0 or 2
save_result(xml, style == 'display' or flag & 1 == 1) local xml, core = process_mlist(mlist, style)
local processed_xml
if flag & 2 == 2 then
processed_xml = save_result(shallow_copy(xml), display)
else
local element_type = token.get_macro'l__luamml_root_tl'
if element_type ~= 'mrow' then
if xml[0] == 'mrow' then
xml[0] = element_type
else
xml = {[0] = element_type, xml}
end end
if flag & 8 == 8 then
write_struct(make_root(shallow_copy(xml), (style == 'display' or flag & 1 == 1) and 0 or 2))
end end
if style == 'text' then processed_xml = xml
end
if not display and flag & 1 == 1 then
local startmath = tex.nest.top.tail local startmath = tex.nest.top.tail
local props = properties[startmath] local props = properties[startmath]
if not props then if not props then
@ -94,6 +106,9 @@ luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(mlist, style)
end end
props.saved_mathml_table, props.saved_mathml_core = xml, core props.saved_mathml_table, props.saved_mathml_core = xml, core
end end
if flag & 8 == 8 then
write_struct(shallow_copy(processed_xml), display and 0 or 2)
end
return true return true
end, 'dump_list') end, 'dump_list')
@ -105,7 +120,7 @@ lua.get_functions_table()[funcid] = function()
"I was asked to provide MathML code for the last formula, but there weren't any new formulas since you last asked." "I was asked to provide MathML code for the last formula, but there weren't any new formulas since you last asked."
}) })
end end
local mml = write_xml(make_root(mlist_result, mlist_display and 0 or 2)) local mml = write_xml(mlist_result)
if tex.count.tracingmathml == 1 then if tex.count.tracingmathml == 1 then
texio.write_nl(mml .. '\n') texio.write_nl(mml .. '\n')
end end

View File

@ -3,21 +3,38 @@
\int_new:N \l__luamml_flag_int \int_new:N \l__luamml_flag_int
\tl_new:N \l__luamml_filename_tl \tl_new:N \l__luamml_filename_tl
\tl_new:N \l__luamml_root_tl
\tl_set:Nn \l__luamml_root_tl { mrow }
\int_new:N \tracingmathml \int_new:N \tracingmathml
\lua_now:n { require'luamml-tex' } \lua_now:n { require'luamml-tex' }
\cs_new:Npn \luamml_flag_save: { \cs_new:Npn \luamml_flag_process: {
\int_set:Nn \l__luamml_flag_int { 2 }
}
\cs_new:Npn \luamml_flag_ignore: {
\int_set:Nn \l__luamml_flag_int { 3 } \int_set:Nn \l__luamml_flag_int { 3 }
} }
\cs_new:Npn \luamml_flag_save: {
\int_set:Nn \l__luamml_flag_int { 1 }
}
\cs_new:Npn \luamml_flag_save:N #1 {
\int_set:Nn \l__luamml_flag_int { 17 + 32 * #1 }
}
\cs_new:Npn \luamml_flag_save:Nn #1 {
\int_set:Nn \l__luamml_flag_int { 21 + 32 * #1 }
\tl_set:Nn \l__luamml_root_tl
}
\cs_new:Npn \luamml_flag_save:n {
\int_set:Nn \l__luamml_flag_int { 5 }
\tl_set:Nn \l__luamml_root_tl
}
\cs_new:Npn \luamml_flag_ignore: {
\int_set:Nn \l__luamml_flag_int { 0 }
}
\cs_new:Npn \luamml_flag_structelem: { \cs_new:Npn \luamml_flag_structelem: {
\int_set:Nn \l__luamml_flag_int { 8 } \int_set:Nn \l__luamml_flag_int { 11 }
} }
\cs_new:Npn \luamml_set_filename:n { \cs_new:Npn \luamml_set_filename:n {
\tl_set:Nn \l__luamml_filename_tl \tl_set:Nn \l__luamml_filename_tl
} }
\luamml_flag_process:
\cs_new_protected:Npn \luamml_annotate:nen #1#2#3 { \cs_new_protected:Npn \luamml_annotate:nen #1#2#3 {