-
Notifications
You must be signed in to change notification settings - Fork 2
/
site.hs
223 lines (191 loc) · 8.29 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
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid (mappend, mconcat)
import Data.Maybe (fromMaybe)
import Hakyll
import System.Directory (getModificationTime)
import Control.Monad (forM, liftM)
--import System.Time (formatCalendarTime, toUTCTime)
import Data.List (sortBy)
import Data.Ord (comparing)
import Text.Pandoc (HTMLMathMethod(MathML),
ObfuscationMethod(ReferenceObfuscation), WriterOptions(..))
--------------------------------------------------------------------------------
main :: IO ()
main = hakyll $ do
match "images/**" $ do
route idRoute
compile copyFileCompiler
match "js/*" $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
match "fonts/*" $ do
route idRoute
compile copyFileCompiler
match "favicon.ico" $ do
route idRoute
compile copyFileCompiler
match "keybase.txt" $ do
route idRoute
compile copyFileCompiler
match (fromRegex "^docs/.*[^\\.page]$") $ do
route idRoute
compile copyFileCompiler
-- Tags
tags <- buildTags "**.page" (fromCapture "tags/*")
match "newsletters/*.page" $ do
route $ setExtension ""
compile $ pandocCompilerWith defaultHakyllReaderOptions woptions
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/default.html" (postCtx tags)
>>= relativizeUrls
match "blog/*.md" $ do
route $ setExtension ""
compile $ pandocCompilerWith defaultHakyllReaderOptions woptions
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/posts.html" (blogPostCtx tags)
>>= loadAndApplyTemplate "templates/bloglist.html" (blogPostCtx tags)
>>= relativizeUrls
-- create index page for blog posts
create ["blog/index"] $ do
route idRoute
compile $ do
posts <- loadAllSnapshots "blog/*.md" "content"
sortedTen <- liftM (take 10) (recentFirst posts)
itemTpl <- loadBody "templates/posts.html"
list <- applyTemplateList itemTpl (blogPostCtx tags) sortedTen
makeItem list
>>= loadAndApplyTemplate "templates/bloglist.html" (allPostsCtx tags)
>>= relativizeUrls
-- create archive page for blog posts
create ["blog/archive"] $ do
route idRoute
compile $ do
posts <- loadAllSnapshots "blog/*.md" "content"
sorted <- recentFirst posts
itemsTpl <- loadBody "templates/postitem.html"
list <- applyTemplateList itemsTpl defaultContext sorted
makeItem (list)
>>= loadAndApplyTemplate "templates/archives.html" (allPostsCtx tags)
>>= relativizeUrls
-- bibliography
match "csl/*" $ compile cslCompiler
match "bib/*" $ compile biblioCompiler
match "**.page" $ do
route $ setExtension ""
compile $ do
item <- getUnderlying
bibFile <- liftM (fromMaybe "") $ getMetadataField item "biblio"
cslFile <- liftM (fromMaybe "chicago") $ getMetadataField item "csl"
let compiler = if bibFile /= "" then
bibtexCompiler cslFile bibFile
else pandocCompilerWith defaultHakyllReaderOptions woptions
compiler
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/default.html" (postCtx tags)
>>= relativizeUrls
match "templates/*" $ compile templateCompiler
-- render RSS from recently changed files
create ["rss.xml"] $ do
route $ idRoute
compile $ do
posts <- fmap (take 10) . createdFirst =<<
loadAllSnapshots "*.page" "content"
renderRss feedConfiguration (feedContext tags) posts
create ["newsletters/rss.xml"] $ do
route $ idRoute
compile $ do
posts <- fmap (take 10) . createdFirst =<<
loadAllSnapshots "newsletters/*.page" "content"
renderRss feedConfiguration (feedContext tags) posts
create ["blog/index.rss"] $ do
route $ idRoute
compile $ do
posts <- fmap (take 10) . createdFirst =<<
loadAllSnapshots "blog/*.md" "content"
renderRss feedConfiguration (feedContext tags) posts
tagsRules tags $ \tag pattern -> do
let title = "Tag: " ++ tag
route idRoute
compile $ tagPage tags title pattern
>>= loadAndApplyTemplate "templates/bloglist.html" (allPostsCtx tags)
>>= relativizeUrls
--------------------------------------------------------------------------------
postCtx :: Tags -> Context String
postCtx tags =
dateField "created" "%Y-%m-%d" `mappend`
modificationTimeField "updated" "%Y-%m-%d" `mappend`
tagsField "tags" tags `mappend`
defaultContext
allPostsCtx :: Tags -> Context String
allPostsCtx tags =
constField "title" "All posts" `mappend` (blogPostCtx tags)
blogPostCtx :: Tags -> Context String
blogPostCtx tags =
dateField "created" "%Y-%m-%d" `mappend`
tagsField "tags" tags `mappend`
defaultContext
bibtexCompiler :: String -> String -> Compiler (Item String)
bibtexCompiler cslFileName bibFileName = do
csl <- load (fromFilePath $ "csl/"++cslFileName)
bib <- load (fromFilePath $ "bib/"++bibFileName)
liftM (writePandocWith woptions) (getResourceBody >>= readPandocBiblio defaultHakyllReaderOptions csl bib)
feedConfiguration :: FeedConfiguration
feedConfiguration = FeedConfiguration
{ feedTitle = "徐栖"
, feedDescription = "RSS feed for Haoyang's website"
, feedAuthorName = "徐栖"
, feedAuthorEmail = "[email protected]"
, feedRoot = "http://expoundite.net"
}
feedContext :: Tags -> Context String
feedContext tags = (postCtx tags) `mappend`
modificationTimeField "published" "%Y-%m-%d" `mappend`
mconcat
[ rssBodyField "description"
, rssTitleField "title"
, urlField "url"
, dateField "updated" "%B %e, %Y"
]
rssTitleField :: String -> Context a
rssTitleField key = field key $ \i -> do
value <- getMetadataField (itemIdentifier i) "title"
let value' = liftM (replaceAll "&" (const "&")) value
maybe empty return value'
empty :: Compiler String
empty = return ""
rssBodyField :: String -> Context String
rssBodyField key = field key $
return . itemBody
woptions :: WriterOptions
woptions = defaultHakyllWriterOptions{ writerSectionDivs = True,
writerTableOfContents = True,
writerColumns = 70,
writerTemplate = Just "<div id=\"TOC\">$toc$</div>\n$body$",
writerHtml5 = True,
writerHTMLMathMethod = Text.Pandoc.MathML (Just ""),
writerHighlight = True,
writerEmailObfuscation = ReferenceObfuscation}
createdFirst :: [Item String] -> Compiler [Item String]
createdFirst items =
let itemsWithTime = unsafeCompiler $ forM items $ \item -> do
utc <- getModificationTime $ toFilePath $ itemIdentifier item
return (utc,item)
in liftM (\xs -> reverse . map snd $ sortBy (comparing fst) xs) itemsWithTime
postList :: Tags -> Pattern -> ([Item String] -> Compiler [Item String]) -> Compiler String
postList tags pattern preprocess' = do
postItemTemplate <- loadBody "templates/postitem.html"
posts' <- loadAll pattern
posts <- preprocess' posts'
applyTemplateList postItemTemplate (postCtx tags) posts
tagPage :: Tags -> String -> Pattern -> Compiler (Item String)
tagPage tags title pattern = do
list <- postList tags pattern (return . id)
makeItem ""
>>= loadAndApplyTemplate "templates/tags.html"
(constField "posts" list `mappend` constField "title" title `mappend`
defaultContext)
>>= relativizeUrls