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 path = if takeExtension path `elem` [".markdown", ".md", ".tex"]
38 then flip addExtension ".html" $ dropExtension path
41-- | Get the relative url to the site root, for a given (absolute) url
42toRoot :: FilePath -> FilePath
43toRoot = emptyException . joinPath . map parent . splitPath
44 . takeDirectory . removeLeadingSeparator
45 where parent = const ".."
46 emptyException [] = "."
49-- | Swaps spaces for '-'.
50removeSpaces :: FilePath -> FilePath
51removeSpaces = map swap
55-- | Given a path to a file, try to make the path writable by making
56-- all directories on the path.
57makeDirectories :: FilePath -> IO ()
58makeDirectories path = createDirectoryIfMissing True dir
59 where dir = takeDirectory path
61-- | Get all contents of a directory. Note that files starting with a dot (.)
63getRecursiveContents :: FilePath -> IO [FilePath]
64getRecursiveContents topdir = do
65 names <- getDirectoryContents topdir
66 let properNames = filter isProper names
67 paths <- forM properNames $ \name -> do
68 let path = topdir </> name
69 isDirectory <- doesDirectoryExist path
71 then getRecursiveContents path
74 where isProper = not . (== '.') . head
76-- | A filter that takes all file names with a given extension. Prefix the
77-- extension with a dot:
79-- > havingExtension ".markdown" ["index.markdown", "style.css"] == ["index.markdown"]
80havingExtension :: String -> [FilePath] -> [FilePath]
81havingExtension extension = filter ((==) extension . takeExtension)
83-- | Perform an IO action on every file in a given directory.
84directory :: (FilePath -> IO ()) -> FilePath -> IO ()
85directory action dir = getRecursiveContents dir >>= mapM_ action
87-- | Check if a cache file is still valid.
88isCacheValid :: FilePath -> [FilePath] -> IO Bool
89isCacheValid cache depends = doesFileExist cache >>= \exists ->
90 if not exists then return False
91 else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
92 cacheModified <- getModificationTime cache
93 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