summaryrefslogtreecommitdiff
path: root/site.hs
blob: 7f90e8cc38e4cb0cf6c62cb7363cfa807e19f7f3 (about) (plain)
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
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
import           Control.Applicative ((<$>))
import           Data.Monoid         (mappend)
import           Hakyll
import           Data.Map as M
import qualified Data.Set as S
import           Data.Maybe          (fromMaybe)
import           Text.Pandoc.Options

config :: Configuration
config = defaultConfiguration
  { deployCommand =
      "rsync -Havz _site/ rekado@elephly.net:/srv/disk1/rekado/elephly.net" }

myFeedConfiguration :: FeedConfiguration
myFeedConfiguration = FeedConfiguration
  { feedTitle       = "Rekado's website"
  , feedDescription = "Music, words, and hacking"
  , feedAuthorName  = "Rekado"
  , feedAuthorEmail = "rekado+feed@elephly.net"
  , feedRoot        = "http://elephly.net"
  }

--------------------------------------------------------------------------------
main :: IO ()
main = hakyllWith config $ do
    match "css/*" $ do
        route   idRoute
        compile compressCssCompiler

    match (    "js/libs/*"
          .||. "js/hyphenator/*"
          .||. "js/hyphenator/patterns/*"
          .||. "js/*"
          .||. "downies/*"
          .||. "downies/music/*"
          .||. "images/*"
          .||. "images/posts/*"
          .||. "images/posts/*/*"
          .||. "favicon.ico"
          ) $ do
      route   idRoute
      compile copyFileCompiler

    match ( "static/*.markdown" .||. "static/*/*.markdown" ) $ do
      route   $ setExtension "html" `composeRoutes` gsubRoute "static/" (const "")
      compile $ pandocCompiler
        >>= loadAndApplyTemplate "templates/default.html" defaultContext
        >>= relativizeUrls

    match "static/*.html" $ do
      route   $ gsubRoute "static/" (const "")
      compile $ do
        getResourceBody
          >>= loadAndApplyTemplate "templates/default.html" defaultContext
          >>= relativizeUrls

    match "posts/*.markdown" $ do
      route   $ setExtension "html"
      compile defaultCompiler
        --itemTpl <- loadBody "templates/photo.html" 
        --metadata <- getMetadata
        --let m = M.lookup "photo" metadata

    -- direct links
    match "posts/2010-03-28-elephly.markdown" $ version "direct" $  do
      route   $ constRoute "elephly.html"
      compile defaultCompiler

    match "posts/2010-03-23-fur-man.markdown" $ version "direct" $  do
      route   $ constRoute "fur-man.html"
      compile defaultCompiler

    -- blog post archive
    create ["posts/index.html"] $ do
      route   idRoute
      compile $ do
        let archiveCtx =
              field "posts" (\_ -> postList recentFirst) `mappend`
              defaultContext

        makeItem ""
          >>= loadAndApplyTemplate "templates/archive.html" archiveCtx
          >>= loadAndApplyTemplate "templates/default.html" archiveCtx
          >>= relativizeUrls

    create ["rss.xml"] $ postFeed renderRss
    create ["atom.xml"] $ postFeed renderAtom

    -- always show the most recent blog post
    create ["index.html"] $ do
      route idRoute
      compile $ do
        mostRecent <- fmap head . recentFirst =<< loadAllSnapshots "posts/*.markdown" "non-relative"
        makeItem (itemBody mostRecent) >>= relativizeUrls

    match "templates/*" $ compile templateCompiler


--------------------------------------------------------------------------------
postCtx :: Context String
postCtx =
    dateField "date" "%B %e, %Y" `mappend`
    photoSnippet `mappend`
    flattrSnippet `mappend`
    licenseSnippet `mappend`
    defaultContext

-- If a post declares a certain key in the metadata header,
-- load the given template, otherwise ignore.
snippet :: String -> String -> Identifier -> Context String
snippet name key templatePath = field name $ \item -> do
    metadata <- getMetadata (itemIdentifier item)
    case M.lookup key metadata of
      Just file -> itemBody <$> loadAndApplyTemplate templatePath postCtx item
      _         -> return ""

photoSnippet   = snippet "photo-snippet" "photo" "templates/photo.html"
flattrSnippet  = snippet "flattr-snippet" "flattr" "templates/flattr.html"
licenseSnippet = snippet "license-snippet" "license" "templates/license.html"

postFeed renderer = do
  route idRoute
  compile $ do
    let feedCtx = postCtx `mappend` bodyField "description"
    posts <- fmap (take 10) . recentFirst =<<
      loadAllSnapshots "posts/*.markdown" "content"
    renderer myFeedConfiguration feedCtx posts

--------------------------------------------------------------------------------
postList :: ([Item String] -> Compiler [Item String]) -> Compiler String
postList sortFilter = do
  posts   <- sortFilter =<< loadAll ("posts/*.markdown" .&&. hasNoVersion)
  itemTpl <- loadBody "templates/post-item.html"
  list    <- applyTemplateList itemTpl postCtx posts
  return list

customPandocCompiler :: Compiler (Item String)
customPandocCompiler =
    pandocCompilerWith
      (addRExt [Ext_pipe_tables] defaultHakyllReaderOptions)
      (addWExt [Ext_pipe_tables] defaultHakyllWriterOptions)
  where
    addRExt es opts =
      opts { readerExtensions = S.union (S.fromList es) (readerExtensions opts)
           , readerSmart = True
           }
    addWExt es opts =
      opts { writerExtensions = S.union (S.fromList es) (writerExtensions opts) }

defaultCompiler = customPandocCompiler
  >>= loadAndApplyTemplate "templates/post.html"    postCtx
  >>= saveSnapshot "content"
  >>= loadAndApplyTemplate "templates/default.html" postCtx
  >>= saveSnapshot "non-relative"
  >>= relativizeUrls