group gallery items by year

This commit is contained in:
Rhiannon Morris 2020-07-18 11:43:35 +02:00
parent e9c19b2955
commit 6b995248a3
1 changed files with 21 additions and 6 deletions

View File

@ -1,9 +1,8 @@
module GalleryPage (make) where
import Control.Exception
import Data.Function (on)
import Data.List (sortBy)
import Data.Ord (comparing)
import Data.Function (on, (&))
import Data.List (sortBy, groupBy)
import qualified Data.Text.Lazy as Lazy
import Data.Text.Lazy.Builder (Builder, toLazyText)
import System.FilePath ((</>), takeDirectory)
@ -42,9 +41,25 @@ make' title nsfw infos = [b|@0
</main>
|]
where
items = map (uncurry makeItem) infos
infos = sortBy (cmpInfo `on` snd) infos'
cmpInfo = flip (comparing #date) <> comparing #title
items = map (uncurry $ makeYearItems nsfw) infosByYear
infosByYear =
infos & sortBy (cmpInfo `on` #second)
& map (\fi -> (fi, #year $ #second fi))
& groupBy ((==) `on` #second)
& map (\ys -> (#second (head ys), map #first ys))
cmpInfo (Info {date = d1, title = t1}) (Info {date = d2, title = t2}) =
compare d2 d1 <> compare t1 t2
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
makeItem :: Bool -> FilePath -> Info -> Builder
makeItem nsfw file info = [b|@4