1-- | A module containing various function for manipulating and examinating
2-- files and directories.
10 , getRecursiveContents
18import System.Directory
20import System.Time (ClockTime)
22import Data.List (isPrefixOf, sortBy)
23import Data.Ord (comparing)
24import Control.Monad.Reader (liftIO)
26import Text.Hakyll.Hakyll
28-- | Auxiliary function to remove pathSeparators form the start. We don't deal
29-- with absolute paths here. We also remove $root from the start.
30removeLeadingSeparator :: FilePath -> FilePath
31removeLeadingSeparator [] = []
32removeLeadingSeparator path
33 | head path' `elem` pathSeparators = tail path'
36 path' = if "$root" `isPrefixOf` path then drop 5 path
39-- | Convert a relative filepath to a filepath in the destination
41toDestination :: FilePath -> Hakyll FilePath
42toDestination url = do dir <- askHakyll siteDirectory
43 enableIndexUrl' <- askHakyll enableIndexUrl
44 let destination = if enableIndexUrl' && separatorEnd
45 then dir </> noSeparator </> "index.html"
46 else dir </> noSeparator
49 noSeparator = removeLeadingSeparator url
50 separatorEnd = not (null url) && last url == '/'
52-- | Convert a relative filepath to a filepath in the cache
53-- (default: @_cache@).
54toCache :: FilePath -> Hakyll FilePath
55toCache path = do dir <- askHakyll cacheDirectory
56 return $ dir </> removeLeadingSeparator path
58-- | Get the url for a given page. For most extensions, this would be the path
59-- itself. It's only for rendered extensions (@.markdown@, @.rst@, @.lhs@ this
60-- function returns a path with a @.html@ extension instead.
61toUrl :: FilePath -> Hakyll FilePath
62toUrl path = do enableIndexUrl' <- askHakyll enableIndexUrl
63 -- If the file does not have a renderable extension, like for
64 -- example favicon.ico, we don't have to change it at all.
65 return $ if not hasRenderableExtension
67 -- If index url's are enabled, we create pick it
68 -- unless the page is an index already.
69 else if enableIndexUrl' && not isIndex
71 else withSimpleHtmlExtension
73 hasRenderableExtension = takeExtension path `elem` [ ".markdown"
87 isIndex = dropExtension (takeFileName path) == "index"
88 withSimpleHtmlExtension = flip addExtension ".html" $ dropExtension path
89 indexUrl = dropExtension path ++ "/"
92-- | Get the relative url to the site root, for a given (absolute) url
93toRoot :: FilePath -> FilePath
94toRoot = emptyException . joinPath . map parent . splitPath
95 . takeDirectory . removeLeadingSeparator
98 emptyException [] = "."
101-- | Swaps spaces for '-'.
102removeSpaces :: FilePath -> FilePath
103removeSpaces = map swap
108-- | Given a path to a file, try to make the path writable by making
109-- all directories on the path.
110makeDirectories :: FilePath -> Hakyll ()
111makeDirectories path = liftIO $ createDirectoryIfMissing True dir
113 dir = takeDirectory path
115-- | Get all contents of a directory. Note that files starting with a dot (.)
117getRecursiveContents :: FilePath -> Hakyll [FilePath]
118getRecursiveContents topdir = do
119 names <- liftIO $ getDirectoryContents topdir
120 let properNames = filter isProper names
121 paths <- forM properNames $ \name -> do
122 let path = topdir </> name
123 isDirectory <- liftIO $ doesDirectoryExist path
125 then getRecursiveContents path
127 return (concat paths)
129 isProper = not . (== '.') . head
131-- | Sort a list of filenames on the basename.
132sortByBaseName :: [FilePath] -> [FilePath]
133sortByBaseName = sortBy compareBaseName
135 compareBaseName = comparing takeFileName
137-- | A filter that takes all file names with a given extension. Prefix the
138-- extension with a dot:
140-- > havingExtension ".markdown" [ "index.markdown"
142-- > ] == ["index.markdown"]
143havingExtension :: String -> [FilePath] -> [FilePath]
144havingExtension extension = filter ((==) extension . takeExtension)
146-- | Perform a Hakyll action on every file in a given directory.
147directory :: (FilePath -> Hakyll ()) -> FilePath -> Hakyll ()
148directory action dir = getRecursiveContents dir >>= mapM_ action
150-- | Check if a timestamp is newer then a number of given files.
151isMoreRecent :: ClockTime -- ^ The time to check.
152 -> [FilePath] -- ^ Dependencies of the cached file.
154isMoreRecent _ [] = return True
155isMoreRecent timeStamp depends = do
156 dependsModified <- liftIO $ mapM getModificationTime depends
157 return (timeStamp >= maximum dependsModified)
159-- | Check if a file is newer then a number of given files.
160isFileMoreRecent :: FilePath -- ^ The cached file.
161 -> [FilePath] -- ^ Dependencies of the cached file.
163isFileMoreRecent file depends = do
164 exists <- liftIO $ doesFileExist file
167 else do timeStamp <- liftIO $ getModificationTime file
168 isMoreRecent timeStamp depends
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