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
, 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

View File

@ -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]

View File

@ -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

View File

@ -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

View File

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

View File

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