gallery/make-pages/Main.hs

80 lines
2.5 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 Text.Printf (printf)
2020-07-09 00:20:57 -04:00
import Control.Applicative
import SinglePage
data Options =
SinglePage {
file :: FilePath,
includeNsfw :: Bool,
output :: Maybe FilePath,
copyImages :: Bool
2020-07-09 00:20:57 -04:00
}
| GalleryPage {
2020-07-12 23:00:46 -04:00
files :: [FilePath],
2020-07-09 00:20:57 -04:00
includeNsfw :: Bool,
2020-07-12 23:00:46 -04:00
output :: Maybe FilePath
2020-07-09 00:20:57 -04:00
}
deriving Show
2020-07-09 00:20:57 -04:00
optionsParser :: Opt.ParserInfo Options
optionsParser =
(Opt.hsubparser (single <> gallery) <**> Opt.helper) `Opt.info` mainInfo
where
single = Opt.command "single" $ singleOpts `Opt.info` singleInfo
2020-07-12 23:00:46 -04:00
singleOpts = SinglePage <$> fileArg <*> nsfwSwitchS <*> outputOpt <*> copyOpt
2020-07-09 00:20:57 -04:00
fileArg = Opt.strArgument $
Opt.metavar "FILE" <> Opt.help "yaml file to read"
2020-07-12 23:00:46 -04:00
nsfwSwitchS = Opt.switch $
2020-07-09 00:20:57 -04:00
Opt.short 'n' <> Opt.long "nsfw" <>
Opt.help "include nsfw versions"
outputOpt = Opt.option (Just <$> Opt.str) $
2020-07-12 23:00:46 -04:00
Opt.short 'o' <> Opt.long "output" <> Opt.metavar "FILE" <>
2020-07-09 00:20:57 -04:00
Opt.value Nothing <>
Opt.help "output file (default: stdout)"
copyOpt = Opt.switch $
Opt.short 'c' <> Opt.long "copy" <>
Opt.help "copy mentioned image files to output directory"
2020-07-09 00:20:57 -04:00
singleInfo = Opt.progDesc "generate a page for a single work"
gallery = Opt.command "gallery" $ galleryOpts `Opt.info` galleryInfo
2020-07-12 23:00:46 -04:00
galleryOpts = GalleryPage <$> filesArg <*> nsfwSwitchG <*> outputOpt
filesArg = many $ Opt.strArgument $
Opt.metavar "FILE..." <> Opt.help "yaml files to read"
nsfwSwitchG = Opt.switch $
Opt.short 'n' <> Opt.long "nsfw" <>
Opt.help "include works with only nsfw versions"
2020-07-09 00:20:57 -04:00
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 s@(SinglePage {file, includeNsfw, output}) = do
print s
page <- make includeNsfw <$> readYAML file
2020-07-09 00:20:57 -04:00
case output of
Nothing -> Text.putStr page
Just out -> Text.writeFile out page
main2 g@(GalleryPage {}) = do
print g
2020-07-09 00:20:57 -04:00
error "surprise! this doesn't exist yet"
readYAML :: YAML.FromYAML a => FilePath -> IO a
readYAML file = do
txt <- ByteString.readFile file
case YAML.decode1 txt of
Right val -> pure val
Left (YAML.Pos {posLine, posColumn}, err) ->
fail $ printf "%s:%i:%i: %s" file posLine posColumn err