summaryrefslogtreecommitdiff
path: root/site.hs
blob: 47a385bd3a8255aacb844ade839aa6b3ef5e58dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{-# LANGUAGE OverloadedStrings #-}

import Hakyll hiding (pandocCompiler)
import Text.Pandoc.Options
import Text.Pandoc.Highlighting

-- Pass custom options to the Pandoc compiler
pandocCompiler :: Compiler (Item String)
pandocCompiler =
  let defaultExtensions = writerExtensions defaultHakyllWriterOptions
      addedExtensions = extensionsFromList [Ext_lists_without_preceding_blankline]
      removedExtensions = extensionsFromList [Ext_blank_before_header, Ext_blank_before_blockquote]
      writerOptions = defaultHakyllWriterOptions {
        writerExtensions = disableExtensions (defaultExtensions <> pandocExtensions <> addedExtensions) removedExtensions,
        writerHTMLMathMethod = MathML,
        writerHighlightStyle = Just zenburn
      }
  in pandocCompilerWith defaultHakyllReaderOptions writerOptions

-- Loads the template specified in a post's metadata, if it exists
loadLayoutTemplate :: Context String -> Item String -> Compiler (Item String)
loadLayoutTemplate context item = do
  field <- getMetadataField (itemIdentifier item) "layout"
  case field of
    Just path ->
      let templatePath = "_templates/" ++ path ++ ".html" in
      loadAndApplyTemplate (fromFilePath templatePath) context item
    _ -> return item

main :: IO ()
main = hakyll $ do
  -- Compile templates for future use
  match "_templates/*" $ compile templateBodyCompiler

  -- Detect whether HTML files are standalone or in need of a template
  match ("**.html" .||. "**.htm") $ do
    route idRoute
    compile $ do
      identifier <- getUnderlying
      field <- getMetadataField identifier "layout"
      case field of
        Just _ -> pandocCompiler
        Nothing -> getResourceBody
      >>= loadLayoutTemplate defaultContext
      >>= relativizeUrls

  -- Match all other renderable files and apply their template, if it exists
  match ("**.md" .||. "**.markdown") $ do
    route $ setExtension "html"
    compile $ pandocCompiler
      >>= loadLayoutTemplate defaultContext
      >>= relativizeUrls

  -- Additionally copy non-HTML files verbatium
  match ("**.md" .||. "**.markdown") $ version "raw" $ do
    route idRoute
    compile pandocCompiler

  -- Copy all additional files verbatium
  match "**" $ do
    route idRoute
    compile copyFileCompiler