{-# LANGUAGE PatternSynonyms #-}
module SinglePage (make) where
import Date
import Info
import BuilderQQ
import NsfwWarning qualified
import TagTransforms
import Control.Exception
import Control.Monad
import Data.Char (isSpace)
import Data.Foldable
import Data.HashSet qualified as HashSet
import Data.List (sort, intersperse)
import Data.Maybe (fromMaybe, isJust)
import Data.Semigroup
import Data.Text qualified as Strict
import Data.Text.Lazy qualified as Lazy
import Data.Traversable
import System.FilePath (joinPath, splitPath)
-- | e.g. only nsfw images are present for a non-nsfw page
newtype NoEligibleImages = NoEligibleImages {title :: Strict.Text}
deriving stock Eq deriving anyclass Exception
instance Show NoEligibleImages where
show (NoEligibleImages {title}) =
Strict.unpack title <> ": no images selected\n" <>
" (probably a nsfw-only work without --nsfw set)"
make :: Text -- ^ website root
-> IndexInfo
-> FilePath -- ^ gallery prefix
-> Bool -- ^ nsfw?
-> FilePath -- ^ data dir
-> FilePath -- ^ subdir of datadir containing this @info.yaml@
-> Info -> IO Lazy.Text
make root iinfo prefix nsfw dataDir dir info =
fmap toLazyText $
make' root iinfo prefix nsfw dataDir dir $
transformInfoTags iinfo.tags info
make' :: Text -> IndexInfo -> FilePath -> Bool -> FilePath -> FilePath -> Info
-> IO Builder
make' root iinfo prefix nsfw _dataDir dir
info@(Info {date, title, artist, bg}) = do
images <- maybe (throw $ NoEligibleImages title) pure $ imagesFor nsfw info
let undir = joinPath (replicate (length (splitPath dir)) "..")
let siteName = iinfo.title
let formattedDate = formatLong date
let buttonBar = makeButtonBar title $ addIds images
let image0 :| otherImages = allImages images
let download0 = fromMaybe (bigFile image0) image0.download
let path0' = pageFile image0
let artistSection = makeArtist artist
let descSection = makeDesc $ descFor nsfw info
let tags = tagsFor nsfw info
let tagsList = makeTags undir tags
let linksList = extLinks $ linksFor nsfw info
let updates = sort $ updatesFor nsfw info
let updatesList = makeUpdates updates
let makePrefetch img = [b||]
where path' = bigFile img
let prefetches = map makePrefetch otherImages
let makeWarning w = [b|
updated $updated|]
let nsfw' = NsfwWarning.Single <$ guard nsfw
let nsfwScript = NsfwWarning.script nsfw'
let nsfwDialog = NsfwWarning.dialog nsfw'
let imageMeta = case previewImage info of
Just (PFull (pageFile -> path)) -> [b|
|]
Just (PThumb (thumbFile -> path)) -> [b|
|]
Nothing -> throw $ NoThumb dir
let escTitle = escAttr title
pure [b|
$imageMeta
$nsfwScript
$bgStyle
$prefetches