-
Notifications
You must be signed in to change notification settings - Fork 17
/
Main.hs
163 lines (139 loc) · 5.33 KB
/
Main.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
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Functor ((<$>))
import Data.List (isPrefixOf)
import Data.Monoid (mappend)
import Data.Text (pack, unpack, replace, empty)
import Hakyll
main :: IO ()
main = hakyll $ do
-- Build tags
tags <- buildTags "posts/*" (fromCapture "tags/*.html")
-- Compress CSS
match "css/*" $ do
route idRoute
compile compressCssCompiler
-- Copy Files
match "files/**" $ do
route idRoute
compile copyFileCompiler
-- Render posts
match "posts/*" $ do
route $ setExtension ".html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/post.html" (tagsCtx tags)
>>= (externalizeUrls $ feedRoot feedConfiguration)
>>= saveSnapshot "content"
>>= (unExternalizeUrls $ feedRoot feedConfiguration)
>>= loadAndApplyTemplate "templates/default.html" (tagsCtx tags)
>>= relativizeUrls
-- Render posts list
create ["posts.html"] $ do
route idRoute
compile $ do
posts <- loadAll "posts/*"
sorted <- recentFirst posts
itemTpl <- loadBody "templates/postitem.html"
list <- applyTemplateList itemTpl postCtx sorted
makeItem list
>>= loadAndApplyTemplate "templates/posts.html" allPostsCtx
>>= loadAndApplyTemplate "templates/default.html" allPostsCtx
>>= relativizeUrls
-- Index
create ["index.html"] $ do
route idRoute
compile $ do
posts <- loadAll "posts/*"
sorted <- take 10 <$> recentFirst posts
itemTpl <- loadBody "templates/postitem.html"
list <- applyTemplateList itemTpl postCtx sorted
makeItem list
>>= loadAndApplyTemplate "templates/index.html" (homeCtx tags list)
>>= loadAndApplyTemplate "templates/default.html" (homeCtx tags list)
>>= relativizeUrls
-- Post tags
tagsRules tags $ \tag pattern -> do
let title = "Posts tagged " ++ tag
route idRoute
compile $ do
list <- postList tags pattern recentFirst
makeItem ""
>>= loadAndApplyTemplate "templates/posts.html"
(constField "title" title `mappend`
constField "body" list `mappend`
defaultContext)
>>= loadAndApplyTemplate "templates/default.html"
(constField "title" title `mappend`
defaultContext)
>>= relativizeUrls
-- Render RSS feed
create ["rss.xml"] $ do
route idRoute
compile $ do
posts <- loadAllSnapshots "posts/*" "content"
sorted <- take 10 <$> recentFirst posts
renderRss feedConfiguration feedCtx (take 10 sorted)
create ["atom.xml"] $ do
route idRoute
compile $ do
posts <- loadAllSnapshots "posts/*" "content"
sorted <- take 10 <$> recentFirst posts
renderAtom feedConfiguration feedCtx sorted
-- Read templates
match "templates/*" $ compile templateCompiler
postCtx :: Context String
postCtx =
dateField "date" "%B %e, %Y" `mappend`
defaultContext
allPostsCtx :: Context String
allPostsCtx =
constField "title" "All posts" `mappend`
postCtx
homeCtx :: Tags -> String -> Context String
homeCtx tags list =
constField "posts" list `mappend`
constField "title" "Index" `mappend`
field "taglist" (\_ -> renderTagList tags) `mappend`
defaultContext
feedCtx :: Context String
feedCtx =
bodyField "description" `mappend`
postCtx
tagsCtx :: Tags -> Context String
tagsCtx tags =
tagsField "prettytags" tags `mappend`
postCtx
feedConfiguration :: FeedConfiguration
feedConfiguration = FeedConfiguration
{ feedTitle = "Clément Delafargue - RSS feed"
, feedDescription = "Musings about FP and CS"
, feedAuthorName = "Clément Delafargue"
, feedAuthorEmail = "clement+blog@delafargue.name"
, feedRoot = "http://blog.clement.delafargue.name"
}
externalizeUrls :: String -> Item String -> Compiler (Item String)
externalizeUrls root item = return $ fmap (externalizeUrlsWith root) item
externalizeUrlsWith :: String -- ^ Path to the site root
-> String -- ^ HTML to externalize
-> String -- ^ Resulting HTML
externalizeUrlsWith root = withUrls ext
where
ext x = if isExternal x then x else root ++ x
-- TODO: clean me
unExternalizeUrls :: String -> Item String -> Compiler (Item String)
unExternalizeUrls root item = return $ fmap (unExternalizeUrlsWith root) item
unExternalizeUrlsWith :: String -- ^ Path to the site root
-> String -- ^ HTML to unExternalize
-> String -- ^ Resulting HTML
unExternalizeUrlsWith root = withUrls unExt
where
unExt x = if root `isPrefixOf` x then unpack $ replace (pack root) empty (pack x) else x
postList :: Tags
-> Pattern
-> ([Item String] -> Compiler [Item String])
-> Compiler String
postList tags pattern preprocess' = do
postItemTpl <- loadBody "templates/postitem.html"
posts <- loadAll pattern
processed <- preprocess' posts
applyTemplateList postItemTpl (tagsCtx tags) processed