--------------------------------------------------------------------------------
-- | Describes writable items; items that can be saved to the disk
{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Hakyll.Core.Writable
    ( Writable (..)
    ) where


--------------------------------------------------------------------------------
import qualified Data.ByteString                 as SB
import qualified Data.ByteString.Lazy            as LB
import           Data.Word                       (Word8)
import           Text.Blaze.Html                 (Html)
import           Text.Blaze.Html.Renderer.String (renderHtml)


--------------------------------------------------------------------------------
import           Hakyll.Core.Item


--------------------------------------------------------------------------------
-- | Describes an item that can be saved to the disk
class Writable a where
    -- | Save an item to the given filepath
    write :: FilePath -> Item a -> IO ()


--------------------------------------------------------------------------------
instance Writable () where
    write :: FilePath -> Item () -> IO ()
write FilePath
_ Item ()
_ = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()


--------------------------------------------------------------------------------
instance Writable [Char] where
    write :: FilePath -> Item FilePath -> IO ()
write FilePath
p = FilePath -> FilePath -> IO ()
writeFile FilePath
p (FilePath -> IO ())
-> (Item FilePath -> FilePath) -> Item FilePath -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item FilePath -> FilePath
forall a. Item a -> a
itemBody


--------------------------------------------------------------------------------
instance Writable SB.ByteString where
    write :: FilePath -> Item ByteString -> IO ()
write FilePath
p = FilePath -> ByteString -> IO ()
SB.writeFile FilePath
p (ByteString -> IO ())
-> (Item ByteString -> ByteString) -> Item ByteString -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item ByteString -> ByteString
forall a. Item a -> a
itemBody


--------------------------------------------------------------------------------
instance Writable LB.ByteString where
    write :: FilePath -> Item ByteString -> IO ()
write FilePath
p = FilePath -> ByteString -> IO ()
LB.writeFile FilePath
p (ByteString -> IO ())
-> (Item ByteString -> ByteString) -> Item ByteString -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item ByteString -> ByteString
forall a. Item a -> a
itemBody


--------------------------------------------------------------------------------
instance Writable [Word8] where
    write :: FilePath -> Item [Word8] -> IO ()
write FilePath
p = FilePath -> Item ByteString -> IO ()
forall a. Writable a => FilePath -> Item a -> IO ()
write FilePath
p (Item ByteString -> IO ())
-> (Item [Word8] -> Item ByteString) -> Item [Word8] -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Word8] -> ByteString) -> Item [Word8] -> Item ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Word8] -> ByteString
SB.pack


--------------------------------------------------------------------------------
instance Writable Html where
    write :: FilePath -> Item Html -> IO ()
write FilePath
p = FilePath -> Item FilePath -> IO ()
forall a. Writable a => FilePath -> Item a -> IO ()
write FilePath
p (Item FilePath -> IO ())
-> (Item Html -> Item FilePath) -> Item Html -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Html -> FilePath) -> Item Html -> Item FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Html -> FilePath
renderHtml