on master help sign in
Added simple pagination (unstable).
commiteaf0c230fb776a9884bd999be7c3b9d6f79fa239 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateMon 1 Mar 2010, 18:17 (17 years ago) parent9576700
4 files changed+86−11
hakyll.cabal+1 src/Text/Hakyll/Context.hs+3−3 src/Text/Hakyll/Paginate.hs+74 src/Text/Hakyll/Render.hs+8−8
hakyll.cabal+1
@@ -47,6 +47,7 @@ library
4747 Text.Hakyll.Renderable
4848 Text.Hakyll.Renderables
4949 Text.Hakyll.Page
50+ Text.Hakyll.Paginate
5051 Text.Hakyll.Util
5152 Text.Hakyll.Tags
5253 Text.Hakyll.Internal.Cache
src/Text/Hakyll/Context.hs+3−3
@@ -27,8 +27,8 @@ type ContextManipulation = Context -> Context
2727-- | Do something with a value in a @Context@, but keep the old value as well.
2828-- This is probably the most common function to construct a
2929-- @ContextManipulation@.
30−renderValue :: String -- ^ Key of which the value should be copied.
31− -> String -- ^ Key the value should be copied to.
30+renderValue :: String -- ^ Key of which the value should be copied.
31+ -> String -- ^ Key the value should be copied to.
3232 -> (String -> String) -- ^ Function to apply on the value.
3333 -> ContextManipulation
3434renderValue src dst f context = case M.lookup src context of
@@ -41,7 +41,7 @@ renderValue src dst f context = case M.lookup src context of
4141-- > changeValue "title" (map toUpper)
4242--
4343-- Will put the title in UPPERCASE.
44−changeValue :: String -- ^ Key of which the value should be changed.
44+changeValue :: String -- ^ Key to change.
4545 -> (String -> String) -- ^ Function to apply on the value.
4646 -> ContextManipulation
4747changeValue key = renderValue key key
src/Text/Hakyll/Paginate.hsnew file+74
@@ -0,0 +1,74 @@
1+-- | Module aimed to paginate web pages.
2+module Text.Hakyll.Paginate
3+ ( PaginateConfiguration (..)
4+ , defaultPaginateConfiguration
5+ , paginate
6+ ) where
7+
8+import Text.Hakyll.Renderables
9+import Text.Hakyll.Renderable (Renderable, getUrl)
10+import Text.Hakyll.Util (link)
11+
12+-- | A configuration for a pagination.
13+data PaginateConfiguration = PaginateConfiguration
14+ { -- | Label for the link to the previous page.
15+ previousLabel :: String
16+ , -- | Label for the link to the next page.
17+ nextLabel :: String
18+ , -- | Label for the link to the first page.
19+ firstLabel :: String
20+ , -- | Label for the link to the last page.
21+ lastLabel :: String
22+ }
23+
24+-- | A simple default configuration for pagination.
25+defaultPaginateConfiguration :: PaginateConfiguration
26+defaultPaginateConfiguration = PaginateConfiguration
27+ { previousLabel = "Previous"
28+ , nextLabel = "Next"
29+ , firstLabel = "First"
30+ , lastLabel = "Last"
31+ }
32+
33+-- | The most important function for pagination. This function operates on a
34+-- list of renderables (the pages), and basically just adds fields to them
35+-- by combining them with a custom page.
36+--
37+-- The following metadata fields will be added:
38+--
39+-- - @$previous@: A link to the previous page.
40+-- - @$next@: A link to the next page.
41+-- - @$first@: A link to the first page.
42+-- - @$last@: A link to the last page.
43+-- - @$index@: 1-based index of the current page.
44+-- - @$length@: Total number of pages.
45+--
46+paginate :: (Renderable a)
47+ => PaginateConfiguration
48+ -> [a]
49+ -> [CombinedRenderable a CustomPage]
50+paginate configuration renderables = paginate' Nothing renderables (1 :: Int)
51+ where
52+ linkWithLabel f r = link (f configuration) `fmap` getUrl r
53+
54+ first = linkWithLabel firstLabel (head renderables)
55+ last' = linkWithLabel lastLabel (last renderables)
56+ length' = length renderables
57+
58+ paginate' _ [] _ = []
59+ paginate' maybePrev (x:xs) index =
60+ let previous = case maybePrev of
61+ (Just r) -> linkWithLabel previousLabel r
62+ Nothing -> return $ previousLabel configuration
63+ next = case xs of
64+ (n:_) -> linkWithLabel nextLabel n
65+ [] -> return $ nextLabel configuration
66+ customPage = createCustomPage "" []
67+ [ ("previous", Right previous)
68+ , ("next", Right next)
69+ , ("first", Right first)
70+ , ("last", Right last')
71+ , ("index", Left $ show index)
72+ , ("length", Left $ show length')
73+ ]
74+ in (x `combine` customPage) : paginate' (Just x) xs (index + 1)
src/Text/Hakyll/Render.hs+8−8
@@ -26,9 +26,9 @@ import Text.Hakyll.Internal.CompressCss
2626import Text.Hakyll.Internal.Render
2727
2828-- | Execute an IO action only when the cache is invalid.
29−depends :: FilePath -- ^ File to be rendered or created.
29+depends :: FilePath -- ^ File to be rendered or created.
3030 -> [FilePath] -- ^ Files the render depends on.
31− -> Hakyll () -- ^ IO action to execute when the file is out of date.
31+ -> Hakyll () -- ^ Action to execute when the file is out of date.
3232 -> Hakyll ()
3333depends file dependencies action = do
3434 destination <- toDestination file
@@ -37,8 +37,8 @@ depends file dependencies action = do
3737
3838-- | Render to a Page.
3939render :: Renderable a
40− => FilePath -- ^ Template to use for rendering.
41− -> a -- ^ Renderable object to render with given template.
40+ => FilePath -- ^ Template to use for rendering.
41+ -> a -- ^ Renderable object to render with given template.
4242 -> Hakyll Page -- ^ The body of the result will contain the render.
4343render = renderWith id
4444
@@ -46,9 +46,9 @@ render = renderWith id
4646-- first.
4747renderWith :: Renderable a
4848 => ContextManipulation -- ^ Manipulation to apply on the context.
49− -> FilePath -- ^ Template to use for rendering.
50− -> a -- ^ Renderable object to render with given template.
51− -> Hakyll Page -- ^ The body of the result will contain the render.
49+ -> FilePath -- ^ Template to use for rendering.
50+ -> a -- ^ Data to render.
51+ -> Hakyll Page -- ^ Result of the render operation.
5252renderWith manipulation templatePath renderable = do
5353 template <- readTemplate templatePath
5454 context <- toContext renderable
@@ -66,7 +66,7 @@ renderWith manipulation templatePath renderable = do
6666--
6767renderAndConcat :: Renderable a
6868 => [FilePath] -- ^ Templates to apply on every renderable.
69− -> [a] -- ^ Renderables to render.
69+ -> [a] -- ^ Renderables to render.
7070 -> Hakyll String
7171renderAndConcat = renderAndConcatWith id
7272