-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsite.hs
280 lines (196 loc) · 6.78 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
{-# LANGUAGE OverloadedStrings #-}
import Hakyll
import Control.Monad (forM_, liftM, msum)
import Data.Char (isAlphaNum)
import Data.Maybe (fromJust, fromMaybe)
import qualified Data.Text as T
import qualified Data.Time.Clock as Clock
import Data.Time.Format (TimeLocale, defaultTimeLocale, formatTime,
parseTimeM)
import Text.Pandoc
import Web.Slug (mkSlug)
main :: IO ()
main = hakyllWith config $ do
forM_ [ "CNAME"
, "robots.txt"
, "_config.yml"
, "images/*"
, "fonts/*"
, ".well-known/*"
] $ \f -> match f $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
match "posts/*" $ do
let ctx = constField "type" "article" <> postCtx
route $ metadataRoute customizedRoute
compile $ pandocCompilerCustom
>>= loadAndApplyTemplate "templates/post.html" ctx
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/default.html" ctx
match "new-zealand/**" $ do
let ctx = constField "type" "article" <> postCtx
route $ setExtension "html"
compile $ pandocCompilerCustom
>>= loadAndApplyTemplate "templates/info.html" ctx
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/default.html" ctx
match "index.html" $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "posts/*"
let indexCtx =
listField "posts" postCtx (return posts) <>
constField "root" root <>
constField "siteName" siteName <>
defaultContext
getResourceBody
>>= applyAsTemplate indexCtx
>>= loadAndApplyTemplate "templates/default.html" indexCtx
match "templates/*" $ compile templateBodyCompiler
create ["sitemap.xml"] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "posts/*"
nzPages <- loadAll "new-zealand/**"
let pages = posts <> nzPages
sitemapCtx =
constField "root" root <>
constField "siteName" siteName <>
listField "pages" postCtx (return pages)
makeItem ""
>>= loadAndApplyTemplate "templates/sitemap.xml" sitemapCtx
create ["rss.xml"] $ do
route idRoute
compile (feedCompiler renderRss)
create ["atom.xml"] $ do
route idRoute
compile (feedCompiler renderAtom)
-- CONFIG
root :: String
root =
"https://robertwpearce.com"
siteName :: String
siteName =
"Robert Pearce | Freelance Software Developer"
config :: Configuration
config =
defaultConfiguration
{ destinationDirectory = "docs"
, previewHost = "127.0.0.1"
, previewPort = 8000
, ignoreFile = const False
}
-- CONTEXT
feedCtx :: Context String
feedCtx =
titleCtx <>
updatedField <>
postCtx <>
bodyField "description"
postCtx :: Context String
postCtx =
constField "root" root <>
constField "siteName" siteName <>
dateField "date" "%Y-%m-%d" <>
defaultContext
titleCtx :: Context String
titleCtx =
field "title" updatedTitle
updatedField :: Context String
updatedField = field "updated" $ \i -> do
let locale = defaultTimeLocale
time <- getUpdatedUTC locale $ itemIdentifier i
return $ formatTime locale "%Y-%m-%dT%H:%M:%SZ" time
-- ripped from https://github.com/jaspervdj/hakyll/blob/c85198d8cb6ce055c788e287c7f2470eac0aad36/lib/Hakyll/Web/Template/Context.hs#L296
getUpdatedUTC :: MonadMetadata m => TimeLocale -> Identifier -> m Clock.UTCTime
getUpdatedUTC locale id' = do
metadata <- getMetadata id'
let tryField k fmt = lookupString k metadata >>= parseTime' fmt
maybe empty' return $ msum [tryField "updated" fmt | fmt <- formats]
where
empty' = fail $ "Hakyll.Web.Template.Context.getUpdatedUTC: " ++ "could not parse time for " ++ show id'
parseTime' = parseTimeM True locale
formats =
[ "%a, %d %b %Y %H:%M:%S %Z"
, "%Y-%m-%dT%H:%M:%S%Z"
, "%Y-%m-%d %H:%M:%S%Z"
, "%Y-%m-%d"
, "%B %e, %Y %l:%M %p"
, "%B %e, %Y"
, "%b %d, %Y"
]
-- TITLE HELPERS
replaceAmp :: String -> String
replaceAmp =
replaceAll "&" (const "&")
replaceTitleAmp :: Metadata -> String
replaceTitleAmp =
replaceAmp . safeTitle
safeTitle :: Metadata -> String
safeTitle =
fromMaybe "no title" . lookupString "title"
updatedTitle :: Item a -> Compiler String
updatedTitle =
fmap replaceTitleAmp . getMetadata . itemIdentifier
-- PANDOC
pandocCompilerCustom :: Compiler (Item String)
pandocCompilerCustom =
pandocCompilerWith pandocReaderOpts pandocWriterOpts
pandocExtensionsCustom :: Extensions
pandocExtensionsCustom =
githubMarkdownExtensions <>
extensionsFromList
[ Ext_auto_identifiers
, Ext_smart
]
pandocReaderOpts :: ReaderOptions
pandocReaderOpts =
defaultHakyllReaderOptions
{ readerExtensions = pandocExtensionsCustom
}
pandocWriterOpts :: WriterOptions
pandocWriterOpts =
defaultHakyllWriterOptions
{ writerExtensions = pandocExtensionsCustom
}
-- FEEDS
type FeedRenderer =
FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
feedCompiler :: FeedRenderer -> Compiler (Item String)
feedCompiler renderer =
loadAll "posts/*"
>>= recentFirst
>>= renderer feedConfiguration feedCtx
feedConfiguration :: FeedConfiguration
feedConfiguration =
FeedConfiguration
{ feedTitle = "Robert Pearce's blog"
, feedDescription = "Posts on JavaScript, Node.js, Haskell, Elm, Ruby and more."
, feedAuthorName = "Robert Pearce"
, feedAuthorEmail = "me@robertwpearce.com"
, feedRoot = root
}
-- CUSTOM ROUTE
getSlugWords :: T.Text -> [T.Text]
getSlugWords =
T.words . T.toLower . T.map f . T.replace "'" "" . T.replace "&" "and"
where
f x = if isAlphaNum x then x else ' '
getSlug :: String -> String
getSlug =
T.unpack . T.intercalate (T.singleton '-') . getSlugWords . T.pack
getTitleFromMeta :: Metadata -> String
getTitleFromMeta =
fromMaybe "no title" . lookupString "title"
fileNameFromTitle :: Metadata -> String
fileNameFromTitle =
(++ ".html") . getSlug . getTitleFromMeta
customizedRoute :: Metadata -> Routes
customizedRoute =
customRoute . const . fileNameFromTitle