on master help sign in
Added an havingExtension filter because it's handy.
commitd29e4157f1647b06367c2e04c1cdea4c8175fd4c authorJasper Van der Jeugt <jaspervdj@gmail.com> dateTue 5 Jan 2010, 11:11 (17 years ago) parentdbb9a0e
2 files changed+24
src/Text/Hakyll/File.hs+7 tests/Tests.hs+17
src/Text/Hakyll/File.hs+7
@@ -6,6 +6,7 @@ module Text.Hakyll.File
66 , toURL
77 , makeDirectories
88 , getRecursiveContents
9+ , havingExtension
910 , isCacheValid
1011 , directory
1112 ) where
@@ -47,6 +48,12 @@ getRecursiveContents topdir = do
4748 return (concat paths)
4849 where isProper = not . (== '.') . head
4950
51+-- | A filter that takes all file names with a given extension. Prefix the
52+-- extension with a dot:
53+-- > havingExtension ".markdown" ["index.markdown", "style.css"] == ["index.markdown"]
54+havingExtension :: String -> [FilePath] -> [FilePath]
55+havingExtension extension = filter ((==) extension . takeExtension)
56+
5057-- | Perform an IO action on every file in a given directory.
5158directory :: (FilePath -> IO ()) -> FilePath -> IO ()
5259directory action dir = getRecursiveContents dir >>= mapM_ action
tests/Tests.hs+17
@@ -11,6 +11,7 @@ import qualified Data.ByteString.Lazy.Char8 as B
1111import Text.Hakyll.CompressCSS
1212import Text.Hakyll.Util
1313import Text.Hakyll.Context
14+import Text.Hakyll.File
1415
1516main = defaultMain tests
1617
@@ -33,6 +34,11 @@ tests = [ testGroup "Util group" [ testProperty "trim length" prop_trim_length
3334 , testGroup "Context group" [ testCase "renderDate 1" test_render_date1
3435 , testCase "renderDate 2" test_render_date1
3536 ]
37+
38+ , testGroup "File group" [ testProperty "havingExtension count" prop_having_extension_count
39+ , testCase "havingExtension 1" test_having_extension1
40+ , testCase "havingExtension 2" test_having_extension2
41+ ]
3642 ]
3743
3844-- Test that a string always becomes shorter when trimmed.
@@ -78,3 +84,14 @@ test_render_date2 = M.lookup (B.pack "date") rendered @?= Just (B.pack "Unknown
7884 "Unknown date"
7985 (M.singleton (B.pack "path")
8086 (B.pack "2009-badness-30-a-title.markdown"))
87+
88+-- Add an extension, and test that they have that extension
89+prop_having_extension_count names extension =
90+ not (any ('.' `elem`) names || any (`elem` extension) "./\\")
91+ ==> havingExtension fullExtension withExtensions == withExtensions
92+ where fullExtension = '.' : extension
93+ withExtensions = map (++ fullExtension) names
94+
95+-- Having extension test cases
96+test_having_extension1 = havingExtension ".foo" ["file.bar", "file.txt"] @?= []
97+test_having_extension2 = havingExtension ".foo" ["file.foo", "file.txt"] @?= ["file.foo"]