Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
118 lines (101 sloc) 3.74 KB
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE TemplateHaskell #-}
import Control.Monad (filterM)
import Data.Monoid ((<>))
import System.FilePath ((</>), takeDirectory)
import Data.Maybe (fromMaybe)
import qualified Language.Haskell.TH as TH
import System.Directory (getCurrentDirectory)
import Agda.Interaction.Options (CommandLineOptions(..), defaultOptions)
import Hakyll
import Hakyll.Web.Agda
import Text.Pandoc.Options (WriterOptions(..))
main :: IO ()
main = hakyll $ do -- Assets
match ("assets/images/*" .||. "assets/css/*" .||. "assets/js/*" .||. "assets/other/*") $ do
route idRoute
compile copyFileCompiler
-- Templates
match "templates/*" $ do
compile templateCompiler
-- Posts
let agdaComp = pandocAgdaCompilerWith defaultHakyllReaderOptions writerOpts agdaOpts
match ("posts/*.md" .||. "posts/*.lagda" .||. "posts/*.lhs") $ do
route $ setExtension "html"
compile $
agdaComp >>=
loadAndApplyTemplate "templates/post.html" postCtx >>=
saveSnapshot "content" >>=
loadAndApplyTemplate "templates/default.html" postCtx >>=
relativizeUrls
-- Archive
create ["archive.html"] $ do
route idRoute
let archiveCtx =
field "posts" (\_ -> postList recentFirst) <> constField "title" "index" <> defaultContext
compile $
makeItem "" >>=
loadAndApplyTemplate "templates/archive.html" archiveCtx >>=
loadAndApplyTemplate "templates/default.html" archiveCtx >>=
relativizeUrls
-- Splash page
match "index.html" $ do
route idRoute
compile $ getResourceBody >>= relativizeUrls
-- RSS and Atom
create ["atom.xml"] $ renderFeed renderAtom
create ["rss.xml"] $ renderFeed renderRss
-- CV
match "cv/*" $ do
route idRoute
compile copyFileCompiler
postCtx :: Context String
postCtx = mconcat
[ dateField "date" "%Y-%m-%d"
, listFieldWith "tags" (field "tag" (return . itemBody)) $ \item -> do
let identifier = itemIdentifier item
meta <- getMetadata identifier
let tags = fromMaybe ["post"] $ lookupStringList "tags" meta
return $ map (Item identifier) tags
, defaultContext
]
postList :: ([Item String] -> Compiler [Item String]) -> Compiler String
postList sortFilter = do
posts <- filterM isPublished =<< sortFilter =<< loadAll "posts/*"
itemTpl <- loadBody "templates/post-item.html"
list <- applyTemplateList itemTpl postCtx posts
return list
isPublished :: MonadMetadata m => Item a -> m Bool
isPublished (itemIdentifier -> ident) = do
publishedM <- getMetadataField ident "published"
case publishedM of
Nothing -> return True
Just "false" -> return False
Just s -> fail ("invalid `published' metadata value: " ++ s)
renderFeed
:: (FeedConfiguration -> Context String -> [Item String] -> Compiler (Item String))
-> Rules ()
renderFeed f = do
route idRoute
let feedCtx = postCtx <> bodyField "description"
compile $ do
posts <- fmap (take 10) . recentFirst =<< filterM isPublished =<< loadAllSnapshots "posts/*" "content"
f feedConf feedCtx posts
feedConf :: FeedConfiguration
feedConf = FeedConfiguration
{ feedTitle = "bitonic's blog."
, feedDescription = "Often in error, never in doubt."
, feedAuthorName = "Francesco Mazzoli"
, feedAuthorEmail = "f@mazzo.li"
, feedRoot = "http://mazzo.li"
}
writerOpts :: WriterOptions
writerOpts = defaultHakyllWriterOptions {writerTableOfContents = True}
websiteDirectory :: FilePath
websiteDirectory = $(do
dir <- TH.runIO getCurrentDirectory
TH.litE (TH.stringL dir))
agdaOpts :: CommandLineOptions
agdaOpts = defaultOptions {optIncludeDirs = Left [".", websiteDirectory </> "agdalib/src"]}