diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2017-02-14 13:01:01 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2017-02-14 13:02:07 +0100 |
commit | 9a71775a4a74a193f89bc408933e23672ac88e96 (patch) | |
tree | 362fca2c9f760627f7e03c4a5b683b7b2af06176 /src/Hakyll/Web | |
parent | ba272c9ba9cddb306cd30c976fb95a8ae7a06b85 (diff) | |
download | hakyll-9a71775a4a74a193f89bc408933e23672ac88e96.tar.gz |
Provide an `$allPages$` key when doing pagination
This list offers in turn the `$num$`, `$url$` and `$isCurrent$` fields
Diffstat (limited to 'src/Hakyll/Web')
-rw-r--r-- | src/Hakyll/Web/Paginate.hs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/Hakyll/Web/Paginate.hs b/src/Hakyll/Web/Paginate.hs index 32e5631..dd058f6 100644 --- a/src/Hakyll/Web/Paginate.hs +++ b/src/Hakyll/Web/Paginate.hs @@ -11,7 +11,8 @@ module Hakyll.Web.Paginate -------------------------------------------------------------------------------- -import Control.Monad (forM_) +import Control.Applicative (empty) +import Control.Monad (forM_, forM) import qualified Data.Map as M import qualified Data.Set as S @@ -93,6 +94,18 @@ paginatePage pag pageNumber -- | A default paginate context which provides the following keys: -- -- +-- * @firstPageNum@ +-- * @firstPageUrl@ +-- * @previousPageNum@ +-- * @previousPageUrl@ +-- * @nextPageNum@ +-- * @nextPageUrl@ +-- * @lastPageNum@ +-- * @lastPageUrl@ +-- * @currentPageNum@ +-- * @currentPageUrl@ +-- * @numPages@ +-- * @allPages@ paginateContext :: Paginate -> PageNumber -> Context a paginateContext pag currentPage = mconcat [ field "firstPageNum" $ \_ -> otherPage 1 >>= num @@ -106,6 +119,20 @@ paginateContext pag currentPage = mconcat , field "currentPageNum" $ \i -> thisPage i >>= num , field "currentPageUrl" $ \i -> thisPage i >>= url , constField "numPages" $ show $ paginateNumPages pag + , Context $ \k _ i -> case k of + "allPages" -> do + let ctx = + field "isCurrent" (\n -> if fst (itemBody n) == currentPage then return "true" else empty) `mappend` + field "num" (num . itemBody) `mappend` + field "url" (url . itemBody) + + list <- forM [1 .. lastPage] $ + \n -> if n == currentPage then thisPage i else otherPage n + items <- mapM makeItem list + return $ ListField ctx items + _ -> do + empty + ] where lastPage = paginateNumPages pag |