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

Commit 59afb09d3cd53085b3d9bed728d4bbca26b6d4b8
Parent: d9a08763081380cd9397e270eebe9186bc435c89
Author: mcol <mcol@posteo.net>
Date: 2021-09-23 21:48:32 +0100
Committer: mcol <mcol@posteo.net>
Committed: 2021-09-23 21:48:32 +0100

Add first (compiling) use of Ginger

src/Templates.hs Modified

@@ -1,20 +1,25 @@
 module Templates (
     Template,
     templateGinger,
-    templatePath,
     loadTemplates,
+    generate,
 ) where
 
 import Control.Monad (filterM, (<=<))
 import Data.Char (toLower)
-import Data.Either (rights)
+import Data.Either (rights, Either)
 import Data.List (isSuffixOf)
 import Data.Maybe (catMaybes)
+import Data.Text (unpack, Text)
+import qualified Data.HashMap.Strict as HashMap
 import System.Directory (doesFileExist, getDirectoryContents)
 import System.FilePath ((</>), FilePath)
 import System.IO.Error (tryIOError)
 import qualified Text.Ginger.AST as G
 import qualified Text.Ginger.Parse as G
+import Text.Ginger.GVal (toGVal, GVal, ToGVal)
+import Text.Ginger.Html (htmlSource, Html)
+import Text.Ginger.Run (easyRenderM, Run, RuntimeError)
 
 import Config (Config, templateDirectory)
 
@@ -71,3 +76,17 @@
 -}
 getFiles :: Config -> IO [FilePath]
 getFiles = filterM isTemplate <=< listTemplates . templateDirectory
+
+{-
+This function gets the output HTML data and is responsible for saving it to file.
+-}
+writeTo :: Html -> IO ()
+writeTo = putStr . unpack . htmlSource
+
+{-
+This is the generator function that receives repository-specific variables and uses
+Ginger to render templates using them.
+-}
+generate :: HashMap.HashMap Text Text -> Template ->
+    IO (Either (RuntimeError G.SourcePos) (GVal (Run G.SourcePos IO Html)))
+generate context template = easyRenderM writeTo context (templateGinger template)

src/Repositories.hs Modified

@@ -10,18 +10,17 @@
 import Control.Monad.Trans.Reader (ReaderT)
 import Data.Foldable (foldMap)
 import Data.Tagged
-import Data.Text (unpack, Text)
+import Data.Text (Text)
 import Git
 import Git.Types (RefTarget)
 import Git.Libgit2 (lgFactory, LgRepo)
+import qualified Data.HashMap.Strict as HashMap
 import System.Directory (createDirectoryIfMissing)
 import System.FilePath ((</>), takeFileName)
 import System.IO.Error (tryIOError)
-import Text.Ginger (runGingerT, makeContextHtmlM, toGVal, GVal)
-import Text.Ginger.Html (htmlSource)
 
 import Config (Config, repoPaths, outputDirectory)
-import Templates (Template, templateGinger, templatePath)
+import Templates (Template, generate)
 
 {-
 This is the entrypoint that receives the ``Config`` and uses it to map over our
@@ -63,13 +62,14 @@
             -- tree: A list of `(TreeFilePath, TreeEntry r)` objects at HEAD.
             tree <- getTree head
 
+            -- Run the generator --
+            let repo = package description commits tree
+            liftIO . sequence . map (generate repo) $ templates
             return ()
   where
     name = takeFileName path
     outPath = outputDirectory </> name
 
-    --mconcat $ runGingerT (makeContextHtmlM (scopeLookup context) (putStr . unpack . htmlSource)) tpl
-
 getCommits :: CommitOid LgRepo -> ReaderT LgRepo IO [Commit LgRepo]
 getCommits commitID = sequence . fmap loadCommit <=<
     runConduit $ sourceObjects Nothing commitID False .| sinkList
@@ -79,8 +79,14 @@
 
 getTree :: CommitOid LgRepo -> ReaderT LgRepo IO [(TreeFilePath, TreeEntry LgRepo)]
 getTree commitID = do
-    headc <- lookupCommit commitID
-    lookupTree (commitTree headc) >>= listTreeEntries
+    head <- lookupCommit commitID
+    lookupTree (commitTree head) >>= listTreeEntries
 
 getDescription :: FilePath -> IO String
 getDescription path = either (const "") id <$> tryIOError (readFile path)
+
+package description commits tree = HashMap.fromList
+    [ ("commits", "commits")
+    , ("description", "description")
+    , ("tree", "tree")
+    ]

gitserve.cabal Modified

@@ -29,3 +29,4 @@
                      , tagged
                      , text
                      , transformers
+                     , unordered-containers