forked from haskell-infra/www.haskell.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.hs
125 lines (104 loc) · 4.02 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
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
import Data.Aeson
import qualified Data.ByteString.Lazy as BL
import Data.Monoid ((<>))
import Data.Time.Calendar
import Data.Time.Clock
import qualified GHC.IO.Encoding as Encoding
import Hakyll
import Hakyll.Core.Compiler
import Hakyll.Core.Compiler.Internal
import Hakyll.Core.Provider
import System.FilePath.Posix
import Testimonial
--------------------------------------------------------------------------------
main :: IO ()
main = do
Encoding.setLocaleEncoding Encoding.utf8
ctx <- mkContext
hakyllWith configuration $ do
match "testimonials/logos/*" $ do
route idRoute
compile copyFileCompiler
match "testimonials/*.yaml" $ do
compile parseTestimonialCompiler
create ["testimonials.json"] $ do
route idRoute
compile $ do
testimonials <- loadAll @Testimonial "testimonials/*.yaml"
item <- (makeItem . BL.unpack . encode . map itemBody) testimonials
saveSnapshot "_final" item
pure item
match "img/*" $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
match "js/*" $ do
route idRoute
compile copyFileCompiler
match "index.html" $ do
route idRoute
compile $ do
testimonials <- loadAll @Testimonial "testimonials/*.yaml"
let
indexCtx = listField "testimonials" testimonialContext (pure testimonials) `mappend`
ctx
defCompiler indexCtx
let redirects = [ ("humor/index.html", "https://wiki.haskell.org/Humor")
, ("hat/index.html", "https://wiki.haskell.org/Hat")
, ("development/views.html", "http://web.archive.org/web/20040107202217/http://haskell.org/development/views.html")
]
version "redirects" $ createRedirects redirects
match ("**/*.markdown" .||. "*.markdown") $ do
route cleanRoute
compile $ mdCompiler ctx
match "*.pdf" $ do
route idRoute
compile copyFileCompiler
match "templates/*" $
compile templateCompiler
configuration :: Configuration
configuration = defaultConfiguration{ providerDirectory = "site" }
parseTestimonialCompiler :: Compiler (Item Testimonial)
parseTestimonialCompiler = do
identifier <- getUnderlying
provider <- compilerProvider <$> compilerAsk
body <- unsafeCompiler $ BL.readFile (resourceFilePath provider identifier)
testimonial <- parseTestimonialM (BL.toStrict body)
makeItem testimonial
mdCompiler :: Context String -> Compiler (Item String)
mdCompiler ctx =
pandocCompiler
>>= applyAsTemplate ctx
>>= loadAndApplyTemplate "templates/default.html" ctx
>>= relativizeUrls
defCompiler :: Context String -> Compiler (Item String)
defCompiler ctx = getResourceBody
>>= applyAsTemplate ctx
>>= loadAndApplyTemplate "templates/default.html" ctx
>>= relativizeUrls
mkContext :: IO (Context String)
mkContext = do
(year, _, _) <- toGregorian . utctDay <$> getCurrentTime
return $ constField "year" (show year)
<> dropIndexHtml
<> defaultContext
cleanRoute :: Routes
cleanRoute = customRoute $
(\(p, _) -> p </> "index" <.> "html") . splitExtension . toFilePath
dropIndexHtml :: Context a
dropIndexHtml = mapContext transform (urlField "url") where
transform url = case splitFileName url of
(p, "index.html") -> takeDirectory p
_ -> url
testimonialContext :: Context Testimonial
testimonialContext =
mconcat [ field "companyName" (pure . companyName . itemBody)
, field "logoURL" (pure . logoURL . itemBody)
, field "shortTestimonial" (pure . shortTestimonial . itemBody)
, field "companyURL" (pure . companyURL . itemBody)
]