HStringTemplate 0.6.10 → 0.6.11
raw patch · 5 files changed
+84/−7 lines, 5 filesdep +dlistdep +ghc-primdep ~base
Dependencies added: dlist, ghc-prim
Dependency ranges changed: base
Files
- HStringTemplate.cabal +5/−1
- Text/StringTemplate.hs +8/−0
- Text/StringTemplate/Classes.hs +20/−2
- Text/StringTemplate/Group.hs +1/−1
- Text/StringTemplate/Instances.hs +50/−3
HStringTemplate.cabal view
@@ -1,5 +1,5 @@ name: HStringTemplate-version: 0.6.10+version: 0.6.11 synopsis: StringTemplate implementation in Haskell. description: A port of the Java library by Terrence Parr. category: Text@@ -29,6 +29,10 @@ build-depends: syb, base >= 4, base < 5, filepath, parsec < 4, containers, pretty, time, old-time, old-locale, bytestring, directory, array, text, deepseq, utf8-string, blaze-builder else build-depends: base > 3, base < 4, filepath, parsec < 4, containers, pretty, time, old-time, old-locale, bytestring, directory, array, text, utf8-string, blaze-builder++ if impl(ghc >= 7.2.1)+ cpp-options: -DGENERICS+ build-depends: ghc-prim >= 0.2, dlist >= 0.2 exposed-modules: Text.StringTemplate Text.StringTemplate.Base
Text/StringTemplate.hs view
@@ -1,3 +1,7 @@+{-# LANGUAGE CPP #-}+#ifdef GENERICS+{-# LANGUAGE DefaultSignatures #-}+#endif ----------------------------------------------------------------------------- -- | -- Module : Text.StringTemplate@@ -36,6 +40,9 @@ StringTemplate, STGroup, -- * Classes ToSElem(..), StringTemplateShows(..), stShowsToSE, Stringable(..), SEType(..),+#ifdef GENERICS+ GToSElem(..),+#endif -- * Creation newSTMP, newAngleSTMP, getStringTemplate, getStringTemplate', -- * Display@@ -57,4 +64,5 @@ import Text.StringTemplate.Base import Text.StringTemplate.Group import Text.StringTemplate.Renderf+import Text.StringTemplate.Classes import Text.StringTemplate.Instances()
Text/StringTemplate/Classes.hs view
@@ -1,8 +1,15 @@-{-# LANGUAGE ExistentialQuantification, FlexibleInstances, StandaloneDeriving, GeneralizedNewtypeDeriving, TypeSynonymInstances #-}+{-# LANGUAGE CPP, ExistentialQuantification, FlexibleContexts, FlexibleInstances, StandaloneDeriving, GeneralizedNewtypeDeriving, TypeSynonymInstances #-}+#ifdef GENERICS+{-# LANGUAGE DefaultSignatures, DeriveGeneric #-}+#endif {-# OPTIONS_HADDOCK not-home #-} module Text.StringTemplate.Classes (SElem(..), StringTemplateShows(..), ToSElem(..), SMap, STShow(..),- StFirst(..), Stringable(..), stShowsToSE+ StFirst(..), Stringable(..),+#ifdef GENERICS+ GToSElem(..),+#endif+ stShowsToSE ) where import qualified Data.Map as M import Data.List@@ -17,6 +24,13 @@ import qualified Data.Text.Lazy.Encoding as LT import qualified Text.PrettyPrint.HughesPJ as PP +#ifdef GENERICS+import GHC.Generics++class GToSElem f where+ gToSElem :: Stringable b => f a -> SElem b+#endif+ newtype StFirst a = StFirst { stGetFirst :: Maybe a } deriving (Eq, Ord, Read, Show) instance Monoid (StFirst a) where@@ -42,6 +56,10 @@ -- inserted as attributes into a StringTemplate. class ToSElem a where toSElem :: Stringable b => a -> SElem b+#ifdef GENERICS+ default toSElem :: (Stringable b, Generic a, GToSElem (Rep a)) => a -> SElem b+ toSElem = gToSElem . from+#endif toSElemList :: Stringable b => [a] -> SElem b toSElemList = LI . map toSElem
Text/StringTemplate/Group.hs view
@@ -49,7 +49,7 @@ dirContents <- filter (not . isPrefixOf ".") <$> getDirectoryContents fp subDirs <- filterM (doesDirectoryExist . (fp </>)) dirContents subs <- concat <$> mapM (\x -> getTmplsRecursive ext (base </> x) (fp </> x)) subDirs- return $ (map ((fp </>) &&& (\x -> base </> dropExtensions x)) $+ return $ (map ((fp </>) &&& (\x -> base </> dropExtension x)) $ filter ((ext ==) . takeExtension) dirContents) ++ subs
Text/StringTemplate/Instances.hs view
@@ -1,8 +1,12 @@-{-# LANGUAGE FlexibleInstances, OverlappingInstances #-}+{-# LANGUAGE FlexibleInstances, OverlappingInstances, CPP #-}+#ifdef GENERICS+{-# LANGUAGE DefaultSignatures, DeriveGeneric, TypeOperators #-}+#endif {-# OPTIONS_GHC -fno-warn-orphans #-} {-# OPTIONS_HADDOCK not-home #-} -module Text.StringTemplate.Instances() where+module Text.StringTemplate.Instances where+--module Text.StringTemplate.Instances() where import Text.StringTemplate.Classes import qualified Data.Map as M@@ -20,7 +24,10 @@ import qualified Data.Text.Encoding as T import qualified Data.Text.Lazy as LT import qualified Data.Text.Lazy.Encoding as LT-+#ifdef GENERICS+import GHC.Generics+#endif+import Debug.Trace {-------------------------------------------------------------------- Additional instances for items that may be set as StringTemplate@@ -28,6 +35,17 @@ --------------------------------------------------------------------} --Basics++instance Stringable a => ToSElem (SElem a) where+ toSElem (STR s) = STR s+ toSElem (BS x) = BS x+ toSElem (STSH x) = STSH x+ toSElem (SM x) = SM $ M.map toSElem x+ toSElem (LI xs) = LI $ map toSElem xs+ toSElem (SBLE x) = STR $ stToString x+ toSElem (SNAT x) = STR $ stToString x+ toSElem SNull = SNull+ instance ToSElem Char where toSElem = STR . (:[]) toSElemList = STR@@ -156,3 +174,32 @@ toSElem (a,b,c,d,e,f,g,h,i) = t2map [toSElem a, toSElem b, toSElem c, toSElem d, toSElem e, toSElem f, toSElem g, toSElem h, toSElem i] instance (ToSElem a, ToSElem b, ToSElem c, ToSElem d, ToSElem e, ToSElem f, ToSElem g, ToSElem h, ToSElem i, ToSElem j) => ToSElem (a, b, c, d, e, f, g, h, i, j) where toSElem (a,b,c,d,e,f,g,h,i,j) = t2map [toSElem a, toSElem b, toSElem c, toSElem d, toSElem e, toSElem f, toSElem g, toSElem h, toSElem i, toSElem j]+++---+#ifdef GENERICS+instance GToSElem V1+ where gToSElem = const SNull+instance GToSElem U1+ where gToSElem = const $ STR ""+instance ToSElem a => GToSElem (K1 i a)+ where gToSElem = toSElem . unK1+-- instance GToSElem a => GToSElem (M1 i c a)+-- where gToSElem = gToSElem . unM1+instance (Constructor c, GToSElem a) => GToSElem (C1 c a)+ where gToSElem x = (gToSElem (unM1 x))+instance GToSElem a => GToSElem (D1 dt a)+ where gToSElem = gToSElem . unM1+instance (Selector s, GToSElem a) => GToSElem (S1 s a)+ where gToSElem x = (gToSElem (unM1 x))+instance (GToSElem a, GToSElem b) => GToSElem (a :*: b)+ where gToSElem (x :*: y) = trace "ay" $ LI [gToSElem x, gToSElem y] -- recursively recover+instance (GToSElem a, GToSElem b) => GToSElem (a :+: b) -- ditto+ where gToSElem (L1 x) = gToSElem x+ gToSElem (R1 x) = gToSElem x++-- TODO special case tuple, list, maybe++data Foo a b = Baz {aasdf :: a, dfdfd :: b, dddd :: String} | Bar deriving Generic++#endif