From 47420cbe4169ebc2d5cd141e4da43fa3b2fd84ff Mon Sep 17 00:00:00 2001 From: "Niels G. W. Serup" Date: Sat, 5 Oct 2024 18:07:26 +0200 Subject: [PATCH] Split template loading into multiple steps --- byg/src/DependencyGenerator.hs | 16 ++++++++++------ byg/src/Evaluation/Function.hs | 5 +++++ byg/src/Evaluation/FunctionIO.hs | 8 ++------ byg/src/SiteGenerator.hs | 2 +- byg/src/Types/Function.hs | 1 + byg/src/Types/FunctionIO.hs | 2 +- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/byg/src/DependencyGenerator.hs b/byg/src/DependencyGenerator.hs index 4adc8db..82f860c 100644 --- a/byg/src/DependencyGenerator.hs +++ b/byg/src/DependencyGenerator.hs @@ -27,11 +27,12 @@ module DependencyGenerator , fileComponents , lowerString , elemOf + , makeTemplate , applyTemplate , toText , listDirectory , isDirectory - , readTemplate + , readTextFile , convertImage , saveFile , copyFile @@ -213,6 +214,12 @@ elemOf a b = do b' <- toToken b runFunction ElemOf $ TupleToken (a', b') +makeTemplate :: (TokenableTo Text a, TokenableTo Text b) => a -> b -> DepGenM' Template +makeTemplate a b = do + a' <- toToken a + b' <- toToken b + runFunction MakeTemplate $ TupleToken (a', b') + applyTemplate :: (TokenableTo Template a, TokenableTo Text b) => a -> b -> DepGenM' Text applyTemplate a b = do a' <- toToken a @@ -228,11 +235,8 @@ listDirectory a = runFunctionIO ListDirectory =<< toToken a isDirectory :: TokenableTo FilePath a => a -> DepGenM' Bool isDirectory a = runFunctionIO IsDirectory =<< toToken a -readTemplate :: (TokenableTo FilePath a, TokenableTo Text b) => a -> b -> DepGenM' Template -readTemplate a b = do - a' <- toToken a - b' <- toToken b - runFunctionIO ReadTemplate $ TupleToken (a', b') +readTextFile :: TokenableTo FilePath a => a -> DepGenM' Text +readTextFile a = runFunctionIO ReadTextFile =<< toToken a convertImage :: (TokenableTo (FilePath, FilePath) a, TokenableTo ImageConversionSettings b) => a -> b -> DepGenM () convertImage a b = do diff --git a/byg/src/Evaluation/Function.hs b/byg/src/Evaluation/Function.hs index aa96038..cc9d0b7 100644 --- a/byg/src/Evaluation/Function.hs +++ b/byg/src/Evaluation/Function.hs @@ -52,6 +52,11 @@ evalFunction f x = case (f, x) of (ElemOf, Tuple (y, List ys)) -> Bool (y `elem` ys) + (MakeTemplate, Tuple (Text t, Text c)) -> + let (beforeContent, after) = T.breakOn c t + afterContent = T.drop (T.length c) after + in Template $ TemplateParts beforeContent afterContent + (ApplyTemplate, Tuple (Template (TemplateParts beforeContent afterContent), Text t)) -> Text $ T.concat [beforeContent, t, afterContent] diff --git a/byg/src/Evaluation/FunctionIO.hs b/byg/src/Evaluation/FunctionIO.hs index b56a9ce..4a87c49 100644 --- a/byg/src/Evaluation/FunctionIO.hs +++ b/byg/src/Evaluation/FunctionIO.hs @@ -7,7 +7,6 @@ import Prelude hiding (String, FilePath) import Types.Values import Types (FunctionIO(..), Value(..), toValue) -import qualified Data.Text as T import qualified Data.Text.Lazy as TL import qualified Data.Text.IO as T import qualified Text.Pandoc as P @@ -24,11 +23,8 @@ evalFunctionIO f x = case (f, x) of (IsDirectory, String (StringWrapper s)) -> Bool <$> doesDirectoryExist s - (ReadTemplate, Tuple (String (StringWrapper s), Text c)) -> do - t <- T.readFile s - let (beforeContent, after) = T.breakOn c t - afterContent = T.drop (T.length c) after - pure $ Template $ TemplateParts beforeContent afterContent + (ReadTextFile, String (StringWrapper s)) -> + Text <$> T.readFile s (ConvertImage, Tuple (Tuple (String (StringWrapper source), String (StringWrapper target)), ImageConversionSettings (ResizeToWidth widthResized))) -> do diff --git a/byg/src/SiteGenerator.hs b/byg/src/SiteGenerator.hs index 258b828..02ca838 100644 --- a/byg/src/SiteGenerator.hs +++ b/byg/src/SiteGenerator.hs @@ -62,7 +62,7 @@ generateSite = do recipesDir <- inject "retter" outputRecipesDir <- joinPaths outputDir recipesDir makeDir outputRecipesDir - template <- readTemplate (inject "template.html") (inject "CONTENT") + template <- makeTemplate (readTextFile (inject "template.html")) (inject "CONTENT") indexName <- inject "index.html" dirNames <- listDirectory recipesDir dirPaths <- mapDepGenM (joinPaths recipesDir) dirNames diff --git a/byg/src/Types/Function.hs b/byg/src/Types/Function.hs index f9ff42c..e863293 100644 --- a/byg/src/Types/Function.hs +++ b/byg/src/Types/Function.hs @@ -12,6 +12,7 @@ data Function = AppendStrings | FileComponents | LowerString | ElemOf + | MakeTemplate | ApplyTemplate | ToText deriving (Show, Lift) diff --git a/byg/src/Types/FunctionIO.hs b/byg/src/Types/FunctionIO.hs index 87e6dbb..4a1f25b 100644 --- a/byg/src/Types/FunctionIO.hs +++ b/byg/src/Types/FunctionIO.hs @@ -6,7 +6,7 @@ import Language.Haskell.TH.Syntax (Lift) data FunctionIO = ListDirectory | IsDirectory - | ReadTemplate + | ReadTextFile | ConvertImage | SaveFile | CopyFile