-
Notifications
You must be signed in to change notification settings - Fork 1
/
site.hs
110 lines (84 loc) · 3.58 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
{-# LANGUAGE FlexibleContexts #-}
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid
import Hakyll
import qualified Data.Set as S
import Text.Pandoc.Options
import qualified Text.HTML.TagSoup as TS
import qualified Data.Map as M
import Text.Regex.TDFA ((=~~),(=~))
import Control.Applicative
import Data.String
--------------------------------------------------------------------------------
main :: IO ()
main = map fromString . lines <$> readFile "DRAFTS" >>= \drafts -> hakyll $ do
let postsPattern = "posts/*.*" .&&. complement (fromList drafts)
match "images/*" $ do
route idRoute
compile copyFileCompiler
match "posts/figures/**" $ do
route idRoute
compile copyFileCompiler
match "fonts/*" $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
match "posts/*" $ do
route $ setExtension "html"
compile $ lhsCompiler
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/post.html" postCtx
>>= loadAndApplyTemplate "templates/default.html" postCtx
>>= relativizeUrls
>>= applyPathMangledClass
create ["archive.html"] $ do
route idRoute
compile $ do
posts <- recentFirst =<< loadAll postsPattern
let archiveCtx =
listField "posts" postCtx (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 postsPattern
let indexCtx =
listField "posts" postCtx (return posts) `mappend`
defaultContext
getResourceBody
>>= applyAsTemplate indexCtx
>>= loadAndApplyTemplate "templates/default.html" indexCtx
>>= relativizeUrls
match "templates/*" $ compile templateCompiler
writerOptions = defaultHakyllWriterOptions
{ writerExtensions = S.delete Ext_literate_haskell
(writerExtensions defaultHakyllWriterOptions)
}
lhsCompiler = pandocCompilerWith defaultHakyllReaderOptions writerOptions
--------------------------------------------------------------------------------
postCtx :: Context String
postCtx =
dateField "date" "%B %e, %Y"
<> boolField "isLhs" (endsWith "lhs" . toFilePath . itemIdentifier)
<> pathField "fpath"
<> teaserField "teaser" "content"
<> defaultContext
endsWith s = (s ==) . reverse . take (length s) . reverse
applyPathMangledClass :: Item String -> Compiler (Item String)
applyPathMangledClass item = return $ fmap (withTags procImg) item
where
procImg (TS.TagOpen s a) | s == "img" = TS.TagOpen s .
procAttrs . M.fromList $ a
procImg t = t
procAttrs ma = M.toList $ maybe ma (procSrc ma) $ M.lookup "src" ma
procSrc ma u = procMangled ma (u =~ ("__([A-Za-z0-9]+)" :: String))
procMangled ma [[_,cls]] = M.insertWith (\a b -> a ++ " " ++ b) "class" cls ma
procMangled ma _ = ma