start real main function

This commit is contained in:
Rhiannon Morris 2020-07-09 06:20:57 +02:00
parent 94cda046fe
commit 103dbfca3d
2 changed files with 53 additions and 6 deletions

View file

@ -1,14 +1,60 @@
module Main (main) where module Main (main) where
import System.Environment (getArgs)
import qualified Data.ByteString.Lazy as ByteString import qualified Data.ByteString.Lazy as ByteString
import qualified Data.YAML as YAML import qualified Data.YAML as YAML
import qualified Data.Text.Lazy.IO as Text import qualified Data.Text.Lazy.IO as Text
import ImagePage import qualified Options.Applicative as Opt
import Control.Applicative
import SinglePage
data Options =
SinglePage {
file :: FilePath,
includeNsfw :: Bool,
output :: Maybe FilePath
}
| GalleryPage {
directory :: FilePath,
includeNsfw :: Bool,
output :: Maybe FilePath
}
optionsParser :: Opt.ParserInfo Options
optionsParser =
(Opt.hsubparser (single <> gallery) <**> Opt.helper) `Opt.info` mainInfo
where
single = Opt.command "single" $ singleOpts `Opt.info` singleInfo
singleOpts = SinglePage <$> fileArg <*> nsfwSwitch <*> outputOpt
fileArg = Opt.strArgument $
Opt.metavar "FILE" <> Opt.help "yaml file to read"
nsfwSwitch = Opt.switch $
Opt.short 'n' <> Opt.long "nsfw" <>
Opt.help "include nsfw versions"
outputOpt = Opt.option (Just <$> Opt.str) $
Opt.short 'o' <> Opt.long "output" <>
Opt.value Nothing <>
Opt.help "output file (default: stdout)"
singleInfo = Opt.progDesc "generate a page for a single work"
gallery = Opt.command "gallery" $ galleryOpts `Opt.info` galleryInfo
galleryOpts = GalleryPage <$> dirArg <*> nsfwSwitch <*> outputOpt
dirArg = Opt.strArgument $
Opt.metavar "DIR" <> Opt.help "directory to search for yaml files"
galleryInfo = Opt.progDesc "generate a gallery page"
mainInfo = Opt.progDesc "static gallery site generator" <> Opt.fullDesc
main :: IO () main :: IO ()
main = do main = main2 =<< Opt.execParser optionsParser
[file] <- getArgs
main2 :: Options -> IO ()
main2 (SinglePage {file, includeNsfw, output}) = do
txt <- ByteString.readFile file txt <- ByteString.readFile file
let Right info = YAML.decode1 txt let Right info = YAML.decode1 txt
Text.putStr $ make info let page = make info
case output of
Nothing -> Text.putStr page
Just out -> Text.writeFile out page
main2 (GalleryPage {}) = do
error "surprise! this doesn't exist yet"

View file

@ -24,6 +24,7 @@ executable make-pages
bytestring ^>= 0.10.8.2, bytestring ^>= 0.10.8.2,
text ^>= 1.2.3.1, text ^>= 1.2.3.1,
vector ^>= 0.12.1.2, vector ^>= 0.12.1.2,
HsYAML ^>= 0.2.1.0 HsYAML ^>= 0.2.1.0,
optparse-applicative ^>= 0.15.1.0
ghc-options: ghc-options:
-Wall -threaded -rtsopts -with-rtsopts=-N -Wall -threaded -rtsopts -with-rtsopts=-N