on master help sign in
Splitted Text.Hakyll.Util into Text.Hakyll.File and Text.Hakyll.Util.
commit221c029dad1cef555b0540b152ec167e90937b16 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateThu 17 Dec 2009, 20:08 (17 years ago) parent6bbf514
5 files changed+64−58
src/Text/Hakyll/File.hs+55 src/Text/Hakyll/Page.hs+1 src/Text/Hakyll/Render.hs+1−1 src/Text/Hakyll/Renderables.hs+1−1 src/Text/Hakyll/Util.hs+6−56
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
1515import System.FilePath
1616import System.IO
1717
18+import Text.Hakyll.File
1819import Text.Hakyll.Util
1920import Text.Hakyll.Renderable
2021import Text.Pandoc
src/Text/Hakyll/Render.hs+1−1
@@ -17,7 +17,7 @@ import System.IO
1717
1818import Text.Hakyll.Page
1919import Text.Hakyll.Renderable
20−import Text.Hakyll.Util
20+import Text.Hakyll.File
2121
2222-- | Execute an IO action only when the cache is invalid.
2323depends :: FilePath -- ^ File to be rendered or created.
src/Text/Hakyll/Renderables.hs+1−1
@@ -9,9 +9,9 @@ import System.FilePath
99import qualified Data.ByteString.Lazy.Char8 as B
1010import qualified Data.Map as M
1111import Control.Monad
12−import Text.Hakyll.Util
1312import Text.Hakyll.Page
1413import Text.Hakyll.Renderable
14+import Text.Hakyll.File
1515
1616-- | A custom page.
1717data CustomPage = CustomPage
src/Text/Hakyll/Util.hs+6−56
@@ -1,65 +1,23 @@
11module Text.Hakyll.Util
2− ( toDestination,
3− toCache,
4− toURL,
5− makeDirectories,
6− getRecursiveContents,
7− trim,
2+ ( trim,
83 split,
9− stripHtml,
10− isCacheValid
4+ stripHTML
115 ) where
126
13−import System.Directory
14−import System.FilePath
15−import Control.Monad
167import Data.Char
178import Data.List
189
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−
5210-- | Trim a string (drop spaces and tabs at both sides).
5311trim :: String -> String
5412trim = reverse . trim' . reverse . trim'
5513 where trim' = dropWhile isSpace
5614
5715-- | Strip html tags.
58−stripHtml :: String -> String
59−stripHtml [] = []
60−stripHtml str = let (beforeTag, rest) = break (== '<') str
16+stripHTML :: String -> String
17+stripHTML [] = []
18+stripHTML str = let (beforeTag, rest) = break (== '<') str
6119 (_, afterTag) = break (== '>') rest
62− in beforeTag ++ (stripHtml $ tail' afterTag)
20+ in beforeTag ++ (stripHTML $ tail' afterTag)
6321 -- We need a failsafe tail function.
6422 where tail' [] = []
6523 tail' xs = tail xs
@@ -72,11 +30,3 @@ split element = unfoldr splitOnce
7230 (x, xs) -> if null xs
7331 then Just (x, [])
7432 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)