hablo/src/Blog/Path.hs

45 lines
1.5 KiB
Haskell

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module Blog.Path (
Path(..)
, build
) where
import Arguments (Arguments)
import qualified Arguments as Arguments (Arguments(..))
import Data.Aeson (ToJSON(..), (.=), pairs)
import Data.Monoid ((<>))
import Files (File(..), filePath, filePathMaybe)
import GHC.Generics (Generic)
import System.Exit (die)
data Path = Path {
articlesPath :: Maybe FilePath
, pagesPath :: Maybe FilePath
, remarkableConfig :: Maybe FilePath
, root :: FilePath
} deriving Generic
instance ToJSON Path where
toEncoding (Path {articlesPath, pagesPath}) = pairs (
"articlesPath" .= articlesPath
<> "pagesPath" .= pagesPath
)
getMarkdownPath :: FilePath -> Maybe FilePath -> IO (Maybe FilePath)
getMarkdownPath defaultPath Nothing = filePathMaybe (Dir defaultPath)
getMarkdownPath _ (Just path) = Just <$> filePath (Dir path)
build :: FilePath -> Arguments -> IO Path
build root arguments = do
articlesPath <- getMarkdownPath "articles" $ Arguments.articlesPath arguments
pagesPath <- getMarkdownPath "pages" $ Arguments.pagesPath arguments
checkForContent articlesPath pagesPath
remarkableConfig <- mapM (filePath . File) $ Arguments.remarkableConfig arguments
return $ Path {articlesPath, pagesPath, remarkableConfig, root}
where
checkForContent Nothing Nothing =
die "No articles ? No pages ? Why did you wake me up ? I'm going back to sleep"
checkForContent _ _ = return ()