-
Notifications
You must be signed in to change notification settings - Fork 2
/
site.hs
199 lines (170 loc) · 6.83 KB
/
site.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
{-# LANGUAGE OverloadedStrings #-}
import Hakyll
import Data.Monoid ((<>))
import Data.List (stripPrefix)
import qualified Data.Set as Set
import Hakyll.Web.Sass (sassCompiler)
import qualified Config as C
main :: IO ()
main = do
msiteConfig <- C.fromConfig "config.yml"
maybe (error "Expected file 'config.yml' not found") main' msiteConfig
main' :: C.Site -> IO ()
main' siteConfig = hakyllWith hakyllConfig $ do
match (fromGlob "images/**" .||. fromGlob "js/**" .||. fromGlob "lib/**") $ do
route idRoute
compile copyFileCompiler
match "css/*.scss"$ do
route $ setExtension "css"
compile (fmap compressCss <$> sassCompiler)
match (fromList [ "pages/about.rst"
, "pages/contact.markdown"
, "pages/LICENSE.markdown"
]) $ do
route $
customRoute (maybe (error "Expected pages to be in 'pages' folder")
id . stripPrefix "pages/" . toFilePath)
`composeRoutes` setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/page.html" siteCtx
>>= loadAndApplyTemplate "templates/default.html" siteCtx
>>= relativizeUrls
tags <- buildTags "posts/**" (fromCapture "tags/*.html")
createTagsRules tags (\xs -> "Posts tagged \"" ++ xs ++ "\"")
categories <- buildCategories "posts/**" (fromCapture "categories/*.html")
createTagsRules categories (\xs -> "Posts categorised as \"" ++ xs ++ "\"")
match "posts/**" $ do
route $ setExtension "html"
let
namedTags =
[("tags", tags), ("categories", categories)]
compile $ pandocCompiler
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/post.html" (ctxWithTags postCtx namedTags)
>>= loadAndApplyTemplate "templates/default.html" (ctxWithTags postCtx namedTags)
>>= relativizeUrls
create ["archive.html"] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "posts/*"
let archiveCtx =
listField "posts" postCtx (return posts) `mappend`
constField "title" "Archives" `mappend`
siteCtx
makeItem ""
>>= loadAndApplyTemplate "templates/archive.html" archiveCtx
>>= loadAndApplyTemplate "templates/default.html" archiveCtx
>>= relativizeUrls
match "pages/index.html" $ do
route (constRoute "index.html")
compile $ do
posts <- recentFirst =<< loadAll "posts/*"
let
indexCtx =
listField "posts" postCtx (return posts)
<> constField "title" "Home"
<> siteCtx
getResourceBody
>>= applyAsTemplate indexCtx
>>= loadAndApplyTemplate "templates/default.html" indexCtx
>>= relativizeUrls
match (fromGlob "partials/*" .||. fromGlob "templates/*") $
compile templateBodyCompiler
create ["feed.xml"] $ do
route idRoute
compile $ do
let
feedConfig =
C.feed siteConfig
feedCtx =
postCtx <> bodyField "description"
posts <- fmap (take 10) . recentFirst =<< loadAllSnapshots "posts/**" "content"
renderAtom (atomFeedConfiguration feedConfig) feedCtx posts
-- SEO-related stuff
create ["sitemap.xml"] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "posts/**"
pages <- loadAll "pages/*"
let
crawlPages =
sitemapPages pages ++ posts
sitemapCtx =
mconcat [ listField "entries" siteCtx (return crawlPages)
, siteCtx
]
makeItem ""
>>= loadAndApplyTemplate "templates/sitemap.xml" sitemapCtx
>>= relativizeUrls
match "robots.txt" $ do
route idRoute
compile $ getResourceBody >>= relativizeUrls
where
ctxWithTags :: Context String -> [(String, Tags)] -> Context String
ctxWithTags ctx =
foldr (\(name, tags) baseCtx -> tagsField name tags <> baseCtx) ctx
siteCtx :: Context String
siteCtx =
generalCtx <> styleCtx <> defaultContext
where
generalCtx =
field "site-title" (toField C.general C.site_title)
<> field "head-title" (toField C.general C.head_title)
<> field "base-url" (toField C.general C.base_url)
styleCtx =
field "header-colour" (toField C.style C.header_colour)
<> field "head-theme-colour" (toField C.style C.head_theme_colour)
<> field "footer-colour" (toField C.style C.footer_colour)
<> field "footer-btn-colour" (toField C.style C.footer_btn_colour)
<> field "footer-link-colour" (toField C.style C.footer_link_colour)
<> field "navbar-text-colour-desktop" (toField C.style C.navbar_text_colour_desktop)
<> field "navbar-text-colour-mobile" (toField C.style C.navbar_text_colour_mobile)
toField configObj configField item = do
metadata <- getMetadata $ itemIdentifier item
return $ configField (configObj siteConfig)
postCtx :: Context String
postCtx =
dateField "date" "%B %e, %Y"
<> teaserField "teaser" "content"
-- create a short version of the teaser. Strip out HTML tags and trim.
<> mapContext (trim . take 160 . stripTags) (teaserField "teaser-short" "content")
<> siteCtx
where
trim xs =
map snd . filter trim' $ zip [0..] xs
where
trim' (ix, x)
| ix == 0 || ix == (length xs - 1) =
x `notElem` [' ' , '\n', '\t']
| otherwise =
True
createTagsRules :: Tags -> (String -> String) -> Rules ()
createTagsRules tags mkTitle =
tagsRules tags $ \tag pattern -> do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll pattern
let
ctx =
constField "title" (mkTitle tag)
<> listField "posts" postCtx (return posts)
<> siteCtx
makeItem ""
>>= loadAndApplyTemplate "templates/tag.html" ctx
>>= loadAndApplyTemplate "templates/default.html" ctx
>>= relativizeUrls
atomFeedConfiguration :: C.Feed -> FeedConfiguration
atomFeedConfiguration fs =
FeedConfiguration { feedTitle = C.title fs
, feedDescription = C.description fs
, feedAuthorName = C.author_name fs
, feedAuthorEmail = C.author_email fs
, feedRoot = C.root fs
}
-- Friendlier config when using docker
hakyllConfig :: Configuration
hakyllConfig =
defaultConfiguration{ previewHost = "0.0.0.0" }
sitemapPages :: [Item String] -> [Item String]
sitemapPages =
filter ((/="pages/LICENSE.markdown") . toFilePath . itemIdentifier)