This repository has been archived by the owner on Aug 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsite.hs
373 lines (339 loc) · 16.1 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
import Data.Monoid (mappend)
import Hakyll
import Data.Time.Clock (UTCTime (..))
import Data.Time.Locale.Compat (defaultTimeLocale)
import Control.Monad
import Control.Arrow
import Control.Applicative
import Data.Maybe
import Data.List
import Data.List.Split
import Data.Ord
import Data.Function
import qualified Data.Map as M
import qualified Data.Set as S
import Text.Pandoc.Shared (headerShift)
import Text.Pandoc.Options
import Data.Time.Format
import Network.HTTP.Types.URI
import qualified Data.ByteString.UTF8 as BS_U8
--------------------------------------------------------------------------------
newtype Year = Year Int deriving (Eq, Ord)
data Quarter = Fall Year | Winter Year | Spring Year deriving (Eq)
instance Ord Quarter where
(Fall y1) `compare` (Fall y2) = y1 `compare` y2
(Fall y1) `compare` (Winter y2) = if y1 < y2 then LT else GT
(Fall y1) `compare` (Spring y2) = if y1 < y2 then LT else GT
(Winter y1) `compare` (Fall y2) = if y1 <= y2 then LT else GT
(Winter y1) `compare` (Winter y2) = y1 `compare` y2
(Winter y1) `compare` (Spring y2) = if y1 <= y2 then LT else GT
(Spring y1) `compare` (Fall y2) = if y1 <= y2 then LT else GT
(Spring y1) `compare` (Winter y2) = if y1 < y2 then LT else GT
(Spring y1) `compare` (Spring y2) = y1 `compare` y2
toQuarter :: String -> Quarter
toQuarter ('f':'a':xs) = Fall (Year $ read xs)
toQuarter ('w':'i':xs) = Winter (Year $ read xs)
toQuarter ('s':'p':xs) = Spring (Year $ read xs)
fromQuarter :: Quarter -> String
fromQuarter (Fall (Year y)) = "fa" ++ show y
fromQuarter (Winter (Year y)) = "wi" ++ show y
fromQuarter (Spring (Year y)) = "sp" ++ show y
rawHTMLPandocCompiler :: Compiler (Item String)
rawHTMLPandocCompiler =
let customExtensions = [Ext_raw_html]
newExtensions = S.fromList customExtensions
readerOptions = defaultHakyllReaderOptions {readerExtensions = newExtensions}
writerOptions = defaultHakyllWriterOptions {
writerExtensions = newExtensions
}
in pandocCompilerWith readerOptions writerOptions
data CalEvt = CalEvt { calEvtName :: String
, calEvtStart :: UTCTime
, calEvtEnd :: UTCTime
} deriving (Show)
main :: IO ()
main = hakyll $ do
match "static/**" $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
match "events/past" $ do
-- INTENTIONAL: Do not route. Just using this to allow loading
-- when deciding whether or not events are upcoming (below).
compile getResourceBody
match "events/*/*" $ do
route $ setExtension "html"
let pandocCompilerLevelShift = pandocCompilerWithTransform
defaultHakyllReaderOptions
defaultHakyllWriterOptions
(headerShift 1)
compile $ pandocCompilerLevelShift
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/event.html" eventCtx
>>= loadAndApplyTemplate "templates/default.html" eventCtx
>>= relativizeUrls
pag <- buildPaginateWith quarterGrouper "events/*/*" eventPageId
paginateRules pag $ \num pat -> do
let q num = fromQuarter
. getQuarter
. head
. fromJust
. M.lookup num
$ paginateMap pag
let thisQ = q num
route $ customRoute $ \_ -> "events-" ++ thisQ ++ ".html"
let allQs = reverse
. M.elems
. M.mapWithKey (curry $ (id *** getQuarter . head))
$ paginateMap pag
let allQItems = sequence $ map makeItem allQs
compile $ do
events <- recentFirst' =<< loadAll pat
let pageCtx =
boolField "isCurrent" ((num==) . fst . itemBody) `mappend`
field "qa" (return . fromQuarter . snd . itemBody) `mappend`
field "num" (return . show . fst . itemBody) `mappend`
field "url" ( fmap fromJust
. getRoute
. eventPageId
. fst
. itemBody
)
let eventsCtx =
constField "title" ("Events List - " ++ thisQ) `mappend`
extraCss ["/css/paginate.css"] `mappend`
listField "pages" pageCtx allQItems `mappend`
listField "events" eventCtx (return events) `mappend`
paginateContext pag num `mappend`
defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/event-paginate.html" eventsCtx
>>= loadAndApplyTemplate "templates/default.html" eventsCtx
>>= relativizeUrls
create ["events.html"] $ do
route idRoute
compile $ do
-- Figure out which quarter to do by looking at the next
-- upcoming event and then reverse-looking-up.
nextEvent <- nextNEvents 1 =<< loadAll @String "events/*/*"
-- If no future events, fall back to the "last" events page
let event = fromJust ( itemIdentifier <$> listToMaybe nextEvent
<|> head . fst <$> M.minView (paginateMap pag)
)
let q = getQuarter event
let n = fst . M.findMin . M.filter ((q==) . snd)
$ M.map ((id &&& getQuarter . head)) (paginateMap pag)
loadBody (eventPageId n) >>= makeItem :: Compiler (Item String)
create ["events/next"] $ do
route idRoute
compile $ do
nextEvent <- nextNEvents 1 =<< loadAll @String "events/*/*"
case listToMaybe nextEvent of
(Just ev) -> do
let id = itemIdentifier ev
ts <- fromJust <$> getMetadataField id "start"
makeItem (unlines [toFilePath id, ts]) :: Compiler (Item String)
Nothing -> makeItem "" :: Compiler (Item String)
match "calendar-events" $ do
-- INTENTIONAL: Do not route. Just using this to allow loading
-- in events.ics (below)
compile getResourceBody
create ["events.ics"] $ do
route idRoute
compile $ do
events <- loadAll "events/*/*"
let icalDateTimeFmt = ":%Y%m%dT%H%M%S"
icalDateFmt = ";VALUE=DATE:%Y%m%d"
fmtDateTime = fmap (formatTime defaultTimeLocale icalDateTimeFmt)
fmtDate = fmap (formatTime defaultTimeLocale icalDateFmt)
urlEncode' = BS_U8.toString . urlEncode True . BS_U8.fromString
fmtdTime f = liftA2 (<|>)
(fmtDateTime . itemTime' [dateTimeFormat] f)
(fmtDate . itemTime' [dateFormat] f)
uidName = return . urlEncode' . toFilePath . itemIdentifier
icalEvCtx =
field "uidname" uidName `mappend`
field "uidtime" (fmtDateTime . itemTime "start") `mappend`
field "start" (fmtdTime "start") `mappend`
field "end" (fmtdTime "end") `mappend`
modificationTimeField "lastmodified" icalDateTimeFmt `mappend`
eventCtx
parseCalEvt s = CalEvt { calEvtName = intercalate " " name'
, calEvtStart = mkT $ date ++ ' ':start
, calEvtEnd = mkT $ date ++ ' ':end
}
where date:time:name' = splitOn " " s
[start, end] = splitOn "--" time
mkT = parseTimeOrError True defaultTimeLocale dateTimeFormat
getCalEvents = do
i <- (load "calendar-events" :: Compiler (Item String))
return . fmap (Item (itemIdentifier i) . parseCalEvt)
. lines
$ itemBody i
calEvtUidName = pure . urlEncode' . calEvtName . itemBody
calEvtStart' = pure . calEvtStart . itemBody
calEvtEnd' = pure . calEvtEnd . itemBody
icalCalEvCtx =
field "title" (pure . calEvtName . itemBody) `mappend`
field "uidname" calEvtUidName `mappend`
field "uidtime" (fmtDateTime . calEvtStart') `mappend`
field "start" (fmtDateTime . calEvtStart') `mappend`
field "end" (fmtDateTime . calEvtEnd') `mappend`
modificationTimeField "lastmodified" icalDateTimeFmt `mappend`
missingField
eventsCtx =
listField "calevents" icalCalEvCtx (getCalEvents) `mappend`
listField "events" icalEvCtx (return events) `mappend`
defaultContext
_ <- getCalEvents
makeItem ""
>>= loadAndApplyTemplate "templates/ical.ics" eventsCtx
match "links" $ do
-- INTENTIONAL: Do not route. Just using this to allow loading
-- in htaccess (below). We could route this to htaccess, but in
-- the future might want to combine multiple pieces of data
-- there.
compile getResourceBody
create [".htaccess"] $ do
route idRoute
compile $ do
x <- loadBody "links"
l <- mapM makeItem $ splitAll "\n" x
let shortUrl = return . head . splitAll " " . itemBody
targetUrl = return . head . tail . splitAll " " . itemBody
urlCtx = field "short" shortUrl `mappend`
field "target" targetUrl `mappend`
defaultContext
ctx = listField "links" urlCtx (return l) `mappend`
defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/htaccess" ctx
match "index.html" $ do
route idRoute
compile $ do
events <- nextNEvents 5 =<< loadAll "events/*/*"
let indexCtx =
extraCss ["/css/gcal.css"] `mappend`
listField "events" eventCtx (return events) `mappend`
defaultContext
getResourceBody
>>= applyAsTemplate indexCtx
>>= loadAndApplyTemplate "templates/default.html" indexCtx
>>= relativizeUrls
match "business.md" $ do
route $ setExtension "html"
compile $ rawHTMLPandocCompiler
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= relativizeUrls
match "archive.md" $ do
route $ setExtension "html"
compile $ rawHTMLPandocCompiler
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= relativizeUrls
match "officers.md" $ do
route $ setExtension "html"
let officersCtx = extraCss ["css/officers.css"] `mappend` defaultContext
compile $ rawHTMLPandocCompiler
>>= loadAndApplyTemplate "templates/default.html" officersCtx
>>= relativizeUrls
match "resources.md" $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= relativizeUrls
match "talks.md" $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= relativizeUrls
match "templates/*" $ compile templateBodyCompiler
--------------------------------------------------------------------------------
eventCtx :: Context String
eventCtx =
--dateField "date" "%B %e, %Y %l:%M %p" `mappend`
--constField "date" "This is a constant" `mappend`
field "date" formatItemDate `mappend`
autoTeaserField "teaser" "content" `mappend`
defaultContext
where formatItemDate i = do
(s, ss) <- (formatFirstTime <$> itemTime' [dateTimeFormat] "start" i)
<|> (formatFirstDate <$> itemTime' [dateFormat] "start" i)
(formatTwoTimes s ss <$> itemTime' [dateTimeFormat] "end" i)
<|> (formatTwoDates s ss <$> itemTime' [dateFormat] "end" i)
<|> (return ss)
dtFmt = "%B %e, %Y"
tmFmt = "%l:%M %p"
dtTmFmt = dtFmt ++ " " ++ tmFmt
formatFirstTime :: UTCTime -> (UTCTime, String)
formatFirstTime = id &&& formatTime defaultTimeLocale dtTmFmt
formatFirstDate :: UTCTime -> (UTCTime, String)
formatFirstDate = id &&& formatTime defaultTimeLocale dtFmt
formatTwoTimes :: UTCTime -> String -> UTCTime -> String
formatTwoTimes s ss e | utctDay s == utctDay e =
ss ++
" to " ++
formatTime defaultTimeLocale tmFmt e
| otherwise =
ss ++
" to " ++
formatTime defaultTimeLocale dtTmFmt e
formatTwoDates :: UTCTime -> String -> UTCTime -> String
formatTwoDates s ss e | utctDay s == utctDay e = ss
| otherwise =
ss ++
" to " ++
formatTime defaultTimeLocale dtFmt e
autoTeaserField :: String -> Snapshot -> Context String
autoTeaserField key snapshot = field key $ \item -> do
body <- itemBody <$> loadSnapshot (itemIdentifier item) snapshot
return $ (unwords . take 50 . words) body
annotateTimes :: MonadMetadata m => [Item a] -> m [(Item a, UTCTime)]
annotateTimes = mapM $ sequence . (id &&& itemTime "start")
chronological' :: MonadMetadata m => [Item a] -> m [Item a]
chronological' = fmap sortNewest . annotateTimes
where sortNewest :: [(Item a, UTCTime)] -> [Item a]
sortNewest = map fst . sortOn snd
recentFirst' :: MonadMetadata m => [Item a] -> m [Item a]
recentFirst' = fmap sortOldest . annotateTimes
where sortOldest :: [(Item a, UTCTime)] -> [Item a]
sortOldest = map fst . sortOn (Down . snd)
nextNEvents :: Int -> [Item a] -> Compiler [Item a]
nextNEvents n is = do
past <- load "events/past"
nextNEvents' n (fromFilePath <$> lines (itemBody past)) is
nextNEvents' :: Int -> [Identifier] -> [Item a] -> Compiler [Item a]
nextNEvents' n olds = fmap (take n . dropOld) . chronological'
where dropOld :: [Item a] -> [Item a]
dropOld = filter (not . flip elem olds . itemIdentifier)
itemTime :: (MonadMetadata m) => String -> Item a -> m UTCTime
itemTime = itemTime' [dateTimeFormat, dateFormat]
dateTimeFormat :: String
dateTimeFormat = "%Y-%m-%d %-H:%M:%S"
dateFormat :: String
dateFormat = "%Y-%m-%d"
itemTime' :: (MonadMetadata m) => [String] -> String -> Item a -> m UTCTime
itemTime' fmts f = tryFormats fmts
<=< flip getMetadataField' f
. itemIdentifier
where tryFormats :: (ParseTime t, Monad m) => [String] -> String -> m t
tryFormats fmts s = maybe (fail "No time parse") return . msum $
map (\x -> parseTimeM True defaultTimeLocale x s) fmts
getQuarter :: Identifier -> Quarter
getQuarter = toQuarter . head . fromJust . capture (fromGlob "events/*/*")
quarterGrouper :: [Identifier] -> Rules [[Identifier]]
quarterGrouper = return . compareQuarters . annotateQuarters
where annotateQuarters :: [Identifier] -> [(Identifier, Quarter)]
annotateQuarters = map (id &&& getQuarter)
compareQuarters :: [(Identifier, Quarter)] -> [[Identifier]]
compareQuarters = fmap (fst<$>) . groupBy ((==) `on` snd)
. sortBy (comparing (Down . snd))
eventPageId :: PageNumber -> Identifier
eventPageId num = fromFilePath $ "events-" ++ (show num) ++ ".html" -- Not actually used
extraCss :: [String] -> Context String
extraCss = listField "extraCss" (bodyField "url") . mapM makeItem