shakespeare-text-1.0.0.5/0000755000000000000000000000000012027770526013326 5ustar0000000000000000shakespeare-text-1.0.0.5/shakespeare-text.cabal0000644000000000000000000000520512027770526017571 0ustar0000000000000000name: shakespeare-text version: 1.0.0.5 license: MIT license-file: LICENSE author: Greg Weber maintainer: Greg Weber synopsis: Interpolation with quasi-quotation: put variables strings description: interpolation with quasi-quotation: stick haskell variables into haskell strings . Note there is no dependency on haskell-src-extras. If you don't mind that dependency, you may want to look at using these packages: Interpolation, interpolatedstring-perl6, interpolatedstring-qq. . This package has 1 other general feature that those others may not (but would be easy to duplicate): instead of using quasi-quoting you can also use an external file. It also has url/embeding interpolation, with \@ and \^, which are used in Yesod. . This package also uses blaze-builder for efficiently constructing strings (I am not sure what the other packages use). This might be of interest to you for large templates or performance sensitive code, or otherwise having a nice interface to blaze-builder . Shakespeare is a template family for type-safe, efficient templates with simple variable interpolation . Shakespeare templates can be used inline with a quasi-quoter or in an external file. Shakespeare interpolates variables according to the type being inserted. In this case, the variable type needs a ToText instance. . Please see http://www.yesodweb.com/book/shakespearean-templates for a more thorough description and examples of the shakespeare family of template languages. category: Web, Yesod stability: Stable cabal-version: >= 1.8 build-type: Simple homepage: http://www.yesodweb.com/book/shakespearean-templates extra-source-files: test/texts/external1.text test/texts/external2.text test/ShakespeareTextTest.hs test.hs library build-depends: base >= 4 && < 5 , shakespeare >= 1.0 && < 1.1 , template-haskell , text >= 0.7 && < 0.12 exposed-modules: Text.Shakespeare.Text ghc-options: -Wall if impl(ghc >= 7.4) cpp-options: -DGHC_7_4 test-suite test hs-source-dirs: test main-is: ../test.hs type: exitcode-stdio-1.0 ghc-options: -Wall build-depends: shakespeare-text , base >= 4 && < 5 , HUnit , hspec >= 1.3 , text >= 0.7 && < 0.12 source-repository head type: git location: git://github.com/yesodweb/shakespeare.git shakespeare-text-1.0.0.5/Setup.lhs0000644000000000000000000000021712027770526015136 0ustar0000000000000000#!/usr/bin/env runhaskell > module Main where > import Distribution.Simple > import System.Cmd (system) > main :: IO () > main = defaultMain shakespeare-text-1.0.0.5/test.hs0000644000000000000000000000012612027770526014640 0ustar0000000000000000import ShakespeareTextTest (spec) import Test.Hspec main :: IO () main = hspec spec shakespeare-text-1.0.0.5/LICENSE0000644000000000000000000000207512027770526014337 0ustar0000000000000000Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/ 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. shakespeare-text-1.0.0.5/Text/0000755000000000000000000000000012027770526014252 5ustar0000000000000000shakespeare-text-1.0.0.5/Text/Shakespeare/0000755000000000000000000000000012027770526016505 5ustar0000000000000000shakespeare-text-1.0.0.5/Text/Shakespeare/Text.hs0000644000000000000000000001033412027770526017766 0ustar0000000000000000{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE CPP #-} {-# OPTIONS_GHC -fno-warn-missing-fields #-} module Text.Shakespeare.Text ( TextUrl , ToText (..) , renderTextUrl , stext , text , textFile , textFileDebug , textFileReload , st -- | strict text , lt -- | lazy text, same as stext :) -- * Yesod code generation , codegen , codegenSt , codegenFile , codegenFileReload ) where import Language.Haskell.TH.Quote (QuasiQuoter (..)) import Language.Haskell.TH.Syntax import Data.Text.Lazy.Builder (Builder, fromText, toLazyText, fromLazyText) import qualified Data.Text as TS import qualified Data.Text.Lazy as TL import Text.Shakespeare import Data.Int (Int32, Int64) renderText :: Builder -> TL.Text renderText = toLazyText renderTextUrl :: RenderUrl url -> TextUrl url -> TL.Text renderTextUrl r s = renderText $ s r type TextUrl url = RenderUrl url -> Builder class ToText a where toText :: a -> Builder instance ToText [Char ] where toText = fromLazyText . TL.pack instance ToText TS.Text where toText = fromText instance ToText TL.Text where toText = fromLazyText instance ToText Int32 where toText = toText . show instance ToText Int64 where toText = toText . show settings :: Q ShakespeareSettings settings = do toTExp <- [|toText|] wrapExp <- [|id|] unWrapExp <- [|id|] return $ defaultShakespeareSettings { toBuilder = toTExp , wrap = wrapExp , unwrap = unWrapExp } stext, lt, st, text :: QuasiQuoter stext = QuasiQuoter { quoteExp = \s -> do rs <- settings render <- [|renderText|] rendered <- shakespeareFromString rs { justVarInterpolation = True } s return (render `AppE` rendered) } lt = stext st = QuasiQuoter { quoteExp = \s -> do rs <- settings render <- [|TL.toStrict . renderText|] rendered <- shakespeareFromString rs { justVarInterpolation = True } s return (render `AppE` rendered) } text = QuasiQuoter { quoteExp = \s -> do rs <- settings quoteExp (shakespeare rs) s } textFile :: FilePath -> Q Exp textFile fp = do rs <- settings shakespeareFile rs fp textFileDebug :: FilePath -> Q Exp textFileDebug = textFileReload {-# DEPRECATED textFileDebug "Please use textFileReload instead" #-} textFileReload :: FilePath -> Q Exp textFileReload fp = do rs <- settings shakespeareFileReload rs fp -- | codegen is designed for generating Yesod code, including templates -- So it uses different interpolation characters that won't clash with templates. codegenSettings :: Q ShakespeareSettings codegenSettings = do toTExp <- [|toText|] wrapExp <- [|id|] unWrapExp <- [|id|] return $ defaultShakespeareSettings { toBuilder = toTExp , wrap = wrapExp , unwrap = unWrapExp , varChar = '~' , urlChar = '*' , intChar = '&' , justVarInterpolation = True -- always! } -- | codegen is designed for generating Yesod code, including templates -- So it uses different interpolation characters that won't clash with templates. -- You can use the normal text quasiquoters to generate code codegen :: QuasiQuoter codegen = QuasiQuoter { quoteExp = \s -> do rs <- codegenSettings render <- [|renderText|] rendered <- shakespeareFromString rs { justVarInterpolation = True } s return (render `AppE` rendered) } -- | Generates strict Text -- codegen is designed for generating Yesod code, including templates -- So it uses different interpolation characters that won't clash with templates. codegenSt :: QuasiQuoter codegenSt = QuasiQuoter { quoteExp = \s -> do rs <- codegenSettings render <- [|TL.toStrict . renderText|] rendered <- shakespeareFromString rs { justVarInterpolation = True } s return (render `AppE` rendered) } codegenFileReload :: FilePath -> Q Exp codegenFileReload fp = do rs <- codegenSettings render <- [|TL.toStrict . renderText|] rendered <- shakespeareFileReload rs{ justVarInterpolation = True } fp return (render `AppE` rendered) codegenFile :: FilePath -> Q Exp codegenFile fp = do rs <- codegenSettings render <- [|TL.toStrict . renderText|] rendered <- shakespeareFile rs{ justVarInterpolation = True } fp return (render `AppE` rendered) shakespeare-text-1.0.0.5/test/0000755000000000000000000000000012027770526014305 5ustar0000000000000000shakespeare-text-1.0.0.5/test/ShakespeareTextTest.hs0000644000000000000000000001057612027770526020612 0ustar0000000000000000{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE TemplateHaskell #-} module ShakespeareTextTest (spec) where import Test.HUnit hiding (Test) import Test.Hspec import Prelude hiding (reverse) import Text.Shakespeare.Text import Data.List (intercalate) import qualified Data.Text.Lazy as TL import qualified Data.List import qualified Data.List as L import Data.Text (Text, pack, unpack) import Data.Monoid (mappend) spec :: Spec spec = do describe "shakespeare-text" $ do it "text" $ do let var = "var" let urlp = (Home, [(pack "p", pack "q")]) flip telper [text|שלום #{var} @{Home} @?{urlp} ^{jmixin} |] $ intercalate "\r\n" [ "שלום" , var , "url" , "url?p=q" , "var x;" ] ++ "\r\n" it "textFile" $ do let var = "var" let urlp = (Home, [(pack "p", pack "q")]) flip telper $(textFile "test/texts/external1.text") $ unlines [ "שלום" , var , "url" , "url?p=q" , "var x;" ] it "textFileReload" $ do let var = "var" let urlp = (Home, [(pack "p", pack "q")]) flip telper $(textFileReload "test/texts/external1.text") $ unlines [ "שלום" , var , "url" , "url?p=q" , "var x;" ] {- TODO it "textFileReload" $ do let var = "somevar" test result = telper result $(textFileReload "test/texts/external2.text") writeFile "test/texts/external2.text" "var #{var} = 1;" test "var somevar = 1;" writeFile "test/texts/external2.text" "var #{var} = 2;" test "var somevar = 2;" writeFile "test/texts/external2.text" "var #{var} = 1;" -} it "text module names" $ let foo = "foo" double = 3.14 :: Double int = -5 :: Int in telper "oof oof 3.14 -5" [text|#{Data.List.reverse foo} #{L.reverse foo} #{show double} #{show int}|] it "stext module names" $ let foo = "foo" double = 3.14 :: Double int = -5 :: Int in simpT "oof oof 3.14 -5" [stext|#{Data.List.reverse foo} #{L.reverse foo} #{show double} #{show int}|] it "single dollar at and caret" $ do telper "$@^" [text|$@^|] telper "#{@{^{" [text|#\{@\{^\{|] it "single dollar at and caret" $ do simpT "$@^" [stext|$@^|] simpT "#{@{^{" [stext|#\{@\{^\{|] it "dollar operator" $ do let val = (1 :: Int, (2 :: Int, 3 :: Int)) telper "2" [text|#{ show $ fst $ snd val }|] telper "2" [text|#{ show $ fst $ snd $ val}|] it "dollar operator" $ do let val = (1 :: Int, (2 :: Int, 3 :: Int)) simpT "2" [stext|#{ show $ fst $ snd val }|] simpT "2" [stext|#{ show $ fst $ snd $ val}|] simpT :: String -> TL.Text -> Assertion simpT a b = pack a @=? TL.toStrict b data Url = Home | Sub SubUrl data SubUrl = SubUrl render :: Url -> [(Text, Text)] -> Text render Home qs = pack "url" `mappend` showParams qs render (Sub SubUrl) qs = pack "suburl" `mappend` showParams qs showParams :: [(Text, Text)] -> Text showParams [] = pack "" showParams z = pack $ '?' : intercalate "&" (map go z) where go (x, y) = go' x ++ '=' : go' y go' = concatMap encodeUrlChar . unpack -- | Taken straight from web-encodings; reimplemented here to avoid extra -- dependencies. encodeUrlChar :: Char -> String encodeUrlChar c -- List of unreserved characters per RFC 3986 -- Gleaned from http://en.wikipedia.org/wiki/Percent-encoding | 'A' <= c && c <= 'Z' = [c] | 'a' <= c && c <= 'z' = [c] | '0' <= c && c <= '9' = [c] encodeUrlChar c@'-' = [c] encodeUrlChar c@'_' = [c] encodeUrlChar c@'.' = [c] encodeUrlChar c@'~' = [c] encodeUrlChar ' ' = "+" encodeUrlChar y = let (a, c) = fromEnum y `divMod` 16 b = a `mod` 16 showHex' x | x < 10 = toEnum $ x + (fromEnum '0') | x < 16 = toEnum $ x - 10 + (fromEnum 'A') | otherwise = error $ "Invalid argument to showHex: " ++ show x in ['%', showHex' b, showHex' c] jmixin :: TextUrl url jmixin = [text|var x;|] telper :: String -> TextUrl Url -> Assertion telper res h = pack res @=? TL.toStrict (renderTextUrl render h) instance Show Url where show _ = "FIXME remove this instance show Url" shakespeare-text-1.0.0.5/test/texts/0000755000000000000000000000000012027770526015454 5ustar0000000000000000shakespeare-text-1.0.0.5/test/texts/external1.text0000644000000000000000000000005312027770526020263 0ustar0000000000000000שלום #{var} @{Home} @?{urlp} ^{jmixin} shakespeare-text-1.0.0.5/test/texts/external2.text0000644000000000000000000000001712027770526020264 0ustar0000000000000000var #{var} = 2;