Skip to content

Commit

Permalink
Provide an $allPages$ key when doing pagination
Browse files Browse the repository at this point in the history
This list offers in turn the `$num$`, `$url$` and `$isCurrent$` fields
  • Loading branch information
jaspervdj committed Feb 14, 2017
1 parent ba272c9 commit 9a71775
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Hakyll/Web/Paginate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9a71775

Please sign in to comment.