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

Commit d6bf4e85560006fe1ff289db1d6923c194bfbcd9
Parent: 9231c16355a66cdd5a9dfad94c9f7d61ecaaef35
Author: mcol <mcol@posteo.net>
Date: 2021-09-30 23:46:09 +0100
Committer: mcol <mcol@posteo.net>
Committed: 2021-09-30 23:46:09 +0100

Clearly define template targets with Target type

templates/tree.html Modified

@@ -14,7 +14,7 @@
     <p>{{- description }}</p>
     <p>git clone {{ host }}/{{- name }}</p>
     {% for file in tree %}
-		<p><a href="{{ host }}/{{ name }}/files/{{ file.href }}">{{- file.path }}</a></p>
+		<p><a href="{{ host }}/{{ name }}/file/{{ file.href }}">{{- file.path }}</a></p>
     {% endfor %}
   </body>
 </html>

templates/log.html Modified

@@ -14,7 +14,7 @@
     <p>{{- description }}</p>
     <p>git clone {{ host }}/{{- name }}</p>
     {% for commit in commits[:50] %}
-    <p><b><a href="{{ host }}/{{ name }}/commits/{{ commit.id }}.html">{{ commit.id[:7] }}</a></b> {{ commit.title }}</p>
+    <p><b><a href="{{ host }}/{{ name }}/commit/{{ commit.id }}.html">{{ commit.id[:7] }}</a></b> {{ commit.title }}</p>
     {% endfor %}
   </body>
 </html>

src/Repositories.hs Modified

@@ -3,6 +3,7 @@
 {-# Language FlexibleInstances #-}  -- Needed for `instance ToGVal`
 {-# Language MultiParamTypeClasses #-}  -- Needed for `instance ToGVal`
 {-# Language InstanceSigs #-}  -- Needed for toGVal type signature
+{-# Language FlexibleContexts #-}  -- Needed for the Target class type constraints
 
 module Repositories (
     run
@@ -77,8 +78,8 @@
             -- Run the generator --
             let scope = package env name description commits tree
             liftIO . mapM (generate output scope) $ envTemplates env
-            liftIO . mapM (generateCommit output scope $ envCommitTemplate env) $ commits
-            liftIO . mapM (generateFile output scope $ envFileTemplate env) $ tree
+            liftIO . mapM (genTarget output scope $ envCommitTemplate env) $ commits
+            liftIO . mapM (genTarget output scope $ envFileTemplate env) $ tree
             return ()
 
 {-
@@ -224,34 +225,29 @@
 
 ----------------------------------------------------------------------------------------
 
-generateCommit
-    :: FilePath
-    -> HashMap.HashMap Text (GVal (Run SourcePos IO Html))
-    -> Maybe Template
-    -> Commit LgRepo
-    -> IO ()
-generateCommit _ _ Nothing _ = return ()
-generateCommit output scope (Just template) commit = do
-    let hash = unpack . renderObjOid . commitOid $ commit
-    let template' = template { templatePath = hash ++ ".html" }
-    let scope' = scope <> HashMap.fromList [("commit", toGVal commit)]
-    liftIO $ createDirectoryIfMissing True (output </> "commits")
-    generate (output </> "commits") scope' template'
-    return ()
+class ToGVal (Run SourcePos IO Html) a => Target a where
+    identify :: a -> String
+    category :: a -> FilePath
+
+instance Target (Commit LgRepo) where
+    identify = unpack . renderObjOid . commitOid 
+    category = const "commit"
 
--- TODO: DRY
+instance Target (TreeFile) where
+    identify = unpack . replace "/" "." . decodeUtf8 . treeFilePath
+    category = const "file"
 
-generateFile
-    :: FilePath
+genTarget
+    :: Target a
+    => FilePath
     -> HashMap.HashMap Text (GVal (Run SourcePos IO Html))
     -> Maybe Template
-    -> TreeFile
+    -> a
     -> IO ()
-generateFile _ _ Nothing _ = return ()
-generateFile output scope (Just template) file = do
-    let path = unpack . replace "/" "." . decodeUtf8 . treeFilePath $ file
-    let template' = template { templatePath = path ++ ".html" }
-    let scope' = scope <> HashMap.fromList [("file", toGVal file)]
-    liftIO $ createDirectoryIfMissing True (output </> "files")
-    generate (output </> "files") scope' template'
+genTarget _ _ Nothing _ = return ()
+genTarget output scope (Just template) target = do
+    let template' = template { templatePath = identify target ++ ".html" }
+    let scope' = scope <> HashMap.fromList [(pack . category $ target, toGVal target)]
+    liftIO $ createDirectoryIfMissing True (output </> category target)
+    generate (output </> category target) scope' template'
     return ()