gallery/make-pages/GalleryPage.hs

86 lines
2.1 KiB
Haskell
Raw Normal View History

2020-07-19 11:55:54 -04:00
{-# LANGUAGE TransformListComp #-}
2020-07-16 10:07:28 -04:00
module GalleryPage (make) where
import Control.Exception
2020-07-19 11:55:54 -04:00
import Data.Function (on)
import Data.List (sortOn, groupBy)
2020-07-16 10:07:28 -04:00
import qualified Data.Text.Lazy as Lazy
import System.FilePath ((</>), takeDirectory)
2020-07-19 11:55:54 -04:00
import GHC.Exts (Down (..), the)
2020-07-16 10:07:28 -04:00
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
2020-07-16 10:07:28 -04:00
make' :: Text -> Bool -> [(FilePath, Info)] -> Builder
make' title nsfw infos = [b|@0
2020-07-16 10:07:28 -04:00
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
2020-07-17 06:29:13 -04:00
<link rel=stylesheet href=/style/gallery.css>
2020-07-16 10:07:28 -04:00
<title>$*title</title>
<header>
<h1>$*title</h1>
</header>
<main>
<ul class=grid>
$4.items
</ul>
</main>
|]
where
2020-07-18 05:43:35 -04:00
items = map (uncurry $ makeYearItems nsfw) infosByYear
infosByYear =
2020-07-19 11:55:54 -04:00
[(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)
2020-07-18 05:43:35 -04:00
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
2020-07-16 10:07:28 -04:00
makeItem :: Bool -> FilePath -> Info -> Builder
makeItem nsfw file info = [b|@4
<li $cls>
2020-07-16 10:07:28 -04:00
<figure>
<a href="$@dir">
<img src="$@thumb">
</a>
$title
</figure>
|]
where
dir = takeDirectory file
thumb = maybe (throw $ NoThumb dir) (\t -> dir </> thumbFile t) $ #thumb info
title = maybe mempty mkTitle $ #title info
mkTitle t = [b|@8
<figcaption>
$*t
</figcaption>
|]
cls | nsfw && #anyNsfw info = [b|class="item nsfw"|]
| otherwise = [b|class=item|]