3ce4335d5cf11bf756d14d1b2ca6f30b4ed405c1
[software/elephly-net.git] / site.hs
1 --------------------------------------------------------------------------------
2 {-# LANGUAGE OverloadedStrings #-}
3 import Control.Applicative ((<$>))
4 import Data.Monoid (mappend)
5 import Hakyll
6 import Data.Map as M
7 import Data.Maybe (fromMaybe)
8
9 config :: Configuration
10 config = defaultConfiguration
11 { deployCommand =
12 "rsync -Havz _site/ rekado@elephly.net:/srv/disk1/rekado/elephly.net" }
13
14 --------------------------------------------------------------------------------
15 main :: IO ()
16 main = hakyllWith config $ do
17 match "css/*" $ do
18 route idRoute
19 compile compressCssCompiler
20
21 match ( "js/libs/*"
22 .||. "js/hyphenator/*"
23 .||. "js/hyphenator/patterns/*"
24 .||. "js/*"
25 .||. "downies/*"
26 .||. "downies/music/*"
27 .||. "images/*"
28 .||. "images/posts/*"
29 .||. "favicon.ico"
30 ) $ do
31 route idRoute
32 compile copyFileCompiler
33
34 match ( "static/*.markdown" .||. "static/*/*.markdown" ) $ do
35 route $ setExtension "html" `composeRoutes` gsubRoute "static/" (const "")
36 compile $ pandocCompiler
37 >>= loadAndApplyTemplate "templates/default.html" defaultContext
38 >>= relativizeUrls
39
40 match "static/*.html" $ do
41 route $ gsubRoute "static/" (const "")
42 compile $ do
43 getResourceBody
44 >>= loadAndApplyTemplate "templates/default.html" defaultContext
45 >>= relativizeUrls
46
47 match "posts/*.markdown" $ do
48 route $ setExtension "html"
49 compile defaultCompiler
50 --itemTpl <- loadBody "templates/photo.html"
51 --metadata <- getMetadata
52 --let m = M.lookup "photo" metadata
53
54 -- direct links
55 match "posts/2010-03-28-elephly.markdown" $ version "direct" $ do
56 route $ constRoute "elephly.html"
57 compile defaultCompiler
58
59 match "posts/2010-03-23-fur-man.markdown" $ version "direct" $ do
60 route $ constRoute "fur-man.html"
61 compile defaultCompiler
62
63 -- blog post archive
64 create ["posts/index.html"] $ do
65 route idRoute
66 compile $ do
67 let archiveCtx =
68 field "posts" (\_ -> postList recentFirst) `mappend`
69 defaultContext
70
71 makeItem ""
72 >>= loadAndApplyTemplate "templates/archive.html" archiveCtx
73 >>= loadAndApplyTemplate "templates/default.html" archiveCtx
74 >>= relativizeUrls
75
76 -- always show the most recent blog post
77 match "index.html" $ do
78 route idRoute
79 compile $ do
80 mostRecent <- (head . recentFirst) <$> loadAllSnapshots "posts/*.markdown" "non-relative"
81 makeItem (itemBody mostRecent) >>= relativizeUrls
82
83 match "templates/*" $ compile templateCompiler
84
85
86 --------------------------------------------------------------------------------
87 postCtx :: Context String
88 postCtx =
89 dateField "date" "%B %e, %Y" `mappend`
90 photoSnippet `mappend`
91 flattrSnippet `mappend`
92 licenseSnippet `mappend`
93 defaultContext
94
95 -- If a post declares a photo in the metadata header,
96 -- build an image tag, otherwise ignore.
97 photoSnippet :: Context String
98 photoSnippet = field "photo-snippet" $ \item -> do
99 metadata <- getMetadata (itemIdentifier item)
100 case M.lookup "photo" metadata of
101 Just file -> itemBody <$> loadAndApplyTemplate "templates/photo.html" postCtx item
102 _ -> return ""
103
104 -- If a post declares a flattr id in the metadata header,
105 -- load the flattr snippet, otherwise ignore.
106 flattrSnippet :: Context String
107 flattrSnippet = field "flattr-snippet" $ \item -> do
108 metadata <- getMetadata (itemIdentifier item)
109 case M.lookup "flattr" metadata of
110 Just _ -> itemBody <$> loadAndApplyTemplate "templates/flattr.html" postCtx item
111 _ -> return ""
112
113 licenseSnippet :: Context String
114 licenseSnippet = field "license-snippet" $ \item -> do
115 metadata <- getMetadata (itemIdentifier item)
116 case M.lookup "license" metadata of
117 Just _ -> itemBody <$> loadAndApplyTemplate "templates/license.html" postCtx item
118 _ -> return ""
119
120
121 --------------------------------------------------------------------------------
122 postList :: ([Item String] -> [Item String]) -> Compiler String
123 postList sortFilter = do
124 posts <- sortFilter <$> loadAll ("posts/*.markdown" .&&. hasNoVersion)
125 itemTpl <- loadBody "templates/post-item.html"
126 list <- applyTemplateList itemTpl postCtx posts
127 return list
128
129 defaultCompiler = pandocCompiler
130 >>= loadAndApplyTemplate "templates/post.html" postCtx
131 >>= loadAndApplyTemplate "templates/default.html" postCtx
132 >>= saveSnapshot "non-relative"
133 >>= relativizeUrls