git.iko.soy/typeable/pandoc
on master help sign in
Factor out a findM function (#6125)

This adds a new function to the API: Text.Pandoc.Shared.findM.

commitf2f559003e4fe40c3623d1b7acfbeea5a8da9f34 authorJoseph C. Sible <josephcsible@users.noreply.github.com> dateSat 8 Feb 2020, 12:12 (7 years ago) parent12c7570
3 files changed+13−18
src/Text/Pandoc/App.hs+2−8 src/Text/Pandoc/App/CommandLineOptions.hs+2−10 src/Text/Pandoc/Shared.hs+9
src/Text/Pandoc/App.hs+2−8
@@ -58,7 +58,7 @@ import Text.Pandoc.PDF (makePDF)
5858import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
5959import Text.Pandoc.Shared (eastAsianLineBreakFilter, stripEmptyParagraphs,
6060 headerShift, isURI, tabFilter, uriPathToPath, filterIpynbOutput,
61− defaultUserDataDirs, tshow)
61+ defaultUserDataDirs, tshow, findM)
6262import Text.Pandoc.Writers.Shared (lookupMetaString)
6363import Text.Pandoc.Readers.Markdown (yamlToMeta)
6464import qualified Text.Pandoc.UTF8 as UTF8
@@ -94,13 +94,7 @@ convertWithOpts opts = do
9494 datadir <- case optDataDir opts of
9595 Nothing -> do
9696 ds <- defaultUserDataDirs
97− let selectUserDataDir [] = return Nothing
98− selectUserDataDir (dir:dirs) = do
99− exists <- doesDirectoryExist dir
100− if exists
101− then return (Just dir)
102− else selectUserDataDir dirs
103− selectUserDataDir ds
97+ findM doesDirectoryExist ds
10498 Just _ -> return $ optDataDir opts
10599
106100 let runIO' :: PandocIO a -> IO a
src/Text/Pandoc/App/CommandLineOptions.hs+2−10
@@ -50,7 +50,7 @@ import Text.Pandoc
5050import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), IpynbOutput (..), addMeta)
5151import Text.Pandoc.Filter (Filter (..))
5252import Text.Pandoc.Highlighting (highlightingStyles)
53−import Text.Pandoc.Shared (ordNub, elemText, safeStrRead, defaultUserDataDirs)
53+import Text.Pandoc.Shared (ordNub, elemText, safeStrRead, defaultUserDataDirs, findM)
5454import Text.Printf
5555
5656#ifdef EMBED_DATA_FILES
@@ -118,14 +118,6 @@ engines = map ("html",) htmlEngines ++
118118pdfEngines :: [String]
119119pdfEngines = ordNub $ map snd engines
120120
121−findFile :: PandocMonad m => [FilePath] -> m (Maybe FilePath)
122−findFile [] = return Nothing
123−findFile (f:fs) = do
124− exists <- fileExists f
125− if exists
126− then return $ Just f
127− else findFile fs
128−
129121-- | A list of functions, each transforming the options data structure
130122-- in response to a command-line option.
131123options :: [OptDescr (Opt -> IO Opt)]
@@ -996,7 +988,7 @@ applyDefaults opt file = runIOorExplode $ do
996988 Nothing -> map (</> ("defaults" </> fp))
997989 dataDirs
998990 Just dd -> [dd </> "defaults" </> fp]
999− fp' <- fromMaybe fp <$> findFile fps
991+ fp' <- fromMaybe fp <$> findM fileExists fps
1000992 inp <- readFileLazy fp'
1001993 case Y.decode1 inp of
1002994 Right (f :: Opt -> Opt) -> return $ f opt
src/Text/Pandoc/Shared.hs+9
@@ -27,6 +27,7 @@ module Text.Pandoc.Shared (
2727 splitTextByIndices,
2828 substitute,
2929 ordNub,
30+ findM,
3031 -- * Text processing
3132 ToString (..),
3233 ToText (..),
@@ -198,6 +199,14 @@ ordNub l = go Set.empty l
198199 go s (x:xs) = if x `Set.member` s then go s xs
199200 else x : go (Set.insert x s) xs
200201
202+findM :: forall m t a. (Monad m, Foldable t) => (a -> m Bool) -> t a -> m (Maybe a)
203+findM p = foldr go (pure Nothing)
204+ where
205+ go :: a -> m (Maybe a) -> m (Maybe a)
206+ go x acc = do
207+ b <- p x
208+ if b then pure (Just x) else acc
209+
201210--
202211-- Text processing
203212--