-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.hs
225 lines (182 loc) · 8.05 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
--------------------------------------------------------------------------------
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Control.Applicative ( empty )
import Data.List.Safe ( find, groupBy, head, intersperse
, isPrefixOf, sortBy, stripPrefix )
import Data.List.Split ( splitOn )
import Data.Semigroup ( (<>) )
import GHC.Exts ( fromString )
import Hakyll
import Prelude hiding ( head )
import System.FilePath ( takeDirectory, takeBaseName, (</>) )
import Text.CSL.Input.Bibtex ( readBibtex )
import Text.CSL ( Reference )
import Text.CSL.Reference ( author, containerTitle, collectionTitle
, issued, note, refId, title, unLiteral
, year )
import Text.CSL.Style ( familyName, givenName, unFormatted
, writeCSLString )
import Text.Pandoc.Definition ( Inline ( LineBreak ) )
-- Rules -----------------------------------------------------------------------
main :: IO ()
main = do
biblio <- readBibtex' "bibliography.bib"
hakyll $ do
match ("CNAME" .||. "img/**" .||. "static/**") $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
matchList ["about.md", "projects.md"] $ do
route $ dirRoute
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= relativizeUrls
match "publications.md" $ do
route $ dirRoute
compile $ do
let papers = filter (("Sam L. Thomas" `elem`) . bibAuthor) biblio
pubCtx = bibByYearCtx papers
<> constField "title" "Publications"
<> defaultContext
getResourceBody
>>= applyAsTemplate pubCtx
>>= renderPandoc
>>= loadAndApplyTemplate "templates/default.html" pubCtx
>>= relativizeUrls
match "blog/**.md" $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/post.html" postListCtx
>>= loadAndApplyTemplate "templates/default.html" postListCtx
>>= relativizeUrls
match "blog.md" $ do
route $ dirRoute
compile $ do
posts <- recentFirst =<< loadAll "blog/**.md"
let writingCtx = listField "posts" postCtx (pure posts)
<> constField "title" "Blog"
<> defaultContext
getResourceBody
>>= applyAsTemplate writingCtx
>>= renderPandoc
>>= loadAndApplyTemplate "templates/default.html" writingCtx
>>= relativizeUrls
match "index.md" $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/home.html" defaultContext
>>= relativizeUrls
create ["404.html"] $ do
route $ idRoute
compile $ do
let ctx = constField "title" "404"
<> constField "body" "Resource not found."
<> defaultContext
makeItem "" >>= loadAndApplyTemplate "templates/default.html" ctx
match "templates/*" $ compile templateBodyCompiler
-- Contexts --------------------------------------------------------------------
postCtx :: Context String
postCtx =
dateField "date" "%Y-%m-%d" <> defaultContext
postListCtx :: Context String
postListCtx =
dateField "date" "%B %e, %Y" <> defaultContext
-- Routes ----------------------------------------------------------------------
dirRoute :: Routes
dirRoute = customRoute $ genRoute . toFilePath
where genRoute path =
takeDirectory path </> takeBaseName path </> "index.html"
-- Utility ---------------------------------------------------------------------
matchList :: [Identifier] -> Rules () -> Rules ()
matchList = match . fromList
readBibtex' :: FilePath -> IO [Reference]
readBibtex' = readBibtex (const True) True False
-- Bibliography ----------------------------------------------------------------
bibId :: Reference -> String
bibId = unLiteral . refId
-- TODO: how will this handle a name with von components?
bibAuthor :: Reference -> [String]
bibAuthor = fmap format . author
where lastName agent =
writeCSLString . unFormatted . familyName $ agent
firstNames agent =
map (writeCSLString . unFormatted) $ givenName agent
format agent =
concat . intersperse " " $ firstNames agent ++ [lastName agent]
bibTitle :: Reference -> String
bibTitle = writeCSLString . unFormatted . title
bibConference :: Reference -> String
bibConference = writeCSLString . unFormatted . containerTitle
bibSeries :: Reference -> String
bibSeries = writeCSLString . unFormatted . collectionTitle
bibYear :: Reference -> Maybe Int
bibYear = (year =<<) . head . issued
bibNote :: Reference -> [String]
bibNote = map writeCSLString . splitOn [LineBreak] . unFormatted . note
bibNoteWithPrefix :: String -> Reference -> Maybe String
bibNoteWithPrefix k = (stripPrefix k =<<) . find (isPrefixOf k) . bibNote
bibNotePdf :: Reference -> Maybe String
bibNotePdf = bibNoteWithPrefix "pdf: "
bibNoteSlides :: Reference -> Maybe String
bibNoteSlides = bibNoteWithPrefix "slides: "
bibNoteAR :: Reference -> Maybe String
bibNoteAR = bibNoteWithPrefix "acceptance rate: "
bibField :: (Reference -> a) -> Item Reference -> Compiler a
bibField f = pure . f . itemBody
bibCtx :: Context Reference
bibCtx =
field "title" (bibField bibTitle) <>
field "conference" (bibField bibConference) <>
maybeField "pdf" bibNotePdf <>
maybeField "slides" bibNoteSlides <>
maybeField "arate" bibNoteAR <>
field "author" authorsField <>
listFieldWith "authors" authorCtx (mapM makeItem . bibAuthor . itemBody)
where highlight who =
fmap (\who' -> if who == who' then "<strong>" ++ who' ++ "</strong>"
else who')
joinAuthors xs@(_ : _ : _) =
(concat . intersperse ", " . init) xs ++ " and " ++ last xs
joinAuthors [x] = x
authorsField =
fmap (joinAuthors . highlight "Sam L. Thomas") . bibField bibAuthor
maybeField :: String -> (a -> Maybe String) -> Context a
maybeField k f = Context g
where g k' [] item = if k == k'
then case f . itemBody $ item of
Just v -> return $ StringField v
Nothing -> empty
else empty
bibByCat :: String -> String -> [Reference] -> Context String
bibByCat cat v rs =
constField cat v <>
listField "publications" bibCtx (mapM makeItem rs)
bibByYearCtx :: [Reference] -> Context String
bibByYearCtx rs =
listField "publications" bibYearCtx (mapM (makeItem . yearAndBibs) $ sortedRefs rs)
where sameYear r r' = bibYear r == bibYear r'
printYear (Just y) = show y
printYear Nothing = "Others"
getYear (g : _) = bibYear g
yearAndBibs g = (printYear . getYear $ g, g)
sortedRefs =
sortBy (\u v -> compare (getYear v) (getYear u)) . groupBy sameYear
bibYearCtx :: Context (String, [Reference])
bibYearCtx = Context f
where f "year" [] item = return $ StringField (fst . itemBody $ item)
f "publications" [] item = return $ ListField bibCtx (fmap bibItem . snd . itemBody $ item)
f _ _ _ = empty
authorCtx :: Context String
authorCtx = Context f
where f "author" [] item = return $ StringField (itemBody item)
f _ _ _ = empty
bibItem :: Reference -> Item Reference
bibItem r =
Item {
itemIdentifier = fromString $ bibId r
, itemBody = r
}