git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: use right fold for escapeString.

This is more elegant than the explicit recursive we were using.

commita6e3f1c7751c946780fab56c0d4f1c7d4ca85833 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 1 Feb 2019, 22:12 (8 years ago) parentf5ebe98
1 file changed+13−15
src/Text/Pandoc/Writers/LaTeX.hs+13−15
src/Text/Pandoc/Writers/LaTeX.hs+13−15
@@ -323,13 +323,12 @@ stringToLaTeX :: PandocMonad m => StringContext -> String -> LW m String
323323stringToLaTeX context zs = do
324324 opts <- gets stOptions
325325 return $
326− go opts context $
326+ foldr (go opts context) mempty $
327327 if writerPreferAscii opts
328328 then T.unpack $ Normalize.normalize Normalize.NFD $ T.pack zs
329329 else zs
330330 where
331− go _ _ [] = ""
332− go opts ctx (x:xs) =
331+ go opts ctx x xs =
333332 let ligatures = isEnabled Ext_smart opts && ctx == TextString
334333 isUrl = ctx == URLString
335334 mbAccentCmd =
@@ -338,24 +337,23 @@ stringToLaTeX context zs = do
338337 else Nothing
339338 emits s =
340339 case mbAccentCmd of
341− Just cmd -> (cmd ++ "{" ++ s ++ "}") ++
342− go opts ctx (drop 1 xs) -- drop combining accent
343− Nothing -> s ++ go opts ctx xs
340+ Just cmd ->
341+ cmd ++ "{" ++ s ++ "}" ++ drop 1 xs -- drop combining accent
342+ Nothing -> s ++ xs
344343 emitc c =
345344 case mbAccentCmd of
346− Just cmd -> cmd ++ "{" ++ [c] ++ "}" ++
347− go opts ctx (drop 1 xs) -- drop combining accent
348− Nothing -> c : go opts ctx xs
345+ Just cmd ->
346+ cmd ++ "{" ++ [c] ++ "}" ++ drop 1 xs -- drop combining accent
347+ Nothing -> c : xs
349348 emitcseq cs = do
350− let rest = go opts ctx xs
351− case rest of
349+ case xs of
352350 c:_ | isLetter c
353351 , ctx == TextString
354− -> cs <> " " <> rest
355− | isSpace c -> cs <> "{}" <> rest
352+ -> cs <> " " <> xs
353+ | isSpace c -> cs <> "{}" <> xs
356354 | ctx == TextString
357− -> cs <> rest
358− _ -> cs <> "{}" <> rest
355+ -> cs <> xs
356+ _ -> cs <> "{}" <> xs
359357 in case x of
360358 '{' -> emits "\\{"
361359 '}' -> emits "\\}"