From 1e3a0e39cb6cdc86a6ba6b570c72c44931cf1c3b Mon Sep 17 00:00:00 2001
From: pacien
Date: Sun, 5 Jan 2020 20:40:41 +0100
Subject: compiler: replace file filter with inclusino and exclusion glob lists

GitHub: closes #16
---
 compiler/package.yaml    |  2 +-
 compiler/src/Compiler.hs | 29 +++++++++++++++++------------
 compiler/src/Config.hs   |  6 ++++--
 3 files changed, 22 insertions(+), 15 deletions(-)

(limited to 'compiler')

diff --git a/compiler/package.yaml b/compiler/package.yaml
index 18d8a33..0922c36 100644
--- a/compiler/package.yaml
+++ b/compiler/package.yaml
@@ -26,7 +26,7 @@ dependencies:
 - JuicyPixels
 - JuicyPixels-extra
 - parallel-io
-- regex-compat
+- Glob
 
 default-extensions:
 - DuplicateRecordFields
diff --git a/compiler/src/Compiler.hs b/compiler/src/Compiler.hs
index fc4e272..b84dedf 100644
--- a/compiler/src/Compiler.hs
+++ b/compiler/src/Compiler.hs
@@ -23,9 +23,8 @@ module Compiler
 
 import Control.Monad (liftM2)
 import Data.List (any)
-import Data.Maybe (isJust, fromMaybe)
-import Text.Regex (Regex, mkRegex, matchRegex)
 import System.FilePath ((</>))
+import qualified System.FilePath.Glob as Glob
 
 import Data.Aeson (ToJSON)
 import qualified Data.Aeson as JSON
@@ -73,26 +72,30 @@ writeJSON outputPath object =
     ensureParentDir JSON.encodeFile outputPath object
 
 
-galleryDirFilter :: Regex -> FSNode -> Bool
-galleryDirFilter excludeRegex =
+galleryDirFilter :: ([Glob.Pattern], [Glob.Pattern]) -> FSNode -> Bool
+galleryDirFilter (inclusionPatterns, exclusionPatterns) =
       (not . isHidden)
+  &&& (matchName True $ anyPattern inclusionPatterns)
   &&& (not . isConfigFile)
   &&& (not . containsOutputGallery)
-  &&& (not . excludedName)
+  &&& (not . (matchName False $ anyPattern exclusionPatterns))
 
   where
     (&&&) = liftM2 (&&)
     (|||) = liftM2 (||)
 
-    matchName :: (FileName -> Bool) -> FSNode -> Bool
-    matchName cond = maybe False cond . nodeName
+    matchName :: Bool -> (FileName -> Bool) -> FSNode -> Bool
+    matchName matchDir _ Dir{} = matchDir
+    matchName _ cond file@File{} = maybe False cond $ nodeName file
 
-    isConfigFile = matchName (== galleryConf)
-    isGalleryIndex = matchName (== indexFile)
-    isViewerIndex = matchName (== viewerMainFile)
+    anyPattern :: [Glob.Pattern] -> FileName -> Bool
+    anyPattern patterns filename = any (flip Glob.match filename) patterns
+
+    isConfigFile = matchName False (== galleryConf)
+    isGalleryIndex = matchName False (== indexFile)
+    isViewerIndex = matchName False (== viewerMainFile)
     containsOutputGallery File{} = False
     containsOutputGallery Dir{items} = any (isGalleryIndex ||| isViewerIndex) items
-    excludedName = isJust . matchRegex excludeRegex . fromMaybe "" . nodeName
 
 
 compileGallery :: FilePath -> FilePath -> Bool -> IO ()
@@ -102,7 +105,9 @@ compileGallery inputDirPath outputDirPath rebuildAll =
     let config = compiler fullConfig
 
     inputDir <- readDirectory inputDirPath
-    let sourceFilter = galleryDirFilter (mkRegex $ ignoreFiles config)
+    let inclusionPatterns = map Glob.compile $ includeFiles config
+    let exclusionPatterns = map Glob.compile $ excludeFiles config
+    let sourceFilter = galleryDirFilter (inclusionPatterns, exclusionPatterns)
     let sourceTree = filterDir sourceFilter inputDir
     inputTree <- readInputTree sourceTree
 
diff --git a/compiler/src/Config.hs b/compiler/src/Config.hs
index 20bc3bb..53333a5 100644
--- a/compiler/src/Config.hs
+++ b/compiler/src/Config.hs
@@ -34,7 +34,8 @@ import Resource (Resolution(..))
 
 data CompilerConfig = CompilerConfig
   { galleryName :: String
-  , ignoreFiles :: String
+  , includeFiles :: [String]
+  , excludeFiles :: [String]
   , tagsFromDirectories :: Int
   , thumbnailMaxResolution :: Resolution
   , pictureMaxResolution :: Maybe Resolution
@@ -43,7 +44,8 @@ data CompilerConfig = CompilerConfig
 instance FromJSON CompilerConfig where
   parseJSON = withObject "CompilerConfig" $ \v -> CompilerConfig
     <$> v .:? "galleryName" .!= "Gallery"
-    <*> v .:? "ignoreFiles" .!= ".^"
+    <*> v .:? "includeFiles" .!= ["*"]
+    <*> v .:? "excludeFiles" .!= []
     <*> v .:? "tagsFromDirectories" .!= 0
     <*> v .:? "thumbnailMaxResolution" .!= (Resolution 400 400)
     <*> v .:? "pictureMaxResolution"
-- 
cgit v1.2.3