~ben/web

ref: d0e53faa6acc534f8b577c831446f0bf748461ef web/Main.hs -rw-r--r-- 9.7 KiB View raw
d0e53faaBen Sima add pgp key 1 year, 1 month ago
                                                                                
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
{-# LANGUAGE OverloadedStrings #-}

module Main where

import qualified GHC.IO.Encoding as E
import Hakyll
import System.FilePath ((</>), takeBaseName, takeDirectory)
import qualified Text.Pandoc as Pandoc

main :: IO ()
main = E.setLocaleEncoding E.utf8 >> hakyll rules

rules :: Rules ()
rules = do
    match "assets/img/*" imageRules
    match "assets/fonts/*/*" fontRules
    match "assets/css/*" cssRules
    match "bsima.asc" $ route idRoute >> compile copyFileCompiler
    match "index.md" indexRules
    match "resume.md" resumeRules
    match "now.md" nowRules
    match "work.md" pageRules
    match "live.md" liveRules
    match "recordings/*" pageRules
    match "essays/*" essayRules
    match "projects/*" projectRules
    create ["projects.html"] projectsPage
    match "clog/*" clogRules
    create ["clog.html"] clogIndex
    create ["feeds/clog.xml"] $ clogFeedRules renderAtom
    create ["feeds/clog.rss"] $ clogFeedRules renderRss
    match "reading/*" readingRules
    create ["reading.html"] readingIndex
    create ["feeds/reading.xml"] $ readingFeedRules renderAtom
    create ["feeds/reading.rss"] $ readingFeedRules renderRss
    match "tea.org" teaRules
    match "templates/*" $ compile templateCompiler

type RenderFeed
     = FeedConfiguration -> Context String -> [Item String] -> Compiler (Item String)

postCtx :: Context String
postCtx = dateField "date" "%Y.%m.%d" <> defaultContext

cleanRoute :: Routes
cleanRoute = customRoute createIndexRoute
  where
    createIndexRoute ident = takeDirectory p </> takeBaseName p </> "index.html"
      where
        p = toFilePath ident

imageRules :: Rules ()
imageRules = do
    route idRoute
    compile copyFileCompiler

fontRules :: Rules ()
fontRules = do
    route idRoute
    compile copyFileCompiler

cssRules :: Rules ()
cssRules = do
    route idRoute
    compile compressCssCompiler

sass :: Compiler (Item String)
sass =
    getResourceString >>= withItemBody (unixFilter "sass" ["-s", "--scss"]) >>=
    return . fmap compressCss

indexRules :: Rules ()
indexRules = do
    route $ setExtension ".html"
    compile $ do
        projects <- recentFirst =<< loadAll "projects/*"
        clogPosts <- recentFirst =<< loadAll "clog/*"
        reading <- recentFirst =<< loadAll "reading/*"
        let ctx = listField "clogPosts" (postCtx) (return clogPosts)
                  <> listField "projects" defaultContext (return projects)
                  <> listField "reading" defaultContext (return reading)
                  <> defaultContext
        pandocCompiler >>=
            loadAndApplyTemplate "templates/index.html" ctx >>=
            loadAndApplyTemplate "templates/default.html" ctx >>=
            relativizeUrls

liveRules :: Rules ()
liveRules = do
    route cleanRoute
    compile $ do
        recordings <- recentFirst =<< loadAll "recordings/*"
        let ctx = listField "recordings" defaultContext (return recordings)
        pandocCompiler >>= loadAndApplyTemplate "templates/live.html" ctx >>=
            loadAndApplyTemplate "templates/page.html" defaultContext >>=
            loadAndApplyTemplate "templates/default.html" defaultContext >>=
            relativizeUrls

pageRules :: Rules ()
pageRules = do
    route cleanRoute
    compile $
        pandocCompiler >>=
        loadAndApplyTemplate "templates/page.html" defaultContext >>=
        loadAndApplyTemplate "templates/default.html" defaultContext >>=
        relativizeUrls

projectRules :: Rules ()
projectRules = do
    route $ setExtension "html"
    compile $
        pandocCompiler >>=
        loadAndApplyTemplate "templates/project.html" defaultContext >>=
        loadAndApplyTemplate "templates/default.html" defaultContext >>=
        relativizeUrls

projectsPage :: Rules ()
projectsPage = do
    route cleanRoute
    compile $ do
        projects <- recentFirst =<< loadAll "projects/*"
        let ctx =
                listField "projects" defaultContext (return projects) <>
                (constField "title" "Projects") <>
                defaultContext
        makeItem "" >>= loadAndApplyTemplate "templates/projects.html" ctx >>=
            loadAndApplyTemplate "templates/default.html" ctx >>=
            relativizeUrls

teaRules :: Rules ()
teaRules = do
    route cleanRoute
    compile $
        resumeCompiler >>=
        loadAndApplyTemplate "templates/page.html" defaultContext >>=
        loadAndApplyTemplate "templates/default.html" defaultContext >>=
        relativizeUrls

resumeRules :: Rules ()
resumeRules = do
    route cleanRoute
    compile $
        resumeCompiler >>=
        loadAndApplyTemplate "templates/page.html" defaultContext >>=
        loadAndApplyTemplate "templates/default.html" defaultContext >>=
        relativizeUrls

nowRules :: Rules ()
nowRules = do
    route cleanRoute
    compile $
        pandocCompiler >>=
        loadAndApplyTemplate "templates/now.html" defaultContext >>=
        loadAndApplyTemplate "templates/default.html" defaultContext >>=
        relativizeUrls

essayRules :: Rules ()
essayRules = do
    route $ setExtension "html"
    compile $
        essayCompiler >>=
        loadAndApplyTemplate "templates/essay.html" defaultContext >>=
        loadAndApplyTemplate "templates/default.html" defaultContext >>=
        relativizeUrls

clogRules :: Rules ()
clogRules = do
    route $ setExtension "html"
    compile $
        essayCompiler >>=
        loadAndApplyTemplate "templates/clog-post.html" postCtx >>=
        saveSnapshot "content" >>=
        loadAndApplyTemplate "templates/default.html" defaultContext >>=
        relativizeUrls

clogIndex :: Rules ()
clogIndex = do
    route cleanRoute
    compile $ do
        clogPosts <- recentFirst =<< loadAll "clog/*"
        projects <- recentFirst =<< loadAll "projects/*"
        let ctx =
                listField "projects" defaultContext (return projects) <>
                listField "clogPosts" postCtx (return clogPosts) <>
                (constField "title" "Ben's code log") <>
                defaultContext
        makeItem "" >>= loadAndApplyTemplate "templates/clog.html" ctx >>=
            loadAndApplyTemplate "templates/default.html" ctx >>=
            relativizeUrls

clogFeedRules :: RenderFeed -> Rules ()
clogFeedRules render = do
    route idRoute
    compile $ do
        let ctx = postCtx <> bodyField "description"
        posts <-
            fmap (take 10) . recentFirst =<< loadAllSnapshots "clog/*" "content"
        render clogFeed ctx posts

clogFeed :: FeedConfiguration
clogFeed =
    FeedConfiguration
        { feedTitle = "Ben Sima's code blog"
        , feedDescription = "(a.k.a. 'clog')"
        , feedAuthorName = "Ben Sima"
        , feedAuthorEmail = "ben@bsima.me"
        , feedRoot = "https://bsima.me/clog"
        }

readingRules :: Rules ()
readingRules = do
    route $ setExtension "html"
    compile $
        essayCompiler >>= loadAndApplyTemplate "templates/post.html" postCtx >>=
        saveSnapshot "content" >>=
        loadAndApplyTemplate "templates/default.html" defaultContext >>=
        relativizeUrls

readingIndex :: Rules ()
readingIndex = do
    route cleanRoute
    compile $ do
        posts <- recentFirst =<< loadAll "reading/*"
        let ctx =
                listField "posts" postCtx (return posts) <>
                (constField "title" "Ben's reading notes") <>
                defaultContext
        makeItem "" >>= loadAndApplyTemplate "templates/post-list.html" ctx >>=
            loadAndApplyTemplate "templates/default.html" ctx >>=
            relativizeUrls

readingFeedRules :: RenderFeed -> Rules ()
readingFeedRules render = do
    route idRoute
    compile $ do
        let ctx = postCtx <> bodyField "description"
        posts <-
            fmap (take 10) . recentFirst =<<
            loadAllSnapshots "reading/*" "content"
        render readingFeed ctx posts

readingFeed :: FeedConfiguration
readingFeed =
    FeedConfiguration
        { feedTitle = "Ben Sima's reading notes"
        , feedDescription = ""
        , feedAuthorName = "Ben Sima"
        , feedAuthorEmail = "ben@bsima.me"
        , feedRoot = "https://bsima.me/reading"
        }

defaultExtensions :: Pandoc.Extensions
defaultExtensions = Pandoc.writerExtensions defaultHakyllWriterOptions

resumeCompiler :: Compiler (Item String)
resumeCompiler =
    let writerOptions =
            defaultHakyllWriterOptions
                { Pandoc.writerExtensions =
                      Pandoc.enableExtension
                          Pandoc.Ext_grid_tables
                          defaultExtensions
                }
     in pandocCompilerWith defaultHakyllReaderOptions writerOptions

essayCompiler :: Compiler (Item String)
essayCompiler =
    let customExtensions =
            [ Pandoc.Ext_definition_lists
            , Pandoc.Ext_yaml_metadata_block
            , Pandoc.Ext_blank_before_blockquote
            , Pandoc.Ext_blank_before_header
            , Pandoc.Ext_header_attributes
            , Pandoc.Ext_auto_identifiers
            , Pandoc.Ext_backtick_code_blocks
            , Pandoc.Ext_line_blocks
            , Pandoc.Ext_fancy_lists
            , Pandoc.Ext_simple_tables
            , Pandoc.Ext_table_captions
            , Pandoc.Ext_multiline_tables
            , Pandoc.Ext_grid_tables
            , Pandoc.Ext_citations
            ]
        writerOptions =
            defaultHakyllWriterOptions
                { Pandoc.writerExtensions =
                      (Pandoc.extensionsFromList customExtensions)
                , Pandoc.writerSectionDivs = True
                , Pandoc.writerTemplate =
                      Just
                          "<div id=\"toc\">$toc$</div>\n<article id=\"content\">$body$</article>"
                , Pandoc.writerTableOfContents = True
                , Pandoc.writerTOCDepth = 2
                }
     in pandocCompilerWith defaultHakyllReaderOptions writerOptions