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

Commit 37ae3b428095f9eb2c235bc32a6fbcc137084118
Parent: 4d4f766cb5cf56bd5210e5e8f57e84badb443b6e
Author: mcol <mcol@posteo.net>
Date: 2022-05-29 01:41:59 +0100
Committer: mcol <mcol@posteo.net>
Committed: 2022-05-29 01:41:59 +0100

make generate function just do rendering

src/Templates.hs Modified

@@ -7,11 +7,10 @@
     generate,
 ) where
 
-import Control.Monad.IO.Class (liftIO)
 import qualified Data.HashMap.Strict as HashMap
 import Data.IORef (modifyIORef', newIORef, readIORef)
 import qualified Data.Text as T
-import Data.Text.Internal.Lazy (Text)
+import Data.Text.Lazy (Text)
 import qualified Data.Text.Lazy.Builder as TB
 import Path (Abs, File, Path, Rel, filename, toFilePath)
 import System.IO.Error (tryIOError)
@@ -51,19 +50,18 @@
     includeResolver p = either (const Nothing) Just <$> tryIOError (readFile p)
 
 {-
-This is the generator function that receives repository-specific variables and uses
-Ginger to render templates using them.
+This is the generator function that receives variables and uses Ginger to render
+templates into Text.
 -}
 generate ::
     Template ->
     HashMap.HashMap T.Text (GVal RunRepo) ->
     IO Text
-generate template context = do
-    content <- liftIO . newIORef . TB.fromText $ ""
+generate template scope = do
+    ioref <- newIORef . TB.fromText $ ""
 
     let emit :: Html -> IO ()
-        emit = liftIO . modifyIORef' content . flip mappend . TB.fromText . htmlSource
+        emit = modifyIORef' ioref . flip mappend . TB.fromText . htmlSource
 
-    liftIO $ runGingerT (easyContext emit context) . templateGinger $ template
-    result <- liftIO . readIORef $ content
-    return . TB.toLazyText $ result
+    runGingerT (easyContext emit scope) (templateGinger template)
+    TB.toLazyText <$> readIORef ioref

src/Repositories.hs Modified

@@ -37,8 +37,7 @@
 import qualified Git
 import Git.Libgit2 (LgRepo, lgDiffTreeToTree, lgFactory)
 import Path (Abs, Dir, Path, Rel, dirname, parseRelDir, parseRelFile, toFilePath, (</>))
-import Path.IO (doesFileExist, ensureDir, ignoringAbsence)
-import System.Directory (removeFile)
+import Path.IO (doesFileExist, ensureDir)
 import qualified System.Directory as D
 import qualified System.FilePath as FP
 import Text.Ginger.GVal (GVal, ToGVal, toGVal)
@@ -370,10 +369,9 @@
     HashMap.HashMap Text (GVal RunRepo) ->
     Template ->
     IO ()
-genRepo output scope template = do
+genRepo output scope template =
     let output' = toFilePath (output </> templatePath template)
-    result <- generate template scope
-    liftIO $ TL.writeFile output' result
+     in TL.writeFile output' =<< generate template scope
 
 ----------------------------------------------------------------------------------------
 -- Targets -----------------------------------------------------------------------------
@@ -407,7 +405,6 @@
     exists <- doesFileExist output'
     when (force || not exists) $ do
         let output'' = toFilePath output'
+            scope' = HashMap.insert category (toGVal target) scope
         unless quiet . putStrLn $ "Writing " <> output''
-        ignoringAbsence . removeFile $ output''
-        result <- generate template $ HashMap.insert category (toGVal target) scope
-        TL.writeFile output'' result
+        TL.writeFile output'' =<< generate template scope'

src/Index.hs Modified

@@ -4,19 +4,16 @@
     runIndex,
 ) where
 
-import Control.Monad (unless, void)
+import Control.Monad (unless)
 import qualified Data.HashMap.Strict as HashMap
-import Data.Text (Text, unpack)
+import Data.Text (Text)
+import qualified Data.Text.Lazy.IO as TL
 import Path (toFilePath)
-import Path.IO (ignoringAbsence)
-import System.Directory (removeFile)
 import System.FilePath (combine)
 import Text.Ginger.GVal (GVal, toGVal)
-import Text.Ginger.Html (htmlSource)
-import Text.Ginger.Run (easyRenderM)
 
 import Env (Env (..))
-import Templates (Template (..))
+import Templates (Template (..), generate)
 import Types
 
 {-
@@ -30,12 +27,7 @@
 runIndexFile env repos template = do
     let output = combine (toFilePath . envOutput $ env) . toFilePath . templatePath $ template
     unless (envQuiet env) . putStrLn $ "Writing " <> output
-    ignoringAbsence . removeFile $ output
-    void $
-        easyRenderM
-            (appendFile output . unpack . htmlSource)
-            (packageIndex env repos)
-            (templateGinger template)
+    TL.writeFile output =<< generate template (packageIndex env repos)
 
 {-
 This packages the variables that are available inside the index scope.