Add two arguments to pass HTML files for the head and the banner of pages

This commit is contained in:
Tissevert 2019-02-02 17:26:07 +01:00
parent a9f220f9d8
commit f81776d3de
2 changed files with 49 additions and 18 deletions

View File

@ -1,12 +1,12 @@
{-# LANGUAGE NamedFieldPuns #-}
module Arguments (
Arguments(..)
, configuration
, get
) where
import Data.Monoid ((<>))
import Options.Applicative
import System.Directory (doesDirectoryExist, makeAbsolute)
import System.Directory (doesDirectoryExist, doesFileExist, makeAbsolute)
import System.Exit (die)
import System.FilePath.Posix (dropTrailingPathSeparator, isValid)
@ -14,11 +14,13 @@ data Arguments = Arguments {
sourceDir :: FilePath
, blogName :: Maybe String
, previewCountArg :: Int
, bannerPath :: Maybe FilePath
, headPath :: Maybe FilePath
}
arguments :: Parser Arguments
arguments = Arguments
<$> argument directory (metavar "INPUT_DIR")
parseArguments :: Parser Arguments
parseArguments = Arguments
<$> argument filePath (metavar "INPUT_DIR")
<*> option (optional str) (
metavar "BLOG_NAME"
<> value Nothing
@ -33,19 +35,48 @@ arguments = Arguments
<> long "preview-count"
<> help "number of articles listed on the page of each category"
)
<*> option (optional filePath) (
metavar "BANNER_PATH"
<> value Nothing
<> short 'b'
<> long "banner"
<> help "path to the file to use for the blog's banner"
)
<*> option (optional filePath) (
metavar "HEAD_PATH"
<> value Nothing
<> short 'H'
<> long "head"
<> help "path to the file to add in the blog's head"
)
directory :: ReadM FilePath
directory = eitherReader $ \path ->
filePath :: ReadM FilePath
filePath = eitherReader $ \path ->
if isValid path
then Right $ dropTrailingPathSeparator path
else Left "This string doesn't represent a valid path"
configuration :: IO Arguments
configuration = do
inputArguments <- execParser $ info (arguments <**> helper) fullDesc
directoryExists <- doesDirectoryExist $ sourceDir inputArguments
if directoryExists
then do
absolutePath <- makeAbsolute $ sourceDir inputArguments
return $ inputArguments { sourceDir = absolutePath }
else die "INPUT_DIR doesn't exist"
ifNotDie :: (a -> IO Bool) -> (a -> String) -> a -> IO ()
ifNotDie check messageBuilder input = do
bool <- check input
if bool
then return ()
else die $ messageBuilder input
get :: IO Arguments
get = do
arguments <- execParser $ info (parseArguments <**> helper) fullDesc
doesDirectoryExist `ifNotDie` noSuchDirectory $ sourceDir arguments
absoluteSourceDir <- makeAbsolute $ sourceDir arguments
mapM_ (doesFileExist `ifNotDie` noSuchFile) $ bannerPath arguments
absoluteBannerPath <- mapM makeAbsolute $ bannerPath arguments
mapM_ (doesFileExist `ifNotDie` noSuchFile) $ headPath arguments
absoluteHeadPath <- mapM makeAbsolute $ headPath arguments
return $ arguments {
sourceDir = absoluteSourceDir
, bannerPath = absoluteBannerPath
, headPath = absoluteHeadPath
}
where
noSuchDirectory = (++ ": no such directory")
noSuchFile = (++ ": no such file")

View File

@ -1,7 +1,7 @@
{- LANGUAGE NamedFieldPuns #-}
module Main where
import Arguments (configuration)
import qualified Arguments (get)
import qualified Blog (get)
import qualified Dom (generate)
import qualified JSON (generate)
@ -9,7 +9,7 @@ import Control.Monad.Reader (runReaderT)
main :: IO ()
main = do
configuration
Arguments.get
>>= Blog.get
>>= runReaderT (do
Dom.generate