From d89a2bae5c91d3a9b59e9c16de4f56b1fee18075 Mon Sep 17 00:00:00 2001 From: Mats Rauhala Date: Thu, 20 Sep 2018 21:57:49 +0300 Subject: [PATCH] Support for guides --- site.cabal | 1 + site.hs | 58 +++++++++++++++++++++++++++--------------- templates/default.html | 1 + templates/post.html | 2 +- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/site.cabal b/site.cabal index 28499ee..95ad990 100644 --- a/site.cabal +++ b/site.cabal @@ -7,5 +7,6 @@ executable site main-is: site.hs build-depends: base == 4.* , hakyll == 4.10.* + , time ghc-options: -threaded default-language: Haskell2010 diff --git a/site.hs b/site.hs index 9e9e59f..e016964 100644 --- a/site.hs +++ b/site.hs @@ -2,6 +2,8 @@ {-# LANGUAGE OverloadedStrings #-} import Data.Monoid (mappend) import Hakyll +import Data.List (sortBy, sortOn) +import Data.Time (formatTime, defaultTimeLocale) -------------------------------------------------------------------------------- @@ -25,34 +27,38 @@ main = hakyll $ do >>= loadAndApplyTemplate "templates/default.html" defaultContext >>= relativizeUrls - -- match "posts/*" $ do - -- route $ setExtension "html" - -- compile $ pandocCompiler - -- >>= loadAndApplyTemplate "templates/post.html" postCtx - -- >>= loadAndApplyTemplate "templates/default.html" postCtx - -- >>= relativizeUrls + match "posts/guides/*" $ do + route $ setExtension "html" + compile $ pandocCompiler + >>= loadAndApplyTemplate "templates/post.html" postCtx + >>= loadAndApplyTemplate "templates/default.html" postCtx + >>= relativizeUrls - -- create ["archive.html"] $ do - -- route idRoute - -- compile $ do - -- posts <- recentFirst =<< loadAll "posts/*" - -- let archiveCtx = - -- listField "posts" postCtx (return posts) `mappend` - -- constField "title" "Archives" `mappend` - -- defaultContext - -- - -- makeItem "" - -- >>= loadAndApplyTemplate "templates/archive.html" archiveCtx - -- >>= loadAndApplyTemplate "templates/default.html" archiveCtx - -- >>= relativizeUrls + match "posts/brainstorming/*" $ do + route $ setExtension "html" + compile $ pandocCompiler + >>= loadAndApplyTemplate "templates/post.html" postCtx + >>= loadAndApplyTemplate "templates/default.html" postCtx + >>= relativizeUrls + create ["guides.html"] $ do + route idRoute + compile $ do + posts <- modFirst =<< loadAll "posts/guides/*" + let archiveCtx = + listField "posts" postCtx (return posts) `mappend` + constField "title" "Archives" `mappend` + defaultContext + + makeItem "" + >>= loadAndApplyTemplate "templates/archive.html" archiveCtx + >>= loadAndApplyTemplate "templates/default.html" archiveCtx + >>= relativizeUrls match "index.html" $ do route idRoute compile $ do - posts <- recentFirst =<< loadAll "posts/*" let indexCtx = - listField "posts" postCtx (return posts) `mappend` constField "title" "Home" `mappend` defaultContext @@ -63,9 +69,19 @@ main = hakyll $ do match "templates/*" $ compile templateBodyCompiler +modFirst :: [Item a] -> Compiler [Item a] +modFirst = fmap reverse . modified + where + modified = sortByM (getItemModificationTime . itemIdentifier) + sortByM f xs = map fst . sortOn snd <$> mapM (\x -> (,) x <$> f x) xs -------------------------------------------------------------------------------- postCtx :: Context String postCtx = dateField "date" "%B %e, %Y" `mappend` + modifiedField "modified" "%B %e, %Y" `mappend` defaultContext + where + modifiedField key format = field key $ \i -> do + time <- getItemModificationTime $ itemIdentifier i + return $ formatTime defaultTimeLocale format time diff --git a/templates/default.html b/templates/default.html index bfa66fc..58991d2 100644 --- a/templates/default.html +++ b/templates/default.html @@ -14,6 +14,7 @@ diff --git a/templates/post.html b/templates/post.html index 732149b..1f717cb 100644 --- a/templates/post.html +++ b/templates/post.html @@ -1,6 +1,6 @@
- Posted on $date$ + Posted on $date$, modified on $modified$ $if(author)$ by $author$ $endif$