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.
49toURL :: FilePath -> FilePath
50toURL path = if takeExtension path `elem` [ ".markdown"
62 then flip addExtension ".html" $ dropExtension path
65-- | Get the relative url to the site root, for a given (absolute) url
66toRoot :: FilePath -> FilePath
67toRoot = emptyException . joinPath . map parent . splitPath
68 . takeDirectory . removeLeadingSeparator
71 emptyException [] = "."
74-- | Swaps spaces for '-'.
75removeSpaces :: FilePath -> FilePath
76removeSpaces = map swap
81-- | Given a path to a file, try to make the path writable by making
82-- all directories on the path.
83makeDirectories :: FilePath -> Hakyll ()
84makeDirectories path = liftIO $ createDirectoryIfMissing True dir
86 dir = takeDirectory path
88-- | Get all contents of a directory. Note that files starting with a dot (.)
90getRecursiveContents :: FilePath -> Hakyll [FilePath]
91getRecursiveContents topdir = do
92 names <- liftIO $ getDirectoryContents topdir
93 let properNames = filter isProper names
94 paths <- forM properNames $ \name -> do
95 let path = topdir </> name
96 isDirectory <- liftIO $ doesDirectoryExist path
98 then getRecursiveContents path
100 return (concat paths)
102 isProper = not . (== '.') . head
104-- | Sort a list of filenames on the basename.
105sortByBaseName :: [FilePath] -> [FilePath]
106sortByBaseName = sortBy compareBaseName
108 compareBaseName f1 f2 = compare (takeFileName f1) (takeFileName f2)
110-- | A filter that takes all file names with a given extension. Prefix the
111-- extension with a dot:
113-- > havingExtension ".markdown" [ "index.markdown"
115-- > ] == ["index.markdown"]
116havingExtension :: String -> [FilePath] -> [FilePath]
117havingExtension extension = filter ((==) extension . takeExtension)
119-- | Perform a Hakyll action on every file in a given directory.
120directory :: (FilePath -> Hakyll ()) -> FilePath -> Hakyll ()
121directory action dir = getRecursiveContents dir >>= mapM_ action
123-- | Check if a file is newer then a number of given files.
124isMoreRecent :: FilePath -- ^ The cached file.
125 -> [FilePath] -- ^ Dependencies of the cached file.
127isMoreRecent file depends = do
128 exists <- liftIO $ doesFileExist file
131 else do dependsModified <- liftIO $ mapM getModificationTime depends
132 fileModified <- liftIO $ getModificationTime file
133 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