From 004e43f52f0a802c49894c48b2caf7121357e6b3 Mon Sep 17 00:00:00 2001 From: Rhiannon Morris Date: Mon, 23 Aug 2021 16:35:55 +0200 Subject: [PATCH] ensure ids are unique --- make-pages/BuilderQQ.hs | 2 +- make-pages/SinglePage.hs | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/make-pages/BuilderQQ.hs b/make-pages/BuilderQQ.hs index 097094c..bde71c7 100644 --- a/make-pages/BuilderQQ.hs +++ b/make-pages/BuilderQQ.hs @@ -1,7 +1,7 @@ {-# LANGUAGE TemplateHaskell #-} module BuilderQQ (b, - Builder, toLazyText, fromText, fromString, fromChar, + Builder, toStrictText, toLazyText, fromText, fromString, fromChar, textMap, ifJust, escId, escAttr) where diff --git a/make-pages/SinglePage.hs b/make-pages/SinglePage.hs index dd07189..5aee691 100644 --- a/make-pages/SinglePage.hs +++ b/make-pages/SinglePage.hs @@ -14,6 +14,8 @@ import qualified Data.Text.Lazy as Lazy import System.FilePath (joinPath, splitPath, ()) import qualified System.Process as Proc import Text.Read (readMaybe) +import qualified Data.HashSet as Set +import Data.Traversable -- | e.g. only nsfw images are present for a non-nsfw page @@ -45,7 +47,7 @@ make' root prefix nsfw dataDir dir info@(Info {date, title, artist, bg}) = do let formattedDate = formatLong date - let buttonBar = makeButtonBar title images + let buttonBar = makeButtonBar title $ addIds images let (image0@(Image {path = path0, download = download0'}), Size {width = width0, height = height0}) : otherImages = #all images @@ -195,7 +197,14 @@ makeDesc (LongDesc fs) = [b|@0 |] -makeButtonBar :: Strict.Text -> Images' (Image, Size) -> Builder +addIds :: Images' (Image, a) -> Images' (Image, a, Text) +addIds = snd . mapAccumL makeId Set.empty where + makeId used (img, x) = (Set.insert newId used, (img, x, newId)) where + newId = head $ filter (\i -> not $ i `Set.member` used) ids + ids = [toStrictText [b|$label$i|] | i <- "" : map show [0::Int ..]] + label = escId $ #label img + +makeButtonBar :: Strict.Text -> Images' (Image, Size, Text) -> Builder makeButtonBar title images = case images of Uncat [] -> throw $ NoEligibleImages title @@ -218,22 +227,21 @@ makeButtonBar title images = |] - where elems = map (uncurry altButton) imgs + where elems = map (\(img,sz,i) -> altButton img sz i) imgs -altButton :: Image -> Size -> Builder -altButton img size = [b|@0 +altButton :: Image -> Size -> Text -> Builder +altButton img size i = [b|@0 - - + |] where Image {label, path, nsfw, warning, download} = img Size {width, height} = size nsfwClass = if nsfw then [b| class=nsfw|] else "" nsfwLabelClass = if nsfw then [b| class=nsfw-label|] else "" - idLabel = escId label path' = pageFile path link = fromMaybe path download warning' = ifJust warning \(escAttr -> w) -> [b| data-warning="$w"|]