Skip to content

Commit 028e3a7

Browse files
committed
MathML reader: properly handle mmultiscripts.
Closes #252.
1 parent 8f41b3f commit 028e3a7

File tree

2 files changed

+98
-14
lines changed

2 files changed

+98
-14
lines changed

src/Text/TeXMath/Readers/MathML.hs

+30-14
Original file line numberDiff line numberDiff line change
@@ -471,22 +471,38 @@ annotation e = do
471471
multiscripts :: Element -> MML Exp
472472
multiscripts e = do
473473
let (xs, pres) = break ((== "mprescripts") . name) (elChildren e)
474-
let row'' e' = if name e' == "none"
475-
then return $ EGrouped []
476-
else row e'
474+
let row'' e' = case name e' of
475+
"none" -> return $ EGrouped []
476+
"mrow" -> row e'
477+
_ -> mkExp <$> expr e'
478+
let maybeGroup [w] = w
479+
maybeGroup ws = EGrouped ws
477480
xs' <- mapM row'' xs
478-
let base =
479-
case xs' of
480-
[x] -> x
481-
[x,y] -> ESub x y
482-
(x:y:z:_) -> ESubsup x y z
483-
[] -> EGrouped []
481+
let getSubs [] = []
482+
getSubs [w] = [w]
483+
getSubs (w:_:ws) = w : getSubs ws
484+
let getSups [] = []
485+
getSups [_] = []
486+
getSups (_:w:ws) = w : getSups ws
487+
let (base, postSubs, postSups) =
488+
case xs' of
489+
[] -> (EGrouped [], [], [])
490+
(w:ws) -> (w, getSubs ws, getSups ws)
491+
let rightSide =
492+
case (postSubs, postSups) of
493+
([],[]) -> base
494+
(ws,[]) -> ESub base (maybeGroup ws)
495+
([],ws) -> ESuper base (maybeGroup ws)
496+
(ws,ys) -> ESubsup base (maybeGroup ws) (maybeGroup ys)
484497
pres' <- mapM row'' $ drop 1 pres
485-
return $
486-
case pres' of
487-
(x:y:_) -> EGrouped [ESubsup (EGrouped []) x y, base]
488-
[x] -> EGrouped [ESub x (EGrouped []), base]
489-
[] -> base
498+
let (preSubs, preSups) = (getSubs pres', getSups pres')
499+
let leftSide =
500+
case (preSubs, preSups) of
501+
([],[]) -> EGrouped []
502+
(ws,[]) -> ESub (EGrouped []) (maybeGroup ws)
503+
([],ws) -> ESuper (EGrouped []) (maybeGroup ws)
504+
(ws,ys) -> ESubsup (EGrouped []) (maybeGroup ws) (maybeGroup ys)
505+
return $ EGrouped [leftSide, rightSide]
490506

491507

492508
-- Table

test/regression/252.test

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<<< mml
2+
<math display="block">
3+
<mmultiscripts>
4+
<mi>X</mi> <!-- base expression -->
5+
<mn>1</mn> <!-- post-sub-script-1 -->
6+
<mn>2</mn> <!-- post-sup-script-1 -->
7+
<mn>3</mn> <!-- post-sub-script-2 -->
8+
<mn>4</mn> <!-- post-sup-script-2 -->
9+
<mprescripts />
10+
<mn>5</mn> <!-- pre-sub-script-1 -->
11+
<mn>6</mn> <!-- pre-sup-script-1 -->
12+
<mn>7</mn> <!-- pre-sub-script-2 -->
13+
<mn>8</mn> <!-- pre-sup-script-2 -->
14+
</mmultiscripts>
15+
<mmultiscripts>
16+
<mi>X</mi> <!-- base expression -->
17+
<mn>1</mn> <!-- post-sub-script -->
18+
<mn>4</mn> <!-- post-sup-script -->
19+
<mprescripts />
20+
<mn>8</mn> <!-- pre-sub-script -->
21+
</mmultiscripts>
22+
<mmultiscripts>
23+
<mi>X</mi> <!-- base expression -->
24+
<none/>
25+
<mn>4</mn> <!-- post-sup-script -->
26+
<mprescripts />
27+
<mn>8</mn> <!-- pre-sub-script -->
28+
</mmultiscripts>
29+
<mmultiscripts>
30+
<mi>X</mi> <!-- base expression -->
31+
<mn>1</mn> <!-- post-sub-script -->
32+
</mmultiscripts>
33+
<MMULTISCRIPTS>
34+
<MROW><MI>F</MI></MROW>
35+
<MROW><MN>2</MN></MROW>
36+
<NONE>
37+
</NONE>
38+
<MPRESCRIPTS></MPRESCRIPTS>
39+
<MROW><MN>3</MN></MROW>
40+
<NONE>
41+
</NONE>
42+
</MMULTISCRIPTS>
43+
</math>
44+
>>> native
45+
[ EGrouped
46+
[ ESubsup
47+
(EGrouped [])
48+
(EGrouped [ ENumber "5" , ENumber "7" ])
49+
(EGrouped [ ENumber "6" , ENumber "8" ])
50+
, ESubsup
51+
(EIdentifier "X")
52+
(EGrouped [ ENumber "1" , ENumber "3" ])
53+
(EGrouped [ ENumber "2" , ENumber "4" ])
54+
]
55+
, EGrouped
56+
[ ESub (EGrouped []) (ENumber "8")
57+
, ESubsup (EIdentifier "X") (ENumber "1") (ENumber "4")
58+
]
59+
, EGrouped
60+
[ ESub (EGrouped []) (ENumber "8")
61+
, ESubsup (EIdentifier "X") (EGrouped []) (ENumber "4")
62+
]
63+
, ESub (EIdentifier "X") (ENumber "1")
64+
, EGrouped
65+
[ ESubsup (EGrouped []) (ENumber "3") (EGrouped [])
66+
, ESubsup (EIdentifier "F") (ENumber "2") (EGrouped [])
67+
]
68+
]

0 commit comments

Comments
 (0)