aboutsummaryrefslogtreecommitdiff
path: root/site.hs
blob: 3d7743085ba569560298a2ef8a7036df56a24d2e (plain)
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
{-# LANGUAGE OverloadedStrings #-}

import Hakyll

import Control.Monad ((>=>))
import Data.Foldable (traverse_)
import Data.Maybe (fromMaybe)
import System.FilePath ((</>), dropTrailingPathSeparator, normalise)

import Debug.Trace

getMount :: Metadata -> Routes
getMount = maybe idRoute constRoute . lookupString "mount"

indexInDir :: Routes -> Routes
indexInDir = flip composeRoutes . customRoute $ (</> "index.html") . toFilePath

extractRedirs :: Identifier -> Rules (Identifier, [Identifier])
extractRedirs ident = do
  md <- getMetadata ident
  let to = fromMaybe ident $ fromFilePath <$> lookupString "mount" md
      froms =
        fromMaybe [] $
        map (fromFilePath . dropTrailingPathSeparator . normalise) <$>
        lookupStringList "redirects" md
  pure (to, froms)

makePage :: Rules ()
makePage = do
  route $ indexInDir (metadataRoute getMount)
  compile $
    pandocCompiler >>= loadAndApplyTemplate "templates/page.html" pageCtx >>=
    loadAndApplyTemplate "templates/default.html" pageCtx >>=
    relativizeUrls

makeRedirects :: Identifier -> [Identifier] -> Rules ()
makeRedirects to froms =
  create froms $ do
    route $ indexInDir idRoute
    compile . makeItem . Redirect . ('/' :) . toFilePath $ to

spawnRedirects :: [Identifier] -> Rules ()
spawnRedirects = traverse_ (extractRedirs >=> uncurry makeRedirects)

main :: IO ()
main =
  hakyll $
    {- Source the pages -}
   do
    match "external/**/*.md" makePage
    {- Source and process the redirects -}
    getMatches "external/**/*.md" >>= spawnRedirects
    {- Compile the templates (no routing, cache-only) -}
    match "templates/*" $ compile templateBodyCompiler

pageCtx :: Context String
pageCtx = defaultContext