1-- | A module containing various function for manipulating and examinating
2-- files and directories.
10 , getRecursiveContents
17import System.Directory
20import Data.List (isPrefixOf, sortBy)
21import Control.Monad.Reader (liftIO)
23import Text.Hakyll.Hakyll
25-- | Auxiliary function to remove pathSeparators form the start. We don't deal
26-- with absolute paths here. We also remove $root from the start.
27removeLeadingSeparator :: FilePath -> FilePath
28removeLeadingSeparator [] = []
29removeLeadingSeparator path
30 | head path' `elem` pathSeparators = tail path'
33 path' = if "$root" `isPrefixOf` path then drop 5 path
36-- | Convert a relative filepath to a filepath in the destination
38toDestination :: FilePath -> Hakyll FilePath
39toDestination path = do dir <- askHakyll siteDirectory
40 return $ dir </> removeLeadingSeparator path
42-- | Convert a relative filepath to a filepath in the cache
43-- (default: @_cache@).
44toCache :: FilePath -> Hakyll FilePath
45toCache path = do dir <- askHakyll cacheDirectory
46 return $ dir </> removeLeadingSeparator path
48-- | Get the url for a given page. For most extensions, this would be the path
49-- itself. It's only for rendered extensions (@.markdown@, @.rst@, @.lhs@ this
50-- function returns a path with a @.html@ extension instead.
51toUrl :: FilePath -> FilePath
52toUrl path = if takeExtension path `elem` [ ".markdown"
64 then flip addExtension ".html" $ dropExtension path
67-- | Get the relative url to the site root, for a given (absolute) url
68toRoot :: FilePath -> FilePath
69toRoot = emptyException . joinPath . map parent . splitPath
70 . takeDirectory . removeLeadingSeparator
73 emptyException [] = "."
76-- | Swaps spaces for '-'.
77removeSpaces :: FilePath -> FilePath
78removeSpaces = map swap
83-- | Given a path to a file, try to make the path writable by making
84-- all directories on the path.
85makeDirectories :: FilePath -> Hakyll ()
86makeDirectories path = liftIO $ createDirectoryIfMissing True dir
88 dir = takeDirectory path
90-- | Get all contents of a directory. Note that files starting with a dot (.)
92getRecursiveContents :: FilePath -> Hakyll [FilePath]
93getRecursiveContents topdir = do
94 names <- liftIO $ getDirectoryContents topdir
95 let properNames = filter isProper names
96 paths <- forM properNames $ \name -> do
97 let path = topdir </> name
98 isDirectory <- liftIO $ doesDirectoryExist path
100 then getRecursiveContents path
102 return (concat paths)
104 isProper = not . (== '.') . head
106-- | Sort a list of filenames on the basename.
107sortByBaseName :: [FilePath] -> [FilePath]
108sortByBaseName = sortBy compareBaseName
110 compareBaseName f1 f2 = compare (takeFileName f1) (takeFileName f2)
112-- | A filter that takes all file names with a given extension. Prefix the
113-- extension with a dot:
115-- > havingExtension ".markdown" [ "index.markdown"
117-- > ] == ["index.markdown"]
118havingExtension :: String -> [FilePath] -> [FilePath]
119havingExtension extension = filter ((==) extension . takeExtension)
121-- | Perform a Hakyll action on every file in a given directory.
122directory :: (FilePath -> Hakyll ()) -> FilePath -> Hakyll ()
123directory action dir = getRecursiveContents dir >>= mapM_ action
125-- | Check if a file is newer then a number of given files.
126isMoreRecent :: FilePath -- ^ The cached file.
127 -> [FilePath] -- ^ Dependencies of the cached file.
129isMoreRecent file depends = do
130 exists <- liftIO $ doesFileExist file
133 else do dependsModified <- liftIO $ mapM getModificationTime depends
134 fileModified <- liftIO $ getModificationTime file
135 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