Split template loading into multiple steps

This commit is contained in:
Niels G. W. Serup 2024-10-05 18:07:26 +02:00
parent 1988beb49a
commit 47420cbe41
No known key found for this signature in database
GPG Key ID: 38EEEBCE67324F19
6 changed files with 20 additions and 14 deletions

View File

@ -27,11 +27,12 @@ module DependencyGenerator
, fileComponents , fileComponents
, lowerString , lowerString
, elemOf , elemOf
, makeTemplate
, applyTemplate , applyTemplate
, toText , toText
, listDirectory , listDirectory
, isDirectory , isDirectory
, readTemplate , readTextFile
, convertImage , convertImage
, saveFile , saveFile
, copyFile , copyFile
@ -213,6 +214,12 @@ elemOf a b = do
b' <- toToken b b' <- toToken b
runFunction ElemOf $ TupleToken (a', 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 :: (TokenableTo Template a, TokenableTo Text b) => a -> b -> DepGenM' Text
applyTemplate a b = do applyTemplate a b = do
a' <- toToken a a' <- toToken a
@ -228,11 +235,8 @@ listDirectory a = runFunctionIO ListDirectory =<< toToken a
isDirectory :: TokenableTo FilePath a => a -> DepGenM' Bool isDirectory :: TokenableTo FilePath a => a -> DepGenM' Bool
isDirectory a = runFunctionIO IsDirectory =<< toToken a isDirectory a = runFunctionIO IsDirectory =<< toToken a
readTemplate :: (TokenableTo FilePath a, TokenableTo Text b) => a -> b -> DepGenM' Template readTextFile :: TokenableTo FilePath a => a -> DepGenM' Text
readTemplate a b = do readTextFile a = runFunctionIO ReadTextFile =<< toToken a
a' <- toToken a
b' <- toToken b
runFunctionIO ReadTemplate $ TupleToken (a', b')
convertImage :: (TokenableTo (FilePath, FilePath) a, TokenableTo ImageConversionSettings b) => a -> b -> DepGenM () convertImage :: (TokenableTo (FilePath, FilePath) a, TokenableTo ImageConversionSettings b) => a -> b -> DepGenM ()
convertImage a b = do convertImage a b = do

View File

@ -52,6 +52,11 @@ evalFunction f x = case (f, x) of
(ElemOf, Tuple (y, List ys)) -> (ElemOf, Tuple (y, List ys)) ->
Bool (y `elem` 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)) -> (ApplyTemplate, Tuple (Template (TemplateParts beforeContent afterContent), Text t)) ->
Text $ T.concat [beforeContent, t, afterContent] Text $ T.concat [beforeContent, t, afterContent]

View File

@ -7,7 +7,6 @@ import Prelude hiding (String, FilePath)
import Types.Values import Types.Values
import Types (FunctionIO(..), Value(..), toValue) import Types (FunctionIO(..), Value(..), toValue)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
import qualified Data.Text.IO as T import qualified Data.Text.IO as T
import qualified Text.Pandoc as P import qualified Text.Pandoc as P
@ -24,11 +23,8 @@ evalFunctionIO f x = case (f, x) of
(IsDirectory, String (StringWrapper s)) -> (IsDirectory, String (StringWrapper s)) ->
Bool <$> doesDirectoryExist s Bool <$> doesDirectoryExist s
(ReadTemplate, Tuple (String (StringWrapper s), Text c)) -> do (ReadTextFile, String (StringWrapper s)) ->
t <- T.readFile s Text <$> T.readFile s
let (beforeContent, after) = T.breakOn c t
afterContent = T.drop (T.length c) after
pure $ Template $ TemplateParts beforeContent afterContent
(ConvertImage, Tuple (Tuple (String (StringWrapper source), String (StringWrapper target)), (ConvertImage, Tuple (Tuple (String (StringWrapper source), String (StringWrapper target)),
ImageConversionSettings (ResizeToWidth widthResized))) -> do ImageConversionSettings (ResizeToWidth widthResized))) -> do

View File

@ -62,7 +62,7 @@ generateSite = do
recipesDir <- inject "retter" recipesDir <- inject "retter"
outputRecipesDir <- joinPaths outputDir recipesDir outputRecipesDir <- joinPaths outputDir recipesDir
makeDir outputRecipesDir makeDir outputRecipesDir
template <- readTemplate (inject "template.html") (inject "CONTENT") template <- makeTemplate (readTextFile (inject "template.html")) (inject "CONTENT")
indexName <- inject "index.html" indexName <- inject "index.html"
dirNames <- listDirectory recipesDir dirNames <- listDirectory recipesDir
dirPaths <- mapDepGenM (joinPaths recipesDir) dirNames dirPaths <- mapDepGenM (joinPaths recipesDir) dirNames

View File

@ -12,6 +12,7 @@ data Function = AppendStrings
| FileComponents | FileComponents
| LowerString | LowerString
| ElemOf | ElemOf
| MakeTemplate
| ApplyTemplate | ApplyTemplate
| ToText | ToText
deriving (Show, Lift) deriving (Show, Lift)

View File

@ -6,7 +6,7 @@ import Language.Haskell.TH.Syntax (Lift)
data FunctionIO = ListDirectory data FunctionIO = ListDirectory
| IsDirectory | IsDirectory
| ReadTemplate | ReadTextFile
| ConvertImage | ConvertImage
| SaveFile | SaveFile
| CopyFile | CopyFile