happstack-heist-7.2.0/0000755000000000000000000000000012226556014013001 5ustar0000000000000000happstack-heist-7.2.0/LICENSE0000644000000000000000000000277412226556014014020 0ustar0000000000000000Copyright (c)2010, Chris Smith, Jeremy Shaw All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Jeremy Shaw nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. happstack-heist-7.2.0/Setup.hs0000644000000000000000000000005612226556014014436 0ustar0000000000000000import Distribution.Simple main = defaultMain happstack-heist-7.2.0/happstack-heist.cabal0000644000000000000000000000236612226556014017064 0ustar0000000000000000Name: happstack-heist Version: 7.2.0 Synopsis: Support for using Heist templates in Happstack Description: Happstack is a web application framework. Heist is an XML templating solution. This package makes it easy to use Heist templates with Happstack. Homepage: http://www.happstack.com/ License: BSD3 License-file: LICENSE Author: Chris Smith, Jeremy Shaw Maintainer: Happstack team -- Copyright: Category: Web, Happstack Build-type: Simple Cabal-version: >=1.6 source-repository head type: darcs subdir: happstack-heist location: http://hub.darcs.net/stepcut/happstack Library Exposed-modules: Happstack.Server.Heist Build-depends: base >= 3.0 && < 5.0, blaze-builder >= 0.2 && <0.4, bytestring >= 0.9 && < 0.11, either == 3.4.*, filepath, happstack-server >= 7.0 && < 7.4, heist >= 0.13 && < 0.14, mtl == 2.*, text >= 0.10 && < 0.12 happstack-heist-7.2.0/Happstack/0000755000000000000000000000000012226556014014717 5ustar0000000000000000happstack-heist-7.2.0/Happstack/Server/0000755000000000000000000000000012226556014016165 5ustar0000000000000000happstack-heist-7.2.0/Happstack/Server/Heist.hs0000644000000000000000000001163712226556014017605 0ustar0000000000000000-- | This module provides support for serving compiled Heist templates using Happstack. -- -- The primary function provided by this module is: -- -- heistServe :: (Happstack m) => HeistState m -> m Response -- -- It also provides the 'initHeistCompiled' helper function for -- creating a 'HeistState'. Though you are free to use other functions -- from the Heist library instead. -- -- Here is a simple example: -- -- > module Main where -- > -- > import Control.Applicative ((<$>)) -- > import Control.Monad (msum) -- > import qualified Data.Text as T -- > import Happstack.Server (dir, nullConf, nullDir, simpleHTTP, seeOther, toResponse) -- > import Happstack.Server.Heist (heistServe, initHeistCompiled) -- > import Heist ((##), getParamNode, noSplices) -- > import Heist.Compiled (Splice, yieldRuntimeText) -- > import qualified Text.XmlHtml as X -- > -- > -- | factorial splice -- > factSplice :: (Monad m) => Splice m -- > factSplice = -- > do intStr <- T.unpack . X.nodeText <$> getParamNode -- > let res = yieldRuntimeText $ -- > do case reads intStr of -- > [(n,[])] -> -- > return (T.pack $ show $ product [1..(n :: Integer)]) -- > _ -> -- > return (T.pack $ "Unable to parse " ++ intStr ++ " as an Integer.") -- > return $ res -- > -- > main :: IO () -- > main = -- > do heistState <- do -- > r <- initHeistCompiled (T.pack "fact" ## factSplice) noSplices "." -- > case r of -- > (Left e) -> error $ unlines e -- > (Right heistState) -> return $ heistState -- > simpleHTTP nullConf $ msum -- > [ dir "heist" $ heistServe heistState -- > , nullDir >> seeOther "/heist/factorial" (toResponse "/heist/factorial") -- > ] -- -- It uses the following template file (@factorial.tpl@): -- -- @ -- \ -- \ -- \Factorial Page\<\/title\> -- \<\/head\> -- \ -- \Factorial Page\<\/h1\> -- \The factorial of 6 is \6\<\/fact\>\<\/p\> -- \<\/body\> -- \<\/html\> -- @ -- -- For more information on using Compiled Heist Templates see: -- -- -- -- And also see the Heist Section of the Happstack Crash Course: -- -- -- module Happstack.Server.Heist ( initHeistCompiled , heistServe ) where import Blaze.ByteString.Builder (toLazyByteString) import Control.Monad (MonadPlus(mzero), msum) import Control.Monad.Trans (MonadIO(liftIO)) import Control.Monad.Trans.Either (EitherT(runEitherT)) import Data.Monoid (mempty) import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import Data.Text (Text) import Happstack.Server (Happstack, Response, ServerMonad, askRq, nullDir, rqPaths, toResponseBS) import Happstack.Server.FileServe.BuildingBlocks (combineSafe) import Heist (AttrSplice, HeistConfig(..), HeistState, Splices, defaultLoadTimeSplices, initHeist, loadTemplates) import Heist.Compiled (Splice, renderTemplate) import System.FilePath (joinPath) initHeistCompiled :: (MonadIO m, Monad n) => Splices (Splice n) -- ^ compiled splices -> Splices (AttrSplice n) -- ^ attribute splices -> FilePath -- ^ path to template directory -> m (Either [String] (HeistState n)) initHeistCompiled splices attrSplices templateDir = liftIO $ runEitherT $ initHeist $ mempty { hcLoadTimeSplices = defaultLoadTimeSplices , hcCompiledSplices = splices , hcAttributeSplices = attrSplices , hcTemplateLocations = [loadTemplates templateDir] } heistServe :: (Happstack m) => HeistState m -> m Response heistServe heistState = msum [ nullDir >> renderHeistTemplate heistState (B.pack "index") , do rq <- askRq case combineSafe "" (joinPath (rqPaths rq)) of Nothing -> mzero (Just safepath) -> renderHeistTemplate heistState (B.pack safepath) ] renderHeistTemplate :: (MonadPlus n) => HeistState n -> ByteString -> n Response renderHeistTemplate heistState templateName = case renderTemplate heistState templateName of Nothing -> mzero Just (builderM, mimeType) -> do builder <- builderM return $ toResponseBS mimeType (toLazyByteString builder)