From a6e3f1c7751c946780fab56c0d4f1c7d4ca85833 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 1 Feb 2019 22:12:54 -0800 Subject: LaTeX writer: use right fold for escapeString. This is more elegant than the explicit recursive we were using. --- src/Text/Pandoc/Writers/LaTeX.hs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index 619ad05a4..a61369330 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -323,13 +323,12 @@ stringToLaTeX :: PandocMonad m => StringContext -> String -> LW m String stringToLaTeX context zs = do opts <- gets stOptions return $ - go opts context $ + foldr (go opts context) mempty $ if writerPreferAscii opts then T.unpack $ Normalize.normalize Normalize.NFD $ T.pack zs else zs where - go _ _ [] = "" - go opts ctx (x:xs) = + go opts ctx x xs = let ligatures = isEnabled Ext_smart opts && ctx == TextString isUrl = ctx == URLString mbAccentCmd = @@ -338,24 +337,23 @@ stringToLaTeX context zs = do else Nothing emits s = case mbAccentCmd of - Just cmd -> (cmd ++ "{" ++ s ++ "}") ++ - go opts ctx (drop 1 xs) -- drop combining accent - Nothing -> s ++ go opts ctx xs + Just cmd -> + cmd ++ "{" ++ s ++ "}" ++ drop 1 xs -- drop combining accent + Nothing -> s ++ xs emitc c = case mbAccentCmd of - Just cmd -> cmd ++ "{" ++ [c] ++ "}" ++ - go opts ctx (drop 1 xs) -- drop combining accent - Nothing -> c : go opts ctx xs + Just cmd -> + cmd ++ "{" ++ [c] ++ "}" ++ drop 1 xs -- drop combining accent + Nothing -> c : xs emitcseq cs = do - let rest = go opts ctx xs - case rest of + case xs of c:_ | isLetter c , ctx == TextString - -> cs <> " " <> rest - | isSpace c -> cs <> "{}" <> rest + -> cs <> " " <> xs + | isSpace c -> cs <> "{}" <> xs | ctx == TextString - -> cs <> rest - _ -> cs <> "{}" <> rest + -> cs <> xs + _ -> cs <> "{}" <> xs in case x of '{' -> emits "\\{" '}' -> emits "\\}" -- cgit v1.2.3