Skip to content

Commit

Permalink
Revert "Removed TemplateDirectory and replaced it with HeistT which p…
Browse files Browse the repository at this point in the history
…rovides similar functionality."

This reverts commit e3727be.
  • Loading branch information
duairc committed Jul 15, 2010
1 parent 6103ba9 commit 0e12582
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 249 deletions.
6 changes: 3 additions & 3 deletions heist.cabal
@@ -1,5 +1,5 @@
name: heist
version: 0.3.0
version: 0.2.3
synopsis: An xhtml templating system
license: BSD3
license-file: LICENSE
Expand Down Expand Up @@ -80,7 +80,7 @@ Library
Text.Templating.Heist.Splices.Ignore,
Text.Templating.Heist.Splices.Markdown,
Text.Templating.Heist.Splices.Static,
Text.Templating.Heist.Monad
Text.Templating.Heist.TemplateDirectory

other-modules:
Text.Templating.Heist.Internal,
Expand All @@ -95,7 +95,7 @@ Library
directory,
directory-tree,
filepath,
hexpat >= 0.16 && <= 0.17,
hexpat >= 0.16 && <0.17,
MonadCatchIO-transformers >= 0.2.1 && < 0.3,
monads-fd,
process,
Expand Down
246 changes: 0 additions & 246 deletions src/Text/Templating/Heist/Monad.hs

This file was deleted.

85 changes: 85 additions & 0 deletions src/Text/Templating/Heist/TemplateDirectory.hs
@@ -0,0 +1,85 @@
{-|
This module defines a TemplateDirectory data structure for convenient
interaction with templates within web apps.
-}

module Text.Templating.Heist.TemplateDirectory
( TemplateDirectory
, newTemplateDirectory
, newTemplateDirectory'

, getDirectoryTS
, reloadTemplateDirectory
) where

------------------------------------------------------------------------------
import Control.Concurrent
import Control.Monad
import Control.Monad.Trans
import Data.ByteString.Char8 (ByteString)
import Text.Templating.Heist
import Text.Templating.Heist.Splices.Static


------------------------------------------------------------------------------
-- | Structure representing a template directory.
data TemplateDirectory m
= TemplateDirectory
FilePath
(TemplateState m)
(MVar (TemplateState m))
StaticTagState


------------------------------------------------------------------------------
-- | Creates and returns a new 'TemplateDirectory' wrapped in an Either for
-- error handling.
newTemplateDirectory :: (MonadIO m, MonadIO n)
=> FilePath
-> TemplateState m
-> n (Either String (TemplateDirectory m))
newTemplateDirectory dir templateState = liftIO $ do
(origTs,sts) <- bindStaticTag templateState
ets <- loadTemplates dir origTs
leftPass ets $ \ts -> do
tsMVar <- newMVar $ ts
return $ TemplateDirectory dir origTs tsMVar sts


------------------------------------------------------------------------------
-- | Creates and returns a new 'TemplateDirectory', using the monad's fail
-- function on error.
newTemplateDirectory' :: (MonadIO m, MonadIO n)
=> FilePath
-> TemplateState m
-> n (TemplateDirectory m)
newTemplateDirectory' = ((either fail return =<<) .) . newTemplateDirectory


------------------------------------------------------------------------------
-- | Gets the 'TemplateState' from a TemplateDirectory.
getDirectoryTS :: (Monad m, MonadIO n)
=> TemplateDirectory m
-> n (TemplateState m)
getDirectoryTS (TemplateDirectory _ _ tsMVar _) = liftIO $ readMVar $ tsMVar


------------------------------------------------------------------------------
-- | Clears cached content and reloads templates from disk.
reloadTemplateDirectory :: (MonadIO m, MonadIO n)
=> TemplateDirectory m
-> n (Either String ())
reloadTemplateDirectory (TemplateDirectory p origTs tsMVar sts) = liftIO $ do
clearStaticTagCache sts
ets <- loadTemplates p origTs
leftPass ets $ \ts -> modifyMVar_ tsMVar (const $ return ts)


------------------------------------------------------------------------------
-- | Prepends an error onto a Left.
leftPass :: Monad m => Either String b -> (b -> m c) -> m (Either String c)
leftPass e m = either (return . Left . loadError) (liftM Right . m) e
where
loadError = (++) "Error loading templates: "

0 comments on commit 0e12582

Please sign in to comment.