Skip to content

Commit 3023b93

Browse files
OmikhleiaDidier Willis
authored andcommitted
feat(math): Support MathML mtext and ms elements
1 parent 3a0ef46 commit 3023b93

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

packages/math/base-elements.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,8 @@ end
908908

909909
function elements.text:_init (kind, attributes, script, text)
910910
elements.terminal._init(self)
911-
if not (kind == "number" or kind == "identifier" or kind == "operator") then
912-
SU.error("Unknown text node kind '" .. kind .. "'; should be one of: number, identifier, operator")
911+
if not (kind == "number" or kind == "identifier" or kind == "operator" or kind == "string") then
912+
SU.error("Unknown text node kind '" .. kind .. "'; should be one of: number, identifier, operator, string")
913913
end
914914
self.kind = kind
915915
self.script = script

packages/math/typesetter.lua

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function ConvertMathML (_, content)
4545
local script = content.options.mathvariant and b.mathVariantToScriptType(content.options.mathvariant)
4646
local text = content[1]
4747
if type(text) ~= "string" then
48-
SU.error("mi command contains " .. text .. ", which is not text")
48+
SU.error("mi command contains content which is not text")
4949
end
5050
script = script or (luautf8.len(text) == 1 and b.scriptType.italic or b.scriptType.upright)
5151
return b.text("identifier", {}, script, text)
@@ -67,15 +67,15 @@ function ConvertMathML (_, content)
6767
end
6868
end
6969
if type(text) ~= "string" then
70-
SU.error("mo command contains " .. text .. ", which is not text")
70+
SU.error("mo command contains content which is not text")
7171
end
7272
return b.text("operator", attributes, script, text)
7373
elseif content.command == "mn" then
7474
local script = content.options.mathvariant and b.mathVariantToScriptType(content.options.mathvariant)
7575
or b.scriptType.upright
7676
local text = content[1]
7777
if type(text) ~= "string" then
78-
SU.error("mn command contains " .. text .. ", which is not text")
78+
SU.error("mn command contains content which is not text")
7979
end
8080
if string.sub(text, 1, 1) == "-" then
8181
text = "" .. string.sub(text, 2)
@@ -136,6 +136,19 @@ function ConvertMathML (_, content)
136136
return b.mtr(convertChildren(content))
137137
elseif content.command == "mtd" then
138138
return b.stackbox("H", convertChildren(content))
139+
elseif content.command == "mtext" or content.command == "ms" then
140+
if #content > 1 then
141+
SU.error("Wrong number of children in " .. content.command .. ": " .. #content)
142+
end
143+
local text = content[1] or "" -- empty mtext is allowed, and found in examples...
144+
if type(text) ~= "string" then
145+
SU.error(content.command .. " command contains content which is not text")
146+
end
147+
-- MathML Core 3.2.1.1 Layout of <mtext> has some wording about forced line breaks
148+
-- and soft wrap opportunities: ignored here.
149+
-- There's also some explanations about CSS, italic correction etc. which we ignore too.
150+
text = text:gsub("[\n\r]", " ")
151+
return b.text("string", {}, b.scriptType.upright, text:gsub("%s+", " "))
139152
else
140153
SU.error("Unknown math command " .. content.command)
141154
end

0 commit comments

Comments
 (0)