packages feed

texmath 0.12.8.6 → 0.12.8.7

raw patch · 13 files changed

+97/−43 lines, 13 files

Files

changelog view
@@ -1,3 +1,15 @@+texmath (0.12.8.7)++  * TeX reader: convert Bin symbols to Ord when appropriate (#234).+    E.g. in '-3', we should have an Ord rather than a Bin, so+    the spacing will be appropriate.++  * Pandoc writer: fix spacing inside EDelimited (#234).+    Previously spaces around binary operators were omitted when+    they occurred inside parens or brackets.++  * test-texmath: allow pandoc output.+ texmath (0.12.8.6)    * Typst writer: avoid redundant `lr`s (#233).
src/Text/TeXMath/Readers/TeX.hs view
@@ -25,7 +25,7 @@ module Text.TeXMath.Readers.TeX (readTeX) where -import Data.List (intercalate, intersperse, find)+import Data.List (intercalate, intersperse, find, foldl') import Control.Monad import Data.Char (isDigit, isAscii, isLetter) import qualified Data.Map as M@@ -68,40 +68,57 @@ readTeX :: Text -> Either Text [Exp] readTeX inp =   let (ms, rest) = parseMacroDefinitions inp in-  either (Left . showParseError inp) (Right . everywhere (mkT fixBins))+  either (Left . showParseError inp) (Right . fixBinList)     $ parse formula "formula" $ applyMacros ms rest---- | Convert Bin after nothing, Open, Pun, or Op to Op (#176).-fixBins :: Exp -> Exp-fixBins e =-  case e of-    EGrouped es -> EGrouped (fixBinList True es)-    EDelimited op cl des -> EDelimited op cl (fixBinListDel True des)-    _ -> e  where-  fixBinList atBeginning xs =-    case xs of-      ESymbol Bin t : rest-        | atBeginning-        -> ESymbol Op t : fixBinList False rest-      ESymbol Bin t : rest@(ESymbol ty _ : _)-        | ty == Open || ty == Pun || ty == Op-        -> ESymbol Op t : fixBinList False rest-      x:rest -> x : fixBinList False rest-      [] -> []+  -- | Convert Bin symbol type in certain contexts (#176, #234).+  fixBinList :: [Exp] -> [Exp]+  fixBinList =+    reverse . foldl' goExp [] . everywhere (mkT fixBins) -  fixBinListDel atBeginning xs =-    case xs of-      Left x : rest-        ->  Left x : fixBinListDel True rest-      Right (ESymbol Bin t) : rest-        | atBeginning-        -> Right (ESymbol Op t) : fixBinListDel False rest-      Right (ESymbol Bin t) : rest@(Right (ESymbol ty _):_)-        | ty == Open || ty == Pun || ty == Op-        -> Right (ESymbol Op t) : fixBinListDel False rest-      x:rest -> x : fixBinListDel False rest-      [] -> []+  -- TeXBook:+  -- 5. If the current item is a Bin atom, and if this was the first+  -- atom in the list, or if the most recent previous atom was Bin, Op,+  -- Rel, Open, or Punct, change the current Bin to Ord and continue with+  -- Rule 14. Otherwise continue with Rule 17.+  -- 6. If the current item is a Rel or Close or Punct atom, and if+  -- the most recent previous atom was Bin, change that previous Bin+  -- to Ord. Continue with Rule 17.++  fixBins :: Exp -> Exp+  fixBins e =+    case e of+      EGrouped es+        -> EGrouped (reverse $ foldl' goExp [] es)+      EDelimited op cl des+        -> EDelimited op cl (reverse $ foldl' goInDel [] des)+      EArray als alines+        -> EArray als (map (map (reverse . foldl' goExp [])) alines)+      EStyled tt es+        -> EStyled tt (reverse $ foldl' goExp [] es)+      _ -> e++  goExp :: [Exp] -> Exp -> [Exp]+  goExp [] (ESymbol Bin t) = [ESymbol Ord t]+  goExp  accum@(ESymbol ty _ : _) (ESymbol Bin t)+    | ty `elem` [Bin, Op, Rel, Open, Pun]+      = ESymbol Ord t : accum+  goExp (ESymbol Bin t' : rest) (ESymbol ty t)+    | ty `elem` [Rel, Close, Pun]+      = ESymbol ty t : ESymbol Ord t' : rest+  goExp xs x = x : xs++  goInDel :: [InEDelimited] -> InEDelimited -> [InEDelimited]+  goInDel [] (Right (ESymbol Bin t)) = [Right (ESymbol Ord t)]+  goInDel accum@(Left _ : _) (Right (ESymbol Bin t))+    = Right (ESymbol Ord t) : accum+  goInDel accum@(Right (ESymbol ty _) : _) (Right (ESymbol Bin t))+    | ty `elem` [Bin, Op, Rel, Open, Pun]+      = Right (ESymbol Ord t) : accum+  goInDel (Right (ESymbol Bin t') : rest) (Right (ESymbol ty t))+    | ty `elem` [Rel, Close, Pun]+      = Right (ESymbol ty t) : Right (ESymbol Ord t') : rest+  goInDel xs x = x : xs  showParseError :: Text -> ParseError -> Text showParseError inp pe =
src/Text/TeXMath/Writers/Pandoc.hs view
@@ -99,8 +99,8 @@ expToInlines tt (EMathOperator s) = Just $ renderStr tt s expToInlines tt (ESymbol _ s) = Just $ renderStr tt s expToInlines tt (EDelimited start end xs) = do-  xs' <- mapM (either (return . renderStr tt) (expToInlines tt)) xs-  return $ renderStr tt start ++ concat xs' ++ renderStr tt end+  xs' <- expsToInlines tt $ map (either (ESymbol Ord) id) xs+  return $ renderStr tt start ++ xs' ++ renderStr tt end expToInlines tt (EGrouped xs)    = expsToInlines tt xs expToInlines _ (EStyled tt' xs)  = expsToInlines (Just tt') xs expToInlines _ (ESpace n)        = Just [Str $ getSpaceChars n]
test/reader/tex/01.test view
@@ -8,7 +8,7 @@ , EFraction     NormalFrac     (EGrouped-       [ ESymbol Op "\8722"+       [ ESymbol Ord "\8722"        , EIdentifier "b"        , ESymbol Bin "\177"        , ESqrt
test/reader/tex/11.test view
@@ -13,7 +13,7 @@ , ESuper     (EIdentifier "\954")     (EGrouped-       [ ESymbol Op "\8722"+       [ ESymbol Ord "\8722"        , ENumber "11"        , ESymbol Ord "/"        , ENumber "3"
test/reader/tex/12.test view
@@ -18,7 +18,7 @@         (EArray            [ AlignLeft , AlignLeft ]            [ [ [ ENumber "1" ]-             , [ ESymbol Bin "\8722"+             , [ ESymbol Ord "\8722"                , ENumber "1"                , ESymbol Rel "\8804"                , EIdentifier "x"
test/reader/tex/quadratic_formula.test view
@@ -7,7 +7,7 @@ , EFraction     NormalFrac     (EGrouped-       [ ESymbol Op "\8722"+       [ ESymbol Ord "\8722"        , EIdentifier "b"        , ESymbol Bin "\177"        , ESqrt
test/reader/tex/schwinger_dyson.test view
@@ -27,7 +27,7 @@     , Right (EIdentifier "\968")     ] , ESymbol Rel "="-, ESymbol Bin "\8722"+, ESymbol Ord "\8722" , EStyled TextNormal [ EIdentifier "i" ] , EDelimited     "\10216"
test/reader/tex/sophomores_dream.test view
@@ -16,11 +16,11 @@ , EGrouped     [ ESuper         (EDelimited-           "(" ")" [ Right (ESymbol Op "\8722") , Right (ENumber "1") ])+           "(" ")" [ Right (ESymbol Ord "\8722") , Right (ENumber "1") ])         (EGrouped [ EIdentifier "n" , ESymbol Bin "+" , ENumber "1" ])     , ESpace (1 % 6)     , ESuper         (EIdentifier "n")-        (EGrouped [ ESymbol Op "\8722" , EIdentifier "n" ])+        (EGrouped [ ESymbol Ord "\8722" , EIdentifier "n" ])     ] ]
test/reader/tex/sphere_volume.test view
@@ -98,7 +98,7 @@         , EDelimited             "("             ")"-            [ Right (ESymbol Op "\8722")+            [ Right (ESymbol Ord "\8722")             , Right (EMathOperator "cos")             , Right (EIdentifier "\952")             ]
+ test/regression/234.test view
@@ -0,0 +1,24 @@+<<< native+[ ENumber "2"+, ESymbol Bin "+"+, EDelimited+    "("+    ")"+    [ Right (ENumber "2")+    , Right (ESymbol Bin "+")+    , Right (ENumber "3")+    ]+]+>>> pandoc+[ Str "2"+, Str "\8197"+, Str "+"+, Str "\8197"+, Str "("+, Str "2"+, Str "\8197"+, Str "+"+, Str "\8197"+, Str "3"+, Str ")"+]
test/test-texmath.hs view
@@ -149,4 +149,5 @@           , ("eqn", writeEqn DisplayBlock)           , ("typst", writeTypst DisplayBlock)           , ("native", T.pack . ppShow)+          , ("pandoc", maybe "" (T.pack . ppShow) . writePandoc DisplayBlock)           ]
texmath.cabal view
@@ -1,5 +1,5 @@ Name:                texmath-Version:             0.12.8.6+Version:             0.12.8.7 Cabal-Version:       >= 1.10 Build-type:          Simple Synopsis:            Conversion between math formats.