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 ++++-- example/gallery.yaml | 7 ++++++- ldgallery.1.md | 7 +++++-- 5 files changed, 33 insertions(+), 18 deletions(-) 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" diff --git a/example/gallery.yaml b/example/gallery.yaml index 5da7328..ccdb16b 100644 --- a/example/gallery.yaml +++ b/example/gallery.yaml @@ -1,6 +1,11 @@ compiler: galleryName: Example gallery - ignoreFiles: .*\.md + + includeFiles: + - "*.jpg" + + #excludeFiles: + #- "*.md" tagsFromDirectories: 0 # default diff --git a/ldgallery.1.md b/ldgallery.1.md index ec685c6..abaeb16 100644 --- a/ldgallery.1.md +++ b/ldgallery.1.md @@ -90,8 +90,11 @@ The gallery settings reside in a file named "gallery.yaml" located at the root o compiler.galleryName : Name of the gallery. Defaults to "Gallery". -compiler.ignoreFiles -: Regular expression matching the name of files to ignore. +compiler.includeFiles[] +: Glob patterns of file names to include in the gallery. Defaults to ["*"] (matches all file names). + +compiler.excludeFiles[] +: Glob patterns of file names to exclude from the gallery. Defaults to [] (none). compiler.tagsFromDirectories : How far to look at parent directories to add implicit tags. Defaults to 0. -- cgit v1.2.3