Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Functions in GenericContext

  • Loading branch information...
commit da057a0cb1c18354ed3cfa4a7362a13c36333090 1 parent a85d1cb
@lymar lymar authored
Showing with 40 additions and 15 deletions.
  1. +21 −5 Text/Hastache/Context.hs
  2. +2 −2 hastache.cabal
  3. +17 −8 tests/test.hs
View
26 Text/Hastache/Context.hs
@@ -21,9 +21,9 @@ import Data.Word
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
+import qualified Data.Map as Map
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LText
-import qualified Data.Map as Map
import Text.Hastache
@@ -48,8 +48,10 @@ data Example = Example {
intField :: Int,
dataField :: InternalData,
simpleListField :: [String],
- dataListField :: [InternalData]
- } deriving (Data, Typeable, Show)
+ dataListField :: [InternalData],
+ stringFunc :: String -> String,
+ byteStringFunc :: Data.ByteString.ByteString -> Data.ByteString.ByteString
+ } deriving (Data, Typeable)
example = hastacheStr defaultConfig (encodeStr template)
(mkGenericContext context)
@@ -63,10 +65,14 @@ example = hastacheStr defaultConfig (encodeStr template)
\"data list: \\n\",
\"{{#dataListField}}\\n\",
\" * {{someField}}, {{anotherField}} \\n\",
- \"{{/dataListField}}\\n\"]
+ \"{{/dataListField}}\\n\",
+ \"{{#stringFunc}}upper{{/stringFunc}} \\n\",
+ \"{{#byteStringFunc}}reverse{{/byteStringFunc}} \\n\"]
context = Example { stringField = \"string value\", intField = 1,
dataField = InternalData \"val\" 123, simpleListField = [\"a\",\"b\",\"c\"],
- dataListField = [InternalData \"aaa\" 1, InternalData \"bbb\" 2] }
+ dataListField = [InternalData \"aaa\" 1, InternalData \"bbb\" 2],
+ stringFunc = map Data.Char.toUpper,
+ byteStringFunc = Data.ByteString.reverse }
@
Result:
@@ -80,6 +86,8 @@ simple list: a b c
data list:
* aaa, 1
* bbb, 2
+UPPER
+esrever
@
-}
mkGenericContext :: (Monad m, Data a) => a -> MuContext m
@@ -121,11 +129,19 @@ procField =
`extQ` (\(i::Text.Text) -> MuVariable i ~> TSimple)
`extQ` (\(i::LText.Text) -> MuVariable i ~> TSimple)
`extQ` (\(i::Bool) -> MuBool i ~> TSimple)
+ `extQ` muLambdaBS
+ `extQ` muLambdaS
where
obj a = case dataTypeRep (dataTypeOf a) of
AlgRep [c] -> toGenTemp a
_ -> TUnknown
list a = map procField a ~> TList
+ muLambdaBS :: (BS.ByteString -> BS.ByteString) -> TD m
+ muLambdaBS f = MuLambda f ~> TSimple
+ muLambdaS :: (String -> String) -> TD m
+ muLambdaS f = MuLambda fd ~> TSimple
+ where
+ fd s = decodeStr s ~> f ~> encodeStr
convertGenTempToContext :: TD t -> MuContext t
convertGenTempToContext v = mkMap "" Map.empty v ~> mkMapContext
View
4 hastache.cabal
@@ -1,5 +1,5 @@
name: hastache
-version: 0.1.3
+version: 0.1.4
license: BSD3
license-file: LICENSE
category: Text
@@ -7,7 +7,7 @@ copyright: Sergey S Lymar (c) 2011
author: Sergey S Lymar <sergey.lymar@gmail.com>
maintainer: Sergey S Lymar <sergey.lymar@gmail.com>
stability: experimental
-tested-with: GHC == 7.0.2
+tested-with: GHC == 7.0.2, GHC == 7.0.3
synopsis: Haskell implementation of Mustache templates
cabal-version: >= 1.8
homepage: http://github.com/lymar/hastache
View
25 tests/test.hs
@@ -1,16 +1,17 @@
{-# LANGUAGE DeriveDataTypeable #-}
module Tests where
+import Control.Monad
+import Control.Monad.Writer
+import Data.Char
+import Data.Data
+import Data.Generics
import Test.HUnit
import Text.Hastache
import Text.Hastache.Context
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LZ
import qualified Data.Text as T
-import Control.Monad
-import Control.Monad.Writer
-import Data.Data
-import Data.Generics
-- Hastache comments
commentsTest = do
@@ -242,15 +243,17 @@ data InternalData = InternalData {
intDataField1 :: String,
intDataField2 :: Int
}
- deriving (Data, Typeable, Show)
+ deriving (Data, Typeable)
data SomeData = SomeData {
someDataField1 :: String,
someDataInternal :: InternalData,
someDataList :: [Int],
- someDataObjList :: [InternalData]
+ someDataObjList :: [InternalData],
+ someMuLambdaBS :: BS.ByteString -> BS.ByteString,
+ someMuLambdaS :: String -> String
}
- deriving (Data, Typeable, Show)
+ deriving (Data, Typeable)
-- Make hastache context from Data.Data deriving type
genericContextTest = do
@@ -273,6 +276,8 @@ genericContextTest = do
\{{#someDataObjList}}\n\
\* {{intDataField1}} : {{intDataField2}} \n\
\{{/someDataObjList}}\n\
+ \{{#someMuLambdaBS}}reverse{{/someMuLambdaBS}}\n\
+ \{{#someMuLambdaS}}upper{{/someMuLambdaS}}\n\
\text 2\n\
\"
context = SomeData {
@@ -281,7 +286,9 @@ genericContextTest = do
intDataField1 = "zzz", intDataField2 = 100 },
someDataList = [1,2,3],
someDataObjList = [InternalData "a" 1, InternalData "b" 2,
- InternalData "c" 3]
+ InternalData "c" 3],
+ someMuLambdaBS = BS.reverse,
+ someMuLambdaS = map toUpper
}
testRes = "\
@@ -297,6 +304,8 @@ genericContextTest = do
\* a : 1 \n\
\* b : 2 \n\
\* c : 3 \n\
+ \esrever\n\
+ \UPPER\n\
\text 2\n\
\"
Please sign in to comment.
Something went wrong with that request. Please try again.