git.iko.soy/typeable/pandoc
on master help sign in
Swap suboptimal uses of maybe and fromMaybe (#6111)

Anywhere "maybe" is used with "id" as its second argument, using "fromMaybe" instead will simplify the code. Conversely, anywhere "fromMaybe" is used with the result of "fmap" or "<$>" as its second argument, using "maybe" instead will simplify the code.

commit1ea5b1802072fe2724977fa859c0e5b48f15917d authorJoseph C. Sible <josephcsible@users.noreply.github.com> dateMon 3 Feb 2020, 23:31 (7 years ago) parentd9b1776
5 files changed+13−13
src/Text/Pandoc/Readers/EPUB.hs+1−1 src/Text/Pandoc/Readers/HTML.hs+2−2 src/Text/Pandoc/Writers/EPUB.hs+4−4 src/Text/Pandoc/Writers/Markdown.hs+3−3 src/Text/Pandoc/Writers/Ms.hs+3−3
src/Text/Pandoc/Readers/EPUB.hs+1−1
@@ -67,7 +67,7 @@ archiveToEPUB os archive = do
6767 (coverId, meta) <- parseMeta content
6868 (cover, items) <- parseManifest content coverId
6969 -- No need to collapse here as the image path is from the manifest file
70− let coverDoc = fromMaybe mempty (imageToPandoc <$> cover)
70+ let coverDoc = maybe mempty imageToPandoc cover
7171 spine <- parseSpine items content
7272 let escapedSpine = map (escapeURI . T.pack . takeFileName . fst) spine
7373 Pandoc _ bs <-
src/Text/Pandoc/Readers/HTML.hs+2−2
@@ -812,8 +812,8 @@ pSpan = try $ do
812812 let isSmallCaps = fontVariant == "small-caps" || "smallcaps" `elem` classes
813813 where styleAttr = fromMaybe "" $ lookup "style" attr
814814 fontVariant = fromMaybe "" $ pickStyleAttrProps ["font-variant"] styleAttr
815− classes = fromMaybe [] $
816− T.words <$> lookup "class" attr
815+ classes = maybe []
816+ T.words $ lookup "class" attr
817817 let tag = if isSmallCaps then B.smallcaps else B.spanWith (mkAttr attr)
818818 return $ tag contents
819819
src/Text/Pandoc/Writers/EPUB.hs+4−4
@@ -356,7 +356,7 @@ metadataFromMeta opts meta = EPUBMetadata{
356356 (writerVariables opts))
357357 `mplus` (metaValueToString <$> lookupMeta "cover-image" meta)
358358 mCss = lookupMeta "css" meta <|> lookupMeta "stylesheet" meta
359− stylesheets = fromMaybe [] (metaValueToPaths <$> mCss) ++
359+ stylesheets = maybe [] metaValueToPaths mCss ++
360360 case lookupContext "css" (writerVariables opts) of
361361 Just xs -> map TS.unpack xs
362362 Nothing ->
@@ -659,8 +659,8 @@ pandocToEPUB version opts doc = do
659659 ("media-type", maybe "" TS.unpack $
660660 getMimeType $ eRelativePath ent)] $ ()
661661
662− let tocTitle = fromMaybe plainTitle $
663− metaValueToString <$> lookupMeta "toc-title" meta
662+ let tocTitle = maybe plainTitle
663+ metaValueToString $ lookupMeta "toc-title" meta
664664 uuid <- case epubIdentifier metadata of
665665 (x:_) -> return $ identifierText x -- use first identifier as UUID
666666 [] -> throwError $ PandocShouldNeverHappenError "epubIdentifier is null" -- shouldn't happen
@@ -1400,7 +1400,7 @@ relatorMap =
14001400 ]
14011401
14021402docTitle' :: Meta -> [Inline]
1403−docTitle' meta = fromMaybe [] $ go <$> lookupMeta "title" meta
1403+docTitle' meta = maybe [] go $ lookupMeta "title" meta
14041404 where go (MetaString s) = [Str s]
14051405 go (MetaInlines xs) = xs
14061406 go (MetaBlocks [Para xs]) = xs
src/Text/Pandoc/Writers/Markdown.hs+3−3
@@ -194,9 +194,9 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
194194 (blockListToMarkdown opts)
195195 (inlineListToMarkdown opts)
196196 meta
197− let title' = maybe empty id $ getField "title" metadata
198− let authors' = maybe [] id $ getField "author" metadata
199− let date' = maybe empty id $ getField "date" metadata
197+ let title' = fromMaybe empty $ getField "title" metadata
198+ let authors' = fromMaybe [] $ getField "author" metadata
199+ let date' = fromMaybe empty $ getField "date" metadata
200200 let titleblock = case writerTemplate opts of
201201 Just _ | isPlain ->
202202 plainTitleBlock title' authors' date'
src/Text/Pandoc/Writers/Ms.hs+3−3
@@ -26,7 +26,7 @@ import Control.Monad.State.Strict
2626import Data.Char (isLower, isUpper, ord)
2727import Data.List (intercalate, intersperse)
2828import qualified Data.Map as Map
29−import Data.Maybe (catMaybes, fromMaybe)
29+import Data.Maybe (catMaybes)
3030import Data.Text (Text)
3131import qualified Data.Text as T
3232import Network.URI (escapeURIString, isAllowedInURI)
@@ -507,8 +507,8 @@ toMacro sty toktype =
507507 tokSty = Map.lookup toktype (tokenStyles sty)
508508 tokCol = (tokSty >>= tokenColor) `mplus` defaultColor sty
509509 -- tokBg = (tokSty >>= tokenBackground) `mplus` backgroundColor sty
510− tokBold = fromMaybe False (tokenBold <$> tokSty)
511− tokItalic = fromMaybe False (tokenItalic <$> tokSty)
510+ tokBold = maybe False tokenBold tokSty
511+ tokItalic = maybe False tokenItalic tokSty
512512 -- tokUnderline = fromMaybe False (tokSty >>= tokUnderline)
513513 -- lnColor = lineNumberColor sty
514514 -- lnBkgColor = lineNumberBackgroundColor sty