-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite.hs
229 lines (186 loc) · 8.07 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
224
225
226
227
228
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid (mappend)
import Data.Maybe (isJust)
import Data.ByteString.Lazy.UTF8 (toString, fromString)
import Hakyll hiding (pandocCompiler)
import Hakyll.Web.Sass (sassCompiler)
import System.Environment (getEnvironment)
import System.IO
import Text.Pandoc.Definition as Pandoc
import Text.Pandoc.Walk as Pandoc
import Text.Pandoc.Extensions as Pandoc
import Text.Pandoc.Options as Pandoc
--------------------------------------------------------------------------------
main :: IO ()
main = do
hSetBuffering stdout NoBuffering
env <- getEnvironment
runHakyll env
runHakyll :: [(String, String)] -> IO ()
runHakyll env = hakyll $ do
match "images/**" $ do
route idRoute
compile copyFileCompiler
match "favicon.ico" $ do
route idRoute
compile copyFileCompiler
match "css/*.scss" $ do
route $ setExtension "css"
let compressCssItem = fmap compressCss
compile (compressCssItem <$> sassCompiler)
match "css/*.css" $ do
route idRoute
compile compressCssCompiler
match "fonts/*" $ do
route idRoute
compile copyFileCompiler
match (fromList ["about.md", "contact.md"]) $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= relativizeUrls
-- Tags
tags <- buildTagsWith (maybeGetTags env) "posts/*" (fromCapture "tags/*.html")
matchMetadata "posts/*" (devOrPublished env) $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/post.html" (postCtx tags)
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/default.html" (postCtx tags)
>>= relativizeUrls
create ["archive.html"] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "posts/*"
let archiveCtx =
listField "posts" (postCtx tags) (return posts) `mappend`
constField "title" "Archives" `mappend`
defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/archive.html" archiveCtx
>>= loadAndApplyTemplate "templates/default.html" archiveCtx
>>= relativizeUrls
match "index.html" $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll "posts/*"
let indexCtx = mconcat
[ listField "posts" (postCtx tags) (return posts)
, field "tags" (\_ -> renderTagList tags)
, defaultContext
]
getResourceBody
>>= applyAsTemplate indexCtx
>>= loadAndApplyTemplate "templates/default.html" indexCtx
>>= relativizeUrls
match "templates/*" $ compile templateBodyCompiler
match "tikz/**.tikz" $ do
route $ gsubRoute "tikz/" (const "images/") `composeRoutes` setExtension ".png"
compile $ getResourceString
>>= loadAndApplyTemplate "templates/default.tikz" defaultContext
>>= withItemBody (unixFilterLBS "bash" ["./scripts/pdflatex.sh"] . fromString)
>>= withItemBody (unixFilterLBS "bash" ["-c", "pdfcrop -margins 10 - >(cat)"])
-- >>= withItemBody (unixFilterLBS "convert" ["-density", "1200", "-trim", "PDF:-", "-quality", "100", "-flatten", "-sharpen", "0x1.0", "-resize", "25%", "PNG:-"])
>>= withItemBody (unixFilterLBS "convert" ["-density", "1200", "-trim", "PDF:-", "-flatten", "-resample", "300", "PNG:-"])
>>= withItemBody (unixFilterLBS "exiftool" ["-all=", "-"])
-- Feeds
create ["index.rss"] $ do
route idRoute
compile (feedCompiler "posts/*" renderRss "All posts")
create ["index.atom"] $ do
route idRoute
compile (feedCompiler "posts/*" renderAtom "All posts")
-- Post tags
tagsRules tags $ \tag pattern -> do
let title = "Posts tagged '" <> tag <> "'"
-- Copied from posts, need to refactor
route idRoute
compile $ do
posts <- recentFirst =<< loadAll pattern
let ctx = constField "title" title <>
listField "posts" (postCtx tags) (return posts) <>
constField "tag" tag <>
defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/tag.html" ctx
>>= loadAndApplyTemplate "templates/default.html" ctx
>>= relativizeUrls
-- Create RSS feed as well
version "rss" $ do
route $ setExtension "rss"
compile $ feedCompiler pattern renderRss title
-- Create ATOM feed as well
version "atom" $ do
route $ setExtension "atom"
compile $ feedCompiler pattern renderAtom title
--------------------------------------------------------------------------------
devOrPublished :: [(String, String)] -> Metadata -> Bool
devOrPublished env meta = isDevelopmentEnv || isPublished
where
isDevelopmentEnv = lookup "HAKYLL_ENV" env == Just "dev"
isPublished = isJust . lookupString "published" $ meta
maybeGetTags env identifier = do
meta <- getMetadata identifier
if devOrPublished env meta
then getTags identifier
else pure []
postCtx :: Tags -> Context String
postCtx tags = mconcat
[ dateField "date" "%B %e, %Y"
, tagsField "tags" tags
, defaultContext
]
feedCtx :: Context String
feedCtx = mconcat
[ bodyField "description"
, Context $ \key -> case key of
"title" -> unContext (mapContext escapeHtml defaultContext) key
_ -> unContext mempty key
, defaultContext
]
feedConfiguration :: String -> FeedConfiguration
feedConfiguration title = FeedConfiguration
{ feedTitle = "Layus' short musings - " <> title
, feedDescription = "A blog about build systems, Nix and everything"
, feedAuthorName = "layus"
, feedAuthorEmail = "layus.on@gmail.com"
, feedRoot = "https://blog.layus.be"
}
feedCompiler pattern render title = do
posts <- fmap (take 10) . recentFirst =<<
loadAllSnapshots pattern "content"
render (feedConfiguration title) feedCtx posts
pandocReaderOptions = defaultHakyllReaderOptions
{ readerExtensions = Pandoc.enableExtension Pandoc.Ext_citations $ readerExtensions defaultHakyllReaderOptions
, readerStripComments = True
}
pandocWriterOptions = defaultHakyllWriterOptions
pandocCompiler = do
--csl <- load "chicago.csl"
--bib <- load "refs.bib"
getResourceBody
>>= readPandocWith pandocReaderOptions
>>= return . filter
>>= return . writePandocWith pandocWriterOptions
where
filter = Pandoc.walk wrapCode
wrapCode code@(Pandoc.CodeBlock _ _) = Pandoc.Div ("", ["scroll-wrapper"], []) [code]
wrapCode x = x
-- match "tikz/**.tikz" $ version "1tex" $ do
-- route $ setExtension ".tex"
-- compile $ getResourceString
-- >>= loadAndApplyTemplate "templates/default.tikz" defaultContext
--
-- match "tikz/**.tikz" $ version "2pdf" $ do
-- route $ setExtension ".pdf"
-- compile $ getResourceString
-- >>= loadAndApplyTemplate "templates/default.tikz" defaultContext
-- >>= withItemBody (unixFilterLBS "bash" ["./scripts/pdflatex.sh"] . fromString)
--
-- match "tikz/**.tikz" $ version "3pdfcrop" $ do
-- route $ setExtension ".pdfcrop"
-- compile $ getResourceString
-- >>= loadAndApplyTemplate "templates/default.tikz" defaultContext
-- >>= withItemBody (unixFilterLBS "bash" ["./scripts/pdflatex.sh"] . fromString)
-- >>= withItemBody (unixFilterLBS "bash" ["-c", "pdfcrop - >(cat)"])