-
Notifications
You must be signed in to change notification settings - Fork 6
/
Main.hs
265 lines (230 loc) · 8.08 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
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
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE DuplicateRecordFields #-}
module Main where
import Control.Lens
import Data.Aeson as A
import Data.Aeson.Lens
import Data.Function (on)
import Data.List (sortBy)
import Data.Map as M
import Data.Monoid
import Data.Set as S
import qualified Data.Text as T
import Data.Text.Lens
import Data.Time
import Development.Shake hiding (Resource)
import Development.Shake.Classes
import Development.Shake.FilePath
import GHC.Generics (Generic)
import Slick
main :: IO ()
main =
shakeArgs shakeOptions {shakeVerbosity = Chatty, shakeThreads = 4} $
-- Set up caches
do
postCache <- jsonCache' loadPost
allPostsCache <-
simpleJsonCache'
(SortedPostsCache ())
(sortByDate <$> (postNames >>= traverse (postCache . PostFilePath)))
sortedPostURLsCache <-
simpleJsonCache'
(SortedPostURLsCache ())
(fmap (url :: Post -> String) <$> allPostsCache)
allTagsCache <- simpleJsonCache' (TagCache ()) (getTags <$> allPostsCache)
-- Require all the things we need to build the whole site
"site" ~>
need ["static", "posts", "tags", "dist/index.html", "dist/atom.xml"]
-- Require all static assets
"static" ~> do
staticFiles <-
getDirectoryFiles "." ["site/css//*", "site/js//*", "site/images//*"]
need (("dist" </>) . dropDirectory1 <$> staticFiles)
-- Rule for handling static assets, just copy them from source to dest
["dist/css//*", "dist/js//*", "dist/images//*"] |%> \out -> do
copyFileChanged ("site" </> dropDirectory1 out) out
-- build the main table of contents
"dist/index.html" %> buildIndex allPostsCache allTagsCache
-- Find and require every post to be built
"posts" ~> findPosts
-- Find and require every tag to be built
"tags" ~> findTags allTagsCache
-- rule for actually building tags
"dist/tag//*.html" %> buildTag allTagsCache
-- rule for actually building posts
"dist/posts//*.html" %> buildPost postCache sortedPostURLsCache
"dist/atom.xml" %> buildAtomFeed allPostsCache
data IndexInfo = IndexInfo
{ posts :: [Post]
, tags :: [Tag]
} deriving (Generic, Show)
instance FromJSON IndexInfo
instance ToJSON IndexInfo
data Tag = Tag
{ tag :: String
, posts :: [Post]
, url :: String
} deriving (Generic, Show)
instance FromJSON Tag
instance ToJSON Tag
data Post = Post
{ title :: String
, author :: String
, content :: String
, url :: String
, image :: String
, tags :: [String]
, nextPostURL :: Maybe String
, prevPostURL :: Maybe String
, isoDate :: String
, date :: String
, srcPath :: String
} deriving (Generic, Eq, Ord, Show)
instance FromJSON Post where
parseJSON v = do
let title = v ^. key "title" . _String . unpacked
author = v ^. key "author" . _String . unpacked
date = v ^. key "date" . _String . unpacked
isoDate = formatDate date
content = v ^. key "content" . _String . unpacked
url = v ^. key "url" . _String . unpacked
tags = v ^.. key "tags" . values . _String . unpacked
nextPostURL = Nothing
prevPostURL = Nothing
srcPath = v ^. key "srcPath" . _String . unpacked
image = v ^. key "image" . _String . unpacked
in return Post {..}
instance ToJSON Post
postNames :: Action [FilePath]
postNames = getDirectoryFiles "." ["site/posts//*.md"]
destToSrc :: FilePath -> FilePath
destToSrc p = "site" </> dropDirectory1 p
srcToDest :: FilePath -> FilePath
srcToDest p = "dist" </> dropDirectory1 p
srcToURL :: FilePath -> String
srcToURL = ("/" ++) . dropDirectory1 . dropExtension
loadPost :: PostFilePath -> Action Post
loadPost (PostFilePath postPath) = do
let srcPath = destToSrc postPath -<.> "md"
postData <- readFile' srcPath >>= markdownToHTML . T.pack
let postURL = T.pack . srcToURL $ postPath
withURL = _Object . at "url" ?~ String postURL
withSrc = _Object . at "srcPath" ?~ String (T.pack srcPath)
convert . withSrc . withURL $ postData
buildIndex :: Action [Post] -> Action [Tag] -> FilePath -> Action ()
buildIndex allPosts allTags out = do
indexT <- compileTemplate' "site/templates/index.html"
posts <- allPosts
tags <- allTags
let indexInfo = IndexInfo {posts, tags}
indexHTML = T.unpack $ substitute indexT (toJSON indexInfo)
writeFile' out indexHTML
findPosts :: Action ()
findPosts = do
pNames <- postNames
need ((\p -> srcToDest p -<.> "html") <$> pNames)
findTags :: Action [Tag] -> Action ()
findTags allTags = do
ts <- allTags
let toTarget Tag {tag} = "dist/tag/" ++ tag ++ ".html"
need (toTarget <$> ts)
buildTag :: Action [Tag] -> FilePath -> Action ()
buildTag allTags out = do
tagList <- allTags
let tagName = (dropExtension . dropDirectory1 . dropDirectory1) out
findTag t@Tag {tag}
| tag == tagName = First (Just t)
findTag _ = First Nothing
maybeTag = getFirst $ foldMap findTag tagList
t <-
case maybeTag of
Nothing -> fail $ "could not find tag: " <> tagName
Just t' -> return t'
tagTempl <- compileTemplate' "site/templates/tag.html"
writeFile' out . T.unpack $ substitute tagTempl (toJSON t)
buildPost ::
(PostFilePath -> Action Post) -> Action [String] -> FilePath -> Action ()
buildPost postCache sortedPostURLsCache out = do
let srcPath = destToSrc out -<.> "md"
postURL = srcToURL srcPath
(prevPostURL, nextPostURL) <- getNeighbours postURL <$> sortedPostURLsCache
post <- postCache (PostFilePath srcPath)
let withNeighbours = post {nextPostURL, prevPostURL}
template <- compileTemplate' "site/templates/post.html"
writeFile' out . T.unpack $ substitute template (toJSON withNeighbours)
getNeighbours :: String -> [String] -> (Maybe String, Maybe String)
getNeighbours i xs =
let ms = pure <$> xs
in go ([Nothing] <> ms <> [Nothing])
where
go (before:Just current:after:_)
| current == i = (before, after)
go (_:rest) = go rest
go [] = (Nothing, Nothing)
getTags :: [Post] -> [Tag]
getTags posts =
let tagToPostsSet = M.unionsWith mappend (toMap <$> posts)
tagToPostsList = fmap S.toList tagToPostsSet
tagObjects =
foldMapWithKey
(\tag ps -> [Tag {tag, posts = ps, url = "/tag/" <> tag}])
tagToPostsList
in tagObjects
where
toMap :: Post -> Map String (Set Post)
toMap p@Post {tags} = M.unionsWith mappend (embed p <$> tags)
embed :: Post -> String -> Map String (Set Post)
embed post tag = M.singleton tag (S.singleton post)
sortByDate :: [Post] -> [Post]
sortByDate = sortBy (flip compareDates)
where
compareDates = compare `on` isoDate
formatDate :: String -> String
formatDate humanDate = toIsoDate parsedTime
where
parsedTime =
parseTimeOrError True defaultTimeLocale "%b %e, %Y" humanDate :: UTCTime
rfc3339 :: Maybe String
rfc3339 = Just "%H:%M:%SZ"
toIsoDate :: UTCTime -> String
toIsoDate = formatTime defaultTimeLocale (iso8601DateFormat rfc3339)
buildAtomFeed :: Action [Post] -> FilePath -> Action ()
buildAtomFeed postsCache out = do
now <- liftIO getCurrentTime
posts <- postsCache
let atomData =
AtomData
{ title = "Chris Penner"
, domain = "https://chrispenner.ca"
, author = "Chris Penner"
, posts = posts
, currentTime = toIsoDate now
, url = "/atom.xml"
}
atomTempl <- compileTemplate' "site/templates/atom.xml"
writeFile' out . T.unpack $ substitute atomTempl (toJSON atomData)
data AtomData = AtomData
{ title :: String
, domain :: String
, author :: String
, posts :: [Post]
, currentTime :: String
, url :: String
} deriving (Generic, Eq, Ord, Show)
instance ToJSON AtomData
newtype SortedPostsCache =
SortedPostsCache ()
deriving (Show, Eq, Hashable, Binary, NFData)
newtype SortedPostURLsCache =
SortedPostURLsCache ()
deriving (Show, Eq, Hashable, Binary, NFData)
newtype TagCache =
TagCache ()
deriving (Show, Eq, Hashable, Binary, NFData)
newtype PostFilePath =
PostFilePath String
deriving (Show, Eq, Hashable, Binary, NFData)