T.P.App.FormatHeuristics: shorten code, improve docs.

This commit is contained in:
Albert Krewinkel 2021-03-13 22:03:36 +01:00
parent 35b66a7671
commit 35688c4262
No known key found for this signature in database
GPG key ID: 388DC0B21F631124

View file

@ -15,18 +15,24 @@ module Text.Pandoc.App.FormatHeuristics
) where
import Data.Char (toLower)
import Data.Foldable (asum)
import Data.Text (Text)
import System.FilePath (takeExtension)
-- Determine default format based on file extensions.
-- | Determines default format based on file extensions; uses the format
-- of the first extension that's associated with a format.
--
-- Examples:
--
-- > formatFromFilePaths ["text.unknown", "no-extension"]
-- Nothing
--
-- > formatFromFilePaths ["my.md", "other.rst"]
-- Just "markdown"
formatFromFilePaths :: [FilePath] -> Maybe Text
formatFromFilePaths [] = Nothing
formatFromFilePaths (x:xs) =
case formatFromFilePath x of
Just f -> Just f
Nothing -> formatFromFilePaths xs
formatFromFilePaths = asum . map formatFromFilePath
-- Determine format based on file extension
-- | Determines format based on file extension.
formatFromFilePath :: FilePath -> Maybe Text
formatFromFilePath x =
case takeExtension (map toLower x) of