/
site.hs
220 lines (190 loc) · 7.09 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
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Applicative
import Control.Monad
import Data.Char (toLower)
import Data.List
import Data.Maybe
import Data.Monoid
import System.FilePath
import qualified Data.Map as M
import qualified Data.Set as S
import Text.Pandoc
import Hakyll
import Hakyll.Images (loadImage, scaleImageCompiler)
config :: Configuration
config = defaultConfiguration
feedConfiguration = FeedConfiguration
{ feedTitle = "timphilipwilliams.com"
, feedDescription = "A functional programming blog."
, feedAuthorName = "Tim Williams"
, feedAuthorEmail = "info@timphilipwilliams.com"
, feedRoot = "http://www.timphilipwilliams.com"
}
main :: IO ()
main = hakyllWith config $ do
-- Read templates
match "templates/*" $ compile templateCompiler
-- Build tags
tags <- buildTags "posts/*" (fromCapture "tags/*.html")
-- Render each post
match "posts/*" $ do
route $ setExtension ".html"
compile $ myPandocCompiler
>>= applyTemplates ["post", "default", "scaffold"] myDefaultCtx
>>= relativizeUrls
-- site index - currently a list of all my posts
create ["index.html"] $ do
route idRoute
compile $ do
items <- recentFirst =<< loadAll "posts/*"
itemTpl <- loadBody "templates/postitem.html"
list <- applyTemplateList itemTpl myDefaultCtx items
let ctx = constField "title" "Posts"
<> constField "postsurl" nullLink
<> constField "postsclass" "active"
<> constField "posts" list
<> myDefaultCtx
makeItem ""
>>= applyTemplates ["posts", "default", "scaffold"] ctx
>>= relativizeUrls
match "slides.html" $ do
route idRoute
let ctx = constField "title" "Slides"
<> constField "slidesurl" nullLink
<> constField "slidesclass" "active"
<> myDefaultCtx
compile $ getResourceBody
>>= applyTemplates ["default", "scaffold"] ctx
>>= relativizeUrls
match "projects.html" $ do
route idRoute
let ctx = constField "title" "Projects"
<> constField "projectsurl" nullLink
<> constField "projectsclass" "active"
<> myDefaultCtx
compile $ getResourceBody
>>= applyTemplates ["default", "scaffold"] ctx
>>= relativizeUrls
match "aboutme.html" $ do
route idRoute
let ctx = constField "title" "About me"
<> constField "aboutmeurl" nullLink
<> constField "aboutmeclass" "active"
<> myDefaultCtx
compile $ getResourceBody
>>= applyTemplates ["default", "scaffold"] ctx
>>= relativizeUrls
tagsRules tags $ \tag pattern -> do
route tagRoute
compile $ do
items <- recentFirst =<< loadAll pattern
itemTpl <- loadBody "templates/postitem.html"
list <- applyTemplateList itemTpl myDefaultCtx items
let ctx = constField "tag" tag
<> constField "posts" list
<> myDefaultCtx
makeItem ""
>>= applyTemplates ["postswithtag", "default", "scaffold"] ctx
>>= relativizeUrls
create ["tags.html"] $ do
route idRoute
compile $ do
cloud <- renderTagCloud 150 300 tags
let ctx = constField "title" "Tags"
<> constField "tagsurl" nullLink
<> constField "tagsclass" "active"
<> constField "tags" cloud
<> myDefaultCtx
makeItem ""
>>= applyTemplates ["tags", "default", "scaffold"] ctx
>>= relativizeUrls
match "font/*" $ do
route idRoute
compile copyFileCompiler
match "css/*.css" $ do
route idRoute
compile compressCssCompiler
match "js/*.js" $ do
route idRoute
compile copyFileCompiler
-- for the Minecraft images
match "img/minecraft/**.png" $ do
route idRoute
compile $ loadImage
>>= scaleImageCompiler 1024 768
match "img/*" $ do
route idRoute
compile copyFileCompiler
match "slides/*" $ do
route idRoute
compile copyFileCompiler
create ["rss.xml"] $ do
route idRoute
compile $ loadAll "posts/*"
>>= renderRss feedConfiguration myDefaultCtx
-- 404
-- Don't relativize URLs
match "static/404.html" $ do
route $ constRoute "404.html"
let ctx = constField "postsclass" ""
<> constField "title" "404"
<> constField "robots" "noindex, nofollow, noarchive, nocache"
<> myDefaultCtx
compile $ getResourceBody
>>= applyTemplates ["default", "scaffold"] ctx
-- | Consistent convention for links that don't go anywhere
nullLink :: String
nullLink = "javascript:void(0)"
-- | Allow for reference style links in markdown
-- and disable the leading >
pandocWriteOptions :: WriterOptions
pandocWriteOptions = defaultHakyllWriterOptions
{ writerExtensions = disableExtension Ext_literate_haskell
(writerExtensions defaultHakyllWriterOptions)
, writerReferenceLinks = True
}
pandocReadOptions :: ReaderOptions
pandocReadOptions = defaultHakyllReaderOptions
{ readerExtensions = disableExtension Ext_markdown_in_html_blocks
(readerExtensions defaultHakyllReaderOptions)
}
myPandocCompiler = pandocCompilerWith pandocReadOptions pandocWriteOptions
-- | Default setup is for individual post pages
myDefaultCtx :: Context String
myDefaultCtx = mconcat [
constField "robots" "index, follow"
, constField "homeclass" "active"
, constField "homeurl" "/index.html"
, constField "postsclass" ""
, constField "slidesclass" ""
, constField "projectsclass" ""
, constField "aboutmeclass" ""
, constField "tagsclass" ""
, constField "postsurl" "/"
, constField "slidesurl" "/slides.html"
, constField "projectsurl" "/projects.html"
, constField "aboutmeurl" "/aboutme.html"
, constField "tagsurl" "/tags.html"
, constField "author" "Tim Williams"
, dateField "date" "%e %B %Y"
, defaultContext
]
applyTemplates :: [String] -> Context String -> Item String -> Compiler (Item String)
applyTemplates names ctx = foldr apply return names
where
apply name = (loadAndApplyTemplate (templ name) ctx >=>)
templ name = fromFilePath $ "templates/" ++ name ++ ".html"
tagRoute :: Routes
tagRoute = foldl1 composeRoutes
[ setExtension ".html"
, gsubRoute "." adjustLink
, gsubRoute "/" (const "")
, gsubRoute "^tag" (const "tag/")
, gsubRoute "-html" (const "/index.html") ]
adjustLink = filter (not . isSlash) . map (toLower . replaceWithDash)
where
replaceWithDash c | c == '.' || c == ' ' = '-'
| otherwise = c
isSlash '/' = True
isSlash _ = False