on master help sign in
Added an havingExtension filter because it's handy.
d29e415Jasper Van der Jeugt
src/Text/Hakyll/File.hs+7
@@ -6,6 +6,7 @@ module Text.Hakyll.File
6 , toURL
6 , toURL
7 , makeDirectories
7 , makeDirectories
8 , getRecursiveContents
8 , getRecursiveContents
9+ , havingExtension
9 , isCacheValid
10 , isCacheValid
10 , directory
11 , directory
11 ) where
12 ) where
@@ -47,6 +48,12 @@ getRecursiveContents topdir = do
47 return (concat paths)
48 return (concat paths)
48 where isProper = not . (== '.') . head
49 where isProper = not . (== '.') . head
49
50
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+
50-- | Perform an IO action on every file in a given directory.
57-- | Perform an IO action on every file in a given directory.
51directory :: (FilePath -> IO ()) -> FilePath -> IO ()
58directory :: (FilePath -> IO ()) -> FilePath -> IO ()
52directory action dir = getRecursiveContents dir >>= mapM_ action
59directory action dir = getRecursiveContents dir >>= mapM_ action
tests/Tests.hs+17
@@ -11,6 +11,7 @@ import qualified Data.ByteString.Lazy.Char8 as B
11import Text.Hakyll.CompressCSS
11import Text.Hakyll.CompressCSS
12import Text.Hakyll.Util
12import Text.Hakyll.Util
13import Text.Hakyll.Context
13import Text.Hakyll.Context
14+import Text.Hakyll.File
14
15
15main = defaultMain tests
16main = defaultMain tests
16
17
@@ -33,6 +34,11 @@ tests = [ testGroup "Util group" [ testProperty "trim length" prop_trim_length
33 , testGroup "Context group" [ testCase "renderDate 1" test_render_date1
34 , testGroup "Context group" [ testCase "renderDate 1" test_render_date1
34 , testCase "renderDate 2" test_render_date1
35 , testCase "renderDate 2" test_render_date1
35 ]
36 ]
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+ ]
36 ]
42 ]
37
43
38-- Test that a string always becomes shorter when trimmed.
44-- 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
78 "Unknown date"
84 "Unknown date"
79 (M.singleton (B.pack "path")
85 (M.singleton (B.pack "path")
80 (B.pack "2009-badness-30-a-title.markdown"))
86 (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"]