Skip to content

Commit

Permalink
Merge pull request #2140 from Omikhleia/feath-mathml-mtext
Browse files Browse the repository at this point in the history
feat(math): Support MathML mtext and ms elements
  • Loading branch information
alerque authored Oct 29, 2024
2 parents d09a58f + 518e497 commit 0cd24ab
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/math/base-elements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,8 @@ end

function elements.text:_init (kind, attributes, script, text)
elements.terminal._init(self)
if not (kind == "number" or kind == "identifier" or kind == "operator") then
SU.error("Unknown text node kind '" .. kind .. "'; should be one of: number, identifier, operator")
if not (kind == "number" or kind == "identifier" or kind == "operator" or kind == "string") then
SU.error("Unknown text node kind '" .. kind .. "'; should be one of: number, identifier, operator, string")
end
self.kind = kind
self.script = script
Expand Down
10 changes: 10 additions & 0 deletions packages/math/texlike.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ local mathGrammar = function (_ENV)
return ret
end
local group = P"{" * V"mathlist" * (P"}" + E("`}` expected"))
-- Simple amsmath-like \text command (no embedded math)
local textgroup = P"{" * C((1-P"}")^1) * (P"}" + E("`}` expected"))
local element_no_infix =
V"def" +
V"text" + -- Important: before command
V"command" +
group +
V"argument" +
Expand Down Expand Up @@ -115,6 +118,11 @@ local mathGrammar = function (_ENV)
sub = element_no_infix * _ * P"_" * _ * element_no_infix
atom = natural + C(utf8code - S"\\{}%^_&") +
(P"\\{" + P"\\}") / function (s) return string.sub(s, -1) end
text = (
P"\\text" *
Cg(parameters, "options") *
textgroup
)
command = (
P"\\" *
Cg(ctrl_sequence_name, "command") *
Expand Down Expand Up @@ -374,6 +382,8 @@ local function compileToMathML_aux (_, arg_env, tree)
return compileToMathML_aux(nil, compiledArgs, tree[1])
end)
return nil
elseif tree.id == "text" then
tree.command = "mtext"
elseif tree.id == "command" and commands[tree.command] then
local argTypes = commands[tree.command][1]
local cmdFun = commands[tree.command][2]
Expand Down
19 changes: 16 additions & 3 deletions packages/math/typesetter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function ConvertMathML (_, content)
local script = content.options.mathvariant and b.mathVariantToScriptType(content.options.mathvariant)
local text = content[1]
if type(text) ~= "string" then
SU.error("mi command contains " .. text .. ", which is not text")
SU.error("mi command contains content which is not text")
end
script = script or (luautf8.len(text) == 1 and b.scriptType.italic or b.scriptType.upright)
return b.text("identifier", {}, script, text)
Expand All @@ -67,15 +67,15 @@ function ConvertMathML (_, content)
end
end
if type(text) ~= "string" then
SU.error("mo command contains " .. text .. ", which is not text")
SU.error("mo command contains content which is not text")
end
return b.text("operator", attributes, script, text)
elseif content.command == "mn" then
local script = content.options.mathvariant and b.mathVariantToScriptType(content.options.mathvariant)
or b.scriptType.upright
local text = content[1]
if type(text) ~= "string" then
SU.error("mn command contains " .. text .. ", which is not text")
SU.error("mn command contains content which is not text")
end
if string.sub(text, 1, 1) == "-" then
text = "" .. string.sub(text, 2)
Expand Down Expand Up @@ -136,6 +136,19 @@ function ConvertMathML (_, content)
return b.mtr(convertChildren(content))
elseif content.command == "mtd" then
return b.stackbox("H", convertChildren(content))
elseif content.command == "mtext" or content.command == "ms" then
if #content > 1 then
SU.error("Wrong number of children in " .. content.command .. ": " .. #content)
end
local text = content[1] or "" -- empty mtext is allowed, and found in examples...
if type(text) ~= "string" then
SU.error(content.command .. " command contains content which is not text")
end
-- MathML Core 3.2.1.1 Layout of <mtext> has some wording about forced line breaks
-- and soft wrap opportunities: ignored here.
-- There's also some explanations about CSS, italic correction etc. which we ignore too.
text = text:gsub("[\n\r]", " ")
return b.text("string", {}, b.scriptType.upright, text:gsub("%s+", " "))
else
SU.error("Unknown math command " .. content.command)
end
Expand Down

0 comments on commit 0cd24ab

Please sign in to comment.