1-- | A module containing various function for manipulating and examinating
2-- files and directories.
10 , getRecursiveContents
16import System.Directory
20-- | Auxiliary function to remove pathSeparators form the start. We don't deal
21-- with absolute paths here.
22removeLeadingSeparator :: FilePath -> FilePath
23removeLeadingSeparator [] = []
24removeLeadingSeparator p@(x:xs) | x `elem` pathSeparators = xs
27-- | Convert a relative filepath to a filepath in the destination (_site).
28toDestination :: FilePath -> FilePath
29toDestination path = "_site" </> (removeLeadingSeparator path)
31-- | Convert a relative filepath to a filepath in the cache (_cache).
32toCache :: FilePath -> FilePath
33toCache path = "_cache" </> (removeLeadingSeparator path)
35-- | Get the url for a given page.
36toURL :: FilePath -> FilePath
37toURL = flip addExtension ".html" . dropExtension
39-- | Get the relative url to the site root, for a given (absolute) url
40toRoot :: FilePath -> FilePath
41toRoot = joinPath . map parent . splitPath
42 . takeDirectory . removeLeadingSeparator
43 where parent = const ".."
45-- | Swaps spaces for '-'.
46removeSpaces :: FilePath -> FilePath
47removeSpaces = map swap
51-- | Given a path to a file, try to make the path writable by making
52-- all directories on the path.
53makeDirectories :: FilePath -> IO ()
54makeDirectories path = createDirectoryIfMissing True dir
55 where dir = takeDirectory path
57-- | Get all contents of a directory. Note that files starting with a dot (.)
59getRecursiveContents :: FilePath -> IO [FilePath]
60getRecursiveContents topdir = do
61 names <- getDirectoryContents topdir
62 let properNames = filter isProper names
63 paths <- forM properNames $ \name -> do
64 let path = topdir </> name
65 isDirectory <- doesDirectoryExist path
67 then getRecursiveContents path
70 where isProper = not . (== '.') . head
72-- | A filter that takes all file names with a given extension. Prefix the
73-- extension with a dot:
75-- > havingExtension ".markdown" ["index.markdown", "style.css"] == ["index.markdown"]
76havingExtension :: String -> [FilePath] -> [FilePath]
77havingExtension extension = filter ((==) extension . takeExtension)
79-- | Perform an IO action on every file in a given directory.
80directory :: (FilePath -> IO ()) -> FilePath -> IO ()
81directory action dir = getRecursiveContents dir >>= mapM_ action
83-- | Check if a cache file is still valid.
84isCacheValid :: FilePath -> [FilePath] -> IO Bool
85isCacheValid cache depends = doesFileExist cache >>= \exists ->
86 if not exists then return False
87 else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
88 cacheModified <- getModificationTime cache
89 return (cacheModified >= dependsModified)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list