templater (empty) → 0.0.2.0
raw patch · 6 files changed
+161/−0 lines, 6 filesdep +HUnitdep +QuickCheckdep +attoparsecsetup-changed
Dependencies added: HUnit, QuickCheck, attoparsec, base, hspec, hspec-attoparsec, templater, text
Files
- LICENSE +20/−0
- README.md +36/−0
- Setup.hs +2/−0
- src/Text/Templater.hs +52/−0
- templater.cabal +50/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Geraud Boyer++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,36 @@+# Templater+[](https://travis-ci.org/geraud/templater)++Simple string templater++## Installation++```bash+cabal update+cabal install templater+```++## Example++```haskell+{-# LANGUAGE OverloadedStrings #-}+import Data.Text+import qualified Data.Text.IO as TIO+import Text.Templater++main :: IO ()+main = do+ let textTemplate = "Hello, %{what is it ?}!"+ res = template textTemplate context+ case res of+ Left error -> do+ putStrLn $ "Got Error:" ++ error+ Right result -> TIO.putStrLn result++context :: Context -- Context is a type alias for Text -> Maybe Text+context "what is it ?" = Just "world"+context _ = Nothing+```+renders to `Hello, world!`++Well that's all there is to it!...
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Text/Templater.hs view
@@ -0,0 +1,52 @@+{-# LANGUAGE OverloadedStrings #-}+module Text.Templater where++import Control.Applicative+import Data.Attoparsec.Text+import Data.Either (partitionEithers)+import Data.List (intercalate)+import Data.Monoid ((<>))+import Data.Text (Text)+import qualified Data.Text as T+import Prelude hiding (takeWhile)++data TemplateItem+ = Literal Text+ | Variable Text+ deriving (Show, Eq)++type Context = Text -> Maybe Text++template :: Text -> Context -> Either String Text+template "" _ = Right ""+template st ctx = case parseOnly templateP st of+ Right items -> expandItems ctx items+ Left e -> Left e++expandItems :: Context -> [TemplateItem] -> Either String Text+expandItems context items =+ let (lefts, rights) = partitionEithers $ expandItem context <$> items+ in if null lefts+ then Right $ T.concat rights+ else Left $ intercalate ", " lefts++expandItem :: Context -> TemplateItem -> Either String Text+expandItem ctx (Literal v) = Right v+expandItem ctx (Variable name) =+ case ctx name of+ Nothing -> Left $ "key '" <> T.unpack name <> "' not found in context"+ Just v -> Right v++templateP :: Parser [TemplateItem]+templateP = many1 templateItemP <* endOfInput++templateItemP :: Parser TemplateItem+templateItemP = choice [escapedOrVariableP, literalItemP]++literalItemP :: Parser TemplateItem+literalItemP = Literal <$> takeWhile1 (/= '%')++escapedOrVariableP :: Parser TemplateItem+escapedOrVariableP = char '%' *> choice [variableItemP, escapedPercentP]+ where variableItemP = Variable <$> ("{" *> takeWhile (/= '}') <* "}")+ escapedPercentP = Literal <$> "%"
+ templater.cabal view
@@ -0,0 +1,50 @@+name: templater+version: 0.0.2.0+synopsis: Simple string templater+-- description:+homepage: https://github.com/geraud/templater+license: MIT+license-file: LICENSE+author: Geraud Boyer+maintainer: geraud@gmail.com+-- copyright:+category: Text+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10+tested-with:+ GHC == 7.6+ , GHC == 7.8+ , GHC == 7.10++library+ build-depends:+ base >=4.5 && <4.9+ , text >= 1.1+ , attoparsec >= 0.12+ hs-source-dirs:+ src+ default-language:+ Haskell2010+ exposed-modules:+ Text.Templater++test-suite tests+ main-is: Spec.hs+ build-depends:+ base+ , templater+ , hspec == 2.*+ , hspec-attoparsec >= 0.1.0+ , HUnit+ , QuickCheck+ , text+ default-language:+ Haskell2010+ ghc-options:+ -Wall+ -Werror+ hs-source-dirs:+ test+ type:+ exitcode-stdio-1.0
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}