From 987eb81cb5d98262299c7917d752c54907cbbc33 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 23 Jan 2020 22:36:21 +0100 Subject: compiler: add directory incl and excl glob settings GitHub: closes #41 --- compiler/src/Compiler.hs | 37 ++++++++++++++++++++----------------- compiler/src/Config.hs | 12 ++++++++---- example/gallery.yaml | 7 +++++-- ldgallery.1.md | 10 ++++++++-- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/compiler/src/Compiler.hs b/compiler/src/Compiler.hs index a347433..13e9232 100644 --- a/compiler/src/Compiler.hs +++ b/compiler/src/Compiler.hs @@ -71,30 +71,35 @@ writeJSON outputPath object = ensureParentDir JSON.encodeFile outputPath object -galleryDirFilter :: ([Glob.Pattern], [Glob.Pattern]) -> FSNode -> Bool -galleryDirFilter (inclusionPatterns, exclusionPatterns) = +galleryDirFilter :: CompilerConfig -> FSNode -> Bool +galleryDirFilter config = (not . isHidden) - &&& (matchName True $ anyPattern inclusionPatterns) - &&& (not . isConfigFile) + &&& (not . matchesFile (== galleryConf)) &&& (not . containsOutputGallery) - &&& (not . (matchName False $ anyPattern exclusionPatterns)) + &&& ((matchesDir $ anyPattern $ includedDirectories config) ||| + (matchesFile $ anyPattern $ includedFiles config)) + &&& (not . ((matchesDir $ anyPattern $ excludedDirectories config) ||| + (matchesFile $ anyPattern $ excludedFiles config))) where (&&&) = liftM2 (&&) (|||) = liftM2 (||) - matchName :: Bool -> (FileName -> Bool) -> FSNode -> Bool - matchName matchDir _ Dir{} = matchDir - matchName _ cond file@File{} = maybe False cond $ nodeName file + matchesDir :: (FileName -> Bool) -> FSNode -> Bool + matchesDir cond dir@Dir{} = maybe False cond $ nodeName dir + matchesDir _ File{} = False - anyPattern :: [Glob.Pattern] -> FileName -> Bool - anyPattern patterns filename = any (flip Glob.match filename) patterns + matchesFile :: (FileName -> Bool) -> FSNode -> Bool + matchesFile cond file@File{} = maybe False cond $ nodeName file + matchesFile _ Dir{} = False - isConfigFile = matchName False (== galleryConf) - isGalleryIndex = matchName False (== indexFile) - isViewerIndex = matchName False (== viewerMainFile) + anyPattern :: [String] -> FileName -> Bool + anyPattern patterns filename = any (flip Glob.match filename) (map Glob.compile patterns) + + containsOutputGallery :: FSNode -> Bool containsOutputGallery File{} = False - containsOutputGallery Dir{items} = any (isGalleryIndex ||| isViewerIndex) items + containsOutputGallery Dir{items} = + any (matchesFile (== indexFile) ||| matchesFile (== viewerMainFile)) items compileGallery :: FilePath -> FilePath -> Bool -> IO () @@ -104,9 +109,7 @@ compileGallery inputDirPath outputDirPath rebuildAll = let config = compiler fullConfig inputDir <- readDirectory inputDirPath - let inclusionPatterns = map Glob.compile $ includeFiles config - let exclusionPatterns = map Glob.compile $ excludeFiles config - let sourceFilter = galleryDirFilter (inclusionPatterns, exclusionPatterns) + let sourceFilter = galleryDirFilter config let sourceTree = filterDir sourceFilter inputDir inputTree <- readInputTree sourceTree diff --git a/compiler/src/Config.hs b/compiler/src/Config.hs index 53333a5..d670aae 100644 --- a/compiler/src/Config.hs +++ b/compiler/src/Config.hs @@ -34,8 +34,10 @@ import Resource (Resolution(..)) data CompilerConfig = CompilerConfig { galleryName :: String - , includeFiles :: [String] - , excludeFiles :: [String] + , includedDirectories :: [String] + , excludedDirectories :: [String] + , includedFiles :: [String] + , excludedFiles :: [String] , tagsFromDirectories :: Int , thumbnailMaxResolution :: Resolution , pictureMaxResolution :: Maybe Resolution @@ -44,8 +46,10 @@ data CompilerConfig = CompilerConfig instance FromJSON CompilerConfig where parseJSON = withObject "CompilerConfig" $ \v -> CompilerConfig <$> v .:? "galleryName" .!= "Gallery" - <*> v .:? "includeFiles" .!= ["*"] - <*> v .:? "excludeFiles" .!= [] + <*> v .:? "includedDirectories" .!= ["*"] + <*> v .:? "excludedDirectories" .!= [] + <*> v .:? "includedFiles" .!= ["*"] + <*> v .:? "excludedFiles" .!= [] <*> v .:? "tagsFromDirectories" .!= 0 <*> v .:? "thumbnailMaxResolution" .!= (Resolution 400 400) <*> v .:? "pictureMaxResolution" diff --git a/example/gallery.yaml b/example/gallery.yaml index ccdb16b..7fb2c92 100644 --- a/example/gallery.yaml +++ b/example/gallery.yaml @@ -1,10 +1,13 @@ compiler: galleryName: Example gallery - includeFiles: + #includedDirectories: ["*"] + #excludedDirectories: [] + + includedFiles: - "*.jpg" - #excludeFiles: + #excludedFiles: #- "*.md" tagsFromDirectories: 0 # default diff --git a/ldgallery.1.md b/ldgallery.1.md index 981dda7..febe2e2 100644 --- a/ldgallery.1.md +++ b/ldgallery.1.md @@ -93,10 +93,16 @@ 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.includeFiles[] +compiler.includedDirectories[] +: Glob patterns of directory names to include in the gallery. Defaults to ["*"] (matches all directory names). + +compiler.excludedDirectories[] +: Glob patterns of directory names to exclude from the gallery. Defaults to [] (none). + +compiler.includedFiles[] : Glob patterns of file names to include in the gallery. Defaults to ["*"] (matches all file names). -compiler.excludeFiles[] +compiler.excludedFiles[] : Glob patterns of file names to exclude from the gallery. Defaults to [] (none). compiler.tagsFromDirectories -- cgit v1.2.3