packages feed

HStringTemplateHelpers 0.0.3 → 0.0.4

raw patch · 2 files changed

+53/−25 lines, 2 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

+ Text.StringTemplate.Helpers: getTemplateGroup :: Stringable a => FilePath -> STDirGroups a -> STGroup a
+ Text.StringTemplate.Helpers: renderTemplateDirGroup :: ToSElem a => STDirGroups String -> FilePath -> String -> [(String, a)] -> String
- Text.StringTemplate.Helpers: directoryGroupSafer :: Stringable a => FilePath -> IO (STGroup a)
+ Text.StringTemplate.Helpers: directoryGroupSafer :: FilePath -> IO (String -> StFirst (StringTemplate String))
- Text.StringTemplate.Helpers: directoryGroupsSafer :: Stringable a => FilePath -> IO (STDirGroups a)
+ Text.StringTemplate.Helpers: directoryGroupsSafer :: FilePath -> IO (Map FilePath (String -> StFirst (StringTemplate String)))

Files

HStringTemplateHelpers.cabal view
@@ -1,5 +1,5 @@ Name: HStringTemplateHelpers-Version: 0.0.3+Version: 0.0.4 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
@@ -9,6 +9,8 @@   directoryGroupSafer   , directoryGroupsSafer   , dirgroupKeys+  , getTemplateGroup+  , renderTemplateDirGroup   , lookupDirgroup   , renderTemplateGroup   , render1@@ -32,6 +34,8 @@ import qualified Data.Map as M import Text.StringTemplate.Classes import Safe++ {- |  Chooses a template from an STGroup, or errors if not found. @@ -76,11 +80,25 @@  The top group has key \".\" (mnemonic, current directory), other groups have key names of subdirectories, including the starting ., eg \".\/templates\/path\/to/\subdir\" -} -directoryGroupsSafer :: (Stringable a) => FilePath -> IO (STDirGroups a)-directoryGroupsSafer d = bracketCD d $ (return . M.fromList =<< ) . ( mapM f =<< ) . findDirectories $ "."-  where f d = do g <- directoryGroupSafer d-                 return (d,g)+--directoryGroupsSafer :: (Stringable a) => FilePath -> IO (STDirGroups a)+directoryGroupsSafer d = bracketCD d $ do+                           subDirs <- findDirectories $ "." +                           -- attempt to make strict, but doesn't make any difference+                           --putStrLn . show . last $ show subDirs                           +                           return . M.fromList =<< mapM f subDirs                               +  where +    f d = do g <- directoryGroupSafer d+             return (d,g)+    -- this seems suspect. Perhaps try using the find module on hackage?  +    -- | wrapper over find \/path\/to\/top\/dir -type d+    findDirectories :: FilePath -> IO [FilePath]+    findDirectories d = runStrings $ render1 [("d",d)] "find $d$ -type d"+      where +        runStrings :: String -> IO [String]+        runStrings = ( return . lines =<< ) . run ++ {- |  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. -}@@ -90,19 +108,37 @@ lookupDirgroup :: (Stringable a) => FilePath -> STDirGroups a -> Maybe (STGroup a) lookupDirgroup d = M.lookup d +++-- | > example: getTG "./baselayout" ts'+getTemplateGroup :: (Stringable a) => FilePath -> STDirGroups a -> STGroup a+getTemplateGroup dir tdg = maybe (error $ "getTG, bad dir:" ++ dir) id . lookupDirgroup dir $ tdg++-- | > example: renderTemplateDirGroup ts' "./baselayout" "base" +renderTemplateDirGroup :: ToSElem a => STDirGroups String -> FilePath -> String -> [(String,a)] -> String+renderTemplateDirGroup tdg dir tname attrs = +  let ts = getTemplateGroup dir tdg +  in  renderTemplateGroup ts attrs tname+++ -- | calculate the STGroup for a given directory, filtering out files that are probably errors (eg emacs backups)-directoryGroupSafer :: (Stringable a) => FilePath -> IO (STGroup a)-directoryGroupSafer path = groupStringTemplates <$>-                      (fmap <$> zip . (map dropExtension)-                       <*> mapM (newSTMP <$$> (readFile . (path </>)))-                           =<< mapM checkTmplName-                           =<< return . filter isTemplateFile -                           =<< getDirectoryContents path)-  where checkTmplName t = if  ( badTmplVarName . takeBaseName ) t-                            then fail $ "safeDirectoryGroup, bad template name: " ++ t+--directoryGroupSafer :: (Stringable a) => FilePath -> IO (STGroup a)+directoryGroupSafer :: FilePath -> IO (String -> StFirst (StringTemplate String) )+directoryGroupSafer path = do +  files <- mapM checkTmplName =<< return . filter isTemplateFile =<< getDirectoryContents path   +  contents <- mapM readFile $ map (path </>) files +  -- does this make it strict? what's the right way to do it?+  -- mm, the buggy behavior persists.+  --putStrLn . show . last . show $ contents +  let sts = map newSTMP contents+      tnames = map dropExtension files+  return $ groupStringTemplates $ zip tnames sts      +  where +    checkTmplName t = if  ( badTmplVarName . takeBaseName ) t +                            then fail $ "directoryGroupSafer, bad template name: " ++ t                              else return t--isTemplateFile f = ( (".st" ==) . takeExtension $ f ) +    isTemplateFile f = ( (".st" ==) . takeExtension $ f )                     && (not . or . map (=='#') $ f ) {-filename doesn't contain naughty emacs backup character-}                           @@ -113,7 +149,7 @@               mbFoundbadattr  (<$$>) :: (Functor f1, Functor f) => (a -> b) -> f (f1 a) -> f (f1 b)-(<$$>) x y = ((<$>) . (<$>)) x y+(<$$>) = fmap . fmap  badTmplVarName :: String -> Bool badTmplVarName t = or . map (not . isAlpha) $ t@@ -139,15 +175,7 @@  --t2 :: IO String --t2 = renderArrg "Matey" -  --- | wrapper over find \/path\/to\/top\/dir -type d-findDirectories :: FilePath -> IO [FilePath]-findDirectories d = runStrings $ render1 [("d",d)] "find $d$ -type d" -runS :: String -> IO String-runS = run-runStrings :: String -> IO [String]-runStrings = ( return . lines =<< ) . run   {- |