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

Commit cfdb7417a77b558368a7b8d2ea9ec6004e35ffad
Parent: b21f7c1c42e1086cadaf89aae83f52443c297505
Author: mcol <mcol@posteo.net>
Date: 2021-09-25 00:43:20 +0100
Committer: mcol <mcol@posteo.net>
Committed: 2021-09-25 00:43:20 +0100

Output to file

src/Templates.hs Modified

@@ -15,7 +15,7 @@
 import Data.Text (unpack, Text)
 import qualified Data.HashMap.Strict as HashMap
 import System.Directory (doesFileExist, getDirectoryContents)
-import System.FilePath ((</>))
+import System.FilePath ((</>), takeFileName)
 import System.IO.Error (tryIOError)
 import qualified Text.Ginger.AST as G
 import Text.Ginger.Parse (SourcePos, parseGingerFile, peErrorMessage)
@@ -91,15 +91,18 @@
 {-
 This function gets the output HTML data and is responsible for saving it to file.
 -}
-writeTo :: Html -> IO ()
-writeTo = putStr . unpack . htmlSource
+writeTo :: FilePath -> Html -> IO ()
+writeTo path html = appendFile path . unpack . htmlSource $ html
 
 {-
 This is the generator function that receives repository-specific variables and uses
 Ginger to render templates using them.
 -}
 generate
-    :: HashMap.HashMap Text (GVal (Run SourcePos IO Html))
+    :: FilePath
+    -> HashMap.HashMap Text (GVal (Run SourcePos IO Html))
     -> Template
     -> IO (Either (RuntimeError SourcePos) (GVal (Run SourcePos IO Html)))
-generate context template = easyRenderM writeTo context (templateGinger template)
+generate output context template = easyRenderM writeToPath context (templateGinger template)
+  where
+    writeToPath = writeTo $ output </> (takeFileName . templatePath $ template)

src/Repositories.hs Modified

@@ -54,7 +54,8 @@
 processRepo' :: [Template] -> Config -> FilePath -> ReaderT LgRepo IO ()
 processRepo' templates config path = do
     let name = takeFileName path
-    liftIO $ createDirectoryIfMissing True $ outputDirectory config </> name
+    let output = outputDirectory config </> name
+    liftIO $ createDirectoryIfMissing True output
     resolveReference "HEAD" >>= \case
         Nothing -> liftIO . print $ "gitserve: " <> name <> ": Failed to resolve HEAD."
         Just commitID -> do
@@ -73,7 +74,7 @@
 
             -- Run the generator --
             let repo = package config name description commits tree
-            liftIO . mapM (generate repo) $ templates
+            liftIO . mapM (generate output repo) $ templates
             return ()
 
 {-