1-- | A module containing various function for manipulating and examinating
2-- files and directories.
15import System.Directory
18import Data.List (isPrefixOf)
19import Control.Monad.Reader (liftIO)
21import Text.Hakyll.Hakyll (Hakyll)
23-- | Auxiliary function to remove pathSeparators form the start. We don't deal
24-- with absolute paths here. We also remove $root from the start.
25removeLeadingSeparator :: FilePath -> FilePath
26removeLeadingSeparator [] = []
27removeLeadingSeparator path
28 | head path' `elem` pathSeparators = tail path'
31 path' = if "$root" `isPrefixOf` path then drop 5 path
34-- | Convert a relative filepath to a filepath in the destination (@_site@).
35toDestination :: FilePath -> FilePath
36toDestination path = "_site" </> removeLeadingSeparator path
38-- | Get the url for a given page.
39toURL :: FilePath -> FilePath
40toURL path = if takeExtension path `elem` [ ".markdown"
51 then flip addExtension ".html" $ dropExtension path
54-- | Get the relative url to the site root, for a given (absolute) url
55toRoot :: FilePath -> FilePath
56toRoot = emptyException . joinPath . map parent . splitPath
57 . takeDirectory . removeLeadingSeparator
60 emptyException [] = "."
63-- | Swaps spaces for '-'.
64removeSpaces :: FilePath -> FilePath
65removeSpaces = map swap
70-- | Given a path to a file, try to make the path writable by making
71-- all directories on the path.
72makeDirectories :: FilePath -> Hakyll ()
73makeDirectories path = liftIO $ createDirectoryIfMissing True dir
75 dir = takeDirectory path
77-- | Get all contents of a directory. Note that files starting with a dot (.)
79getRecursiveContents :: FilePath -> Hakyll [FilePath]
80getRecursiveContents topdir = do
81 names <- liftIO $ getDirectoryContents topdir
82 let properNames = filter isProper names
83 paths <- forM properNames $ \name -> do
84 let path = topdir </> name
85 isDirectory <- liftIO $ doesDirectoryExist path
87 then getRecursiveContents path
91 isProper = not . (== '.') . head
93-- | A filter that takes all file names with a given extension. Prefix the
94-- extension with a dot:
96-- > havingExtension ".markdown" [ "index.markdown"
98-- > ] == ["index.markdown"]
99havingExtension :: String -> [FilePath] -> [FilePath]
100havingExtension extension = filter ((==) extension . takeExtension)
102-- | Perform a Hakyll action on every file in a given directory.
103directory :: (FilePath -> Hakyll ()) -> FilePath -> Hakyll ()
104directory action dir = getRecursiveContents dir >>= mapM_ action
106-- | Check if a cache file is still valid.
107isCacheValid :: FilePath -- ^ The cached file.
108 -> [FilePath] -- ^ Dependencies of the cached file.
110isCacheValid cache depends = do
111 exists <- liftIO $ doesFileExist cache
114 else do dependsModified <- liftIO $ mapM getModificationTime depends
115 cacheModified <- liftIO $ getModificationTime cache
116 return (cacheModified >= 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