HStringTemplateHelpers 0.0.8 → 0.0.10
raw patch · 2 files changed
+52/−16 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Text.StringTemplate.Helpers: badTmplVarName :: String -> Bool
+ Text.StringTemplate.Helpers: directoryGroupNew' :: (Stringable a) => (FilePath -> Bool) -> (String -> Bool) -> FilePath -> IO (STGroup a)
Files
HStringTemplateHelpers.cabal view
@@ -1,5 +1,5 @@ Name: HStringTemplateHelpers-Version: 0.0.8+Version: 0.0.10 License: GPL License-file: gpl.txt Description: Convenience functions and instances for HStringTemplate. I will deprecate this package if its contents are integrated into HStringTemplate.
Text/StringTemplate/Helpers.hs view
@@ -20,6 +20,8 @@ , readTmplDef , readTmplM , readTmplTuples+ , badTmplVarName+ , directoryGroupNew' ) where@@ -74,8 +76,9 @@ type STDirGroups a = M.Map FilePath (STGroup a) + {- | -calculate a map of directory groups from a top-level directory+Helper function to calculate a map of directory groups from a top-level directory Each directory gives rise to its own groups. @@ -83,7 +86,6 @@ The top group has key \".\" (mnemonic, current directory), other groups have key names of subdirectories, including the starting ., eg \".\/templates\/path\/to/\subdir\" -} -directoryGroupsOld = directoryGroups' directoryGroup directoryGroups' :: (FilePath -> IO a) -> FilePath -> IO (M.Map FilePath a) directoryGroups' f' d = bracketCD d $ do subDirs <- findDirectories $ "." @@ -93,27 +95,60 @@ return (d,g) findDirectories d = Find.find Find.always (Find.fileType Find.==? Find.Directory) d +{- | Non-strict. I'm pretty sure this is wrong. Based on default directoryGroup function in HStringTemplate package -}+directoryGroupsOld :: (Stringable a) => FilePath -> IO (M.Map FilePath (STGroup a))+directoryGroupsOld = directoryGroups' directoryGroup ++{- | Strict directoryGroups, which is the right thing.+-}+directoryGroups :: (Stringable a) => FilePath -> IO (M.Map FilePath (STGroup a)) directoryGroups = directoryGroups' directoryGroupNew-{- | I think this does the same thing as directoryGroup (modulo IO strictness), ++{- | I'm this does the same thing as directoryGroup (modulo IO strictness), which uses an applicative idiom that melts my brain. Not a direct translation, but it's easier for me to understand when written Important change: readFile is strict. If it is left lazy, appkiller.sh causes happstutorial to crash- when in dynamicTemplateReload mode.+ when in dynamicTemplateReload mode. (See HAppS GoogleGroup.) I think this needs to be fixed in HStringTemplate distribution as well.+ This should be fixed in HSTringTemplate package as well. -}-directoryGroupNew :: Stringable a => FilePath -> IO (STGroup a)-directoryGroupNew path = do- fs <- return . ( filter ((".st" ==) . takeExtension) ) =<< getDirectoryContents path- templates <- mapM g fs+directoryGroupNew :: (Stringable a) => FilePath -> IO (STGroup a)+directoryGroupNew = directoryGroupNew' (\_ -> False) (\_ -> False)++{- | directoryGroup helper function for more flexibility, and rewritten to use+ do notation rather than applicative style that melted my brain.+ + ignoreTemplate specifies a filter for templates that should be skipped, eg backup files etc.++ errorTemplate specifies a filter which will cause function to fail.++ > directoryGroupHAppS = directoryGroupNew' ignoret badTmplVarName+ > where ignoret f = not . null . filter (=='#') $ f +-}+directoryGroupNew' :: (Stringable a) =>+ (FilePath -> Bool)+ -> (String -> Bool)+ -> FilePath+ -> IO (STGroup a)+directoryGroupNew' ignoreTemplate errorTemplate path = do+ fs1 <- return . ( filter filt ) =<< getDirectoryContents path+ fs <- mapM errT =<< return fs1+ templates <- mapM readTemplate fs stmapping <- return . zip (map dropExtension fs) $ templates return $ groupStringTemplates stmapping- -- For a while when debugging a problem in HAppS I thought this had to be- -- System.IO.Strict.ReadFile, but now I think it makes no difference- where g f = do contents <- Strict.readFile $ path </> f- return . newSTMP $ contents+ where+ readTemplate f = do + contents <- Strict.readFile $ path </> f+ return . newSTMP $ contents+ errT t = if ( errorTemplate . takeBaseName ) t + then fail $ "directoryGroupNew', bad template name: " ++ t + else return t+ filt f = ( ( (".st" ==) . takeExtension ) $ f)+ && ( (not . ignoreTemplate) $ f) -{- | -The STGroup can't be shown in a useful way because it's a function type, but you can at least show the directories via Data.Map.keys.+++{- | The STGroup can't be shown in a useful way because it's a function type, but you can at least show the directories via Data.Map.keys. -} dirgroupKeys :: (Stringable a) => STDirGroups a -> [FilePath] dirgroupKeys = M.keys@@ -142,8 +177,9 @@ (<$$>) :: (Functor f1, Functor f) => (a -> b) -> f (f1 a) -> f (f1 b) (<$$>) = (<$>) . (<$>) +{- Check for a variable that will cause errors and heartache using HStringTemplate -} badTmplVarName :: String -> Bool-badTmplVarName t = or . map (not . isAlpha) $ t+badTmplVarName t = not . null . filter (not . isAlpha) $ t {- | > render1 [("name","Bill")] "Hi, my name is $name$"