-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.hs
275 lines (233 loc) · 9.17 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
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad (forM_, forM, liftM)
import Data.Monoid (mappend, mconcat)
import Data.Maybe
import qualified Data.Map as M
import Hakyll
import Data.List (isSuffixOf, isPrefixOf, isInfixOf,
intercalate, sort)
import System.FilePath.Posix (takeBaseName, takeDirectory,
(</>), takeFileName)
import Text.Blaze.Html (toHtml, toValue, (!))
import Text.Blaze.Html.Renderer.String (renderHtml)
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A
--------------------------------------------------------------------------------
host :: String
host = "http://www.rohanjain.in"
siteName :: String
siteName = "The Perpetual Amateur"
sourceRepository :: String
sourceRepository = "https://github.com/crodjer/rohanjain.in"
myFeedConfiguration :: FeedConfiguration
myFeedConfiguration = FeedConfiguration
{ feedTitle = siteName
, feedDescription = ""
, feedAuthorName = "Rohan Jain"
, feedAuthorEmail = "crodjer@gmail.com"
, feedRoot = host
}
copyFiles :: [Pattern]
copyFiles = [ "static/img/*"
, "static/js/*"
, "static/html/404.html"
, "CNAME"
, "robots.txt"
, "favicon.ico"
, ".htaccess"
]
config :: Configuration
config = defaultConfiguration
{ deployCommand = "./scripts/deploy-cmd.sh"
, ignoreFile = ignoreFile'
}
where
ignoreFile' path
| "~" `isPrefixOf` fileName = True
| ".#" `isPrefixOf` fileName = True
| "~" `isSuffixOf` fileName = True
| ".swp" `isSuffixOf` fileName = True
| ".git" `isInfixOf` path = True
| "_cache" `isInfixOf` path = True
| otherwise = False
where
fileName = takeFileName path
main :: IO ()
main = hakyllWith config site
site :: Rules ()
site = do
forM_ copyFiles $ \pattern->
match pattern $ do
route idRoute
compile copyFileCompiler
match "static/css/*" $ do
route idRoute
compile compressCssCompiler
tags <- buildTags "posts/*/*" (fromCapture "tags/*.html")
years <- buildYears "posts/*/*"
let draftCtx = mconcat [ postSlugField "slug"
, postYearField "year"
, pageCtx ]
let postCtx = mconcat [ tagsField "tags" tags
, draftCtx]
match "drafts/*" (postRules $ draftCtx)
match "posts/*/*" (postRules $ postCtx)
match "pages/*" $ do
route $ cleanRoute `composeRoutes` (gsubRoute "pages/" (const ""))
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/page.html" pageCtx
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/default.html" pageCtx
>>= relativizeUrls
>>= cleanIndexUrls
create ["index.html"] $ do
route idRoute
compile $ do
posts <- fmap (take 7) . recentFirst =<< loadAll "posts/*/*"
let indexCtx = mconcat
[ listField "posts" postCtx (return posts)
, constField "title" "Recent posts"
, field "tags" (\_ -> renderTagCloud 100 300 tags)
, field "years" (\_ -> renderYears years)
, myCtx
]
makeItem ""
>>= loadAndApplyTemplate "templates/index.html" indexCtx
>>= loadAndApplyTemplate "templates/default.html" indexCtx
>>= relativizeUrls
>>= cleanIndexUrls
forM_ years $ \(year, _)->
create [yearId year] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll (fromGlob $ "posts/" ++ year ++"/*")
let postsCtx = mconcat
[ listField "posts" postCtx (return posts)
, constField "title" ("Posts published in " ++ year)
, field "years" (\_ -> renderYears years)
, myCtx
]
makeItem ""
>>= loadAndApplyTemplate "templates/posts.html" postsCtx
>>= loadAndApplyTemplate "templates/default.html" postsCtx
>>= relativizeUrls
>>= cleanIndexUrls
tagsRules tags $ \tag pattern -> do
-- Copied from posts, need to refactor
route cleanRoute
compile $ do
posts <- recentFirst =<< loadAll pattern
let postsCtx = mconcat
[ listField "posts" postCtx (return posts)
, constField "title" ("Posts tagged " ++ tag)
, field "years" (\_ -> renderYears years)
, myCtx
]
makeItem ""
>>= loadAndApplyTemplate "templates/posts.html" postsCtx
>>= loadAndApplyTemplate "templates/default.html" postsCtx
>>= relativizeUrls
>>= cleanIndexUrls
create ["sitemap.xml"] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "posts/*/*"
pages <- loadAll "pages/*"
let allPosts = (return (pages ++ posts))
let sitemapCtx = mconcat
[ listField "entries" pageCtx allPosts
, constField "host" host
, myCtx
]
makeItem ""
>>= loadAndApplyTemplate "templates/sitemap.xml" sitemapCtx
>>= cleanIndexHtmls
create ["feed.xml"] $ do
route idRoute
compile $ do
let feedCtx = pageCtx `mappend` bodyField "description"
posts <- fmap (take 10) . recentFirst =<<
loadAllSnapshots "posts/*/*" "content"
renderAtom myFeedConfiguration feedCtx posts
>>= cleanIndexHtmls
match "templates/*" $ compile templateCompiler
--------------------------------------------------------------------------------
myCtx :: Context String
myCtx = mconcat
[ constField "superTitle" siteName
, defaultContext
]
pageCtx :: Context String
pageCtx = mconcat
[ modificationTimeField "mtime" "%U"
, modificationTimeField "lastmod" "%Y-%m-%d"
, dateField "updated" "%Y-%m-%dT%H:%M:%SZ"
, constField "host" host
, constField "source" sourceRepository
, dateField "date" "%B %e, %Y"
, myCtx
]
postRules :: Context String -> Rules ()
postRules ctx = do
route $ postCleanRoute
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/post-content.html" ctx
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/post.html" ctx
>>= loadAndApplyTemplate "templates/default.html" ctx
>>= relativizeUrls
>>= cleanIndexUrls
-- custom routes
--------------------------------------------------------------------------------
postCleanRoute :: Routes
postCleanRoute = cleanRoute
`composeRoutes` (gsubRoute "(posts/[0-9]{4}/|drafts/)" (const ""))
cleanRoute :: Routes
cleanRoute = customRoute createIndexRoute
where
createIndexRoute ident = takeDirectory p </> takeBaseName p </> "index.html"
where p = toFilePath ident
cleanIndexUrls :: Item String -> Compiler (Item String)
cleanIndexUrls = return . fmap (withUrls cleanIndex)
cleanIndexHtmls :: Item String -> Compiler (Item String)
cleanIndexHtmls = return . fmap (replaceAll pattern replacement)
where
pattern = "/index.html"
replacement = const "/"
cleanIndex :: String -> String
cleanIndex url
| idx `isSuffixOf` url = take (length url - length idx) url
| otherwise = url
where idx = "index.html"
-- utils
--------------------------------------------------------------------------------
type Year = String
buildYears :: MonadMetadata m => Pattern -> m [(Year, Int)]
buildYears pattern = do
ids <- getMatches pattern
return . frequency . (map getYear) $ ids
where
frequency xs = M.toList (M.fromListWith (+) [(x, 1) | x <- xs])
postSlugField :: String -> Context a
postSlugField key = field key $ return . baseName
where baseName = takeBaseName . toFilePath . itemIdentifier
postYearField :: String -> Context a
postYearField key = field key $ return . getYear . itemIdentifier
getYear :: Identifier -> Year
getYear = takeBaseName . takeDirectory . toFilePath
yearPath :: Year -> FilePath
yearPath year = "archive/" ++ year ++ "/index.html"
yearId :: Year -> Identifier
yearId = fromFilePath . yearPath
renderYears :: [(Year, Int)] -> Compiler String
renderYears years = do
years' <- forM (reverse . sort $ years) $ \(year, count) -> do
route' <- getRoute $ yearId year
return (year, route', count)
return . intercalate ", " $ map makeLink years'
where
makeLink (year, route', count) =
(renderHtml (H.a ! A.href (yearUrl year) $ toHtml year)) ++
" (" ++ show count ++ ")"
yearUrl = toValue . toUrl . yearPath