From 056c8d278bb32cc939afecbbca99c39487d7229e Mon Sep 17 00:00:00 2001 From: "Niels G. W. Serup" Date: Wed, 25 Sep 2024 23:06:53 +0200 Subject: [PATCH] Introduce unzipping and use it --- byg/src/DependencyGenerator.hs | 28 ++++++++++++++++++++++++++++ byg/src/SiteGenerator.hs | 1 + byg/src/Types/Dependency.hs | 2 ++ byg/src/Types/Function.hs | 1 + 4 files changed, 32 insertions(+) diff --git a/byg/src/DependencyGenerator.hs b/byg/src/DependencyGenerator.hs index 1bbd808..be1c392 100644 --- a/byg/src/DependencyGenerator.hs +++ b/byg/src/DependencyGenerator.hs @@ -9,8 +9,12 @@ module DependencyGenerator , mapDepGenM , mapDepGenM_ , filterDepGenM + , unzipFstDepGenM + , unzipSndDepGenM + , unzipDepGenM , joinPaths + , fileComponents , isImageFilename , convertedImageFilename , applyTemplate @@ -98,6 +102,27 @@ filterDepGenM f input = do conds <- mapDepGenM f input genDependency (makeDependency (TupleToken (input, conds)) FilterComp) +unzipFstDepGenM :: TokenableTo [(a, b)] t => t -> DepGenM (Token [a]) +unzipFstDepGenM t = do + t' <- toToken t + case t' of + ZipToken (a, _) -> pure a + Token _ -> genDependency (makeDependency t' UnzipFst) + +unzipSndDepGenM :: TokenableTo [(a, b)] t => t -> DepGenM (Token [b]) +unzipSndDepGenM t = do + t' <- toToken t + case t' of + ZipToken (_, b) -> pure b + Token _ -> genDependency (makeDependency t' UnzipSnd) + +unzipDepGenM :: TokenableTo [(a, b)] t => t -> DepGenM (Token [a], Token [b]) +unzipDepGenM t = do + t' <- toToken t + a <- unzipFstDepGenM t' + b <- unzipSndDepGenM t' + pure (a, b) + class TokenableTo t s | s -> t where toToken :: s -> DepGenM' t @@ -113,6 +138,9 @@ joinPaths (a, b) = do b' <- toToken b runFunction JoinPaths $ TupleToken (a', b') +fileComponents :: TokenableTo FilePath a => a -> DepGenM' (String, String) +fileComponents a = runFunction FileComponents =<< toToken a + isImageFilename :: TokenableTo FilePath a => a -> DepGenM' Bool isImageFilename a = runFunction IsImageFilename =<< toToken a diff --git a/byg/src/SiteGenerator.hs b/byg/src/SiteGenerator.hs index 4d0668e..23394de 100644 --- a/byg/src/SiteGenerator.hs +++ b/byg/src/SiteGenerator.hs @@ -6,6 +6,7 @@ import DependencyGenerator handleRecipeDir :: Token FilePath -> Token Template -> Token FilePath -> DepGenM () handleRecipeDir outputDir template dir = do dirContents <- listDirectory dir + (bases, exts) <- unzipDepGenM (mapDepGenM fileComponents dirContents) imageFilenames <- filterDepGenM isImageFilename dirContents convertedImageFilenames <- mapDepGenM convertedImageFilename imageFilenames mapDepGenM_ diff --git a/byg/src/Types/Dependency.hs b/byg/src/Types/Dependency.hs index 7a43dda..2725acc 100644 --- a/byg/src/Types/Dependency.hs +++ b/byg/src/Types/Dependency.hs @@ -23,6 +23,8 @@ data Action = Function Function | FilterComp | GetListElem | SetListElem + | UnzipFst + | UnzipSnd | MapComp [Dependency] deriving (Show, Lift) diff --git a/byg/src/Types/Function.hs b/byg/src/Types/Function.hs index b7f3de4..f7ff953 100644 --- a/byg/src/Types/Function.hs +++ b/byg/src/Types/Function.hs @@ -5,6 +5,7 @@ module Types.Function import Language.Haskell.TH.Syntax (Lift) data Function = JoinPaths + | FileComponents | IsImageFilename | ConvertedImageFilename | ApplyTemplate