summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2012-12-26 15:00:45 +0100
committerJasper Van der Jeugt <m@jaspervdj.be>2012-12-26 15:00:45 +0100
commit99200aef5d99a9ffe1c9c98fc9d31db4a78d2e26 (patch)
tree61a095714808ffe027e4bcce3621b402bf2b3817 /src
parentf61c5a7278613486c214c65080c003682f033c85 (diff)
downloadhakyll-99200aef5d99a9ffe1c9c98fc9d31db4a78d2e26.tar.gz
Foldable/Traversable instance for Item
Diffstat (limited to 'src')
-rw-r--r--src/Hakyll/Core/Item.hs30
-rw-r--r--src/Hakyll/Core/UnixFilter.hs6
2 files changed, 27 insertions, 9 deletions
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
@@ -32,6 +36,16 @@ instance Functor Item where
--------------------------------------------------------------------------------
+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
get = Item <$> get <*> get
@@ -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