{-# LANGUAGE OverloadedStrings #-} module Main where import Control.Monad (Monad(..), filterM, void) import Data.Bool import Data.Either (Either(..)) import Data.Eq (Eq(..)) import Data.Function (($), (.), const) import Data.Functor ((<$>)) import Data.Functor.Identity (runIdentity) import Data.Maybe (Maybe(..)) import Data.Monoid (mconcat) import Data.Semigroup (Semigroup(..)) import Data.String (String) import Hakyll.Web.Sass (sassCompiler) import Prelude (error, (-)) import System.IO (IO) import qualified Data.List as List import qualified Data.Text as T import qualified Hakyll as H import qualified System.Environment as Env import qualified Text.Pandoc.Options as Pandoc import qualified Text.Pandoc.Templates as Pandoc main :: IO () main = do args <- Env.getArgs Env.withArgs args $ H.hakyllWith H.defaultConfiguration $ do let postsPattern = "content/posts/*.md" tags <- H.buildTags postsPattern $ H.fromCapture "tags/*/index.html" let siteCtx = mconcat [ H.defaultContext ] let tagsCtx = mconcat [ H.tagsField "tags-links" tags , H.field "tags-counts" (\_ -> H.renderTagList tags) ] let postCtx = mconcat [ H.dateField "date" "%e %B %Y" , H.dateField "datetime" "%Y-%m-%d" -- , tagLinks H.getTags "tags" tags , tagsCtx , siteCtx ] let postList sortFilter = do itemTmpl <- H.loadBody "templates/post-link.html" H.loadAll postsPattern >>= sortFilter >>= H.applyTemplateList itemTmpl postCtx H.match "content/index.html" $ do let ctx = mconcat [ H.constField "title" "Index" -- , H.field "tags-cloud" $ \_ -> tagCloud 60 150 tags , tagsCtx , siteCtx ] H.route $ stripRoute "content/" H.compile $ do body <- H.itemBody <$> H.templateBodyCompiler H.loadAllSnapshots postsPattern "teaser" >>= ((List.take 10 <$>) . H.recentFirst) >>= H.applyTemplateList body (postCtx <> H.bodyField "posts") >>= H.makeItem >>= H.loadAndApplyTemplate "templates/index.html" (ctx <> H.bodyField "posts") >>= H.loadAndApplyTemplate "templates/default.html" (ctx <> H.bodyField "posts") >>= H.relativizeUrls >>= chopIndexUrls H.create ["posts/index.html"] $ do H.route H.idRoute H.compile $ do let ctx = mconcat [ H.constField "title" "Blog" , H.constField "description" "Web log entries" , H.field "posts" (\_ -> postList H.recentFirst) , siteCtx ] H.makeItem "" >>= H.loadAndApplyTemplate "templates/posts-list.html" ctx >>= H.loadAndApplyTemplate "templates/default.html" ctx >>= H.relativizeUrls >>= chopIndexUrls H.match postsPattern $ do H.route $ --directorizeDate `H.composeRoutes` stripRoute "content/" `H.composeRoutes` H.setExtension "html" H.compile $ do ident <- H.getUnderlying toc <- H.getMetadataField ident "tableOfContents" source <- H.getResourceFilePath updated <- H.unixFilter "git" ["log", "-1", "--format=%ad", "--", source] "" let ctx = mconcat [ H.constField "source" source , H.constField "updated" updated , postCtx ] compiledContent <- H.pandocCompilerWith H.defaultHakyllReaderOptions $ case toc of Just "true" -> H.defaultHakyllWriterOptions { Pandoc.writerNumberSections = True , Pandoc.writerTableOfContents = True , Pandoc.writerTOCDepth = 3 , Pandoc.writerTemplate = Just tocTemplate } where tocTemplate = case runIdentity $ Pandoc.compileTemplate "" tmpl of Left err -> error err Right template -> template where tmpl = T.unlines [ "" , "