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

Commit 15fe645cbfacce1f81678e412625472f01b2ca62
Parent: 32db3a70cb523e9cfcbf003c68891cdc3cd5f042
Author: mcol <mcol@posteo.net>
Date: 2021-10-07 11:09:01 +0100
Committer: mcol <mcol@posteo.net>
Committed: 2021-10-07 11:09:01 +0100

Add license and readme template variables

templates/index.html Modified

@@ -17,5 +17,14 @@
 		<p><a href="./tree.html">Tree</a></p>
 		<p><a href="./tags.html">Tags</a></p>
 		<p><a href="./branches.html">Branches</a></p>
+		{% if license %}
+		<p><a href="{{ host }}/{{ name }}/file/{{ license.href }}">License</a></p>
+		{% endif %}
+		{% if readme %}
+		<p><a href="{{ host }}/{{ name }}/file/{{ readme.href }}">Readme</a></p>
+		<pre>
+		{{ readme.contents }}
+		</pre>
+		{% endif %}
   </body>
 </html>

src/Types.hs Modified

@@ -15,7 +15,7 @@
 import Data.Text.Encoding.Error (lenientDecode)
 import Git
 import Git.Libgit2 (LgRepo)
-import Text.Ginger.GVal (GVal, toGVal, ToGVal, asText, asHtml, asLookup, asList)
+import Text.Ginger.GVal (GVal, toGVal, ToGVal, asText, asHtml, asLookup, asList, asBoolean)
 import Text.Ginger.Html (html)
 import qualified Data.Text as T
 
@@ -98,6 +98,7 @@
         { asHtml = html . pack . toString . treeFilePath $ treefile
         , asText = pack . show . treeFilePath $ treefile
         , asLookup = Just . treeAsLookup $ treefile
+        , asBoolean = True  -- Used for conditionally checking readme/license template variables.
         }
 
 instance ToGVal m TreeFileContents where

src/Repositories.hs Modified

@@ -14,7 +14,7 @@
 import Control.Monad.Trans.Reader (ReaderT)
 import Data.Either (fromRight)
 import Data.Tagged
-import Data.Text (pack, unpack, Text, isPrefixOf, stripPrefix)
+import Data.Text (pack, unpack, Text, isPrefixOf, stripPrefix, toLower)
 import Data.Text.Encoding (decodeUtf8With)
 import Data.Text.Encoding.Error (lenientDecode)
 import Data.Maybe (mapMaybe, catMaybes, fromJust)
@@ -118,6 +118,8 @@
     , ("tree", toGVal tree)
     , ("tags", toGVal tags)
     , ("branches", toGVal branches)
+    , ("readme", toGVal . findFile "readme" $ tree)
+    , ("license", toGVal . findFile "license" $ tree)
     ]
 
 {-
@@ -153,6 +155,16 @@
 getEntryModes (CommitEntry _) = return ModeSubmodule
 
 {-
+Find a file in the tree starting with the specified prefix. The prefix is looked for on
+the full path, so will only find files in the top level directory.
+-}
+findFile :: Text -> [TreeFile] -> Maybe TreeFile
+findFile _ [] = Nothing
+findFile prefix (f:fs) = if (isReadme f) then (Just f) else (findFile prefix fs)
+  where
+    isReadme = isPrefixOf prefix . toLower . decodeUtf8With lenientDecode . treeFilePath
+
+{-
 Collect information about references. TODO: Find a more canonical way to split
 references into tags or branches rather than filtering the refnames.
 -}

README.rst Modified

@@ -35,8 +35,8 @@
 
 [x] Generate html file per commit with commit scope
 [x] Generate html file per tree file with file scope
-[ ] Expose readme url in repo scope for convenient hyperlinking
-[ ] Expose license url in repo scope for convenient hyperlinking
+[x] Expose readme url in repo scope for convenient hyperlinking
+[x] Expose license url in repo scope for convenient hyperlinking
 [x] Expose repo branches and tags in repo scope
 [ ] Commit diffs
 [x] Tree entry filemodes