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

Commit e3264fb4d5ba0308a64f94d5bb04cb3806b5506a
Parent: f74a6a14a03425b91253f18e69ac2305fbc0c1dd
Author: mcol <mcol@posteo.net>
Date: 2021-12-05 01:51:17 +0300
Committer: mcol <mcol@posteo.net>
Committed: 2021-12-05 01:51:17 +0300

Let for nested folders to also display contents non-recursively

templates/docs/repo/file.html Modified

@@ -15,7 +15,7 @@
   </thead>
 
   <tbody>
-    {% for child in file.contents %}
+    {% for child in file.tree %}
     <tr>
       <td>{{ child.mode_symbolic }}</td>
       <td><a href="{{ host }}/{{ name }}/file/{{ child.href }}">{{ child.path }}</a></td>

templates/base/repo/file.html Modified

@@ -15,7 +15,7 @@
   </thead>
 
   <tbody>
-    {% for child in file.contents %}
+    {% for child in file.tree %}
     <tr>
       <td>{{ child.mode_symbolic }}</td>
       <td><a href="{{ host }}/{{ name }}/file/{{ child.href }}">{{ child.path }}</a></td>

src/Types.hs Modified

@@ -16,7 +16,7 @@
 import Control.Monad.IO.Class (liftIO)
 import Control.Monad.Trans.Reader (ReaderT)
 import Data.Default (def)
-import Data.Maybe (listToMaybe)
+import Data.Maybe (fromMaybe, listToMaybe)
 import Data.Tagged (untag)
 import Data.Text (Text, pack, strip)
 import qualified Data.Text as T
@@ -27,7 +27,7 @@
 import Git
 import Git.Libgit2 (LgRepo, getOid, repoObj)
 import Path (Abs, Dir, Path, dirname, toFilePath)
-import System.FilePath (takeFileName)
+import qualified System.FilePath as FP
 import Text.Ginger.GVal (GVal, ToGVal, asBoolean, asHtml, asList, asLookup, asText, toGVal)
 import Text.Ginger.Html (Html, html)
 import Text.Ginger.Parse (SourcePos)
@@ -187,9 +187,11 @@
 treeAsLookup :: TreeFile -> Text -> Maybe (GVal RunRepo)
 treeAsLookup treefile = \case
     "path" -> Just . toGVal . treeFilePath $ treefile
-    "name" -> Just . toGVal . takeFileName . T.unpack . treeFilePath $ treefile
+    "name" -> Just . toGVal . FP.takeFileName . T.unpack . treeFilePath $ treefile
     "href" -> Just . toGVal . treePathToHref $ treefile
     "contents" -> Just . toGVal . treeFileContents $ treefile
+    "tree" -> Just . toGVal . treeFileGetTree (treeFilePath treefile) . treeFileContents $ treefile
+    "tree_recursive" -> Just . toGVal . treeFileGetTreeRecursive . treeFileContents $ treefile
     "mode" -> Just . toGVal . drop 4 . show . treeFileMode $ treefile
     "mode_octal" -> Just . toGVal . modeToOctal . treeFileMode $ treefile
     "mode_symbolic" -> Just . toGVal . modeToSymbolic . treeFileMode $ treefile
@@ -197,6 +199,16 @@
     "is_directory" -> Just . toGVal . treeFileIsDirectory $ treefile
     _ -> Nothing
 
+treeFileGetTree :: Text -> TreeFileContents -> [TreeFile]
+treeFileGetTree parent (FolderContents fs) = filter atTop fs
+  where
+    atTop = notElem FP.pathSeparator . drop 1 . T.unpack . fromMaybe "" . T.stripPrefix parent . treeFilePath
+treeFileGetTree _ _ = []
+
+treeFileGetTreeRecursive :: TreeFileContents -> [TreeFile]
+treeFileGetTreeRecursive (FolderContents fs) = fs
+treeFileGetTreeRecursive _ = []
+
 treeFileIsBinary :: TreeFile -> Bool
 treeFileIsBinary treefile = case treeFileContents treefile of
     BinaryContents -> True

DOCUMENTATION.md Modified

@@ -191,6 +191,8 @@
 |            | mode\_symbolic   | Mode in symbolic form e.g. ""-rw-r--r--" for plain files.|
 |            | is\_directory    | A boolean, useful for ginger conditionals.               |
 |            | is\_binary       | A boolean, tells you if the contents can be rendered.    |
+|            | tree             | A list of a directory's direct contents.                 |
+|            | tree\_recursive  | A list of *all* of a directory's contents.               |
 | ref        | name             | The tag or branch name.                                  |
 |            | commit           | The commit pointed to by the tag or branch.              |