-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blog.hs
320 lines (270 loc) · 10.1 KB
/
Blog.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
{-# LANGUAGE CPP #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
module Main
( main
) where
import Blog.Config
import Blog.Pandoc
import Blog.Slug
import Blog.Utils
import Control.Monad
import Data.Aeson qualified as Aeson
import Data.ByteString.Lazy.Char8 qualified as B
import Data.Foldable
import Data.Map.Strict qualified as Map
import Data.Maybe (fromMaybe)
import Data.Set qualified as Set
import Hakyll hiding (dateField, defaultContext,
pandocCompiler)
import Hakyll.Core.Compiler.Internal
import Hakyll.Core.Provider
import System.Directory (doesDirectoryExist, listDirectory,
makeAbsolute)
import System.FilePath (joinPath, takeFileName, (</>))
import System.Process (readProcess)
import Text.RawString.QQ
#if defined(PREVIEW_SERVER)
import Data.Text qualified as T
import Network.HTTP.Types.Status (status404)
import Network.Wai qualified as W
import System.Directory (doesFileExist)
import System.FilePath (hasExtension, (<.>))
import WaiAppStatic.Types
#endif
hakyllConfig :: Configuration
hakyllConfig = defaultConfiguration
#if defined(PREVIEW_SERVER)
-- https://github.com/LightAndLight/lightandlight.github.io/blob/a29bac1b084b86abe43e28c4062ca963d0647b98/site.hs#L31-L55
{ previewSettings = \path ->
let s@StaticSettings{..} = config.previewSettings path in s
{ ss404Handler = Just \_ respond ->
B.readFile (config.destinationDirectory </> "404.html")
>>= respond . W.responseLBS status404 []
, ssLookupFile = \pieces ->
case splitAt (length pieces - 1) pieces of
(prefix, [fromPiece -> fileName])
| nullExtension (T.unpack fileName) ->
ssLookupFile $ prefix <> [unsafeToPiece $ fileName <> ".html"]
_ -> ssLookupFile pieces
, ssGetMimeType = \file ->
let fileName = T.unpack $ fromPiece file.fileName in
if nullExtension fileName then do
htmlExists <- doesFileExist $ path </> fileName <.> "html"
if htmlExists then pure "text/html" else ssGetMimeType file
else ssGetMimeType file
}
}
where
nullExtension :: FilePath -> Bool
nullExtension = not . hasExtension
config :: Configuration
config = defaultConfiguration
#endif
feedConfig :: FeedConfiguration
feedConfig = FeedConfiguration
{ feedTitle = siteTitle
, feedDescription = author ++ "'s Blog"
, feedAuthorName = author
, feedAuthorEmail = email
, feedRoot = siteURL
}
jsonTagsCtx :: Context a
jsonTagsCtx = field "tags" \(Item id _) -> do
meta <- getMetadata id
pure . B.unpack . Aeson.encode . fromMaybe [] $ lookupStringList "tags" meta
data PostList
= Tag !String
| Only !Int
| All
draftsPattern :: Pattern
draftsPattern = foldr1 (.||.)
[ "drafts/*.md"
, "drafts/*.lhs"
-- , "drafts/*.html"
, "drafts/*/*.md"
, "drafts/*/*.lhs"
-- , "drafts/*/*.html"
]
postsPattern :: Pattern
postsPattern = foldr1 (.||.)
[ "posts/*.md"
, "posts/*.lhs"
-- , "posts/*.html"
, "posts/*/*.md"
, "posts/*/*.lhs"
-- , "posts/*/*.html"
]
resumePattern :: Pattern
resumePattern = "resume/*.tex" .||. "resume/*.sty"
tagPageEntry :: String
tagPageEntry = [r|
<h2 id="$tag$"><a href="#$tag$">#$tag$</a></h2>
$partial("templates/post-list.html")$
|]
staticFiles :: Pattern
-- staticFiles = foldr1 (.||.) ["fonts/**"]
staticFiles = "fonts/**"
bibFiles :: Pattern
bibFiles = foldr1 (.||.)
[ "bib/*.bibtex"
, "bib/*.bib"
, "bib/*.json"
, "bib/*.yaml"
]
getBibInfo :: Compiler BibInfo
getBibInfo = do
id <- getUnderlying
cslFile <- fromMaybe defaultCSLFile <$> getMetadataField id "csl"
csl <- load (fromFilePath cslFile)
BibInfo csl <$> loadAll bibFiles
pandocBib :: Compiler (Item String)
pandocBib = getBibInfo >>= pandocCompiler'
getPostSlug :: Metadata -> String
getPostSlug meta =
case lookupString "title" meta of
Nothing -> error "found post with no title"
Just (titleSlug -> title) ->
fromMaybe title $ lookupString "slug" meta
main :: IO ()
main = do
hakyllArgs <- defaultParser hakyllConfig
let
isWatch :: Bool
isWatch =
case optCommand hakyllArgs of
Watch {} -> True; _ -> False
allPostsPattern :: Pattern
allPostsPattern
| isWatch = postsPattern .||. draftsPattern
| otherwise = postsPattern
postList :: PostList -> Compiler [Item String]
postList p = do
posts <- recentFirst =<< loadAll allPostsPattern
case p of
All -> pure posts
Only n -> pure $ take n posts
Tag tag ->
filterM (\(Item id _) -> elem tag <$> getTags id) posts
createFeed name feedT itemT f g ctx =
create [name] $ version "generated" do
route idRoute
compile do
posts <- recentFirst =<< loadAllSnapshots allPostsPattern "content"
feedT <- loadBody . fromFilePath $ "templates" </> feedT
itemT <- loadBody . fromFilePath $ "templates" </> itemT
let feedCtx = fold
[ ctx
, field "description" $ return . g . itemBody
, modificationTimeField "updated" "%Y-%m-%dT%H:%M:%S%Ez"
, postCtx
]
f feedT itemT feedConfig feedCtx posts
hakyllWithArgs hakyllConfig hakyllArgs do
match "templates/*" $
compile templateBodyCompiler
match bibFiles $ compile biblioCompiler
match "bib/*.csl" $ compile cslCompiler
match ("css/*.css" .&&. complement ("css/default.css" .||. "css/noscript.css")) $
compile templateBodyCompiler
-- NOTE: even though these are not in the final site (no `route`),
-- we need to use `copyFileCompiler` so they are in the store and
-- dependency tracking works correctly
match resumePattern do
compile copyFileCompiler
resumeDeps <- makePatternDependency resumePattern
rulesExtraDependencies [resumeDeps] do
create ["static/resume.pdf"] do
route idRoute
when isWatch do
compile $ do
_ <- unsafeCompiler $ readProcess "latexmk" [] []
makeItem ()
-- copy post subfiles and source files
getAllMetadata allPostsPattern >>=
mapM_ \(id, getPostSlug -> slug) -> do
let src = "static" </> slug
files <- preprocess $
doesDirectoryExist src >>= \case
True ->
fmap (fromFilePath . (src </>))
<$> listDirectory src
False -> pure []
unless (null files) $ match (fromList files) $ do
route $ gsubRoute "static" (const "posts")
compile copyFileCompiler
filePath <- preprocess . makeAbsolute $ joinPath
["_site" , "posts" , slug
, takeFileName (toFilePath id)
]
create [fromFilePath filePath] $ do
route idRoute
compile $ do
provider <- compilerProvider <$> compilerAsk
makeItem . CopyFile $ resourceFilePath provider id
match staticFiles do
route idRoute
compile copyFileCompiler
match "css/noscript.css" do
route idRoute
compile compressCssCompiler
match "css/default.css" do
route idRoute
compile $
getResourceString >>= (fmap (fmap compressCss) . applyAsTemplate mempty)
match (fromList ["pages/404.md", "pages/bibliography.md"]) do
route $ basename `composeRoutes` setExtension "html"
compile $ do
bib <- getBibInfo
pandocCompilerRaw bib pure defaultPostPass
>>= loadAndApplyTemplate "templates/default.html" defaultContext
match allPostsPattern do
route $ metadataRoute \meta ->
constRoute $ joinPath ["posts", getPostSlug meta, "index.html"]
compile $ pandocBib
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/post.html" postCtx
>>= loadAndApplyTemplate "templates/default.html" postCtx
>>= relativizeUrls
draftsDep <- makePatternDependency draftsPattern
rulesExtraDependencies [draftsDep] do
match "pages/index.md" do
route $ basename `composeRoutes` setExtension "html"
let indexCtx = listField "posts" postCtx (postList (Only 5))
<> defaultContext
compile $ pandocCompilerRaw NoBib
(applyAsTemplate indexCtx) defaultPostPass
>>= loadAndApplyTemplate "templates/default.html" indexCtx
>>= relativizeUrls
create ["posts/index.html"] $ version "generated" do
route idRoute
compile do
let postListCtx = fold
[ listField "posts" postCtx (postList All)
, constField "title" "Posts"
, defaultContext
]
makeItem [r|$partial("templates/post-list.html")$|]
>>= applyAsTemplate postListCtx
>>= loadAndApplyTemplate "templates/default.html" postListCtx
>>= relativizeUrls
create ["tags.html"] $ version "generated" do
postTags <-
fmap (maybe Set.empty Set.fromList . lookupStringList "tags")
. Map.fromList <$> getAllMetadata allPostsPattern
route idRoute
compile do
let postListCtx tag = constField "tag" tag
<> listField "posts" postCtx (postList (Tag tag))
pageCtx = constField "title" "Tags" <> defaultContext
uItem <- Item <$> getUnderlying
-- NOTE: foldrM to avoid quadratic slowdown
page <- foldrM `flip` [] `flip` fold postTags $ \tag acc -> do
Item _ item <- applyAsTemplate (postListCtx tag) $ uItem tagPageEntry
pure $ item ++ acc
loadAndApplyTemplate "templates/default.html" pageCtx (uItem page)
>>= relativizeUrls
createFeed "feed.xml" "atom.xml" "atom-item.xml"
renderAtomWithTemplates escapeHtml mempty
createFeed "feed.json" "feed.json" "feed-item.json"
renderJsonWithTemplates id jsonTagsCtx