From 035aa78920bd179a2a154cd712f53858e38127a0 Mon Sep 17 00:00:00 2001 From: rhiannon morris Date: Wed, 21 Aug 2024 07:40:28 +0200 Subject: [PATCH] require $& for whitespace before or after a builder --- make-pages/BuilderQQ.hs | 8 +++++++- make-pages/GalleryPage.hs | 8 ++++---- make-pages/IndexPage.hs | 4 ++-- make-pages/SinglePage.hs | 6 +++--- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/make-pages/BuilderQQ.hs b/make-pages/BuilderQQ.hs index c7cee73..95bba6b 100644 --- a/make-pages/BuilderQQ.hs +++ b/make-pages/BuilderQQ.hs @@ -18,9 +18,15 @@ import Data.List.NonEmpty (NonEmpty, toList) data Chunk = Lit String | Var String + +-- | +-- * use @$var@ to insert a variable (instance of 'CanBuild') +-- * use @$&@ to insert nothing like @\&@ in a string (e.g. to add whitespace +-- at the start or end, or to have a variable followed by a letter +-- * use @$$@ for a literal @$@ parseB :: String -> ExpQ parseB = toExpQ . reverse . go "" [] . trim where - trim = dropWhileEnd isSpace . dropWhile (== '\n') + trim = dropWhileEnd isSpace . dropWhile isSpace go acc cs [] = addLit acc cs go acc cs ('$':'&':rest) = go acc cs rest -- $&: expands to nothing go acc cs ('$':'$':rest) = go ('$' : acc) cs rest -- $$: expands to one $ diff --git a/make-pages/GalleryPage.hs b/make-pages/GalleryPage.hs index ae2a1bd..3e9c7fb 100644 --- a/make-pages/GalleryPage.hs +++ b/make-pages/GalleryPage.hs @@ -129,8 +129,8 @@ makeFilter prefix initial tag count = [b| where id' = [b|$prefix$&_$tag'|] tag' = escId tag - checked = if HashSet.member tag initial then [b| checked|] else "" - hidden = if count <= 1 then [b| hidden|] else "" + checked = if HashSet.member tag initial then [b|$& checked|] else "" + hidden = if count <= 1 then [b|$& hidden|] else "" makeYearItems :: Bool -- ^ nsfw -> Int -- ^ year @@ -159,12 +159,12 @@ makeItem nsfw file info@(Info {bg}) = [b| title = fromMaybe info.title $ info.galleryTitle dir = takeDirectory file thumbnail = getThumb dir info - nsfw' = if nsfw && anyNsfw info then [b| nsfw|] else "" + nsfw' = if nsfw && anyNsfw info then [b|$& nsfw|] else "" tags' = fold $ intersperse ";" $ map fromText $ tagsFor nsfw info date = latestDateFor nsfw info date' = formatTooltip date year' = date.year updated' = if hasUpdatesFor nsfw info then [b|true|] else [b|false|] - bgStyle = case bg of Other col -> [b| style="background: $col"|]; _ -> "" + bgStyle = case bg of Other col -> [b|$& style="background: $col"|]; _ -> "" tooltip = [b|$title ($upd$date')|] where upd = if hasUpdatesFor nsfw info then "updated " else "" :: Builder diff --git a/make-pages/IndexPage.hs b/make-pages/IndexPage.hs index 3efce99..1b1600d 100644 --- a/make-pages/IndexPage.hs +++ b/make-pages/IndexPage.hs @@ -74,13 +74,13 @@ makeItem :: GalleryInfo -> Builder makeItem (GalleryInfo {title, desc, prefix, filters}) = [b| $title |] - where nsfw = if hasNsfw filters then [b| class=nsfw|] else "" + where nsfw = if hasNsfw filters then [b|$& class=nsfw|] else "" makeLink :: Link -> Builder makeLink (Link {title, url, nsfw}) = [b| $title |] - where nsfw' = if nsfw then [b| class=nsfw|] else "" + where nsfw' = if nsfw then [b|$& class=nsfw|] else "" hasNsfw :: GalleryFilters -> Bool hasNsfw (GalleryFilters {nsfw}) = nsfw /= NoNsfw diff --git a/make-pages/SinglePage.hs b/make-pages/SinglePage.hs index 8124b50..28bfc10 100644 --- a/make-pages/SinglePage.hs +++ b/make-pages/SinglePage.hs @@ -302,11 +302,11 @@ altButton img i = [b| |] where Image {label, nsfw, warning, download} = img - nsfwClass = if nsfw then [b| class=nsfw|] else "" - nsfwLabelClass = if nsfw then [b| class=nsfw-label|] else "" + nsfwClass = if nsfw then [b|$& class=nsfw|] else "" + nsfwLabelClass = if nsfw then [b|$& class=nsfw-label|] else "" path' = pageFile img link = fromMaybe (bigFile img) download - warning' = ifJust warning \(escAttr -> w) -> [b| data-warning="$w"|] + warning' = ifJust warning \(escAttr -> w) -> [b|$& data-warning="$w"|] makeTags :: FilePath -> [Strict.Text] -> Builder makeTags undir tags =