@@ -1,8 +1,7 @@
11--------------------------------------------------------------------------------
22{-# LANGUAGE OverloadedStrings #-}
33import Control.Arrow (second)
4−import Control.Monad (forM_)
5−import Data.Char (isDigit)
4+import Control.Monad (filterM, forM_)
65import Data.List (isPrefixOf, sortBy)
76import Data.Monoid ((<>))
87import Data.Ord (comparing)
@@ -57,17 +56,11 @@ main = hakyllWith config $ do
5756 create ["tutorials.html"] $ do
5857 route idRoute
5958 compile $ do
60− tuts <-
59+ ctx <- tutorialsCtx <$>
6160 sortBy (comparing itemIdentifier) <$> loadAll "tutorials/*"
62−
63− let tutorialsCtx =
64− constField "title" "Tutorials" `mappend`
65− listField "tutorials" tutorialCtx (return tuts) `mappend`
66− defaultContext
67−
6861 makeItem ""
69− >>= loadAndApplyTemplate "templates/tutorials.html" tutorialsCtx
70− >>= loadAndApplyTemplate "templates/default.html" tutorialsCtx
62+ >>= loadAndApplyTemplate "templates/tutorials.html" ctx
63+ >>= loadAndApplyTemplate "templates/default.html" ctx
7164 >>= relativizeUrls
7265
7366 -- Templates
@@ -108,28 +101,16 @@ hackage url
108101 reverse $ splitPath url
109102
110103
111−--------------------------------------------------------------------------------
112−data TutorialType = SeriesTutorial | ArticlesTutorial | ExternalTutorial
113− deriving (Eq)
114−
115−
116104--------------------------------------------------------------------------------
117105-- | Partition tutorials into tutorial series, other articles, external articles
118−tutorialCtx :: Context String
119−tutorialCtx =
120− field "isSeries" (isTutorialType SeriesTutorial) <>
121− field "isArticle" (isTutorialType ArticlesTutorial) <>
122− field "isExternal" (isTutorialType ExternalTutorial) <>
106+tutorialsCtx :: [Item String] -> Context String
107+tutorialsCtx tuts =
108+ constField "title" "Tutorials" <>
109+ listField "main" defaultContext (ofType "main") <>
110+ listField "articles" defaultContext (ofType "article") <>
111+ listField "externals" defaultContext (ofType "external") <>
123112 defaultContext
124113 where
125− getTutorialType item = do
126− mbExternal <- getMetadataField (itemIdentifier item) "external"
127− return $ case mbExternal of
128− Just _ -> ExternalTutorial
129− _ -> case splitPath (toFilePath $ itemIdentifier item) of
130− [_, (x : _)] -> if isDigit x then SeriesTutorial else ArticlesTutorial
131− _ -> ArticlesTutorial
132−
133− isTutorialType ty0 item = do
134− ty1 <- getTutorialType item
135− if ty0 == ty1 then return "yes" else fail "no"
114+ ofType ty = filterM (\item -> do
115+ mbType <- getMetadataField (itemIdentifier item) "type"
116+ return $ Just ty == mbType) tuts