Skip to content
Permalink
Browse files

Add Git information to post template (#3)

Why:

* Information such as the author, date of creation, date of last modification are useful for the reader.
* The Git revision is an addition for nerds.

How:

* For every information that has to be available in a post, a context was created.
* Closes #1
* Closes #2
  • Loading branch information...
peri4n committed Mar 28, 2019
1 parent fbed40a commit cf4031ee03755f14772755780909fd4acbce3e1d
@@ -6,7 +6,10 @@ cabal-version: >= 1.10
executable site
main-is: site.hs
build-depends: base == 4.*
, hakyll == 4.12.*
, hakyll-sass == 0.2.3
, process
, hakyll
, hakyll-sass
, hakyll-filestore
, filestore
ghc-options: -threaded
default-language: Haskell2010
@@ -1,6 +1,7 @@
---
layout: post
title: "Running a CLI command on each Kubernetes pod with a label"
title: Changing the /etc/hosts on every Kubernetes pod with a label
keywords: kubernetes,kubectl,saas,zsh,bash, shell
---

I once was tasked to setup a SaaS application in [Kubernetes][1] (short K8S).
@@ -4,8 +4,9 @@
}

a {
color: $red;
text-decoration: none;
&:visited {
color: #000;
color: $red;
}
}
@@ -1,4 +1,9 @@
$color1: #286DA8;
$color2: #CD5360;
$color3: #B37B4E;
$color4: #438496;
$red: #CD5360;
$black: #000000;

// Post list colors
$post_list_link: $black;
$post_list_link_hover: $red;

// Syntax highlighting colors
$code_keyword: #AC4142;
@@ -42,7 +42,7 @@ pre.numberSource {
}

&:before {
content: attr(data-line-number);
content: attr(title);
position: absolute; left: -5em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all;
-webkit-touch-callout: none; -webkit-user-select: none;
@@ -120,7 +120,7 @@ code span {
font-style: italic;
}
&.kw { /* Keyword */
color: #ac4142;
color: $code_keyword;
font-weight: bold;
}
&.op { /* Operator */
@@ -1,6 +1,6 @@
@import "base/meyer-reset";
@import "base/base";
@import "base/colors";
@import "base/base";
@import "base/syntax";
@import "base/typography";
@import "base/grid";
@@ -3,9 +3,22 @@ body main article {
title {
font-size: 2em;
text-align: center;
padding-bottom: 15px;
padding-bottom: 25px;
display: block;
}
.description {
display: block;
margin-left: 50%;
font-size: 0.6em;

.author {
color: grey;
}
.date {
color: grey;
}
}

padding-bottom: 30px;
}
section {
@@ -32,4 +45,9 @@ body main article {
}
}
}

footer {
width: 70%;
margin: 60px auto;
}
}
@@ -2,6 +2,11 @@ body > main div.post-list {
ul {
li {
padding-left: 30px;

.post-title a {
color: $post_list_link;
}

.post-title {
padding-bottom: 10px;
display: inline-block;
@@ -16,15 +21,18 @@ body > main div.post-list {
}

&:before {
padding-left: 10px;
width: 30px;
@extend .fas;
@extend .fa-angle-right;
}

&:hover {
color: orange;
&, .post-title a, .post-date {
color: $post_list_link_hover;
}

&:before {
padding-left: 20px;
padding-left: 10px;
width: 20px;
}
}
142 site.hs
@@ -1,69 +1,113 @@
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid (mappend)

import Data.FileStore (Revision, authorName, revAuthor,
revDateTime, revId)
import Data.Monoid
import Hakyll
import System.Environment (getArgs)
import Hakyll.FileStore.Git.Context (gitGetAuthorNames,
gitGetRevisions)
import Hakyll.Web.CompressCss (compressCss)
import Hakyll.Web.Template.Context (metadataField)
import System.Environment (getArgs)
import System.Process (readProcess)

--------------------------------------------------------------------------------
main :: IO ()
main = do
draftMode <- fmap (elem "--with-drafts") getArgs
let postsPattern = if (draftMode) then "posts/*" .||. "drafts/*" else "posts/*"
draftMode <- fmap (elem "--with-drafts") getArgs
let postsPattern =
if draftMode
then "posts/*" .||. "drafts/*"
else "posts/*"
hakyllWith myConfig $ do
match "fonts/*" $ do
route idRoute
compile copyFileCompiler
match "sass/**.scss" $ compile getResourceBody
scssDependencies <- makePatternDependency "sass/**.scss"
rulesExtraDependencies [scssDependencies] $
create ["css/main.css"] $ do
route idRoute
compile compressedSassCompiler
match postsPattern $ do
route $ setExtension "html"
compile $
pandocCompiler >>= loadAndApplyTemplate "templates/post.html" postCtx >>=
loadAndApplyTemplate "templates/default.html" postCtx >>=
relativizeUrls
match (fromList ["projects.md", "contact.md", "about.md"]) $ do
route $ setExtension "html"
compile $
pandocCompiler >>= loadAndApplyTemplate "templates/default.html" pageCtx >>=
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`
pageCtx
getResourceBody >>= applyAsTemplate indexCtx >>=
loadAndApplyTemplate "templates/default.html" indexCtx >>=
relativizeUrls
match "templates/*" $ compile templateBodyCompiler

--------------------------------------------------------------------------------
-- Contexts
--------------------------------------------------------------------------------
myDefaultCtx :: Context String
myDefaultCtx = defaultContext <> dateCtx <> metadataField

hakyllWith myConfig $ do
dateCtx :: Context String
dateCtx = dateField "date" "%B %e, %Y"

match "fonts/*" $ do
route idRoute
compile copyFileCompiler
postCtx :: Context String
postCtx = myDefaultCtx <> gitCtx

match "sass/**.scss" $ do
compile getResourceBody
pageCtx :: Context String
pageCtx = myDefaultCtx

scssDependencies <- makePatternDependency "sass/**.scss"
rulesExtraDependencies [scssDependencies] $ do
create ["css/main.css"] $ do
route idRoute
compile sassCompiler
metaKeywordCtx :: Context String
metaKeywordCtx =
field "metaKeywords" $ \item -> do
tags <- getMetadataField (itemIdentifier item) "keywords"
return $ maybe "" showMetaTags tags
where
showMetaTags t = "<meta name=\"keywords\" content=\"" ++ t ++ "\">\n"

match (postsPattern .||. fromList ["projects.md", "contact.md", "about.md"]) $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/post.html" postCtx
>>= loadAndApplyTemplate "templates/default.html" postCtx
>>= relativizeUrls
gitCtx :: Context String
gitCtx = gitAuthorCtx <> gitRevisionCtx <> gitDateCtx

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
fromLastRevisionCtx :: String -> (Revision -> String) -> Context String
fromLastRevisionCtx name f =
field name (\item -> f . head <$> gitGetRevisions (itemIdentifier item))

getResourceBody
>>= applyAsTemplate indexCtx
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/default.html" indexCtx
>>= relativizeUrls
gitAuthorCtx :: Context String
gitAuthorCtx = fromLastRevisionCtx "author" (authorName . revAuthor)

match "templates/*" $ compile templateBodyCompiler
gitRevisionCtx :: Context String
gitRevisionCtx = fromLastRevisionCtx "revision" revId

gitDateCtx :: Context String
gitDateCtx = fromLastRevisionCtx "lastmod" (show . revDateTime)

--------------------------------------------------------------------------------
postCtx :: Context String
postCtx =
dateField "date" "%B %e, %Y" `mappend`
defaultContext

-- Compilers
--------------------------------------------------------------------------------
sassCompiler :: Compiler (Item String)
sassCompiler = loadBody (fromFilePath "sass/main.scss")
>>= makeItem
>>= withItemBody (unixFilter "sass" args)
where args = ["-s", "--scss", "-I", "sass"]
sassCompiler =
loadBody (fromFilePath "sass/main.scss") >>= makeItem >>=
withItemBody (unixFilter "sass" args)
where
args = ["-s", "--scss", "-I", "sass"]

compressedSassCompiler :: Compiler (Item String)
compressedSassCompiler = fmap compressCss <$> sassCompiler

--------------------------------------------------------------------------------
-- Configuration
--------------------------------------------------------------------------------
myConfig :: Configuration
myConfig = defaultConfiguration {
deployCommand = "aws s3 cp ./_site/ s3://bioinform.at/ --recursive"
}
myConfig =
defaultConfiguration
{deployCommand = "aws s3 cp ./_site/ s3://bioinform.at/ --recursive"}
@@ -18,7 +18,7 @@
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: lts-11.11
resolver: lts-13.14

# User packages to be built.
# Various formats can be used as shown in the example below.
@@ -39,7 +39,11 @@ packages:
# using the same syntax as the packages field.
# (e.g., acme-missiles-0.3)
extra-deps:
- hakyll-sass-0.2.3
- hakyll-4.12.5.1
- hakyll-sass-0.2.4
- hakyll-filestore-0.1.6
- lrucache-1.2.0.1
- filestore-0.6.3.4

# Override default flag values for local packages and extra-deps
# flags: {}
@@ -64,3 +68,4 @@ extra-deps:
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
allow-newer: true
@@ -4,6 +4,7 @@
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
$if(metaKeywords)$$metaKeywords$$endif$
<title>$title$</title>
<link rel="stylesheet" href="/css/main.css" />
</head>
@@ -1,8 +1,14 @@
<article>
<header>
<title>$title$</title>
<span class="description">
by <span class="author">$author$</span> on <span class="date">$date$</span>
</span>
</header>
<section>
$body$
</section>
<footer>
This article last edited on the $lastmod$. Last revision: <a href="https://github.com/peri4n/my-site/commit/$revision$">$revision$</a>
</footer>
</article>

0 comments on commit cf4031e

Please sign in to comment.
You can’t perform that action at this time.