From 74d917df67f7b099ba477dfd6879a4a7c84b73dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20van=20Br=C3=BCgge?= Date: Fri, 22 Mar 2024 22:15:10 +0000 Subject: [PATCH] Add post lists for the tags --- blog.cabal | 1 + src/Main.hs | 19 +++++++++++-------- static/main.css | 5 ++--- templates/postHeader.html | 2 +- templates/postList.html | 3 +++ 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/blog.cabal b/blog.cabal index ba94553..428bc3d 100644 --- a/blog.cabal +++ b/blog.cabal @@ -21,6 +21,7 @@ executable blog , OverloadedStrings , LambdaCase build-depends: base ^>=4.18.0.0 + , containers , aeson , slick , shake diff --git a/src/Main.hs b/src/Main.hs index 806f311..68a4cba 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -12,7 +12,7 @@ import Data.Aeson.Types qualified as A import Data.Text qualified as T import Slick (markdownToHTML, compileTemplate', substitute) import Slick.Utils (convert) -import qualified Data.Aeson.KeyMap as KM +import Data.Aeson.KeyMap qualified as KM import Development.Shake.FilePath (dropDirectory1, (), dropExtension) import Data.Functor (void) import Data.Maybe (fromMaybe) @@ -20,6 +20,7 @@ import Control.Monad (foldM) import Data.Aeson.Key (fromText) import Data.List (sortOn) import Data.Ord (Down(..)) +import Data.Set qualified as Set outputFolder :: FilePath outputFolder = "build/" @@ -76,7 +77,9 @@ getRendered _ = error "Could not find content key in object" buildRules :: Action () buildRules = do allPosts <- mapP buildPost =<< getDirectoryFiles "." ["articles//*.md"] - buildPostList allPosts + buildPostList Nothing allPosts + let allTags = Set.toList $ foldr (Set.union . Set.fromList . tags) Set.empty allPosts + void . forP allTags $ \tag -> buildPostList (Just (tag, "tags" tag)) (filter (elem tag . tags) allPosts) copyStaticFiles buildPost :: FilePath -> Action Post @@ -93,15 +96,15 @@ buildPost srcPath = cacheAction ("build" :: T.Text, srcPath) $ do writeFile' (outputFolder T.unpack postUrl "index.html") . T.unpack $ rendered convert $ A.Object postData' -buildPostList :: [Post] -> Action () -buildPostList posts = do +buildPostList :: Maybe (String, FilePath) -> [Post] -> Action () +buildPostList tag posts = do let posts' = sortOn (Down . date) posts - let postData = A.Object $ KM.fromList + let postData = A.Object $ KM.fromList $ [ (fromText "posts", A.toJSON posts') - , (fromText "prefix", A.String ".") - ] + , (fromText "prefix", A.String (maybe "." (const "../..") tag)) + ] <> maybe [] (\(t, _) -> [(fromText "tag", A.String (T.pack t))]) tag rendered <- getRendered <$> renderTemplates postData ["postList.html", "shell.html"] - writeFile' (outputFolder "index.html") . T.unpack $ rendered + writeFile' (outputFolder <> maybe "" snd tag "index.html") . T.unpack $ rendered copyStaticFiles :: Action () copyStaticFiles = do diff --git a/static/main.css b/static/main.css index 61c180f..6985b89 100644 --- a/static/main.css +++ b/static/main.css @@ -76,7 +76,6 @@ nav > ul { } main { - margin-top: 1.5em; height: auto; padding-bottom: 1em; @@ -84,7 +83,7 @@ main { font-size: 18px; } -article + article { +article { margin-top: 2em; } @@ -126,7 +125,7 @@ article > header ul { color: var(--secondary-text); } -article > header li { +.tag { border: 0.05em solid white; border-radius: 0.2em; padding: 0 0.2em; diff --git a/templates/postHeader.html b/templates/postHeader.html index 28910db..a06288d 100644 --- a/templates/postHeader.html +++ b/templates/postHeader.html @@ -3,7 +3,7 @@

{{title}}

diff --git a/templates/postList.html b/templates/postList.html index 74cf1d7..68c4761 100644 --- a/templates/postList.html +++ b/templates/postList.html @@ -1,3 +1,6 @@ +{{#tag}} + All posts in {{.}}: +{{/tag}} {{#posts}}
{{> templates/postHeader.html}}