Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Context instances for Contravariant, Divisible, Decidable #577

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions hakyll.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Library
blaze-markup >= 0.5.1 && < 0.9,
bytestring >= 0.9 && < 0.11,
containers >= 0.3 && < 0.6,
contravariant >= 1.0 && < 1.5,
cryptohash >= 0.7 && < 0.12,
data-default >= 0.4 && < 0.8,
deepseq >= 1.3 && < 1.5,
Expand Down Expand Up @@ -254,6 +255,7 @@ Test-suite hakyll-tests
blaze-markup >= 0.5.1 && < 0.9,
bytestring >= 0.9 && < 0.11,
containers >= 0.3 && < 0.6,
contravariant >= 1.0 && < 1.5,
cryptohash >= 0.7 && < 0.12,
data-default >= 0.4 && < 0.8,
deepseq >= 1.3 && < 1.5,
Expand Down
35 changes: 26 additions & 9 deletions lib/Hakyll/Web/Template/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,25 @@ module Hakyll.Web.Template.Context


--------------------------------------------------------------------------------
import Control.Applicative (Alternative (..))
import Control.Monad (msum)
import Data.List (intercalate)
import Data.Time.Clock (UTCTime (..))
import Data.Time.Format (formatTime)
import qualified Data.Time.Format as TF
import Data.Time.Locale.Compat (TimeLocale, defaultTimeLocale)
import Control.Applicative (Alternative (..))
import Control.Monad (msum)
import Data.Functor.Contravariant (Contravariant (..))
import Data.Functor.Contravariant.Divisible (Decidable (..), Divisible (..))
import Data.List (intercalate)
import Data.Time.Clock (UTCTime (..))
import Data.Time.Format (formatTime)
import qualified Data.Time.Format as TF
import Data.Time.Locale.Compat (TimeLocale, defaultTimeLocale)
import Data.Void (absurd)
import Hakyll.Core.Compiler
import Hakyll.Core.Compiler.Internal
import Hakyll.Core.Identifier
import Hakyll.Core.Item
import Hakyll.Core.Metadata
import Hakyll.Core.Provider
import Hakyll.Core.Util.String (needlePrefix, splitAll)
import Hakyll.Core.Util.String (needlePrefix, splitAll)
import Hakyll.Web.Html
import System.FilePath (splitDirectories, takeBaseName)
import System.FilePath (splitDirectories, takeBaseName)


--------------------------------------------------------------------------------
Expand Down Expand Up @@ -83,6 +86,20 @@ instance Monoid (Context a) where
mappend (Context f) (Context g) = Context $ \k a i -> f k a i <|> g k a i


--------------------------------------------------------------------------------
instance Contravariant Context where
contramap f (Context g) = Context $ \k a i -> g k a (f <$> i)

instance Divisible Context where
conquer = mempty
divide f c1 c2 = contramap (fst . f) c1 `mappend` contramap (snd . f) c2

instance Decidable Context where
lose refute = Context $ \_k _a i -> absurd . refute . itemBody $ i
choose chooser (Context f) (Context g) = Context $ \k a i -> case chooser (itemBody i) of
Left l -> f k a (itemSetBody l i)
Right r -> g k a (itemSetBody r i)

--------------------------------------------------------------------------------
field' :: String -> (Item a -> Compiler ContextField) -> Context a
field' key value = Context $ \k _ i -> if k == key then value i else empty
Expand Down