/
site.hs
543 lines (484 loc) · 17.9 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
import Control.Monad ((<=<))
import Data.List (isInfixOf, isPrefixOf, nub)
import Data.Maybe (mapMaybe)
import Data.Monoid (First (..))
import qualified Data.Text as Text
import Data.Time.Clock (UTCTime, getCurrentTime)
import Data.Time.Format (defaultTimeLocale, formatTime)
-- use the "With" variants of these functions instead
import Data.Text (Text)
import Hakyll hiding (pandocCompiler, readPandoc, writePandoc)
import Network.Wai.Application.Static (StaticSettings (..))
import System.Directory (doesFileExist)
import System.FilePath (dropExtension, takeBaseName, takeExtension, (<.>), (</>))
import Text.Pandoc (Block, Inline (..), Pandoc (..), nullAttr, readHtml, runPure)
import qualified Text.Pandoc as Pandoc
import Text.Pandoc.Options
import Text.Pandoc.Walk (Walkable, query, walk)
import Text.Pandoc.Writers (writePlain)
import WaiAppStatic.Types (File (..), fromPiece, unsafeToPiece)
root :: String
root = "https://blog.ielliott.io"
-- | Serves the contents of `/path/to/x.html` when `/path/to/x` is requested.
withPrettyUrls :: Configuration -> Configuration
withPrettyUrls config =
config
{ previewSettings = \path ->
let settings = config.previewSettings path
in settings
{ ssLookupFile =
\pieces ->
case splitAt (length pieces - 1) pieces of
(prefix, [piece])
| let fileName = fromPiece piece
, takeExtension (Text.unpack fileName) == "" ->
settings.ssLookupFile $ prefix <> [unsafeToPiece $ fileName <> ".html"]
_ -> settings.ssLookupFile pieces
, ssGetMimeType =
\file ->
if takeExtension (Text.unpack (fromPiece file.fileName)) == ""
then do
htmlExists <- doesFileExist $ path </> Text.unpack (fromPiece file.fileName) <.> "html"
if htmlExists
then pure "text/html"
else settings.ssGetMimeType file
else settings.ssGetMimeType file
}
}
data BreakOn a b
= Found {prefix :: [a], target :: b, suffix :: [a]}
| Missing [a]
breakOn :: (a -> Maybe b) -> [a] -> BreakOn a b
breakOn predicate items =
case items of
[] ->
Missing []
item : items' ->
case predicate item of
Nothing ->
case breakOn predicate items' of
Found{prefix, target, suffix} ->
Found{prefix = item : prefix, target, suffix}
Missing items'' ->
Missing (item : items'')
Just item' ->
Found{prefix = [], target = item', suffix = items'}
separateBy :: (a -> Maybe b) -> [a] -> ([a], [(b, [a])])
separateBy predicate items =
case breakOn predicate items of
Missing items' ->
(items', [])
Found{prefix, target, suffix} ->
case separateBy predicate suffix of
(prefix', suffix') ->
(prefix, (target, prefix') : suffix')
main :: IO ()
main = do
now <- getCurrentTime
hakyllWith (withPrettyUrls defaultConfiguration) $ do
match "templates/*" $ compile templateBodyCompiler
match "css/*" $ do
route $ gsubRoute "css/" (const "res/")
compile compressCssCompiler
match "js/*" $ do
route $ gsubRoute "js/" (const "res/")
compile copyFileCompiler
match "fonts/*" $ do
route $ gsubRoute "fonts/" (const "res/")
compile copyFileCompiler
match "talks/*" $ do
route idRoute
compile copyFileCompiler
match "files/*" $ do
route idRoute
compile copyFileCompiler
match "posts/images/*" $ do
route $ gsubRoute "posts/" (const "")
compile copyFileCompiler
match (fromList ["favicon.ico", "CNAME"]) $ do
route idRoute
compile copyFileCompiler
create ["index.html"] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "posts/*"
metadata <-
loadAndApplyTemplate
"templates/index-metadata.html"
( constField "root" root
<> constField "url" "/"
<> constField "title" "blog.ielliott.io"
<> constField "description" "Isaac Elliott's personal blog."
)
=<< makeItem ("" :: String)
makeItem ""
>>= loadAndApplyTemplate "templates/post-list.html" (postListCtx posts Nothing)
>>= loadAndApplyTemplate
"templates/page.html"
(bodyField "body" <> constField "metadata" metadata.itemBody)
>>= prettifyUrls
match (fromList ["about.md", "resources.md", "cv.md", "talks.md", "404.md"]) $ do
route $ setExtension "html"
compile $ do
metadata <-
loadAndApplyTemplate
"templates/page-metadata.html"
(constField "root" root <> urlField "url" <> metadataField)
=<< makeItem ("" :: String)
pandocCompilerWith pandocReaderOptions pandocWriterOptions
>>= loadAndApplyTemplate
"templates/page.html"
(pageCtx <> constField "metadata" metadata.itemBody)
>>= prettifyUrls
postInfos :: [(Identifier, PostInfo)] <- getPostInfos "posts/*"
match "posts/*" $ do
route $ metadataRoute fromPermalink
compile $ do
identifier <- getUnderlying
postPandocItem <- do
document <- readPandocWith pandocReaderOptions =<< getResourceBody
pure $ linkHeaders . tableOfContents <$> document
post <- saveSnapshot "content" $ writePandocWith pandocWriterOptions postPandocItem
excerpt <- saveSnapshot "excerpt" =<< getExcerpt identifier postPandocItem
_tags <- saveSnapshot "tags" =<< makeItem =<< getTags post.itemIdentifier
let postCtx = mkPostCtx identifier postInfos
metadata <-
loadAndApplyTemplate
"templates/post-metadata.html"
( dateField "date" "%0Y-%m-%dT%H:%M%Ez"
<> constField "root" root
<> constField "excerpt" excerpt.itemBody
<> functionField
"plaintext"
( \args item -> do
case args of
[arg] ->
case runPure $ writePlain pandocWriterOptions =<< readHtml pandocReaderOptions (Text.pack arg) of
Left err -> error $ "error in function \"plaintext\" in " <> toFilePath item.itemIdentifier <> ": " <> show err
Right string -> pure . Text.unpack $ Text.strip string
_ ->
error $
"incorrect number of arguments to function \"plaintext\" in "
<> toFilePath item.itemIdentifier
)
<> postCtx
)
=<< makeItem ""
loadAndApplyTemplate "templates/post.html" postCtx post
>>= loadAndApplyTemplate "templates/page.html" (pageCtx <> constField "metadata" metadata.itemBody)
>>= prettifyUrls
tags :: [String] <- getAllTags "posts/*"
create (fmap (\tag -> fromFilePath $ "tags" </> tag <.> "html") tags) $ do
route idRoute
compile $ do
tag <- takeBaseName . toFilePath <$> getUnderlying
postsForTag <- do
posts <- loadAll "posts/*"
postsWithTags <- traverse (\post -> (,) post <$> getTags post.itemIdentifier) posts
recentFirst $ mapMaybe (\(post, postTags) -> if tag `elem` postTags then Just post else Nothing) postsWithTags
metadata <-
loadAndApplyTemplate
"templates/page-metadata.html"
( constField "root" root
<> urlField "url"
<> constField "title" ("Posts about " <> tag)
<> constField "description" ("Posts about " <> tag <> ".")
)
=<< makeItem ("" :: String)
makeItem ""
>>= loadAndApplyTemplate "templates/post-list.html" (postListCtx postsForTag $ Just tag)
>>= loadAndApplyTemplate
"templates/page.html"
( constField "title" ("Posts about " <> tag)
<> bodyField "body"
<> constField "metadata" metadata.itemBody
)
>>= prettifyUrls
create ["sitemap.xml"] $ do
route idRoute
compile $ do
singlePages <- loadAll $ fromList ["about.md", "resources.md", "404.md"]
posts <- recentFirst =<< loadAll @String "posts/*"
tagsPages <- loadAll "tags/*"
let pages = singlePages <> posts <> tagsPages
makeItem ""
>>= loadAndApplyTemplate "templates/sitemap.xml" (sitemapCtx pages)
>>= prettifyUrls
create ["feed.xml"] $ do
route idRoute
compile $ do
let feedConfiguration =
FeedConfiguration
{ feedTitle = "blog.ielliott.io"
, feedDescription = "Isaac Elliott's personal blog"
, feedAuthorName = "Isaac Elliott"
, feedAuthorEmail = "isaace71295@gmail.com"
, feedRoot = root
}
posts <- recentFirst =<< loadAllSnapshots "posts/*" "content"
atomTemplate <- loadBody "templates/atom.xml"
atomItemTemplate <- loadBody "templates/atom-item.xml"
renderAtomWithTemplates atomTemplate atomItemTemplate feedConfiguration (feedCtx now) posts
linkHeaders :: Pandoc -> Pandoc
linkHeaders =
walk @Block
( \block ->
case block of
Pandoc.Header level attr@(identifier, _classes, _attributes) content
| level > 1
, not (Text.null identifier) ->
Pandoc.Header level attr [Link nullAttr content ("#" <> identifier, "")]
_ -> block
)
tableOfContents :: Pandoc -> Pandoc
tableOfContents document =
walk @Block
( \block ->
case block of
Pandoc.Div attr@("toc", _classes, _attributes) _ ->
Pandoc.Div
attr
-- This breaks if I use `Pandoc.Header 3 nullAttr [Str "Contents"]`
-- I don't know why.
[ Pandoc.RawBlock "html" "<h3>Contents</h3>"
, contents
]
_ -> block
)
document
where
contents =
fromContents . toContents $
query @Block
( \block -> case block of
Pandoc.Header{} -> [block]
_ -> mempty
)
document
data Contents = Header
{ level :: Int
, id :: Text
, classes :: [Text]
, attrs :: [(Text, Text)]
, content :: [Inline]
, children :: [Contents]
}
deriving (Eq, Show)
toContents :: [Block] -> [Contents]
toContents = go 2
where
go :: Int -> [Block] -> [Contents]
go level =
fmap
( \((headerLevel, (identifier, classes, attrs), content), blocks) ->
let omitChildren =
case lookup "toc:omit_children" attrs of
Just value ->
case value of
"true" ->
True
"false" ->
False
_ ->
error $ "invalid contents:omit_children value: " <> Text.unpack value
Nothing ->
False
in Header
headerLevel
identifier
classes
attrs
content
(if omitChildren then [] else go (level + 1) blocks)
)
. snd
. separateBy
( \case
Pandoc.Header headerLevel attrs content
| level == headerLevel ->
Just (headerLevel, attrs, content)
_ -> Nothing
)
fromContents :: [Contents] -> Block
fromContents contents =
Pandoc.BulletList $
( \content ->
Pandoc.Plain [Link nullAttr content.content ("#" <> content.id, "")]
: case content.children of
[] -> []
_ ->
[fromContents content.children]
)
<$> contents
postInfoCtx :: Context String
postInfoCtx =
dateField "date" "%e %B, %Y"
<> prettyUrlField "url"
<> listFieldWith @String
"tags"
(field "name" (pure . (.itemBody)))
(\post -> traverse makeItem . (.itemBody) =<< loadSnapshot @[String] post.itemIdentifier "tags")
<> metadataField
mkPostCtx :: Identifier -> [(Identifier, PostInfo)] -> Context String
mkPostCtx identifier postInfos =
bodyField "body"
<> postInfoCtx
<> maybe
(boolField "has_previous" (const False) <> boolField "has_previous" (const False))
( \postInfo ->
maybe
(boolField "has_previous" (const False))
( \previousId ->
boolField "has_previous" (const True)
<> prettyUrlFromField "previous_url" previousId
<> field "previous_title" (\_ -> getMetadataField' previousId "title")
)
postInfo.previous
<> maybe
(boolField "has_next" (const False))
( \nextId ->
boolField "has_next" (const True)
<> prettyUrlFromField "next_url" nextId
<> field "next_title" (\_ -> getMetadataField' nextId "title")
)
postInfo.next
)
(lookup identifier postInfos)
pageCtx :: Context String
pageCtx =
metadataField <> bodyField "body"
postListCtx :: [Item String] -> Maybe String -> Context String
postListCtx posts mTag =
listField
"posts"
( postInfoCtx
<> field "excerpt" (\post -> (.itemBody) <$> loadSnapshot post.itemIdentifier "excerpt")
)
(return posts)
<> foldMap (constField "tag") mTag
pandocReaderOptions :: ReaderOptions
pandocReaderOptions =
defaultHakyllReaderOptions
{ readerExtensions =
-- Syntax for adding attributes to Markdown elements.
-- `tableOfContents` uses this to omit a heading's children from the contents listing.
enableExtension Ext_header_attributes
.
-- Uses `Div` blocks for `<div>` tags so that I can post-process them.
-- See `tableOfContents` for an example.
enableExtension Ext_native_divs
. enableExtension Ext_backtick_code_blocks
. enableExtension Ext_markdown_in_html_blocks
. enableExtension Ext_auto_identifiers
. enableExtension Ext_gfm_auto_identifiers
$ getDefaultExtensions "commonmark_x"
}
pandocWriterOptions :: WriterOptions
pandocWriterOptions =
defaultHakyllWriterOptions
{ writerExtensions = enableExtension Ext_tex_math_dollars defaultHakyllWriterOptions.writerExtensions
, writerHTMLMathMethod = MathML
}
prettyUrl :: String -> String
prettyUrl url =
if isLocal && takeExtension url == ".html"
then dropExtension url
else url
where
isLocal = root `isPrefixOf` url || not ("://" `isInfixOf` url)
prettyUrlField :: String -> Context a
prettyUrlField key =
field key $ \item ->
maybe
(fail $ "no route url found for item " ++ show item.itemIdentifier)
(toUrl . prettyUrl)
<$> getRoute item.itemIdentifier
prettyUrlFromField :: String -> Identifier -> Context a
prettyUrlFromField key identifier =
field key $ \item ->
maybe
(fail $ "no route url found for item " ++ show item.itemIdentifier)
(toUrl . prettyUrl)
<$> getRoute identifier
prettifyUrls :: Item String -> Compiler (Item String)
prettifyUrls =
pure . fmap (withUrls prettyUrl)
fromPermalink :: Metadata -> Routes
fromPermalink metadata =
case lookupString "permalink" metadata of
Nothing ->
idRoute <> setExtension "html"
Just permalink ->
constRoute $
( case permalink of
'/' : permalink' -> permalink'
_ -> permalink
)
<> ".html"
data PostInfo = PostInfo {previous :: Maybe Identifier, next :: Maybe Identifier}
getPostInfos :: Pattern -> Rules [(Identifier, PostInfo)]
getPostInfos postsPattern =
fmap
( \postIds ->
zipWith3
(\postId previous next -> (postId, PostInfo{previous, next}))
postIds
(Nothing : fmap Just postIds)
(fmap Just (drop 1 postIds) <> [Nothing])
)
. sortChronological
=<< getMatches postsPattern
getExcerpt :: Identifier -> Item Pandoc -> Compiler (Item String)
getExcerpt identifier postPandocItem = do
mMetadataExcerpt <- getMetadataField identifier "excerpt"
case mMetadataExcerpt of
Just excerpt ->
makeItem excerpt
Nothing -> do
excerptPandocItem <-
traverse
( maybe
( do
path <- getResourceFilePath
fail $ "failed to extract excerpt from " <> path
)
(\block -> pure $ Pandoc Pandoc.nullMeta [removeFootnotes block])
. getFirst
. query
( \block -> case block of
Pandoc.Para{} -> First $ Just block
_ -> mempty
)
)
postPandocItem
pure $ writePandocWith pandocWriterOptions excerptPandocItem
where
removeFootnotes :: Walkable [Inline] b => b -> b
removeFootnotes = walk @[Inline] (filter (\case; Note{} -> False; _ -> True))
getAllTags :: Pattern -> Rules [String]
getAllTags =
fmap (nub . concat) . traverse getTags <=< getMatches
sitemapCtx :: [Item String] -> Context String
sitemapCtx pages =
constField "root" root
<> listField
"pages"
( dateField "date" "%0Y-%m-%dT%H:%M%Ez"
<> prettyUrlField "url"
<> constField "root" root
)
(return pages)
feedCtx :: UTCTime -> Context String
feedCtx now =
metadataField
<> prettyUrlField "url"
<> field "description" (\item -> (.itemBody) <$> loadSnapshot @String item.itemIdentifier "excerpt")
<> constField "updated" (formatTime defaultTimeLocale "%0Y-%m-%dT%H:%M%Ez" now)
<> bodyField "body"