forked from HackSoc/hacksoc.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hakyll.hs
68 lines (57 loc) · 2.29 KB
/
hakyll.hs
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
63
64
65
66
67
68
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Applicative (Alternative(empty))
import Data.Monoid ((<>))
import Hakyll
main :: IO ()
main = hakyllWith defaultConfiguration $ do
-- Load templates
match "templates/*" $ compile templateCompiler
-- Copy static files
match "static/**" $ do
route $ gsubRoute "static/" (const "")
compile copyFileCompiler
-- Build news posts
match "news/*" $ do
route $ setExtension ".html"
compile $ pandocCompiler
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/news.html" postContext
>>= loadAndApplyTemplate "templates/wrapper.html" postContext
>>= relativizeUrls
-- Build index page with paginated news list
let everyN n = fmap (paginateEvery n) . sortRecentFirst
let fname n = fromFilePath $ if n == 1 then "index.html" else "news-" ++ show n ++ ".html"
paginator <- buildPaginateWith (everyN 5) "news/*" fname
paginateRules paginator $ \pageNum pattern -> do
route idRoute
compile $ do
entries <- recentFirst =<< loadAll pattern
let ctx = dropField "title" $
paginateContext paginator pageNum <>
listField "entries" (excerptField "content" <> postContext) (return entries) <>
defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/newslist.html" ctx
>>= loadAndApplyTemplate "templates/wrapper.html" ctx
>>= relativizeUrls
-- Build pages
match "*.html" $ do
route idRoute
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/wrapper.html" defaultContext
>>= relativizeUrls
-- | Context for rendering posts: infer the date from the filename.
postContext :: Context String
postContext = dateField "date" "%B %e, %Y" <> defaultContext
-- | Extract the first non-blank line from a news post.
excerptField :: Snapshot -> Context String
excerptField snapshot = field "excerpt" $ \item -> do
body <- itemBody <$> loadSnapshot (itemIdentifier item) snapshot
case lines (trim body) of
(excerpt:_) -> return excerpt
[] -> fail $ "excerptField: no excerpt defined for " ++ show (itemIdentifier item)
-- | Drop a field from a context.
dropField :: String -> Context a -> Context a
dropField field (Context f) = Context $ \k a i ->
if k == field then empty else f k a i