gallery/make-pages/GalleryPage.hs

86 lines
2.3 KiB
Haskell

{-# LANGUAGE TransformListComp #-}
module GalleryPage (make) where
import Control.Exception
import Data.Function (on, (&))
import Data.List (sortOn, groupBy)
import qualified Data.Text.Lazy as Lazy
import System.FilePath ((</>), takeDirectory)
import GHC.Exts (Down (..), the)
import BuilderQQ
import Depend (thumbFile)
import Info
newtype NoThumb = NoThumb FilePath
deriving stock Eq deriving anyclass Exception
instance Show NoThumb where show (NoThumb dir) = "no thumbnail for " ++ dir
make :: Text -- ^ title
-> Bool -- ^ nsfw is included?
-> [(FilePath, Info)]
-> Lazy.Text
make title nsfw infos = toLazyText $ make' title nsfw infos
make' :: Text -> Bool -> [(FilePath, Info)] -> Builder
make' title nsfw infos = [b|@0
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<link rel=stylesheet href=/style/shiny/gallery.css>
<link rel='alternate stylesheet' href=/style/tum/gallery.css>
<link rel=alternate href=rss.xml type=application/rss+xml>
<title>$*title</title>
<header>
<h1>$*title</h1>
<h2 class="right corner">
<a href=rss.xml>rss</a>
</h2>
</header>
<main>
<ul class=grid>
$4.items
</ul>
</main>
|]
where
items = map (uncurry $ makeYearItems nsfw) infosByYear
infosByYear =
[(the year, infopath) |
infopath@(_, info) <- infos,
then sortOn by Down info,
let year = #year info,
then group by Down year using groupBy']
groupBy' f = groupBy ((==) `on` f)
makeYearItems :: Bool -- ^ nsfw
-> Integer -- ^ year
-> [(FilePath, Info)]
-> Builder
makeYearItems nsfw year infos = [b|@4
<li class="item year-marker">
<span class=year-text>$year'</span>
$4.items
|]
where
items = map (uncurry $ makeItem nsfw) infos
year' = show year & foldMap \c -> [b|<span class=y>$'c</span>|]
makeItem :: Bool -> FilePath -> Info -> Builder
makeItem nsfw file info@(Info {title}) = [b|@4
<li class="item post$nsfw'">
<figure>
<a href="$@dir">
<img src="$@thumb">
</a>
<figcaption>$*title</figcaption>
</figure>
|]
where
dir = takeDirectory file
thumb = maybe (throw $ NoThumb dir) (\t -> dir </> thumbFile t) $ #thumb info
nsfw' = if nsfw && #anyNsfw info then " nsfw" else ""