🐙 Templated web page generator for your git repositories
git clone https://github.com/m-col/gitja
Files | Refs | Readme | License

Commit 99e5fa538c92925dc31ba43b7596fb99e6a8e7e5
Parent: 57468a35760f2784135e1f264e9c7a8366034894
Author: mcol <mcol@posteo.net>
Date: 2021-09-24 20:50:34 +0100
Committer: mcol <mcol@posteo.net>
Committed: 2021-09-24 20:50:34 +0100

Add indexTemplate setting and load it as a template

src/Templates.hs Modified

@@ -4,6 +4,7 @@
     Template,
     templateGinger,
     loadTemplates,
+    loadIndexTemplate,
     generate,
 ) where
 
@@ -22,7 +23,7 @@
 import Text.Ginger.Html (htmlSource, Html)
 import Text.Ginger.Run (easyRenderM, Run, RuntimeError)
 
-import Config (Config, templateDirectory)
+import Config (Config, templateDirectory, indexTemplate)
 
 data Template = Template
     { templatePath :: FilePath
@@ -30,8 +31,9 @@
     }
 
 {-
-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.
+This takes the session's `Config` and returns the set of available templates loaded from
+files found in the template directory, excluding the index template if it is in the same
+directory.
 -}
 loadTemplates :: Config -> IO [Template]
 loadTemplates config = do
@@ -39,6 +41,19 @@
     parsed <- sequence $ parseGingerFile includeResolver <$> files
     return $ Template <$> files <*> rights parsed
 
+{-
+This takes the session's `Config` and maybe returns a loaded template for the
+``indexTemplate`` setting.
+-}
+loadIndexTemplate :: Config -> IO (Maybe Template)
+loadIndexTemplate config = parseGingerFile includeResolver file >>= \case
+    Right parsed -> return . Just . Template file $ parsed
+    Left err -> do
+        print err
+        return Nothing
+  where
+    file = indexTemplate config
+
 ----------------------------------------------------------------------------------------
 
 {-

src/Main.hs Modified

@@ -6,10 +6,11 @@
 
 import Config (getConfig)
 import Repositories (run)
-import Templates (loadTemplates)
+import Templates (loadTemplates, loadIndexTemplate)
 
 main :: IO ()
 main = do
     config <- getConfig "./config.dhall"
     templates <- loadTemplates config
+    index <- loadIndexTemplate config
     run config templates

src/Config.hs Modified

@@ -5,6 +5,7 @@
     Config,
     repoPaths,
     templateDirectory,
+    indexTemplate,
     getConfig,
     outputDirectory,
     host
@@ -15,6 +16,7 @@
 data Config = Config
     { repoPaths :: [FilePath]
     , templateDirectory :: FilePath
+    , indexTemplate :: FilePath
     , outputDirectory :: FilePath
     , host :: Text
     }

config.dhall Modified

@@ -13,6 +13,7 @@
 let config =
     { repoPaths = folders
     , templateDirectory = "./templates"
+    , indexTemplate = "./templates/main_index.html"
     , outputDirectory = "./output"
     , host = "http://localhost"
     }