HStringTemplate 0.6.1 → 0.6.2
raw patch · 4 files changed
+28/−16 lines, 4 filesdep +utf8-stringdep ~basePVP ok
version bump matches the API change (PVP)
Dependencies added: utf8-string
Dependency ranges changed: base
API changes (from Hackage documentation)
Files
- HStringTemplate.cabal +3/−3
- Text/StringTemplate.hs +2/−6
- Text/StringTemplate/Base.hs +18/−3
- Text/StringTemplate/Group.hs +5/−4
HStringTemplate.cabal view
@@ -1,5 +1,5 @@ name: HStringTemplate-version: 0.6.1+version: 0.6.2 synopsis: StringTemplate implementation in Haskell. description: A port of the Java library by Terrence Parr. category: Text@@ -26,9 +26,9 @@ exposed-modules: Text.StringTemplate.QQ if flag(smaller-base)- build-depends: syb, base >= 4, base < 5, filepath, parsec < 3, containers, pretty, time, old-time, old-locale, bytestring, directory, array, text, parallel+ build-depends: syb, base >= 4, base < 5, filepath, parsec < 3, containers, pretty, time, old-time, old-locale, bytestring, directory, array, text, parallel, utf8-string else- build-depends: base > 3, base < 4, filepath, parsec < 3, containers, pretty, time, old-time, old-locale, bytestring, directory, array, text+ build-depends: base > 3, base < 4, filepath, parsec < 3, containers, pretty, time, old-time, old-locale, bytestring, directory, array, text, utf8-string exposed-modules: Text.StringTemplate Text.StringTemplate.Base
Text/StringTemplate.hs view
@@ -9,7 +9,8 @@ -- -- A StringTemplate is a String with \"holes\" in it. -- This is a port of the Java StringTemplate library written by Terrence Parr.--- (<http://www.stringtemplate.org>).+-- (<http://www.stringtemplate.org>). User-contributed documentation available at+-- <http://www.haskell.org/haskellwiki/HStringTemplate>. -- -- This library implements the basic 3.1 grammar, lacking group files -- (though not groups themselves), Regions, and Interfaces.@@ -19,11 +20,6 @@ -- HughesPJ Pretty Printing library. Calling toPPDoc on a StringTemplate -- yields a Doc with appropriate paragraph-fill wrapping that can be -- rendered in the usual fashion.------ This library extends the current StringTemplate grammar by allowing the--- application of alternating attributes to anonymous--- as well as regular templates, including therefore sets of alternating--- attributes. -- -- Basic instances are provided of the StringTemplateShows and ToSElem class. -- Any type deriving ToSElem can be passed automatically as a StringTemplate
Text/StringTemplate/Base.hs view
@@ -14,7 +14,7 @@ parseSTMPNames ) where import Control.Arrow-import Control.Applicative hiding ((<|>),many)+import Control.Applicative hiding ((<|>),many,optional) import Control.Monad import Control.Parallel.Strategies(rnf, NFData(..)) import qualified Control.Exception as C@@ -216,8 +216,14 @@ mergeSEnvs x y = SEnv {smp = M.union (smp x) (smp y), sopts = (sopts y ++ sopts x), sgen = sgen x, senc = senc y} parseSTMP :: (Stringable a) => (Char, Char) -> String -> Either String (SEnv a -> a)-parseSTMP x = either (Left . show) Right . runParser (stmpl False) (x,[],[],[]) ""+parseSTMP x = either (Left . show) Right . runParser (stmpl False) (x,[],[],[]) "" . dropTrailingBr +dropTrailingBr :: String -> String+dropTrailingBr ('\r':'\n':[]) = []+dropTrailingBr ('\n':[]) = []+dropTrailingBr [] = []+dropTrailingBr (x:xs) = x : dropTrailingBr xs+ getSeps :: TmplParser (Char, Char) getSeps = (\(x,_,_,_) -> x) <$> getState @@ -328,12 +334,17 @@ noneOf chs >>= \x -> if x == '\\' then anyChar >>= \y -> return [y] else return [x] escapedStr chs = concat <$> many1 (escapedChar chs) +{-+escapedStr' chs = dropTrailingBr <$> escapedStr chs+-}+ {-------------------------------------------------------------------- The Grammar --------------------------------------------------------------------} myConcat :: Stringable a => [SEnv a -> a] -> (SEnv a -> a) myConcat xs a = smconcat $ map ($ a) xs + -- | if p is true, stmpl can fail gracefully, false it dies hard. -- Set to false at the top level, and true within if expressions. stmpl :: Stringable a => Bool -> TmplParser (SEnv a -> a)@@ -389,6 +400,9 @@ Statements --------------------------------------------------------------------} +optLine :: TmplParser ()+optLine = optional (char '\r') >> optional (char '\n')+ --if env then do stuff getProp :: Stringable a => [SEnv a -> SElem a] -> SElem a -> SEnv a -> SElem a getProp (p:ps) (SM mp) env =@@ -409,7 +423,7 @@ n <- option True (char '!' >> return False) e <- subexprn p <- props- char ')' >> char cb+ char ')' >> char cb >> optLine act <- stmpl True cont <- (try elseifstat <|> try elsestat <|> endifstat) return (ifIsSet act cont n =<< getProp p =<< e)@@ -421,6 +435,7 @@ elsestat = do (ca, cb) <- getSeps around ca (string "else") cb+ optLine act <- stmpl True char ca >> string "endif" return act
Text/StringTemplate/Group.hs view
@@ -18,6 +18,7 @@ import Data.IORef import System.IO.Unsafe import System.IO.Error+import System.IO.UTF8 as U import qualified Data.Map as M import Text.StringTemplate.Base@@ -32,7 +33,7 @@ readFile' :: FilePath -> IO String readFile' f = do- x <- readFile f+ x <- U.readFile f length x `seq` return x groupFromFiles :: Stringable a => (FilePath -> IO String) -> [(FilePath,String)] -> IO (STGroup a)@@ -79,7 +80,7 @@ -- these exceptions in unexpected places. directoryGroupLazy :: (Stringable a) => FilePath -> IO (STGroup a) directoryGroupLazy path =- groupFromFiles readFile .+ groupFromFiles U.readFile . map ((</>) path &&& takeBaseName) . filter ((".st" ==) . takeExtension) =<< getDirectoryContents path @@ -90,7 +91,7 @@ -- | See documentation for 'directoryGroupRecursive'. directoryGroupRecursiveLazy :: (Stringable a) => FilePath -> IO (STGroup a)-directoryGroupRecursiveLazy path = groupFromFiles readFile =<< getTmplsRecursive "" path+directoryGroupRecursiveLazy path = groupFromFiles U.readFile =<< getTmplsRecursive "" path -- | Adds a supergroup to any StringTemplate group such that templates from -- the original group are now able to call ones from the supergroup as well.@@ -134,7 +135,7 @@ unsafeVolatileDirectoryGroup path m = return . flip addSubGroup extraTmpls $ cacheSTGroup stfg where stfg = StFirst . Just . newSTMP . unsafePerformIO . flip catch (return . (\e -> "IO Error: " ++ show (ioeGetFileName e) ++ " -- " ++ ioeGetErrorString e))- . readFile . (path </>) . (++".st")+ . U.readFile . (path </>) . (++".st") extraTmpls = addSubGroup (groupStringTemplates [("dumpAttribs", dumpAttribs)]) nullGroup cacheSTGroup :: STGroup a -> STGroup a cacheSTGroup g = unsafePerformIO $ do