gallery/make-pages/Main.hs

78 lines
2.0 KiB
Haskell
Raw Normal View History

2020-07-15 14:06:19 -04:00
{-# LANGUAGE CPP, ImplicitParams #-}
2020-07-07 18:21:08 -04:00
module Main (main) where
import qualified Data.ByteString.Lazy as ByteString
2020-07-12 23:01:57 -04:00
import Data.ByteString.Lazy (ByteString)
import qualified Data.YAML as YAML
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy.IO as Text
2020-07-15 06:07:04 -04:00
import System.IO (hPrint, stderr)
import System.FilePath (makeRelative)
import Text.Printf (printf)
2020-07-12 23:01:57 -04:00
import Control.Monad
2020-07-15 14:06:19 -04:00
import Options
2020-07-09 00:20:57 -04:00
import SinglePage
import Depend
2020-07-09 00:20:57 -04:00
2020-07-15 05:43:57 -04:00
#ifdef PRETTY_VERBOSE
2020-07-15 06:07:04 -04:00
import qualified Text.Show.Pretty as PP
import qualified Text.PrettyPrint as PP
2020-07-15 05:43:57 -04:00
#endif
2020-07-09 00:20:57 -04:00
2020-07-15 14:06:19 -04:00
type HasVerbose = (?verbose :: Bool)
2020-07-07 18:21:08 -04:00
main :: IO ()
2020-07-12 23:01:57 -04:00
main = do
2020-07-15 14:06:19 -04:00
opts@(Options {verbose, mode}) <- parseOptions
let ?verbose = verbose
printV $ "options" :- opts
main2 mode
2020-07-09 00:20:57 -04:00
2020-07-15 14:06:19 -04:00
main2 :: HasVerbose => ModeOptions -> IO ()
main2 (SinglePage {file, nsfw, output}) = do
2020-07-12 23:01:57 -04:00
info <- readYAML file
2020-07-15 14:06:19 -04:00
printV $ "contents" :- info
2020-07-13 02:32:59 -04:00
let page = make nsfw info
writeOutput output page
2020-07-15 14:06:19 -04:00
main2 (GalleryPage {}) = do
2020-07-09 00:20:57 -04:00
error "surprise! this doesn't exist yet"
2020-07-15 14:06:19 -04:00
main2 (DependSingle {file, nsfw, output, prefix, buildDir, dataDir}) = do
info <- readYAML file
2020-07-15 14:06:19 -04:00
printV $ "contents" :- info
let path = makeRelative dataDir file
2020-07-15 14:06:19 -04:00
printV $ "path" :- path
2020-07-15 14:10:09 -04:00
let deps = dependSingle path info prefix buildDir nsfw
writeOutput output deps
2020-07-15 05:43:57 -04:00
2020-07-15 14:06:19 -04:00
printV :: (Show a, HasVerbose) => a -> IO ()
printV x = when ?verbose $ hPrint stderr x
2020-07-12 23:01:57 -04:00
readYAML :: YAML.FromYAML a => FilePath -> IO a
2020-07-12 23:01:57 -04:00
readYAML file = ByteString.readFile file >>= decode1Must file
decode1Must :: YAML.FromYAML a => FilePath -> ByteString -> IO a
decode1Must file txt =
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
2020-07-15 05:43:57 -04:00
writeOutput :: Maybe FilePath -> Text -> IO ()
writeOutput (Just f) = Text.writeFile f
writeOutput Nothing = Text.putStrLn
2020-07-15 06:07:04 -04:00
data Tag a = String :- a
instance Show a => Show (Tag a) where
show (tag :- a) =
#ifdef PRETTY_VERBOSE
PP.render $ PP.hang (PP.text tag <> ":") 2 (PP.ppDoc a)
#else
tag ++ ": " ++ show a
#endif