diff options
| author | jamie <git@skilet.ro> | 2025-10-14 14:57:18 +0100 |
|---|---|---|
| committer | jamie <git@skilet.ro> | 2025-10-14 21:06:23 +0100 |
| commit | 49b15415e3d48d6e80a21d644fc3762fecfba0f5 (patch) | |
| tree | c22667da10a68e39845a5ffd2fb42ee6af5aab96 /site.hs | |
| parent | 215f64b61c51dd6eace1b5b10bcd749955ad4113 (diff) | |
refactor: massive template rewrite
code is still a mess, but it looks a tad nicer now. still a LOT to do
Diffstat (limited to 'site.hs')
| -rw-r--r-- | site.hs | 33 |
1 files changed, 28 insertions, 5 deletions
@@ -1,12 +1,15 @@ -------------------------------------------------------------------------------- {-# LANGUAGE OverloadedStrings #-} +import Control.Exception (SomeException, catch) import Data.List (isInfixOf, sortBy) import Data.Monoid (mappend) import Hakyll import Hakyll.Web.Sass (sassCompiler) +import System.Environment import System.FilePath.Posix (splitFileName, takeBaseName, takeDirectory, (</>)) +import System.Process -------------------------------------------------------------------------------- main :: IO () @@ -28,7 +31,7 @@ main = hakyll $ do route idRoute compile $ pandocCompiler - >>= loadAndApplyTemplate "templates/default.html" defaultContext + >>= loadAndApplyTemplate "templates/default.html" defaultCtx >>= relativizeUrls >>= removeIndexHtml @@ -36,7 +39,7 @@ main = hakyll $ do route prettyRoute compile $ pandocCompiler - >>= loadAndApplyTemplate "templates/default.html" defaultContext + >>= loadAndApplyTemplate "templates/default.html" defaultCtx >>= relativizeUrls >>= removeIndexHtml @@ -57,7 +60,7 @@ main = hakyll $ do let archiveCtx = listField "posts" postCtx (return posts) `mappend` constField "title" "Archives" - `mappend` defaultContext + `mappend` defaultCtx makeItem "" >>= loadAndApplyTemplate "templates/archive.html" archiveCtx @@ -89,7 +92,7 @@ main = hakyll $ do posts <- recentFirst =<< loadAll "posts/*" let indexCtx = listField "posts" postCtx (return posts) - `mappend` defaultContext + `mappend` defaultCtx getResourceBody >>= applyAsTemplate indexCtx @@ -132,12 +135,32 @@ removeIndexHtml item = return $ fmap (withUrls removeIndexStr) item -------------------------------------------------------------------------------- +defaultCtx :: Context String +defaultCtx = gitRevisionCtx <> defaultContext + postCtx :: Context String -postCtx = hDateCtx <> cDateCtx <> defaultContext +postCtx = hDateCtx <> cDateCtx <> defaultCtx hDateCtx :: Context String hDateCtx = dateField "hdate" "%B %e, %Y" cDateCtx :: Context String cDateCtx = dateField "cdate" "%Y-%m-%d" + +gitRevisionField :: String -> Context String +gitRevisionField = flip field (const gitRevision) + +-- Compiler which attempts to find the current git revision. This is done by +-- first checking the "REVISION" environment variable and, if this fails, +-- running the command "git rev-parse HEAD". +gitRevision :: Compiler String +gitRevision = do + rev <- unsafeCompiler (getEnv "REVISION" `catch` (\e -> return (e :: SomeException) >> return "")) + if rev == "" + then unixFilter "git" ["rev-parse", "--short=7", "HEAD"] "" + else return rev + +gitRevisionCtx :: Context String +gitRevisionCtx = gitRevisionField "revision" + -------------------------------------------------------------------------------- |
