on master help sign in
Splitted Text.Hakyll.Util into Text.Hakyll.File and Text.Hakyll.Util.
221c029Jasper Van der Jeugt
src/Text/Hakyll/File.hsnew file+55
@@ -0,0 +1,55 @@
1+-- | A module containing various function for manipulating and examinating
2+-- files and directories.
3+module Text.Hakyll.File
4+ ( toDestination,
5+ toCache,
6+ toURL,
7+ makeDirectories,
8+ getRecursiveContents,
9+ isCacheValid
10+ ) where
11+
12+import System.Directory
13+import System.FilePath
14+import Control.Monad
15+
16+-- | Convert a relative filepath to a filepath in the destination (_site).
17+toDestination :: FilePath -> FilePath
18+toDestination path = "_site" </> path
19+
20+-- | Convert a relative filepath to a filepath in the cache (_cache).
21+toCache :: FilePath -> FilePath
22+toCache path = "_cache" </> path
23+
24+-- | Get the url for a given page.
25+toURL :: FilePath -> FilePath
26+toURL = flip addExtension ".html" . dropExtension
27+
28+-- | Given a path to a file, try to make the path writable by making
29+-- all directories on the path.
30+makeDirectories :: FilePath -> IO ()
31+makeDirectories path = createDirectoryIfMissing True dir
32+ where dir = takeDirectory path
33+
34+-- | Get all contents of a directory. Note that files starting with a dot (.)
35+-- will be ignored.
36+getRecursiveContents :: FilePath -> IO [FilePath]
37+getRecursiveContents topdir = do
38+ names <- getDirectoryContents topdir
39+ let properNames = filter isProper names
40+ paths <- forM properNames $ \name -> do
41+ let path = topdir </> name
42+ isDirectory <- doesDirectoryExist path
43+ if isDirectory
44+ then getRecursiveContents path
45+ else return [path]
46+ return (concat paths)
47+ where isProper = not . (== '.') . head
48+
49+-- | Check is a cache file is still valid.
50+isCacheValid :: FilePath -> [FilePath] -> IO Bool
51+isCacheValid cache depends = doesFileExist cache >>= \exists ->
52+ if not exists then return False
53+ else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
54+ cacheModified <- getModificationTime cache
55+ return (cacheModified >= dependsModified)
src/Text/Hakyll/Page.hs+1
@@ -15,6 +15,7 @@ import Control.Monad
15import System.FilePath
15import System.FilePath
16import System.IO
16import System.IO
17
17
18+import Text.Hakyll.File
18import Text.Hakyll.Util
19import Text.Hakyll.Util
19import Text.Hakyll.Renderable
20import Text.Hakyll.Renderable
20import Text.Pandoc
21import Text.Pandoc
src/Text/Hakyll/Render.hs+1−1
@@ -17,7 +17,7 @@ import System.IO
17
17
18import Text.Hakyll.Page
18import Text.Hakyll.Page
19import Text.Hakyll.Renderable
19import Text.Hakyll.Renderable
20−import Text.Hakyll.Util
20+import Text.Hakyll.File
21
21
22-- | Execute an IO action only when the cache is invalid.
22-- | Execute an IO action only when the cache is invalid.
23depends :: FilePath -- ^ File to be rendered or created.
23depends :: FilePath -- ^ File to be rendered or created.
src/Text/Hakyll/Renderables.hs+1−1
@@ -9,9 +9,9 @@ import System.FilePath
9import qualified Data.ByteString.Lazy.Char8 as B
9import qualified Data.ByteString.Lazy.Char8 as B
10import qualified Data.Map as M
10import qualified Data.Map as M
11import Control.Monad
11import Control.Monad
12−import Text.Hakyll.Util
13import Text.Hakyll.Page
12import Text.Hakyll.Page
14import Text.Hakyll.Renderable
13import Text.Hakyll.Renderable
14+import Text.Hakyll.File
15
15
16-- | A custom page.
16-- | A custom page.
17data CustomPage = CustomPage
17data CustomPage = CustomPage
src/Text/Hakyll/Util.hs+6−56
@@ -1,65 +1,23 @@
1module Text.Hakyll.Util
1module Text.Hakyll.Util
2− ( toDestination,
3− toCache,
4− toURL,
5− makeDirectories,
6− getRecursiveContents,
7− trim,
2+ ( trim,
8 split,
3 split,
9− stripHtml,
4+ stripHTML
10− isCacheValid
11 ) where
5 ) where
12
6
13−import System.Directory
14−import System.FilePath
15−import Control.Monad
16import Data.Char
7import Data.Char
17import Data.List
8import Data.List
18
9
19−-- | Convert a relative filepath to a filepath in the destination (_site).
20−toDestination :: FilePath -> FilePath
21−toDestination path = "_site" </> path
22−
23−-- | Convert a relative filepath to a filepath in the cache (_cache).
24−toCache :: FilePath -> FilePath
25−toCache path = "_cache" </> path
26−
27−-- | Get the url for a given page.
28−toURL :: FilePath -> FilePath
29−toURL = flip addExtension ".html" . dropExtension
30−
31−-- | Given a path to a file, try to make the path writable by making
32−-- all directories on the path.
33−makeDirectories :: FilePath -> IO ()
34−makeDirectories path = createDirectoryIfMissing True dir
35− where dir = takeDirectory path
36−
37−-- | Get all contents of a directory. Note that files starting with a dot (.)
38−-- will be ignored.
39−getRecursiveContents :: FilePath -> IO [FilePath]
40−getRecursiveContents topdir = do
41− names <- getDirectoryContents topdir
42− let properNames = filter isProper names
43− paths <- forM properNames $ \name -> do
44− let path = topdir </> name
45− isDirectory <- doesDirectoryExist path
46− if isDirectory
47− then getRecursiveContents path
48− else return [path]
49− return (concat paths)
50− where isProper = not . (== '.') . head
51−
52-- | Trim a string (drop spaces and tabs at both sides).
10-- | Trim a string (drop spaces and tabs at both sides).
53trim :: String -> String
11trim :: String -> String
54trim = reverse . trim' . reverse . trim'
12trim = reverse . trim' . reverse . trim'
55 where trim' = dropWhile isSpace
13 where trim' = dropWhile isSpace
56
14
57-- | Strip html tags.
15-- | Strip html tags.
58−stripHtml :: String -> String
16+stripHTML :: String -> String
59−stripHtml [] = []
17+stripHTML [] = []
60−stripHtml str = let (beforeTag, rest) = break (== '<') str
18+stripHTML str = let (beforeTag, rest) = break (== '<') str
61 (_, afterTag) = break (== '>') rest
19 (_, afterTag) = break (== '>') rest
62− in beforeTag ++ (stripHtml $ tail' afterTag)
20+ in beforeTag ++ (stripHTML $ tail' afterTag)
63 -- We need a failsafe tail function.
21 -- We need a failsafe tail function.
64 where tail' [] = []
22 where tail' [] = []
65 tail' xs = tail xs
23 tail' xs = tail xs
@@ -72,11 +30,3 @@ split element = unfoldr splitOnce
72 (x, xs) -> if null xs
30 (x, xs) -> if null xs
73 then Just (x, [])
31 then Just (x, [])
74 else Just (x, tail xs)
32 else Just (x, tail xs)
75−
76−-- | Check is a cache file is still valid.
77−isCacheValid :: FilePath -> [FilePath] -> IO Bool
78−isCacheValid cache depends = doesFileExist cache >>= \exists ->
79− if not exists then return False
80− else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
81− cacheModified <- getModificationTime cache
82− return (cacheModified >= dependsModified)