From d832cd80ddf43872532db8943f310eed4edb7fe5 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Sun, 27 Jan 2019 16:40:40 +0100 Subject: Refactor tutorials on hakyll website --- web/site.hs | 45 +++++++++++++-------------------------------- 1 file changed, 13 insertions(+), 32 deletions(-) (limited to 'web/site.hs') diff --git a/web/site.hs b/web/site.hs index a20d15b..2bfaf3e 100644 --- a/web/site.hs +++ b/web/site.hs @@ -1,8 +1,7 @@ -------------------------------------------------------------------------------- {-# LANGUAGE OverloadedStrings #-} import Control.Arrow (second) -import Control.Monad (forM_) -import Data.Char (isDigit) +import Control.Monad (filterM, forM_) import Data.List (isPrefixOf, sortBy) import Data.Monoid ((<>)) import Data.Ord (comparing) @@ -57,17 +56,11 @@ main = hakyllWith config $ do create ["tutorials.html"] $ do route idRoute compile $ do - tuts <- + ctx <- tutorialsCtx <$> sortBy (comparing itemIdentifier) <$> loadAll "tutorials/*" - - let tutorialsCtx = - constField "title" "Tutorials" `mappend` - listField "tutorials" tutorialCtx (return tuts) `mappend` - defaultContext - makeItem "" - >>= loadAndApplyTemplate "templates/tutorials.html" tutorialsCtx - >>= loadAndApplyTemplate "templates/default.html" tutorialsCtx + >>= loadAndApplyTemplate "templates/tutorials.html" ctx + >>= loadAndApplyTemplate "templates/default.html" ctx >>= relativizeUrls -- Templates @@ -108,28 +101,16 @@ hackage url reverse $ splitPath url --------------------------------------------------------------------------------- -data TutorialType = SeriesTutorial | ArticlesTutorial | ExternalTutorial - deriving (Eq) - - -------------------------------------------------------------------------------- -- | Partition tutorials into tutorial series, other articles, external articles -tutorialCtx :: Context String -tutorialCtx = - field "isSeries" (isTutorialType SeriesTutorial) <> - field "isArticle" (isTutorialType ArticlesTutorial) <> - field "isExternal" (isTutorialType ExternalTutorial) <> +tutorialsCtx :: [Item String] -> Context String +tutorialsCtx tuts = + constField "title" "Tutorials" <> + listField "main" defaultContext (ofType "main") <> + listField "articles" defaultContext (ofType "article") <> + listField "externals" defaultContext (ofType "external") <> defaultContext where - getTutorialType item = do - mbExternal <- getMetadataField (itemIdentifier item) "external" - return $ case mbExternal of - Just _ -> ExternalTutorial - _ -> case splitPath (toFilePath $ itemIdentifier item) of - [_, (x : _)] -> if isDigit x then SeriesTutorial else ArticlesTutorial - _ -> ArticlesTutorial - - isTutorialType ty0 item = do - ty1 <- getTutorialType item - if ty0 == ty1 then return "yes" else fail "no" + ofType ty = filterM (\item -> do + mbType <- getMetadataField (itemIdentifier item) "type" + return $ Just ty == mbType) tuts -- cgit v1.2.3