Commit abdb0b1edc398a543a9e458c3119e8f2277611e2 Parent: 8186443d7455cef856b7a5d0de910c7715b50e77 Author: mcol <mcol@posteo.net> Date: 2021-09-18 18:31:19 +0100 Committer: mcol <mcol@posteo.net> Committed: 2021-09-18 18:31:19 +0100 Get template-reading workable and plugged into Repositories
src/Templates.hs Modified
@@ -19,31 +19,47 @@ , templateContents :: String } ---tryLoad :: FilePath -> IO (Maybe String) ---tryLoad path = do --- e <- tryIOError $ readFile path --- case e of --- Right contents -> --- return $ Just contents --- Left error -> do --- print error --- return Nothing +{- +This is the main function of this module, which takes the session's `Config` and returns +the set of available templates loaded from files found in the template directory. +-} +loadTemplates :: Config -> IO [Template] +loadTemplates config = do + files <- getFiles config + sequence $ loadTemplate <$> files +---------------------------------------------------------------------------------------- +-- Private + +{- +This reads the contents of a file and stores it within a `Template` object. +-} loadTemplate :: FilePath -> IO Template loadTemplate path = do contents <- readFile path return $ Template path contents +{- +This is used to filter files in the template directory so that we only try to load +HTML/CSS/JS files. +-} isTemplate :: FilePath -> IO Bool isTemplate path = ((&&) $ isTemplate' path) <$> (doesFileExist path) - -isTemplate' :: FilePath -> Bool -isTemplate' path = isSuffixOf "html" p || isSuffixOf "css" p || isSuffixOf "js" p where p = map toLower path + isTemplate' :: FilePath -> Bool + isTemplate' path = isSuffixOf "html" p || isSuffixOf "css" p || isSuffixOf "js" p +{- +This wraps getDirectoryContents so that we get a list of fully qualified paths of the +directory's contents. +-} listTemplates :: FilePath -> IO [FilePath] listTemplates directory = fmap (directory </>) <$> getDirectoryContents directory -loadTemplates :: Config -> IO [FilePath] -loadTemplates = filterM isTemplate <=< listTemplates . templateDirectory +{- +getFiles will look inside the template directory and generate a list of paths to likely +valid template files. +-} +getFiles :: Config -> IO [FilePath] +getFiles = filterM isTemplate <=< listTemplates . templateDirectory
src/Repositories.hs Modified
@@ -10,13 +10,14 @@ import Git.Libgit2 (lgFactory) import Config (Config, repoPaths) +import Templates (Template, templatePath, templateContents) {- This is the entrypoint that receives the ``Config`` and uses it to map over our repositories, reading from them and writing out their web pages. -} -run :: Config -> IO () -run = mconcat . fmap processRepo . repoPaths +run :: Config -> [Template] -> IO () +run config templates = mconcat . fmap processRepo . repoPaths $ config {- This receives a file path to a single repository and tries to process it. If the
src/Main.hs Modified
@@ -9,5 +9,4 @@ main = do config <- getConfig "./config.dhall" templates <- loadTemplates config - --run config templates - return () + run config templates