    {-# LANGUAGE OverloadedStrings #-}
    {- |
       Module      : Text.Pandoc.App.FormatHeuristics
       Copyright   : Copyright (C) 2006-2020 John MacFarlane
       License     : GNU GPL, version 2 or above

       Maintainer  : John MacFarlane <jgm@berkeley@edu>
       Stability   : alpha
       Portability : portable

    Guess the format of a file from its name.
    -}
    module Text.Pandoc.App.FormatHeuristics
      ( formatFromFilePaths
      ) where

    import Data.Char (toLower)
    import Data.Text (Text)
    import System.FilePath (takeExtension)

    -- Determine default format based on file extensions.
    formatFromFilePaths :: [FilePath] -> Maybe Text
    formatFromFilePaths :: [FilePath] -> Maybe Text
    formatFromFilePaths [] = Maybe Text
    forall a. Maybe a
    Nothing
    formatFromFilePaths (x :: FilePath
    x:xs :: [FilePath]
    xs) =
      case FilePath -> Maybe Text
    formatFromFilePath FilePath
    x of
        Just f :: Text
    f     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    f
        Nothing    -> [FilePath] -> Maybe Text
    formatFromFilePaths [FilePath]
    xs

    -- Determine format based on file extension
    formatFromFilePath :: FilePath -> Maybe Text
    formatFromFilePath :: FilePath -> Maybe Text
    formatFromFilePath x :: FilePath
    x =
      case FilePath -> FilePath
    takeExtension ((Char -> Char) -> FilePath -> FilePath
    forall a b. (a -> b) -> [a] -> [b]
    map Char -> Char
    toLower FilePath
    x) of
        ".adoc"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "asciidoc"
        ".asciidoc" -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "asciidoc"
        ".context"  -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "context"
        ".ctx"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "context"
        ".db"       -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "docbook"
        ".doc"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "doc"  -- so we get an "unknown reader" error
        ".docx"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "docx"
        ".dokuwiki" -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "dokuwiki"
        ".epub"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "epub"
        ".fb2"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "fb2"
        ".htm"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "html"
        ".html"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "html"
        ".icml"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "icml"
        ".json"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "json"
        ".latex"    -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "latex"
        ".lhs"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "markdown+lhs"
        ".ltx"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "latex"
        ".markdown" -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "markdown"
        ".md"       -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "markdown"
        ".ms"       -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "ms"
        ".muse"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "muse"
        ".native"   -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "native"
        ".odt"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "odt"
        ".opml"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "opml"
        ".org"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "org"
        ".pdf"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "pdf"  -- so we get an "unknown reader" error
        ".pptx"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "pptx"
        ".roff"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "ms"
        ".rst"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "rst"
        ".rtf"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "rtf"
        ".s5"       -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "s5"
        ".t2t"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "t2t"
        ".tei"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "tei"
        ".tei.xml"  -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "tei"
        ".tex"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "latex"
        ".texi"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "texinfo"
        ".texinfo"  -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "texinfo"
        ".text"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "markdown"
        ".textile"  -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "textile"
        ".txt"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "markdown"
        ".wiki"     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "mediawiki"
        ".xhtml"    -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "html"
        ".ipynb"    -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "ipynb"
        ".csv"      -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "csv"
        ['.',y :: Char
    y]     | Char
    y Char -> FilePath -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` ['1'..'9'] -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just "man"
        _           -> Maybe Text
    forall a. Maybe a
    Nothing