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

Commit 5d883de62ade297d0dfcc4d4e7f059b02e0ccfa5
Parent: a7298b6f4d3356e2209af616a14cabf1dccf8536
Author: mcol <mcol@posteo.net>
Date: 2022-05-28 21:12:08 +0100
Committer: mcol <mcol@posteo.net>
Committed: 2022-05-28 21:12:08 +0100

move file writing out of generate

src/Templates.hs Modified

@@ -12,12 +12,10 @@
 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 qualified Data.Text.Lazy.Builder as TB
-import qualified Data.Text.Lazy.IO as T
 import Git.Libgit2 (LgRepo)
 import Path (Abs, File, Path, Rel, filename, toFilePath)
-import Path.IO (ignoringAbsence)
-import System.Directory (removeFile)
 import System.IO.Error (tryIOError)
 import qualified Text.Ginger.AST as G
 import Text.Ginger.GVal (GVal)
@@ -59,13 +57,10 @@
 Ginger to render templates using them.
 -}
 generate ::
-    Path Abs File ->
     Template ->
     HashMap.HashMap T.Text (GVal RunRepo) ->
-    ReaderT LgRepo IO ()
-generate output template context = do
-    let output' = toFilePath output
-    liftIO . ignoringAbsence . removeFile $ output'
+    ReaderT LgRepo IO Text
+generate template context = do
     content <- liftIO . newIORef . TB.fromText $ ""
 
     let emit :: Html -> ReaderT LgRepo IO ()
@@ -73,4 +68,4 @@
 
     runGingerT (easyContext emit context) . templateGinger $ template
     result <- liftIO . readIORef $ content
-    liftIO . T.writeFile output' . TB.toLazyText $ result
+    return . TB.toLazyText $ result

src/Repositories.hs Modified

@@ -3,6 +3,7 @@
 {-# LANGUAGE FlexibleInstances #-}
 {-# LANGUAGE LambdaCase #-}
 {-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE ScopedTypeVariables #-}
 
 module Repositories (
     run,
@@ -28,6 +29,7 @@
 import qualified Data.Text as T
 import qualified Data.Text.Encoding as T
 import qualified Data.Text.Encoding.Error as T
+import qualified Data.Text.Lazy.IO as TL
 import Foreign.C.String (CString)
 import Foreign.C.Types (CChar, CFloat, CInt, CSize)
 import Foreign.Ptr (Ptr)
@@ -35,7 +37,8 @@
 import qualified Git
 import Git.Libgit2 (LgRepo, lgDiffTreeToTree, lgFactory)
 import Path (Abs, Dir, Path, Rel, dirname, parseRelDir, parseRelFile, toFilePath, (</>))
-import Path.IO (doesFileExist, ensureDir)
+import Path.IO (doesFileExist, ensureDir, ignoringAbsence)
+import System.Directory (removeFile)
 import qualified System.Directory as D
 import qualified System.FilePath as FP
 import Text.Ginger.GVal (GVal, ToGVal, toGVal)
@@ -367,8 +370,10 @@
     HashMap.HashMap Text (GVal RunRepo) ->
     Template ->
     ReaderT LgRepo IO ()
-genRepo output scope template =
-    generate (output </> templatePath template) template scope
+genRepo output scope template = do
+    let output' = toFilePath (output </> templatePath template)
+    result <- generate template scope
+    liftIO $ TL.writeFile output' result
 
 ----------------------------------------------------------------------------------------
 -- Targets -----------------------------------------------------------------------------
@@ -401,5 +406,8 @@
     output' <- liftIO . fmap (output </>) . parseRelFile . href $ target
     exists <- liftIO . doesFileExist $ output'
     when (force || not exists) $ do
-        liftIO . unless quiet . putStrLn $ "Writing " <> toFilePath output'
-        generate output' template $ HashMap.insert category (toGVal target) scope
+        let output'' = toFilePath output'
+        liftIO . unless quiet . putStrLn $ "Writing " <> output''
+        liftIO . ignoringAbsence . removeFile $ output''
+        result <- generate template $ HashMap.insert category (toGVal target) scope
+        liftIO $ TL.writeFile output'' result