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

Commit 577b81cc825d587456c0f6cdc73a96bf6c40c8f0
Parent: 67f2596e606f0059150c81cc2a83da015710ad4c
Author: mcol <mcol@posteo.net>
Date: 2021-09-24 12:29:06 +0100
Committer: mcol <mcol@posteo.net>
Committed: 2021-09-24 12:29:06 +0100

Add way to lookup properties on commits in templates

And add a `message` property.

Also, let's accept the orphan instance of toGVal on Commit r.

templates/log.html Modified

@@ -14,7 +14,7 @@
     <p>{{- description }}</p>
     <p>git clone {{ host }}/{{- name }}</p>
     {% for commit in commits %}
-    <p>{{- commit }}</p>
+    <p>{{- commit.message }}</p>
     {% endfor %}
   </body>
 </html>

stack.yaml Modified

@@ -13,4 +13,4 @@
   "$locals": -Wall -Wcompat -Widentities -Wincomplete-uni-patterns
     -Wincomplete-record-updates -Wredundant-constraints -Wmissing-export-lists
     -Wpartial-fields -Wmissing-deriving-strategies -Wunused-packages
-    -fno-warn-unused-do-bind
+    -fno-warn-unused-do-bind -fno-warn-orphans

src/Repositories.hs Modified

@@ -2,6 +2,7 @@
 {-# Language OverloadedStrings #-}  -- Needed for resolveReference
 {-# Language FlexibleInstances #-}  -- Needed for `instance ToGVal`
 {-# Language MultiParamTypeClasses #-}  -- Needed for `instance ToGVal`
+{-# Language InstanceSigs #-}  -- Needed for toGVal type signature
 
 module Repositories (
     run
@@ -14,14 +15,14 @@
 import Data.Default (def)
 import Data.Either (fromRight)
 import Data.Tagged
-import Data.Text (pack, Text)
+import Data.Text (pack, Text, strip)
 import Data.Maybe (mapMaybe)
 import Git
 import Git.Libgit2 (lgFactory, LgRepo)
 import System.Directory (createDirectoryIfMissing)
 import System.FilePath ((</>), takeFileName)
 import System.IO.Error (tryIOError)
-import Text.Ginger.GVal (GVal, toGVal, ToGVal, asText, asHtml)
+import Text.Ginger.GVal (GVal, toGVal, ToGVal, asText, asHtml, asLookup)
 import Text.Ginger.Html (Html, html)
 import Text.Ginger.Run (Run)
 import Text.Ginger.Parse (SourcePos)
@@ -113,8 +114,20 @@
 getDescription :: FilePath -> IO Text
 getDescription path = fromRight "" <$> tryIOError (pack <$> readFile path)
 
+{-
+Here we define how commits can be accessed and represented in Ginger templates.
+
+This is an orphan instance but we can let it slide.
+-}
 instance ToGVal m (Commit LgRepo) where
-    toGVal x = def
-        { asHtml = html . pack . show . commitLog $ x
-        , asText = pack . show . commitLog $ x
+    toGVal :: Commit LgRepo -> GVal m
+    toGVal commit = def
+        { asHtml = html . pack . show . commitLog $ commit
+        , asText = pack . show . commitLog $ commit
+        , asLookup = Just . commitAsLookup $ commit
         }
+
+commitAsLookup :: Commit LgRepo -> Text -> Maybe (GVal m)
+commitAsLookup commit = \case
+    "message" -> Just . toGVal . strip . commitLog $ commit
+    _ -> Nothing