-
Notifications
You must be signed in to change notification settings - Fork 3
/
site.hs
290 lines (225 loc) · 9.33 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
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Control.Monad
import Data.List
import Data.Monoid
import Data.String
import Data.Version
import Hakyll
import System.Environment
import System.FilePath
import Text.Pandoc
import Text.HTML.TagSoup
------------- Configuration ---------------------------------
websiteSrc :: String -- source from which the website is built
websiteSrc = "src"
rsyncPath :: String -- where to rsync the generated site
rsyncPath = "[email protected]:"
++ "/homepages/local/admissions/admissions/"
dateFormat :: String
dateFormat = "%B %e, %Y (%A)"
maxAnnouncements :: Int
maxAnnouncements = 10
-- The version of the twitter bootstrap that is used. When you use a
-- new version, make sure to change this.
bootstrapVersion :: Version
bootstrapVersion = Version [3, 2, 0] []
-- The version of font awesome that is used. When you use a new
-- version, make sure to change this.
fontAwesomeVersion :: Version
fontAwesomeVersion = Version [4, 3, 0] []
-- The configuration of feeds.
feedConfig :: FeedConfiguration
feedConfig = FeedConfiguration
{ feedTitle = "Admissions at CSE.IITK"
, feedDescription = "PG admissions at Dept. CSE, IIT Kanpur"
, feedAuthorName = "Admissions"
, feedAuthorEmail = "[email protected]"
, feedRoot = "http://cse.iitk.ac.in/users/admissions"
}
-------------- Rules for building ------------------------------
bootstrapPat :: Pattern
bootstrapPat = fromString $ "bootstrap-"
++ showVersion bootstrapVersion
</> "**"
fontAwesomePat :: Pattern
fontAwesomePat = fromString $ "font-awesome-"
++ showVersion fontAwesomeVersion
</> "**"
rules :: Rules ()
rules = do
-- Get the bootstrap stuff on.
match bootstrapPat $ do
route idRoute
compile copyFileCompiler
-- Font Awesome stuff
match fontAwesomePat $ do
route idRoute
compile copyFileCompiler
match "letters/*.pdf" $ do
route idRoute
compile copyFileCompiler
match "images/*" $ do
route idRoute
compile copyFileCompiler
-- Templates
match "templates/*" $ compile templateCompiler
-- Announcements. This is essentially a blog.
match announcePat $ do
route $ beautifulRoute
compilePipeline announcePage
-- Create atom feeds for announcements.
create ["atom.xml"] $ do
route idRoute
compile announceFeeds
dateTags <- buildTagsWith (\ ident -> return [getYear ident])
announcePat
$ fromCapture "announcements/archive/*.html"
match "announcements.html" $ do
deps <- makePatternDependency announcePat
rulesExtraDependencies [deps] $ do
route idRoute
compilePipeline $ allAnnounce $ sortTagsBy sortYear dateTags
match "index.md" $ do
route $ setExtension "html"
compilePipeline indexPage
match "**/index.md" $ do
route $ setExtension "html"
compilePipeline page
match "faq.md" $ do
route $ setExtension "html"
compilePipeline $ faqPandoc >=> postPandoc defaultContext
--------------- Compilers and contexts --------------------------
type Pipeline a b = Item a -> Compiler (Item b)
-- | Similar to compile but takes a compiler pipeline instead.
compilePipeline :: Pipeline String String -> Rules ()
compilePipeline pipeline = compile $ getResourceBody >>= pipeline
-- Better named routes for announcements.
beautifulRoute :: Routes
beautifulRoute = customRoute $ \ ident -> dropExtension (toFilePath ident)
</> "index.html"
-- | Convert foo/index.html to foo/
cleanUrl :: Item String -> Compiler (Item String)
cleanUrl = return . fmap (withUrls rmIndex)
where rmIndex url
| isInfixOf "://" url = url
| otherwise = case splitFileName url of
(dir, "index.html") -> dir
_ -> url
-- | Add some bootstrap classes to the tables.
styleTable :: Item String -> Compiler (Item String)
styleTable = return . fmap (withTags mapper)
where mapper (TagOpen "table" attrs) = TagOpen "table"
$ extraAttr : attrs
mapper x = x
extraAttr = ("class", tableClasses)
tableClasses = unwords [ "table"
, "table-striped"
, "table-hover"
, "table-bordered"
, "table-condensed"
]
defaultTemplates :: [Identifier]
defaultTemplates = [ "templates/layout.html"
, "templates/wrapper.html"
]
pandoc :: Pipeline String String
pandoc = pandocWith readPandoc writePandoc
faqPandoc :: Pipeline String String
faqPandoc = pandocWith readPandoc $ writePandocWith opts
where opts = defaultHakyllWriterOptions
{ writerTableOfContents = True
, writerStandalone = True
, writerTOCDepth = 4
, writerNumberSections = True
, writerTemplate = "$toc$\n<hr/>$body$"
}
-- Pandoc with a given reader and writer functions.
pandocWith :: (Item String -> Item Pandoc) -- the reader
-> (Item Pandoc -> Item String) -- The writer
-> Pipeline String String
pandocWith reader writer = return . reader >=> return . writer
postPandoc :: Context String -> Pipeline String String
postPandoc cxt = applyTemplates cxt defaultTemplates
>=> styleTable >=> cleanUrl >=> relativizeUrls
applyTemplates :: Context String
-> [Identifier]
-> Pipeline String String
applyTemplates cxt = foldr1 (>=>) . map apt
where apt = flip loadAndApplyTemplate cxt
--------------- Index page ----------------------------------
indexPage :: Pipeline String String
indexPage = applyAsTemplate indexContext
>=> pandoc
>=> postPandoc indexContext
page :: Pipeline String String
page = pandoc >=> postPandoc defaultContext
indexContext :: Context String
indexContext = defaultContext
<> listField "announcements" announceContext announceIndex
<> totalAnnounce "announcecount"
where announceIndex = loadAll announcePat
>>= fmap (take maxAnnouncements) . recentFirst
--------------- Compilers and routes for announcements --------
announcePat :: Pattern
announcePat = "announcements/*.md"
announcePage :: Pipeline String String
announcePage = pandoc
>=> saveSnapshot "content"
>=> postPandoc announceContext
announceContext :: Context String
announceContext = defaultContext <> dateField "date" dateFormat
<> dateField "month" "%b"
<> dateField "year" "%Y"
<> dateField "day" "%a"
<> dateField "dayofmonth" "%e"
<> teaserField "teaser" "content"
announceFeedContext :: Context String
announceFeedContext = announceContext <> bodyField "description"
-- | Generating feeds.
announceFeeds :: Compiler (Item String)
announceFeeds = loadAllSnapshots announcePat "content"
>>= recentFirst
>>= renderAtom feedConfig feedContext
>>= relativizeUrls
where feedContext = announceContext <> bodyField "description"
------------------- All announcements.
announceCount :: Compiler String
announceCount = fmap (show . length) $ getMatches announcePat
totalAnnounce :: String -> Context String
totalAnnounce nm = field nm (const announceCount)
allAnnounce :: Tags -> Pipeline String String
allAnnounce tags = applyAsTemplate allContext
>=> postPandoc indexContext
where allContext = defaultContext
<> listField "yearly" announceContext
announceIndex
announceIndex = yearTagsCompiler tags
-- | This function generates the year of the post from its
-- identifier. This is used in building the archives.
getYear :: Identifier -> String
getYear = takeWhile (/= '-') . takeFileName . toFilePath
-- | Sorting year tags with
sortYear :: (String,a) -> (String, a) -> Ordering
sortYear (y',_) (y,_) = compare (read y :: Int) $ read y'
yearTagsCompiler :: Tags -> Compiler [Item String]
yearTagsCompiler tags =
sequence [ yearTagRender y ids | (y,ids) <- tagsMap tags ]
yearTagRender :: String -> [Identifier] -> Compiler (Item String)
yearTagRender y idents = makeItem ("" :: String)
>>= loadAndApplyTemplate "templates/year.html"
context
where context = constField "year" y
<> constField "count" (show $ length idents)
<> listField "announcements" announceContext annIndex
annIndex = loadAll (fromList idents) >>= recentFirst
--------------- Main and sundry ---------------------------------
main :: IO ()
main = do conf <- config
hakyllWith conf rules
config :: IO Configuration
config = fromEnv <$> getEnvironment
where fromEnv = maybe siteConf setRsyncUrl . lookup "SITE_RSYNC_URL"
siteConf = defaultConfiguration { providerDirectory = websiteSrc }
setRsyncUrl u = siteConf { deployCommand = rsync u }
rsync dest = "rsync -avvz _site/ " ++ dest