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