1-- | A module containing various function for manipulating and examinating
2-- files and directories.
10 , getRecursiveContents
16import System.Directory
19import Data.List (isPrefixOf)
20import Control.Monad.Reader (liftIO)
22import Text.Hakyll.Hakyll
24-- | Auxiliary function to remove pathSeparators form the start. We don't deal
25-- with absolute paths here. We also remove $root from the start.
26removeLeadingSeparator :: FilePath -> FilePath
27removeLeadingSeparator [] = []
28removeLeadingSeparator path
29 | head path' `elem` pathSeparators = tail path'
32 path' = if "$root" `isPrefixOf` path then drop 5 path
35-- | Convert a relative filepath to a filepath in the destination
37toDestination :: FilePath -> Hakyll FilePath
38toDestination path = do dir <- askHakyll siteDirectory
39 return $ dir </> removeLeadingSeparator path
41-- | Convert a relative filepath to a filepath in the cache
42-- (default: @_cache@).
43toCache :: FilePath -> Hakyll FilePath
44toCache path = do dir <- askHakyll cacheDirectory
45 return $ dir </> removeLeadingSeparator path
47-- | Get the url for a given page.
48toURL :: FilePath -> FilePath
49toURL path = if takeExtension path `elem` [ ".markdown"
61 then flip addExtension ".html" $ dropExtension path
64-- | Get the relative url to the site root, for a given (absolute) url
65toRoot :: FilePath -> FilePath
66toRoot = emptyException . joinPath . map parent . splitPath
67 . takeDirectory . removeLeadingSeparator
70 emptyException [] = "."
73-- | Swaps spaces for '-'.
74removeSpaces :: FilePath -> FilePath
75removeSpaces = map swap
80-- | Given a path to a file, try to make the path writable by making
81-- all directories on the path.
82makeDirectories :: FilePath -> Hakyll ()
83makeDirectories path = liftIO $ createDirectoryIfMissing True dir
85 dir = takeDirectory path
87-- | Get all contents of a directory. Note that files starting with a dot (.)
89getRecursiveContents :: FilePath -> Hakyll [FilePath]
90getRecursiveContents topdir = do
91 names <- liftIO $ getDirectoryContents topdir
92 let properNames = filter isProper names
93 paths <- forM properNames $ \name -> do
94 let path = topdir </> name
95 isDirectory <- liftIO $ doesDirectoryExist path
97 then getRecursiveContents path
101 isProper = not . (== '.') . head
103-- | A filter that takes all file names with a given extension. Prefix the
104-- extension with a dot:
106-- > havingExtension ".markdown" [ "index.markdown"
108-- > ] == ["index.markdown"]
109havingExtension :: String -> [FilePath] -> [FilePath]
110havingExtension extension = filter ((==) extension . takeExtension)
112-- | Perform a Hakyll action on every file in a given directory.
113directory :: (FilePath -> Hakyll ()) -> FilePath -> Hakyll ()
114directory action dir = getRecursiveContents dir >>= mapM_ action
116-- | Check if a file is newer then a number of given files.
117isMoreRecent :: FilePath -- ^ The cached file.
118 -> [FilePath] -- ^ Dependencies of the cached file.
120isMoreRecent file depends = do
121 exists <- liftIO $ doesFileExist file
124 else do dependsModified <- liftIO $ mapM getModificationTime depends
125 fileModified <- liftIO $ getModificationTime file
126 return (fileModified >= maximum 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