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

Commit dd5033c4253dbe6e9c90b75b675783e51c4fa4fd
Parent: 55a9c26b5349a0f6c95c224de3be7b9b88faa273
Author: mcol <mcol@posteo.net>
Date: 2021-09-25 13:48:44 +0100
Committer: mcol <mcol@posteo.net>
Committed: 2021-09-25 13:48:44 +0100

Generate index listing all repositories

templates/index-main.html Added

@@ -0,0 +1,19 @@
+<!doctype html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8">
+    <meta http-equiv="x-ua-compatible" content="ie=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+    <title>gitserve</title>
+    <link rel="stylesheet" href="{{- host }}/style.css" />
+  </head>
+
+  <body>
+    <p>gitserve</p>
+    <hr>
+		<p>My git repositories - {{ host }}</p>
+    {% for repo in repositories %}
+		<p><a href="{{- host }}/{{- repo.name }}">{{- repo.name }}</a> -- {{ repo.description }}</p>
+    {% endfor %}
+  </body>
+</html>

src/Templates.hs Modified

@@ -6,6 +6,7 @@
     loadTemplates,
     loadIndexTemplate,
     generate,
+    templatePath,
 ) where
 
 import Control.Monad (filterM, (<=<))

src/Repositories.hs Modified

@@ -29,15 +29,17 @@
 import qualified Data.HashMap.Strict as HashMap
 
 import Config (Config, repoPaths, outputDirectory, host)
-import Templates (Template, generate)
+import Templates (Template, generate, templatePath)
 
 {-
 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 using the given
 templates.
 -}
-run :: Config -> [Template] -> IO ()
-run config templates = foldMap (processRepo templates config) . repoPaths $ config
+run :: Config -> [Template] -> Maybe Template -> IO ()
+run config templates index = do
+    foldMap (processRepo templates config) . repoPaths $ config
+    runIndex config index
 
 ----------------------------------------------------------------------------------------
 
@@ -161,3 +163,52 @@
 treeAsLookup treefile = \case
     "path" -> Just . toGVal . treeFilePath $ treefile
     _ -> Nothing
+
+----------------------------------------------------------------------------------------
+
+{-
+This creates the main index file from the index template, using information from all
+configured respositories.
+-}
+runIndex :: Config -> Maybe Template -> IO ()
+runIndex config Nothing = return ()
+runIndex config (Just template) = do
+    let paths = repoPaths config
+    descriptions <- sequence . fmap (getDescription . flip (</>) "description") $ paths
+    let repos = zipWith ($) (Repo <$> takeFileName <$> paths) descriptions
+    let indexScope = packageIndex config repos
+    generate (outputDirectory config) indexScope (template { templatePath = "index.html" })
+    return ()
+
+packageIndex
+    :: Config
+    -> [Repo]
+    -> HashMap.HashMap Text (GVal (Run SourcePos IO Html))
+packageIndex config repos = HashMap.fromList
+    [ ("host", toGVal $ host config)
+    , ("repositories", toGVal repos)
+    ]
+
+{-
+The index template can access variables in the index scope. The primary variable here is
+the list of repositories, which can be looped over and each repo entry has some
+properties that can be accessed. These are defined here.
+-}
+data Repo = Repo
+    { repoName :: FilePath
+    , repoDescription :: Text
+    }
+
+instance ToGVal m Repo where
+    toGVal :: Repo -> GVal m
+    toGVal repo = def
+        { asHtml = html . pack . show . repoName $ repo
+        , asText = pack . show . repoName $ repo
+        , asLookup = Just . repoAsLookup $ repo
+        }
+
+repoAsLookup :: Repo -> Text -> Maybe (GVal m)
+repoAsLookup repo = \case
+    "name" -> Just . toGVal . repoName $ repo
+    "description" -> Just . toGVal . repoDescription $ repo
+    _ -> Nothing

src/Main.hs Modified

@@ -13,4 +13,4 @@
     config <- getConfig "./config.dhall"
     templates <- loadTemplates config
     index <- loadIndexTemplate config
-    run config templates
+    run config templates index

config.dhall Modified

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