From 99200aef5d99a9ffe1c9c98fc9d31db4a78d2e26 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Wed, 26 Dec 2012 15:00:45 +0100 Subject: Foldable/Traversable instance for Item --- src/Hakyll/Core/Item.hs | 30 ++++++++++++++++++++++++------ src/Hakyll/Core/UnixFilter.hs | 6 +++--- 2 files changed, 27 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/Hakyll/Core/Item.hs b/src/Hakyll/Core/Item.hs index ccf9e9a..8933d5b 100644 --- a/src/Hakyll/Core/Item.hs +++ b/src/Hakyll/Core/Item.hs @@ -5,17 +5,21 @@ module Hakyll.Core.Item ( Item (..) , itemSetBody - , itemM + , withItemBody ) where -------------------------------------------------------------------------------- -import Control.Applicative ((<$>), (<*>)) -import Data.Binary (Binary (..)) -import Data.Typeable (Typeable) +import Control.Applicative (Applicative, (<$>), (<*>)) +import Data.Binary (Binary (..)) +import Data.Foldable (Foldable (..)) +import Data.Traversable (Traversable (..)) +import Data.Typeable (Typeable) +import Prelude hiding (foldr) -------------------------------------------------------------------------------- +import Hakyll.Core.Compiler.Internal import Hakyll.Core.Identifier @@ -31,6 +35,16 @@ instance Functor Item where fmap f (Item i x) = Item i (f x) +-------------------------------------------------------------------------------- +instance Foldable Item where + foldr f z (Item _ x) = f x z + + +-------------------------------------------------------------------------------- +instance Traversable Item where + traverse f (Item i x) = Item i <$> f x + + -------------------------------------------------------------------------------- instance Binary a => Binary (Item a) where put (Item i x) = put i >> put x @@ -43,5 +57,9 @@ itemSetBody x (Item i _) = Item i x -------------------------------------------------------------------------------- -itemM :: Monad m => (a -> m b) -> Item a -> m (Item b) -itemM f (Item i b) = f b >>= \b' -> return (Item i b') +-- | Perform a compiler action on the item body. This is the same as 'traverse', +-- but looks less intimidating. +-- +-- > withItemBody = traverse +withItemBody :: (a -> Compiler b) -> Item a -> Compiler (Item b) +withItemBody = traverse diff --git a/src/Hakyll/Core/UnixFilter.hs b/src/Hakyll/Core/UnixFilter.hs index dc82366..e493aee 100644 --- a/src/Hakyll/Core/UnixFilter.hs +++ b/src/Hakyll/Core/UnixFilter.hs @@ -26,7 +26,7 @@ import Hakyll.Core.Compiler -- as a compiler. -- -- > rev :: Compiler String --- > rev = getResourceString >>= itemM (unixFilter "rev" []) +-- > rev = getResourceString >>= withItemBody (unixFilter "rev" []) -- -- A more realistic example: one can use this to call, for example, the sass -- compiler on CSS files. More information about sass can be found here: @@ -38,7 +38,7 @@ import Hakyll.Core.Compiler -- > match "style.scss" $ do -- > route $ setExtension "css" -- > compile $ getResourceString >>= --- > itemM (unixFilter "sass" ["-s", "--scss"]) >>= +-- > withItemBody (unixFilter "sass" ["-s", "--scss"]) >>= -- > compressCssCompiler unixFilter :: String -- ^ Program name -> [String] -- ^ Program args @@ -59,7 +59,7 @@ unixFilter = unixFilterWith writer reader -- -- > match "music.wav" $ do -- > route $ setExtension "ogg" --- > compile $ getResourceLBS >>= unixFilter "oggenc" ["-"] +-- > compile $ getResourceLBS >>= withItemBody (unixFilter "oggenc" ["-"]) unixFilterLBS :: String -- ^ Program name -> [String] -- ^ Program args -> ByteString -- ^ Program input -- cgit v1.2.3