-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
site.hs
162 lines (133 loc) Β· 5.79 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
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
import Hakyll (Context, Identifier, Item (..), MonadMetadata, Pattern, Rules, applyAsTemplate,
buildTags, compile, compressCssCompiler, constField, copyFileCompiler, create,
dateField, defaultContext, field, fromCapture, functionField, getMetadata, getTags,
hakyll, idRoute, listField, loadAll, loadAndApplyTemplate, lookupString, makeItem,
match, pandocCompiler, recentFirst, relativizeUrls, route, setExtension, tagsRules,
templateBodyCompiler, (.||.))
import Kowainik.Post (createPostsWithToc)
import Kowainik.Project (makeProjectContext)
import Kowainik.Readme (createProjectMds)
import Kowainik.Social (makeSocialContext)
import Kowainik.Team (TeamMember, makeTeamContext, parseTeam)
import qualified Data.Text as T
main :: IO ()
main = createProjectMds
>> createPostsWithToc
>> parseTeam "team.json"
>>= mainHakyll
mainHakyll :: [TeamMember] -> IO ()
mainHakyll team = hakyll $ do
match ("images/**" .||. "fonts/**" .||. "js/*" .||. "favicon.ico") $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
-- Main page
create ["index.html"] $ do
route idRoute
compile $ do
let ctx = makeProjectContext <> makeSocialContext <> makeTeamContext team <> defaultContext
makeItem ""
>>= applyAsTemplate ctx
>>= loadAndApplyTemplate "templates/team.html" ctx
>>= loadAndApplyTemplate "templates/main.html" ctx
>>= relativizeUrls
-- Posts pages
match "posts/*" $ do
route $ setExtension "html"
compile $ do
i <- pandocCompiler
tgs <- getTags (itemIdentifier i)
let postTagsCtx = postCtxWithTags tgs
loadAndApplyTemplate "templates/post.html" postTagsCtx i
>>= loadAndApplyTemplate "templates/posts-default.html" postTagsCtx
>>= relativizeUrls
-- All posts page
create ["posts.html"] $ compilePosts "Posts" "templates/posts.html" "posts/*"
-- build up tags
tags <- buildTags "posts/*" (fromCapture "tags/*.html")
tagsRules tags $ \tag pattern -> do
let title = "Posts tagged \"" ++ tag ++ "\""
compilePosts title "templates/tag.html" pattern
----------------------------------------------------------------------------
-- Project pages
----------------------------------------------------------------------------
match "projects/*" $ do
route $ setExtension "html"
compile $ do
pandocCompiler
>>= loadAndApplyTemplate "templates/readme.html" defaultContext
>>= loadAndApplyTemplate "templates/posts-default.html" defaultContext
>>= relativizeUrls
-- All projects page
create ["projects.html"] $ compileProjects "Projects" "templates/readmes.html" "projects/*"
-- Render the 404 page, we don't relativize URL's here.
create ["404.html"] $ do
route idRoute
compile $ do
let ctx = defaultContext
makeItem ""
>>= applyAsTemplate ctx
>>= loadAndApplyTemplate "templates/404.html" ctx
match "templates/*" $ compile templateBodyCompiler
compilePosts :: String -> Identifier -> Pattern -> Rules ()
compilePosts title page pat = do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll pat
let ids = map itemIdentifier posts
tagsList <- ordNub . concat <$> traverse getTags ids
let ctx = postCtxWithTags tagsList
<> constField "title" title
<> listField "posts" postCtx (return posts)
<> defaultContext
makeItem ""
>>= loadAndApplyTemplate page ctx
>>= loadAndApplyTemplate "templates/posts-default.html" ctx
>>= relativizeUrls
compileProjects :: String -> Identifier -> Pattern -> Rules ()
compileProjects title page pat = do
route idRoute
compile $ do
projects <- moreStarsFirst =<< loadAll pat
let projectsCtx = stripExtension <> defaultContext
let ctx = constField "title" title
<> listField "readmes" projectsCtx (pure projects)
<> projectsCtx
makeItem ""
>>= loadAndApplyTemplate page ctx
>>= loadAndApplyTemplate "templates/posts-default.html" ctx
>>= relativizeUrls
where
moreStarsFirst :: MonadMetadata m => [Item a] -> m [Item a]
moreStarsFirst = sortByM $ getItemStars . itemIdentifier
where
sortByM :: (Monad m, Ord k) => (a -> m k) -> [a] -> m [a]
sortByM f xs = fmap (map fst . sortBy (flip $ comparing snd)) $
mapM (\x -> (x,) <$> (f x)) xs
getItemStars :: MonadMetadata m
=> Identifier -- ^ Input page
-> m Int -- ^ Parsed GitHub Stars
getItemStars id' = do
metadata <- getMetadata id'
let mbStar = lookupString "stars" metadata >>= readMaybe @Int
maybe starError pure mbStar
where
starError = error "Couldn't parse stars"
-- | Removes the @.html@ suffix in the post URLs.
stripExtension :: Context a
stripExtension = functionField "stripExtension" $ \args _ ->
case args of
[k] -> pure $ fromMaybe k $ toString <$> (T.stripSuffix ".html" $ toText k)
_ -> error "relativizeUrl only needs a single argument"
-- Context to used for posts
postCtx :: Context String
postCtx = stripExtension
<> dateField "date" "%B %e, %Y"
<> defaultContext
postCtxWithTags :: [String] -> Context String
postCtxWithTags tags = listField "tagsList" (field "tag" $ pure . itemBody) (traverse makeItem tags)
<> postCtx