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

Commit cd4445b62184f9e779e1a4ab5855c53a0a67ad52
Parent: 244316a3583c67556840c5bc30fa8d3d1e178b5e
Author: mcol <mcol@posteo.net>
Date: 2021-09-18 19:53:19 +0100
Committer: mcol <mcol@posteo.net>
Committed: 2021-09-18 19:53:19 +0100

Read templates in using Ginger

src/Templates.hs Modified

@@ -1,22 +1,26 @@
 module Templates (
     Template,
+    templateGinger,
     templatePath,
-    templateContents,
     loadTemplates,
 ) where
 
 import Control.Monad (filterM, (<=<))
 import Data.Char (toLower)
+import Data.Either (rights)
 import Data.List (isSuffixOf)
+import Data.Maybe (catMaybes)
 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 Config (Config, templateDirectory)
 
 data Template = Template
     { templatePath :: FilePath
-    , templateContents :: String
+    , templateGinger :: G.Template G.SourcePos
     }
 
 {-
@@ -26,17 +30,22 @@
 loadTemplates :: Config -> IO [Template]
 loadTemplates config = do
     files <- getFiles config
-    sequence $ loadTemplate <$> files
+    parsed <- sequence $ G.parseGingerFile resolver <$> files 
+    return $ Template <$> files <*> rights parsed
 
 ----------------------------------------------------------------------------------------
 
 {-
-This reads the contents of a file and stores it within a `Template` object.
+This is a Ginger `IncludeResolver` that will eventually be extended to enable caching of
+templates.
 -}
-loadTemplate :: FilePath -> IO Template
-loadTemplate path = do
-    contents <- readFile path
-    return $ Template path contents
+resolver :: FilePath -> IO (Maybe String)
+resolver path = tryIOError (readFile path) >>= \e ->
+    case e of
+        Right contents -> return $ Just contents
+        Left err -> do
+            print err
+            return Nothing
 
 {-
 This is used to filter files in the template directory so that we only try to load

src/Repositories.hs Modified

@@ -11,7 +11,7 @@
 import Git.Libgit2 (lgFactory)
 
 import Config (Config, repoPaths)
-import Templates (Template, templatePath, templateContents)
+import Templates (Template, templateGinger, templatePath)
 
 {-
 This is the entrypoint that receives the ``Config`` and uses it to map over our