Skip to content

Commit

Permalink
OMML reader: consolidate adjacent texts with same style.
Browse files Browse the repository at this point in the history
This affects EStyled and EText elements. This way we get
`\mathbf{123abc456}` in LaTeX output instead of
`\mathbf{123}\mathbf{abc}\mathbf{456}`. See
jgm/pandoc#10560 for discussion.
  • Loading branch information
jgm committed Jan 22, 2025
1 parent 827ecab commit 12c19fc
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 117 deletions.
13 changes: 11 additions & 2 deletions src/Text/TeXMath/Readers/OMML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ elemToOMML element | isElem "m" "oMathPara" element = do
[x] -> x
xs -> EGrouped xs) expList
elemToOMML element | isElem "m" "oMath" element =
Just $ concat $ mapMaybe elemToExps $ unwrapWTags $ elChildren element
Just $ elemsToExps $ unwrapWTags $ elChildren element
elemToOMML _ = Nothing

-- oMath can contain w:hyperlink, w:sdt, etc. I can't find a complete
Expand Down Expand Up @@ -221,8 +221,17 @@ oMathRunTextStyleToTextType (Styled scr sty)
Just $ TextBoldItalic
| otherwise = Nothing

-- merge adjacent Exps with same style; see jgm/pandoc#10560:
mergeExps :: [Exp] -> [Exp]
mergeExps [] = []
mergeExps (EStyled tt xs : EStyled tt' xs' : rest)
| tt == tt' = mergeExps (EStyled tt (xs <> xs') : rest)
mergeExps (EText tt x : EText tt' x' : rest)
| tt == tt' = mergeExps (EText tt (x <> x') : rest)
mergeExps (x:xs) = x : mergeExps xs

elemsToExps :: [Element] -> [Exp]
elemsToExps = concat . mapMaybe elemToExps
elemsToExps = mergeExps . concat . mapMaybe elemToExps

elemToExps :: Element -> Maybe [Exp]
elemToExps element = unGroup <$> elemToExps' element
Expand Down
236 changes: 121 additions & 115 deletions test/reader/omml/20.test
Original file line number Diff line number Diff line change
Expand Up @@ -814,119 +814,125 @@
</m:oMathPara>

>>> native
[ EStyled TextScript [ EIdentifier "A" ]
, EStyled TextScript [ EIdentifier "B" ]
, EStyled TextScript [ EIdentifier "C" ]
, EStyled TextScript [ EIdentifier "D" ]
, EStyled TextScript [ EIdentifier "E" ]
, EStyled TextScript [ EIdentifier "F" ]
, EStyled TextScript [ EIdentifier "G" ]
, EStyled TextScript [ EIdentifier "H" ]
, EStyled TextScript [ EIdentifier "I" ]
, EStyled TextScript [ EIdentifier "J" ]
, EStyled TextScript [ EIdentifier "K" ]
, EStyled TextScript [ EIdentifier "L" ]
, EStyled TextScript [ EIdentifier "M" ]
, EStyled TextScript [ EIdentifier "N" ]
, EStyled TextScript [ EIdentifier "O" ]
, EStyled TextScript [ EIdentifier "P" ]
, EStyled TextScript [ EIdentifier "Q" ]
, EStyled TextScript [ EIdentifier "R" ]
, EStyled TextScript [ EIdentifier "S" ]
, EStyled TextScript [ EIdentifier "T" ]
, EStyled TextScript [ EIdentifier "U" ]
, EStyled TextScript [ EIdentifier "V" ]
, EStyled TextScript [ EIdentifier "W" ]
, EStyled TextScript [ EIdentifier "X" ]
, EStyled TextScript [ EIdentifier "Y" ]
, EStyled TextScript [ EIdentifier "Z" ]
, EStyled TextScript [ EIdentifier "a" ]
, EStyled TextScript [ EIdentifier "b" ]
, EStyled TextScript [ EIdentifier "c" ]
, EStyled TextScript [ EIdentifier "d" ]
, EStyled TextScript [ EIdentifier "e" ]
, EStyled TextScript [ EIdentifier "f" ]
, EStyled TextScript [ EIdentifier "g" ]
, EStyled TextScript [ EIdentifier "h" ]
, EStyled TextScript [ EIdentifier "i" ]
, EStyled TextScript [ EIdentifier "j" ]
, EStyled TextScript [ EIdentifier "k" ]
, EStyled TextScript [ EIdentifier "l" ]
, EStyled TextScript [ EIdentifier "m" ]
, EStyled TextScript [ EIdentifier "n" ]
, EStyled TextScript [ EIdentifier "o" ]
, EStyled TextScript [ EIdentifier "p" ]
, EStyled TextScript [ EIdentifier "q" ]
, EStyled TextScript [ EIdentifier "r" ]
, EStyled TextScript [ EIdentifier "s" ]
, EStyled TextScript [ EIdentifier "t" ]
, EStyled TextScript [ EIdentifier "u" ]
, EStyled TextScript [ EIdentifier "v" ]
, EStyled TextScript [ EIdentifier "w" ]
, EStyled TextScript [ EIdentifier "x" ]
, EStyled TextScript [ EIdentifier "y" ]
, EStyled TextScript [ EIdentifier "z" ]
, EStyled TextDoubleStruck [ EIdentifier "A" ]
, EStyled TextDoubleStruck [ EIdentifier "B" ]
, EStyled TextDoubleStruck [ EIdentifier "C" ]
, EStyled TextDoubleStruck [ EIdentifier "D" ]
, EStyled TextDoubleStruck [ EIdentifier "E" ]
, EStyled TextDoubleStruck [ EIdentifier "F" ]
, EStyled TextDoubleStruck [ EIdentifier "G" ]
, EStyled TextDoubleStruck [ EIdentifier "H" ]
, EStyled TextDoubleStruck [ EIdentifier "I" ]
, EStyled TextDoubleStruck [ EIdentifier "J" ]
, EStyled TextDoubleStruck [ EIdentifier "K" ]
, EStyled TextDoubleStruck [ EIdentifier "L" ]
, EStyled TextDoubleStruck [ EIdentifier "M" ]
, EStyled TextDoubleStruck [ EIdentifier "N" ]
, EStyled TextDoubleStruck [ EIdentifier "O" ]
, EStyled TextDoubleStruck [ EIdentifier "P" ]
, EStyled TextDoubleStruck [ EIdentifier "Q" ]
, EStyled TextDoubleStruck [ EIdentifier "R" ]
, EStyled TextDoubleStruck [ EIdentifier "S" ]
, EStyled TextDoubleStruck [ EIdentifier "T" ]
, EStyled TextDoubleStruck [ EIdentifier "U" ]
, EStyled TextDoubleStruck [ EIdentifier "V" ]
, EStyled TextDoubleStruck [ EIdentifier "W" ]
, EStyled TextDoubleStruck [ EIdentifier "X" ]
, EStyled TextDoubleStruck [ EIdentifier "Y" ]
, EStyled TextDoubleStruck [ EIdentifier "Z" ]
, EStyled TextDoubleStruck [ EIdentifier "a" ]
, EStyled TextDoubleStruck [ EIdentifier "b" ]
, EStyled TextDoubleStruck [ EIdentifier "c" ]
, EStyled TextDoubleStruck [ EIdentifier "d" ]
, EStyled TextDoubleStruck [ EIdentifier "e" ]
, EStyled TextDoubleStruck [ EIdentifier "f" ]
, EStyled TextDoubleStruck [ EIdentifier "g" ]
, EStyled TextDoubleStruck [ EIdentifier "h" ]
, EStyled TextDoubleStruck [ EIdentifier "i" ]
, EStyled TextDoubleStruck [ EIdentifier "j" ]
, EStyled TextDoubleStruck [ EIdentifier "k" ]
, EStyled TextDoubleStruck [ EIdentifier "l" ]
, EStyled TextDoubleStruck [ EIdentifier "m" ]
, EStyled TextDoubleStruck [ EIdentifier "n" ]
, EStyled TextDoubleStruck [ EIdentifier "o" ]
, EStyled TextDoubleStruck [ EIdentifier "p" ]
, EStyled TextDoubleStruck [ EIdentifier "q" ]
, EStyled TextDoubleStruck [ EIdentifier "r" ]
, EStyled TextDoubleStruck [ EIdentifier "s" ]
, EStyled TextDoubleStruck [ EIdentifier "t" ]
, EStyled TextDoubleStruck [ EIdentifier "u" ]
, EStyled TextDoubleStruck [ EIdentifier "v" ]
, EStyled TextDoubleStruck [ EIdentifier "w" ]
, EStyled TextDoubleStruck [ EIdentifier "x" ]
, EStyled TextDoubleStruck [ EIdentifier "y" ]
, EStyled TextDoubleStruck [ EIdentifier "z" ]
, EStyled TextDoubleStruck [ ENumber "0" ]
, EStyled TextDoubleStruck [ ENumber "1" ]
, EStyled TextDoubleStruck [ ENumber "2" ]
, EStyled TextDoubleStruck [ ENumber "3" ]
, EStyled TextDoubleStruck [ ENumber "4" ]
, EStyled TextDoubleStruck [ ENumber "5" ]
, EStyled TextDoubleStruck [ ENumber "6" ]
, EStyled TextDoubleStruck [ ENumber "7" ]
, EStyled TextDoubleStruck [ ENumber "8" ]
, EStyled TextDoubleStruck [ ENumber "9" ]
, EStyled TextDoubleStruck [ ENumber "0" ]
[ EStyled
TextScript
[ EIdentifier "A"
, EIdentifier "B"
, EIdentifier "C"
, EIdentifier "D"
, EIdentifier "E"
, EIdentifier "F"
, EIdentifier "G"
, EIdentifier "H"
, EIdentifier "I"
, EIdentifier "J"
, EIdentifier "K"
, EIdentifier "L"
, EIdentifier "M"
, EIdentifier "N"
, EIdentifier "O"
, EIdentifier "P"
, EIdentifier "Q"
, EIdentifier "R"
, EIdentifier "S"
, EIdentifier "T"
, EIdentifier "U"
, EIdentifier "V"
, EIdentifier "W"
, EIdentifier "X"
, EIdentifier "Y"
, EIdentifier "Z"
, EIdentifier "a"
, EIdentifier "b"
, EIdentifier "c"
, EIdentifier "d"
, EIdentifier "e"
, EIdentifier "f"
, EIdentifier "g"
, EIdentifier "h"
, EIdentifier "i"
, EIdentifier "j"
, EIdentifier "k"
, EIdentifier "l"
, EIdentifier "m"
, EIdentifier "n"
, EIdentifier "o"
, EIdentifier "p"
, EIdentifier "q"
, EIdentifier "r"
, EIdentifier "s"
, EIdentifier "t"
, EIdentifier "u"
, EIdentifier "v"
, EIdentifier "w"
, EIdentifier "x"
, EIdentifier "y"
, EIdentifier "z"
]
, EStyled
TextDoubleStruck
[ EIdentifier "A"
, EIdentifier "B"
, EIdentifier "C"
, EIdentifier "D"
, EIdentifier "E"
, EIdentifier "F"
, EIdentifier "G"
, EIdentifier "H"
, EIdentifier "I"
, EIdentifier "J"
, EIdentifier "K"
, EIdentifier "L"
, EIdentifier "M"
, EIdentifier "N"
, EIdentifier "O"
, EIdentifier "P"
, EIdentifier "Q"
, EIdentifier "R"
, EIdentifier "S"
, EIdentifier "T"
, EIdentifier "U"
, EIdentifier "V"
, EIdentifier "W"
, EIdentifier "X"
, EIdentifier "Y"
, EIdentifier "Z"
, EIdentifier "a"
, EIdentifier "b"
, EIdentifier "c"
, EIdentifier "d"
, EIdentifier "e"
, EIdentifier "f"
, EIdentifier "g"
, EIdentifier "h"
, EIdentifier "i"
, EIdentifier "j"
, EIdentifier "k"
, EIdentifier "l"
, EIdentifier "m"
, EIdentifier "n"
, EIdentifier "o"
, EIdentifier "p"
, EIdentifier "q"
, EIdentifier "r"
, EIdentifier "s"
, EIdentifier "t"
, EIdentifier "u"
, EIdentifier "v"
, EIdentifier "w"
, EIdentifier "x"
, EIdentifier "y"
, EIdentifier "z"
, ENumber "0"
, ENumber "1"
, ENumber "2"
, ENumber "3"
, ENumber "4"
, ENumber "5"
, ENumber "6"
, ENumber "7"
, ENumber "8"
, ENumber "9"
, ENumber "0"
]
]

0 comments on commit 12c19fc

Please sign in to comment.