Shuangrimu Static Site Generator
Welcome to the source code of www.shuangrimu.com!
This file is a literate Haskell file, that is it is the literal code from which this site is compiled (or at least the static site generator parts of it). For a higher-level overview of this entire project, see the README that should be bundled with this code. The documentation in this file will deal mainly with the details of this site generates the static HTML files that we push.
This also serves as a bit of a Hakyll tutorial for people who are unfamiliar with it.
I assume an intermediate level knowledge of Haskell and that you have gone through at least the basic tutorial of Hakyll https://jaspervdj.be/hakyll/tutorials.html. Essentially you should’ve seen a “Hello World” example of Hakyll already.
Design
At a high level every static site generator follows the same workflow.
- Read in a bunch of content (often written in Markdown)
- Apply a series of templates to it
- Collate all the results as some set of HTML/CSS/JS files that can be immediately served by any static file webserver.
So what is Hakyll? Unlike, say Jekyll, Hakyll is not a static site generator itself per se. It is better thought of as a toolbox for creating your own static site generator that best suits your needs.
It gives you an embedded DSL for Haskell that lets you craft a custom compiler based on Pandoc. Usually you will end up creating a compiler from Markdown to HTML that has a lot of conventions around where files are supposed to be located, what the format of the markdown file should be, etc.
In effect you can think of Hakyll as a static site generator generator. You use
it to create an executable (or you can interpret it with runhaskell
) which is
then your own personal static site generator that you can run.
Ultimately what we’re building here is a compiler that takes everything in
this repository and generates a bunch of HTML, Javascript, and CSS in the
_site
directory which we can deploy with any webserver capable of serving
static files.
Let’s start with some import
boilerplate and then dive into what Hakyll gives
us that we wouldn’t get if we just tried to build everything from scratch
ourselves.
Actual Code
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
import Data.Monoid (mappend, (<>))
import Hakyll
import Text.Blaze.Html5 (toHtml, toValue, (!))
import qualified Text.Blaze.Html5 as Html
import qualified Text.Blaze.Html5.Attributes as A
import Text.Printf
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.HashMap.Lazy as Map
import qualified Data.Aeson.Types as Aeson
import Data.Aeson.KeyMap (toHashMap)
import Data.Text (Text, pack, strip, unpack)
import qualified Data.Text as Text
import Control.Monad
import Control.Monad.Except
import Data.Typeable
import Text.Pandoc (writeMarkdown, Pandoc, runPure, writeLaTeX)
If we think back to the steps laid out in the Design section, Hakyll provides
the first step, the ability to read in content, through the match
function.
The reason why a custom function is provided, rather than simply using
something like readFile
is that Hakyll provides its own custom monad, the
Rules
monad, in which all these actions live. By providing a custom monad
rather than simply using IO
, Hakyll is able to offer some features to make
life easier. Foremost among them is incremental compilation and a preview
server that can automatically pick up on changes and compile them. This is
because under the hood the Rules
monad is storing the dependencies among your
files of which files depend on which other ones in order to work.
However, this design choice, to access content via match
, has other
consequences for how to design a program with Hakyll that might seem
unintuitive at first.
Another way of thinking about this is that We are basically building a
dependency graph with Rules
of actions to execute. match
allows us to
associate parts of that graph with other parts of our dependency graph. (This
paragraph needs to be rewritten, it’s not particularly clear)
Let’s dive into the easiest example of using match
. Most uses of match
will
follow this pattern, where pass to match
a route
, which indicates where in
our _site
folder our final product will be and a “compiler”, which indicates
what transformation we will be doing on top of the file to generate the
artifact that goes into the _site
folder.
compileHtAccessFile :: Rules ()
=
compileHtAccessFile "htaccess" $ do
match ".htaccess")
route (constRoute compile copyFileCompiler
In this case since we have a single file, we’re going to use constRoute
to
indicate the artifact’s name in _site
should always be the same. We also are
copying the file entirely unchanged so we just the copyFileCompiler
.
This generates a rule that reads in a file named htaccess
and then places it
in some build artifact folder (by default this is _site
under the name
.htaccess
). The lack of a period in the first argument of the match
is
because by default Hakyll ignores files starting with a period (this can be
changed in its configuration settings).
A lot of other actions we need to do also are to simply copy files that already
exist into _site
.
copyFavicon :: Rules ()
=
copyFavicon "favicon.ico" $ do
match
route idRoute
compile copyFileCompiler
copyStaticFiles :: Rules ()
=
copyStaticFiles "static/*" $ do
match
route idRoute
compile copyFileCompiler
copyImages :: Rules ()
=
copyImages "images/*" $ do
match
route idRoute
compile copyFileCompiler
copyCustomJavascript :: Rules ()
=
copyCustomJavascript "js/*" $ do
match
route idRoute
compile copyFileCompiler
copyPrettifyJsLibrary :: Rules ()
=
copyPrettifyJsLibrary "prettify/*" $ do
match
route idRoute compile copyFileCompiler
We also can compress our CSS to make it more lightweight.
compileCss :: Rules ()
=
compileCss "css/*" $ do
match
route idRoute compile compressCssCompiler
Now we get to meat of things, which is actually turning our posts into properly formatted HTML.
Let’s first specify where the folder with all our Markdown posts is.
locationOfPosts :: Pattern
= "posts/*" locationOfPosts
Notice the asterisk, which indicates we want to match against all files in the
posts
folder.
Next we need to build tags which are then used later to create a sidebar with tags in all of our pages.
= buildTags locationOfPosts (fromCapture "tags/*.html") buildTagsFromPosts
= createBasePage "about.md" "#about" (tagsCtx tags)
createAboutPage tags = createBasePage "contact.md" "#contact" (tagsCtx tags)
createContactPage tags = createBasePage "licensing.md" "#licensing" (tagsCtx tags)
createLicensingPage tags = createBasePageNonRelative "404.md" "" (tagsCtx tags) create404Page tags
As an added bit of cuteness we can create an HTML page out of this very file itself (since it’s valid Markdown after all)!
= createBasePage "site.lhs" "" (tagsCtx tags) createSiteCodePage tags
main :: IO ()
= hakyll $ do
main
compileHtAccessFile
copyFavicon
copyStaticFiles
copyImages
copyCustomJavascript
copyPrettifyJsLibrary
compileCss
<- buildTagsFromPosts
tags
let postCtxWithTags = injectCustomColor "" <> tagsCtx tags <> postCtx
createAboutPage tags
createContactPage tags
createLicensingPage tags
create404Page tags
createSiteCodePage tags
"posts/*" $ do
match $ setExtension "html"
route $ pandocCompiler
compile >>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/post.html" (tagsField "tags" tags <> injectCustomColor "" <> postCtx)
>>= loadAndApplyTemplate "templates/default.html" postCtxWithTags
>>= relativizeUrls
let pandoced = getResourceBody >>= readPandoc
let writeThroughItemMd (Item itemId body) = case runPure $ fmap unpack $ writeMarkdown defaultHakyllWriterOptions body of
Left err -> error $ "blahblah" ++ show err
Right item' -> Item itemId item'
let writeThroughItemTex (Item itemId body) = case runPure $ fmap unpack $ writeLaTeX defaultHakyllWriterOptions body of
Left err -> error $ "blahblah" ++ show err
Right item' -> Item itemId item'
"posts/*" $ version "markdown" $ do
match $ setExtension "md"
route $ fmap writeThroughItemMd pandoced
compile
"posts/*" $ version "latex" $ do
match $ setExtension "tex"
route $ fmap writeThroughItemTex pandoced
compile
"archives.html"] $ do
create [
route idRoute$ do
compile <- recentFirst =<< loadAllSnapshotsNoVersion "posts/*" "content"
posts let archiveCtx =
"posts" postCtx (return posts) <>
listField "title" "Archives" <>
constField "#archives" <>
injectCustomColor <>
tagsCtx tags
defaultContext
""
makeItem >>= loadAndApplyTemplate "templates/archive.html" archiveCtx
>>= loadAndApplyTemplate "templates/default.html" archiveCtx
>>= relativizeUrls
"atom.xml"] renderAtom
createFeed [
"rss.xml"] renderRss
createFeed [
"popular-posts" $ do
match $ getResourceBody >>= saveSnapshot "popular-posts"
compile
"index.html" $ do
match
route idRoute$ do
compile <- (loadBody "popular-posts" :: Compiler String)
listOfPopularPostsStr let listOfPopularPosts = pack listOfPopularPostsStr
<- parsePopularPostTitles listOfPopularPosts
mostPopularPostTitles <- recentFirst =<< loadAllSnapshotsNoVersion "posts/*" "content"
posts <- (filterM (\post -> fmap (\x -> Set.member x mostPopularPostTitles) (getItemTitle (itemIdentifier post)))) =<< loadAllSnapshotsNoVersion "posts/*" "content"
popularPosts let indexCtx =
"popularPosts" postCtx (return popularPosts) <>
listField "posts" postCtxWithIdx (return (zipWith (\post idx -> fmap (\postContents -> (postContents, idx)) post) posts (fmap show [1 :: Integer .. 3]))) <>
listField "title" "Home" <>
constField <>
tagsCtx tags "" <>
injectCustomColor
defaultContext
getResourceBody>>= applyAsTemplate indexCtx
>>= loadAndApplyTemplate "templates/withSlider.html" indexCtx
>>= relativizeUrls
$ \tag pattern -> do
tagsRules tags
route idRoute$ do
compile <- recentFirst =<< loadAll pattern
posts let ctx =
"tagname" tag <>
constField "" <>
injectCustomColor <>
tagsCtx tags "posts" postCtx (return posts) <>
listField
defaultContext""
makeItem >>= loadAndApplyTemplate "templates/tag.html" ctx
>>= loadAndApplyTemplate "templates/default.html" ctx
>>= relativizeUrls
"templates/*" $ compile templateCompiler
match
pattern = loadAllSnapshots (pattern .&&. hasNoVersion) loadAllSnapshotsNoVersion
This is for parsing
parsePopularPostTitles :: (MonadError [String] m) => Text -> m (Set Text)
= return $ Set.fromList $ fmap strip $ Text.lines titles
parsePopularPostTitles titles
createBasePage :: Pattern -> String -> Context String -> Rules ()
=
createBasePage sourcefile colortagname generatedTagsCtx $ do
match sourcefile $ setExtension "html"
route $ do
compile
pandocCompiler>>= loadAndApplyTemplate "templates/default.html" (injectCustomColor colortagname <> generatedTagsCtx <> defaultContext)
>>= relativizeUrls
createBasePageNonRelative :: Pattern -> String -> Context String -> Rules ()
=
createBasePageNonRelative sourcefile colortagname generatedTagsCtx $ do
match sourcefile $ setExtension "html"
route $ do
compile
pandocCompiler>>= loadAndApplyTemplate "templates/default.html" (injectCustomColor colortagname <> generatedTagsCtx <> defaultContext)
postCtx :: Context String
=
postCtx "date" "%B %e, %Y" `mappend`
dateField
defaultContext
type IdxAsString = String
postCtxWithIdx :: Context (String, IdxAsString)
= Context f
postCtxWithIdx where
= unContext (postCtx)
postCtxF = unContext (field "listIndex" $ return . itemBody)
idxCtxF "listIndex" things item = idxCtxF "listIndex" things (fmap snd item)
f = postCtxF key things (fmap fst item)
f key things item
tagsCtx :: Tags -> Context a
= tagsFieldAsLIs "tags" tags
tagsCtx tags
tagsFieldAsLIs :: String -> Tags -> Context a
= listField contextKey defaultContext (return (collectTags tags))
tagsFieldAsLIs contextKey tags where
= map (\(t, _) -> Item (tagsMakeId tagsToCollect t) t) (tagsMap tagsToCollect)
collectTags tagsToCollect
injectCustomColor :: String -> Context String
= constField "headstyle" . colorSelection
injectCustomColor where
"#about" = "#about" <> toInterpolate (255, 127, 127)
colorSelection "#archives" = "#archives" <> toInterpolate (127, 127, 255)
colorSelection "#contact" = "#contact" <> toInterpolate (127, 200, 127)
colorSelection "#licensing" = "#licensing" <> toInterpolate (200, 200, 127)
colorSelection = ""
colorSelection _
toInterpolate :: (Int, Int, Int) -> String
= printf "{color:rgb(%s, %s, %s); background-color:rgb(220, 220, 220)}" (show x) (show y) (show z)
toInterpolate (x, y, z)
simpleRenderLink :: String -> (Maybe FilePath) -> Maybe Html.Html
Nothing = Nothing
simpleRenderLink _ Just filePath) =
simpleRenderLink tag (Just $ Html.a ! A.href (toValue $ toUrl filePath) $ toHtml tag
feedConfig :: FeedConfiguration
= FeedConfiguration
feedConfig = "Shuang Rimu"
{ feedTitle = "A blog about random things"
, feedDescription = "Changlin Li"
, feedAuthorName = "rimu@shuangrimu.com"
, feedAuthorEmail = "http://www.shuangrimu.com"
, feedRoot
}
createFeed :: [Identifier]
-> (FeedConfiguration
-> Context String -> [Item String]
-> Compiler (Item String)
)-> Rules ()
= create filename $ do
createFeed filename renderer
route idRoute$ do
compile let feedCtx = postCtx <> bodyField "description"
<- fmap (take 10) . recentFirst =<<
posts "posts/*" "content"
loadAllSnapshotsNoVersion
renderer feedConfig feedCtx posts
getItemTitle :: (MonadFail m, MonadMetadata m) => Identifier -> m Text
= do
getItemTitle identifier <- getMetadata identifier
metadata case Map.lookup "title" (toHashMap metadata) of
Nothing -> fail $ "We were unable to find a title for " ++ show identifier
Just title -> case title of
Aeson.String titleText -> return titleText
-> fail $ "We found a title for " ++ show identifier ++ " but it doesn't look like a valid JSON string: " ++ show title _