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 = emptyException . joinPath . map parent . splitPath
42 . takeDirectory . removeLeadingSeparator
43 where parent = const ".."
44 emptyException [] = "."
47-- | Swaps spaces for '-'.
48removeSpaces :: FilePath -> FilePath
49removeSpaces = map swap
53-- | Given a path to a file, try to make the path writable by making
54-- all directories on the path.
55makeDirectories :: FilePath -> IO ()
56makeDirectories path = createDirectoryIfMissing True dir
57 where dir = takeDirectory path
59-- | Get all contents of a directory. Note that files starting with a dot (.)
61getRecursiveContents :: FilePath -> IO [FilePath]
62getRecursiveContents topdir = do
63 names <- getDirectoryContents topdir
64 let properNames = filter isProper names
65 paths <- forM properNames $ \name -> do
66 let path = topdir </> name
67 isDirectory <- doesDirectoryExist path
69 then getRecursiveContents path
72 where isProper = not . (== '.') . head
74-- | A filter that takes all file names with a given extension. Prefix the
75-- extension with a dot:
77-- > havingExtension ".markdown" ["index.markdown", "style.css"] == ["index.markdown"]
78havingExtension :: String -> [FilePath] -> [FilePath]
79havingExtension extension = filter ((==) extension . takeExtension)
81-- | Perform an IO action on every file in a given directory.
82directory :: (FilePath -> IO ()) -> FilePath -> IO ()
83directory action dir = getRecursiveContents dir >>= mapM_ action
85-- | Check if a cache file is still valid.
86isCacheValid :: FilePath -> [FilePath] -> IO Bool
87isCacheValid cache depends = doesFileExist cache >>= \exists ->
88 if not exists then return False
89 else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
90 cacheModified <- getModificationTime cache
91 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