gallery/make-pages/Main.hs

61 lines
1.9 KiB
Haskell
Raw Normal View History

2020-07-07 18:21:08 -04:00
module Main (main) where
import qualified Data.ByteString.Lazy as ByteString
import qualified Data.YAML as YAML
import qualified Data.Text.Lazy.IO as Text
2020-07-09 00:20:57 -04:00
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
2020-07-07 18:21:08 -04:00
main :: IO ()
2020-07-09 00:20:57 -04:00
main = main2 =<< Opt.execParser optionsParser
main2 :: Options -> IO ()
main2 (SinglePage {file, includeNsfw, output}) = do
txt <- ByteString.readFile file
let Right info = YAML.decode1 txt
2020-07-09 00:20:57 -04:00
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"