From 3cb14be99950801e9885b5a59dc5c29c63d9bc84 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Tue, 5 Nov 2024 14:08:28 -0500 Subject: [PATCH] iso dates --- app/Main.hs | 7 ++++--- app/Templates.hs | 4 +++- app/Utilities.hs | 17 +++++------------ 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index c6d403a..67804ba 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -138,8 +138,8 @@ home = take 3 . sortOn (Ord.Down . postDate) <$> forM postPaths readPost - html <- applyTemplate "home.html" $ HM.singleton "posts" posts - + let posts' = map fromPost posts + html <- applyTemplate "home.html" $ HM.singleton "posts" posts' let page = Page (T.pack "Home") html applyTemplateAndWrite "default.html" page target Shake.putInfo $ "Built " <> target @@ -149,7 +149,8 @@ postList = outputDir "posts/index.html" %> \target -> do postPaths <- getPublishedPosts posts <- sortOn (Ord.Down . postDate) <$> forM postPaths readPost - html <- applyTemplate "posts.html" $ HM.singleton "posts" posts + let posts' = map fromPost posts + html <- applyTemplate "posts.html" $ HM.singleton "posts" posts' let page = Page (T.pack "Blog Posts") html applyTemplateAndWrite "default.html" page target Shake.putInfo $ "Built " <> target diff --git a/app/Templates.hs b/app/Templates.hs index aca8225..7daa290 100644 --- a/app/Templates.hs +++ b/app/Templates.hs @@ -11,10 +11,12 @@ import GHC.Stack (HasCallStack) import qualified Text.Mustache as Mus import qualified Text.Mustache.Compile as Mus import Types (Post (postAuthor, postContent, postDate, postLink, postTags, postTitle), RenderedPost (RenderedPost, rPostAuthor, rPostContent, rPostDate, rPostHasTags, rPostIsoDate, rPostLink, rPostTags, rPostTitle)) +import Utilities applyTemplate :: (HasCallStack, (ToJSON a)) => String -> a -> Action Text applyTemplate templateName context = do tmpl <- readTemplate $ "templates" templateName + -- liftIO $ print $ A.toJSON context case Mus.checkedSubstitute tmpl (A.toJSON context) of ([], text) -> return text (errs, _) -> @@ -51,7 +53,7 @@ fromPost post = rPostTags = postTags post, rPostHasTags = not . null . postTags $ post, rPostDate = postDate post, - rPostIsoDate = postDate post, + rPostIsoDate = postDate post >>= parseDate, rPostContent = postContent post, rPostLink = postLink post } diff --git a/app/Utilities.hs b/app/Utilities.hs index 732f0ac..43b879f 100644 --- a/app/Utilities.hs +++ b/app/Utilities.hs @@ -5,7 +5,6 @@ import Control.Monad (filterM) import Data.Aeson (Result (Error, Success)) import qualified Data.Aeson as A import Data.List (find) -import Data.Maybe (fromMaybe) import Data.Text (Text) import qualified Data.Text as T import Data.Time @@ -133,17 +132,6 @@ yamlToPost path = do post <- decodeFileThrow path -- let post' = dateTransform post return post - where - dateTransform post@(Post {postDate}) = do - postDate' <- postDate - let postDate'' = dateStrTransform $ T.unpack postDate' - Just - post - { postDate = postDate'' - } - dateStrTransform date = do - date' <- parseTimeM False defaultTimeLocale "%Y-%-m-%-d" date - Just $ T.pack $ formatTime @UTCTime defaultTimeLocale "%b %e, %Y" date' isTypstPost :: FilePath -> Bool isTypstPost path = FP.takeExtension path == ".typ" @@ -169,3 +157,8 @@ getPublishedPosts :: Action [FilePath] getPublishedPosts = do postPaths <- Shake.getDirectoryFiles "" postGlobs filterM (fmap not . isDraft) postPaths + +parseDate :: Text -> Maybe Text +parseDate str = do + date <- parseTimeM False defaultTimeLocale "%Y-%-m-%-d" $ T.unpack str + return $ T.pack $ formatTime @UTCTime defaultTimeLocale "%Y-%m-%d" date