-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.hs
220 lines (172 loc) · 7.31 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
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
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
import Data.List (isInfixOf)
import Data.Monoid ((<>))
import Hakyll hiding (withUrls)
import qualified Text.HTML.TagSoup as TS
import System.FilePath.Posix (takeBaseName,takeDirectory
,(</>),splitFileName, replaceDirectory )
--------------------------------------------------------------------------------
-- mconcat, mappend
-- replace a foo/bar.md by foo/bar/index.html
-- this way the url looks like: foo/bar in most browsers
niceRoute :: Routes
niceRoute = customRoute createIndexRoute
where
createIndexRoute ident =
takeDirectory (toFilePath ident) </> takeBaseName (toFilePath ident) </> "index.html"
baseRoute :: Routes
baseRoute = customRoute base
where
base ident = replaceDirectory (toFilePath ident) ""
niceBaseRoute :: Routes
niceBaseRoute = customRoute base
where
base ident = takeBaseName (toFilePath ident) </> "index.html"
-- replace url of the form foo/bar/index.html by foo/bar
removeIndexHtml :: Item String -> Compiler (Item String)
removeIndexHtml item = return $ fmap (withUrls removeIndexStr) item
where
removeIndexStr :: String -> String
removeIndexStr url = case splitFileName url of
(dir, "index.html") | isLocal dir -> dir
_ -> url
isLocal :: String -> Bool
isLocal uri = not ("://" `isInfixOf` uri)
--------------------------------------------------------------------------------
-- | Apply a function to each URL on a webpage
withUrls :: (String -> String) -> String -> String
withUrls f = withTags tag
where
tag (TS.TagOpen s a) = TS.TagOpen s $ map attr a
tag x = x
attr (k, v) = (k, if isUrlAttribute k then f v else v)
isUrlAttribute :: String -> Bool
isUrlAttribute = (`elem` ["src", "href", "data", "data-url"])
feedConfig :: FeedConfiguration
feedConfig = FeedConfiguration
{ feedTitle = "Mechanical Elephant: Code, Design, Science"
, feedDescription = "Haskell, Python, and Design Process"
, feedAuthorName = "Matthew Griffith"
, feedAuthorEmail = "matt@mechanical-elephant.com"
, feedRoot = "http://mechanical-elephant.com"
}
mainDescription :: String
mainDescription = "Programming in haskell, creativity, and the design process."
main :: IO ()
main = hakyll $ do
match "static/favicon.ico" $ do
route baseRoute
compile copyFileCompiler
match "static/img/*" $ do
route idRoute
compile copyFileCompiler
match "static/js/*" $ do
route idRoute
compile copyFileCompiler
match "static/js/vendor/*" $ do
route idRoute
compile copyFileCompiler
match "pages/styleguide.markdown" $ do
route niceBaseRoute
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/alt-base.html" defaultContext
>>= relativizeUrls
>>= removeIndexHtml
-- match "thoughts/book/*" $ do
-- route niceRoute
-- let draftCtx = constField "book" "true"
-- <> defaultContext
-- compile $ pandocCompiler
-- >>= loadAndApplyTemplate "templates/post.html" draftCtx
-- >>= loadAndApplyTemplate "templates/base.html" draftCtx
-- >>= relativizeUrls
-- >>= removeIndexHtml
-- match "thoughts/draft/*" $ do
-- route niceRoute
-- let draftCtx = constField "draft" "true"
-- <> defaultContext
-- compile $ pandocCompiler
-- >>= loadAndApplyTemplate "templates/post.html" draftCtx
-- >>= loadAndApplyTemplate "templates/base.html" draftCtx
-- >>= relativizeUrls
-- >>= removeIndexHtml
match "thoughts/*" $ do
route niceRoute
let thoughtCtx = constField "nav" "true"
<> postCtx
compile $ pandocCompiler
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/post.html" thoughtCtx
>>= saveSnapshot "rendered"
>>= loadAndApplyTemplate "templates/alt-base.html" thoughtCtx
>>= relativizeUrls
>>= removeIndexHtml
create ["atom.xml"] $ do
route idRoute
compile $ do
let feedCtx = postCtx <> bodyField "description"
posts <- fmap (take 10) . recentFirst =<<
loadAllSnapshots "thoughts/*" "rendered"
renderAtom feedConfig feedCtx posts
create ["rss"] $ do
route idRoute
compile $ do
let feedCtx = postCtx <> bodyField "description"
posts <- fmap (take 10) . recentFirst =<<
loadAllSnapshots "thoughts/*" "rendered"
renderRss feedConfig feedCtx posts
create ["sitemap.xml"] $ do
route idRoute
compile $ do
pages <- loadAll ("pages/about.markdown" .||. "thoughts/*")
let sitemapCtx = listField "pages" sitemapItemCtx (return pages)
<> defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/sitemap.xml" sitemapCtx
>>= removeIndexHtml
>>= relativizeUrls
match "pages/about.markdown" $ do
let aboutCtx = constField "title" "About Mechanical Elephant"
<> constField "nav-selection-about" "true"
<> constField "description" mainDescription
<> defaultContext
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/pass-through.html" aboutCtx
-- >>= relativizeUrls
create ["low-poly.html"] $ do
route niceRoute
compile $ do
makeItem ""
>>= loadAndApplyTemplate "templates/low-poly.html" defaultContext
-- >>= removeIndexHtml
-- >>= relativizeUrls
create ["index.html"] $ do
route idRoute
compile $ do
posts <- chronological =<< loadAll "thoughts/*"
aboutBody <- loadBody "pages/about.markdown" :: Compiler String
let indexCtx = listField "articles" postCtx (return posts)
<> constField "title" "Mechanical Elephant"
<> constField "nav-selection-thoughts" "true"
<> constField "description" mainDescription
<> constField "about-body" aboutBody
<> defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/project-list.html" indexCtx
>>= loadAndApplyTemplate "templates/alt-base.html" indexCtx
>>= relativizeUrls
>>= removeIndexHtml
match "templates/*" $ compile templateCompiler
--------------------------------------------------------------------------------
postCtx :: Context String
postCtx =
dateField "date" "%B %e, %Y" <>
defaultContext
teaserCtx :: Context String
teaserCtx = teaserField "teaser" "content" <> postCtx
sitemapItemCtx :: Context String
sitemapItemCtx = mconcat [ constField "root" (feedRoot feedConfig)
, dateField "isodate" "%F"
, defaultContext
]