packages feed

hastache-aeson (empty) → 0.1.0.0

raw patch · 4 files changed

+164/−0 lines, 4 filesdep +aesondep +attoparsecdep +basesetup-changed

Dependencies added: aeson, attoparsec, base, bytestring, containers, hastache, text, unordered-containers, vector

Files

+ README.md view
@@ -0,0 +1,68 @@+hastache-aeson+==============++* Lets you pass [aeson](http://hackage.haskell.org/package/aeson) `Value` as `MuContext` to [hastache](http://hackage.haskell.org/package/hastache)+* Since [yaml](http://hackage.haskell.org/package/yaml) is API-compatible to `aeson`, you can also render `yaml` `Value`s.++Example+-------++```haskell+{-# LANGUAGE OverloadedStrings #-}++import qualified Data.ByteString.Char8 as BS+import qualified Data.ByteString.Lazy.Char8 as LBS++import Control.Monad+import Data.Maybe (fromJust)++import Data.Yaml+import Text.Hastache+import Text.Hastache.Aeson (jsonValueContext)++event = BS.readFile "event.yaml"+template = BS.readFile "index.mustache"++render template value = hastacheStr defaultConfig template (jsonValueContext value)++main = do+    value <- liftM (fromJust . decode) event :: IO Value+    template' <- template+    render template' value >>= LBS.putStrLn+```++BSD3 License+------------++```+Copyright (c) 2013 Vladimir Kirillov++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:++1. Redistributions of source code must retain the above copyright+   notice, this list of conditions and the following disclaimer.++2. 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.++3. Neither the name of the author nor the names of his contributors+   may be used to endorse or promote products derived from this software+   without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE 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 AUTHORS 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.+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Text/Hastache/Aeson.hs view
@@ -0,0 +1,59 @@+{-# LANGUAGE OverloadedStrings #-}+-- Module:      Text.Hastache.Aeson+-- Copyright:   Vladimir Kirillov (c) 2013+-- License:     BSD3+-- Maintainer:  Vladimir Kirillov <proger@hackndev.com>+-- Stability:   experimental+-- Portability: portable++module Text.Hastache.Aeson (+      jsonValueContext+    ) where++import qualified Data.ByteString.Char8 as BS+import qualified Data.Text as T++import Control.Monad+import Control.Applicative+import qualified Data.Foldable as Foldable+import Data.Maybe (fromMaybe)++import qualified Data.Map as Map+import qualified Data.Vector as V+import qualified Data.HashMap.Strict as HM+import Data.Attoparsec.Number (Number(I, D))++import Data.Aeson.Types++import Text.Hastache +import Text.Hastache.Context ++jsonValueContext :: Monad m => Value -> MuContext m+jsonValueContext v = buildMapContext $ valueMap v++valueMap v = buildMap "" Map.empty v++buildMap name m (Object obj) =+    Map.insert (encodeStr name)+      (MuList [buildMapContext $ HM.foldlWithKey' (foldObject "") Map.empty obj])+      (HM.foldlWithKey' (foldObject name) m obj)+buildMap name m value = Map.insert (encodeStr name) muValue m+    where+        muValue = case value of+                      Array arr -> MuList . V.toList $ fmap jsonValueContext arr+                      Number (D float) -> MuVariable float+                      Number (I int) -> MuVariable int+                      String s -> MuVariable s+                      Bool b -> MuBool b+                      Null -> MuNothing+                      t -> MuVariable $ show t++buildName name newName+    | not (null name) = concat [name, ".", newName]+    | otherwise = newName++foldObject name m k v = buildMap (buildName name (T.unpack k)) m v++buildMapContext m a = return $ fromMaybe+    (if a == BS.pack "." then maybe MuNothing id $ Map.lookup BS.empty m else MuNothing)+    (Map.lookup a m)
+ hastache-aeson.cabal view
@@ -0,0 +1,35 @@+name:                hastache-aeson+version:             0.1.0.0+license:             BSD3+license-file:        README.md+homepage:            https://github.com/proger/hastache-aeson+bug-reports:         https://github.com/proger/hastache-aeson/issues++copyright:           Vladimir Kirillov+author:              Vladimir Kirillov <proger@hackndev.com>+maintainer:          Vladimir Kirillov <proger@hackndev.com>++synopsis:            render hastache templates using aeson values+description:         +    The Module lets you render Hastache templates from aeson values. See the GitHub page for examples.++category:            Text+build-type:          Simple+cabal-version:       >=1.8++library+  exposed-modules:     Text.Hastache.Aeson+  -- other-modules:       +  build-depends:       base ==4.6.*+                     , hastache ==0.5.*+                     , aeson ==0.6.*+                     , attoparsec ==0.10.*+                     , unordered-containers ==0.2.*+                     , vector ==0.10.*+                     , containers ==0.5.*+                     , text ==0.11.*+                     , bytestring ==0.10.*++source-repository head+  type:     git+  location: git://github.com/proger/hastache-aeson.git