-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
203 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,60 @@ | ||
-- app/Main.hs | ||
|
||
-- | Entry point for the hs-blog-gen program | ||
|
||
module Main where | ||
|
||
import qualified HsBlog | ||
import OptParse | ||
|
||
import System.Exit (exitFailure) | ||
import System.Directory (doesFileExist) | ||
import System.IO | ||
|
||
main :: IO () | ||
main = HsBlog.main | ||
main = do | ||
options <- parse | ||
case options of | ||
ConvertDir input output -> | ||
HsBlog.convertDirectory input output | ||
|
||
ConvertSingle input output -> do | ||
(title, inputHandle) <- | ||
case input of | ||
Stdin -> | ||
pure ("", stdin) | ||
InputFile file -> | ||
(,) file <$> openFile file ReadMode | ||
|
||
outputHandle <- | ||
case output of | ||
Stdout -> pure stdout | ||
OutputFile file -> do | ||
exists <- doesFileExist file | ||
shouldOpenFile <- | ||
if exists | ||
then confirm | ||
else pure True | ||
if shouldOpenFile | ||
then | ||
openFile file WriteMode | ||
else | ||
exitFailure | ||
|
||
HsBlog.convertSingle title inputHandle outputHandle | ||
hClose inputHandle | ||
hClose outputHandle | ||
|
||
------------------------------------------------ | ||
-- * Utilities | ||
|
||
-- | Confirm user action | ||
confirm :: IO Bool | ||
confirm = | ||
putStrLn "Are you sure? (y/n)" *> | ||
getLine >>= \answer -> | ||
case answer of | ||
"y" -> pure True | ||
"n" -> pure False | ||
_ -> putStrLn "Invalid response. use y or n" *> | ||
confirm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
-- app/OptParse.hs | ||
|
||
-- | Command-line options parsing | ||
|
||
module OptParse | ||
( Options(..) | ||
, SingleInput(..) | ||
, SingleOutput(..) | ||
, parse | ||
) | ||
where | ||
|
||
import Data.Maybe (fromMaybe) | ||
import Options.Applicative | ||
|
||
------------------------------------------------ | ||
-- * Our command-line options model | ||
|
||
-- | Model | ||
data Options | ||
= ConvertSingle SingleInput SingleOutput | ||
| ConvertDir FilePath FilePath | ||
deriving Show | ||
|
||
-- | A single input source | ||
data SingleInput | ||
= Stdin | ||
| InputFile FilePath | ||
deriving Show | ||
|
||
-- | A single output sink | ||
data SingleOutput | ||
= Stdout | ||
| OutputFile FilePath | ||
deriving Show | ||
|
||
------------------------------------------------ | ||
-- * Parser | ||
|
||
-- | Parse command-line options | ||
parse :: IO Options | ||
parse = execParser opts | ||
|
||
opts :: ParserInfo Options | ||
opts = | ||
info (pOptions <**> helper) | ||
( fullDesc | ||
<> header "hs-blog-gen - a static blog generator" | ||
<> progDesc "Convert markup files or directories to html" | ||
) | ||
|
||
-- | Parser for all options | ||
pOptions :: Parser Options | ||
pOptions = | ||
subparser | ||
( command | ||
"convert" | ||
( info | ||
(helper <*> pConvertSingle) | ||
(progDesc "Convert a single markup source to html") | ||
) | ||
<> command | ||
"convert-dir" | ||
( info | ||
(helper <*> pConvertDir) | ||
(progDesc "Convert a directory of markup files to html") | ||
) | ||
) | ||
|
||
------------------------------------------------ | ||
-- * Single source to sink conversion parser | ||
|
||
-- | Parser for single source to sink option | ||
pConvertSingle :: Parser Options | ||
pConvertSingle = | ||
ConvertSingle <$> pSingleInput <*> pSingleOutput | ||
|
||
-- | Parser for single input source | ||
pSingleInput :: Parser SingleInput | ||
pSingleInput = | ||
fromMaybe Stdin <$> optional pInputFile | ||
|
||
-- | Parser for single output sink | ||
pSingleOutput :: Parser SingleOutput | ||
pSingleOutput = | ||
fromMaybe Stdout <$> optional pOutputFile | ||
|
||
-- | Input file parser | ||
pInputFile :: Parser SingleInput | ||
pInputFile = fmap InputFile parser | ||
where | ||
parser = | ||
strOption | ||
( long "input" | ||
<> short 'i' | ||
<> metavar "FILE" | ||
<> help "Input file" | ||
) | ||
|
||
-- | Output file parser | ||
pOutputFile :: Parser SingleOutput | ||
pOutputFile = OutputFile <$> parser | ||
where | ||
parser = | ||
strOption | ||
( long "output" | ||
<> short 'o' | ||
<> metavar "FILE" | ||
<> help "Output file" | ||
) | ||
|
||
------------------------------------------------ | ||
-- * Directory conversion parser | ||
|
||
pConvertDir :: Parser Options | ||
pConvertDir = | ||
ConvertDir <$> pInputDir <*> pOutputDir | ||
|
||
-- | Parser for input directory | ||
pInputDir :: Parser FilePath | ||
pInputDir = | ||
strOption | ||
( long "input" | ||
<> short 'i' | ||
<> metavar "DIRECTORY" | ||
<> help "Input directory" | ||
) | ||
|
||
-- | Parser for output directory | ||
pOutputDir :: Parser FilePath | ||
pOutputDir = | ||
strOption | ||
( long "output" | ||
<> short 'o' | ||
<> metavar "DIRECTORY" | ||
<> help "Output directory" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters