rauhala.info/site/app/site.hs
2021-01-25 21:11:14 +02:00

108 lines
3.0 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
import Hakyll
import Data.List
(isPrefixOf, isSuffixOf)
import System.FilePath
(takeFileName)
--------------------------------------------------------------------------------
main :: IO ()
main = hakyllWith defaultConfiguration{ignoreFile = ignore} $ do
match "well-known/*" $ do
route (customRoute (prepend '.'. toFilePath))
compile copyFileCompiler
match "images/*" $ do
route idRoute
compile copyFileCompiler
match "resources/*" $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do
route idRoute
compile compressCssCompiler
match "js/*" $ do
route idRoute
compile compressCssCompiler
match "posts/*" $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/post.html" postContext
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= relativizeUrls
match "projects/*" $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/project.html" postContext
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= relativizeUrls
match (fromList ["index.markdown", "contact.markdown"]) $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= relativizeUrls
archive $ Archive { output = "posts.html"
, input = "posts/*"
, title = "Posts"
, template = "templates/post-list.html"
, context = postContext
}
archive $ Archive { output = "projects.html"
, input = "projects/*"
, title = "Projects"
, template = "templates/project-list.html"
, context = postContext
}
match "templates/*" $ compile templateBodyCompiler
where
postContext :: Context String
postContext = dateField "date" "%B %e, %Y" <> defaultContext
ignore :: FilePath -> Bool
ignore path = any ($ takeFileName path)
[ ("." `isPrefixOf`)
, ("#" `isPrefixOf`)
, ("~" `isSuffixOf`)
, (".swp" `isSuffixOf`)
]
data Archive
= Archive { output :: Identifier
, input :: Pattern
, title :: String
, template :: Identifier
, context :: Context String
}
archive :: Archive -> Rules ()
archive Archive{..} = create [output] $ do
route idRoute
compile $ do
let itemsContext =
listField "items" context items
<> constField "title" title
<> defaultContext
items = recentFirst =<< loadAll input
makeItem ""
>>= loadAndApplyTemplate template itemsContext
>>= loadAndApplyTemplate "templates/default.html" itemsContext
>>= relativizeUrls
prepend :: a -> [a] -> [a]
prepend = (:)