--------------------------------------------------------------------------------
-- | A module dealing with pandoc file extensions and associated file types
module Hakyll.Web.Pandoc.FileType
    ( FileType (..)
    , fileType
    , itemFileType
    ) where


--------------------------------------------------------------------------------
import           System.FilePath        (splitExtension)


--------------------------------------------------------------------------------
import           Hakyll.Core.Identifier
import           Hakyll.Core.Item


--------------------------------------------------------------------------------
-- | Datatype to represent the different file types Hakyll can deal with by
-- default
data FileType
    = Binary
    | Css
    | DocBook
    | Html
    | Jupyter
    | LaTeX
    | LiterateHaskell FileType
    | Markdown
    | MediaWiki
    | OrgMode
    | PlainText
    | Rst
    | Textile
    deriving (FileType -> FileType -> Bool
(FileType -> FileType -> Bool)
-> (FileType -> FileType -> Bool) -> Eq FileType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FileType -> FileType -> Bool
$c/= :: FileType -> FileType -> Bool
== :: FileType -> FileType -> Bool
$c== :: FileType -> FileType -> Bool
Eq, Eq FileType
Eq FileType
-> (FileType -> FileType -> Ordering)
-> (FileType -> FileType -> Bool)
-> (FileType -> FileType -> Bool)
-> (FileType -> FileType -> Bool)
-> (FileType -> FileType -> Bool)
-> (FileType -> FileType -> FileType)
-> (FileType -> FileType -> FileType)
-> Ord FileType
FileType -> FileType -> Bool
FileType -> FileType -> Ordering
FileType -> FileType -> FileType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FileType -> FileType -> FileType
$cmin :: FileType -> FileType -> FileType
max :: FileType -> FileType -> FileType
$cmax :: FileType -> FileType -> FileType
>= :: FileType -> FileType -> Bool
$c>= :: FileType -> FileType -> Bool
> :: FileType -> FileType -> Bool
$c> :: FileType -> FileType -> Bool
<= :: FileType -> FileType -> Bool
$c<= :: FileType -> FileType -> Bool
< :: FileType -> FileType -> Bool
$c< :: FileType -> FileType -> Bool
compare :: FileType -> FileType -> Ordering
$ccompare :: FileType -> FileType -> Ordering
$cp1Ord :: Eq FileType
Ord, Int -> FileType -> ShowS
[FileType] -> ShowS
FileType -> String
(Int -> FileType -> ShowS)
-> (FileType -> String) -> ([FileType] -> ShowS) -> Show FileType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FileType] -> ShowS
$cshowList :: [FileType] -> ShowS
show :: FileType -> String
$cshow :: FileType -> String
showsPrec :: Int -> FileType -> ShowS
$cshowsPrec :: Int -> FileType -> ShowS
Show, ReadPrec [FileType]
ReadPrec FileType
Int -> ReadS FileType
ReadS [FileType]
(Int -> ReadS FileType)
-> ReadS [FileType]
-> ReadPrec FileType
-> ReadPrec [FileType]
-> Read FileType
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [FileType]
$creadListPrec :: ReadPrec [FileType]
readPrec :: ReadPrec FileType
$creadPrec :: ReadPrec FileType
readList :: ReadS [FileType]
$creadList :: ReadS [FileType]
readsPrec :: Int -> ReadS FileType
$creadsPrec :: Int -> ReadS FileType
Read)


--------------------------------------------------------------------------------
-- | Get the file type for a certain file. The type is determined by extension.
fileType :: FilePath -> FileType
fileType :: String -> FileType
fileType = (String -> String -> FileType) -> (String, String) -> FileType
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry String -> String -> FileType
fileType' ((String, String) -> FileType)
-> (String -> (String, String)) -> String -> FileType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> (String, String)
splitExtension
  where
    fileType' :: String -> String -> FileType
fileType' String
_ String
".css"       = FileType
Css
    fileType' String
_ String
".dbk"       = FileType
DocBook
    fileType' String
_ String
".ipynb"     = FileType
Jupyter
    fileType' String
_ String
".htm"       = FileType
Html
    fileType' String
_ String
".html"      = FileType
Html
    fileType' String
f String
".lhs"       = FileType -> FileType
LiterateHaskell (FileType -> FileType) -> FileType -> FileType
forall a b. (a -> b) -> a -> b
$ case String -> FileType
fileType String
f of
        -- If no extension is given, default to Markdown + LiterateHaskell
        FileType
Binary -> FileType
Markdown
        -- Otherwise, LaTeX + LiterateHaskell or whatever the user specified
        FileType
x      -> FileType
x
    fileType' String
_ String
".markdown"  = FileType
Markdown
    fileType' String
_ String
".mediawiki" = FileType
MediaWiki
    fileType' String
_ String
".md"        = FileType
Markdown
    fileType' String
_ String
".mdn"       = FileType
Markdown
    fileType' String
_ String
".mdown"     = FileType
Markdown
    fileType' String
_ String
".mdwn"      = FileType
Markdown
    fileType' String
_ String
".mkd"       = FileType
Markdown
    fileType' String
_ String
".mkdwn"     = FileType
Markdown
    fileType' String
_ String
".org"       = FileType
OrgMode
    fileType' String
_ String
".page"      = FileType
Markdown
    fileType' String
_ String
".rst"       = FileType
Rst
    fileType' String
_ String
".tex"       = FileType
LaTeX
    fileType' String
_ String
".text"      = FileType
PlainText
    fileType' String
_ String
".textile"   = FileType
Textile
    fileType' String
_ String
".txt"       = FileType
PlainText
    fileType' String
_ String
".wiki"      = FileType
MediaWiki
    fileType' String
_ String
_            = FileType
Binary  -- Treat unknown files as binary


--------------------------------------------------------------------------------
-- | Get the file type for the current file
itemFileType :: Item a -> FileType
itemFileType :: Item a -> FileType
itemFileType = String -> FileType
fileType (String -> FileType) -> (Item a -> String) -> Item a -> FileType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identifier -> String
toFilePath (Identifier -> String)
-> (Item a -> Identifier) -> Item a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item a -> Identifier
forall a. Item a -> Identifier
itemIdentifier