diff options
author | pacien | 2020-01-05 20:40:41 +0100 |
---|---|---|
committer | pacien | 2020-01-05 20:40:41 +0100 |
commit | 1e3a0e39cb6cdc86a6ba6b570c72c44931cf1c3b (patch) | |
tree | 2746d5fe93eb00162b146a054761a2c8e7f76ed2 /compiler | |
parent | 2ad60869c2e8d0846672ccb18b2de99c9cf33671 (diff) | |
download | ldgallery-1e3a0e39cb6cdc86a6ba6b570c72c44931cf1c3b.tar.gz |
compiler: replace file filter with inclusino and exclusion glob lists
GitHub: closes #16
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/package.yaml | 2 | ||||
-rw-r--r-- | compiler/src/Compiler.hs | 29 | ||||
-rw-r--r-- | compiler/src/Config.hs | 6 |
3 files changed, 22 insertions, 15 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: | |||
26 | - JuicyPixels | 26 | - JuicyPixels |
27 | - JuicyPixels-extra | 27 | - JuicyPixels-extra |
28 | - parallel-io | 28 | - parallel-io |
29 | - regex-compat | 29 | - Glob |
30 | 30 | ||
31 | default-extensions: | 31 | default-extensions: |
32 | - DuplicateRecordFields | 32 | - 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 | |||
23 | 23 | ||
24 | import Control.Monad (liftM2) | 24 | import Control.Monad (liftM2) |
25 | import Data.List (any) | 25 | import Data.List (any) |
26 | import Data.Maybe (isJust, fromMaybe) | ||
27 | import Text.Regex (Regex, mkRegex, matchRegex) | ||
28 | import System.FilePath ((</>)) | 26 | import System.FilePath ((</>)) |
27 | import qualified System.FilePath.Glob as Glob | ||
29 | 28 | ||
30 | import Data.Aeson (ToJSON) | 29 | import Data.Aeson (ToJSON) |
31 | import qualified Data.Aeson as JSON | 30 | import qualified Data.Aeson as JSON |
@@ -73,26 +72,30 @@ writeJSON outputPath object = | |||
73 | ensureParentDir JSON.encodeFile outputPath object | 72 | ensureParentDir JSON.encodeFile outputPath object |
74 | 73 | ||
75 | 74 | ||
76 | galleryDirFilter :: Regex -> FSNode -> Bool | 75 | galleryDirFilter :: ([Glob.Pattern], [Glob.Pattern]) -> FSNode -> Bool |
77 | galleryDirFilter excludeRegex = | 76 | galleryDirFilter (inclusionPatterns, exclusionPatterns) = |
78 | (not . isHidden) | 77 | (not . isHidden) |
78 | &&& (matchName True $ anyPattern inclusionPatterns) | ||
79 | &&& (not . isConfigFile) | 79 | &&& (not . isConfigFile) |
80 | &&& (not . containsOutputGallery) | 80 | &&& (not . containsOutputGallery) |
81 | &&& (not . excludedName) | 81 | &&& (not . (matchName False $ anyPattern exclusionPatterns)) |
82 | 82 | ||
83 | where | 83 | where |
84 | (&&&) = liftM2 (&&) | 84 | (&&&) = liftM2 (&&) |
85 | (|||) = liftM2 (||) | 85 | (|||) = liftM2 (||) |
86 | 86 | ||
87 | matchName :: (FileName -> Bool) -> FSNode -> Bool | 87 | matchName :: Bool -> (FileName -> Bool) -> FSNode -> Bool |
88 | matchName cond = maybe False cond . nodeName | 88 | matchName matchDir _ Dir{} = matchDir |
89 | matchName _ cond file@File{} = maybe False cond $ nodeName file | ||
89 | 90 | ||
90 | isConfigFile = matchName (== galleryConf) | 91 | anyPattern :: [Glob.Pattern] -> FileName -> Bool |
91 | isGalleryIndex = matchName (== indexFile) | 92 | anyPattern patterns filename = any (flip Glob.match filename) patterns |
92 | isViewerIndex = matchName (== viewerMainFile) | 93 | |
94 | isConfigFile = matchName False (== galleryConf) | ||
95 | isGalleryIndex = matchName False (== indexFile) | ||
96 | isViewerIndex = matchName False (== viewerMainFile) | ||
93 | containsOutputGallery File{} = False | 97 | containsOutputGallery File{} = False |
94 | containsOutputGallery Dir{items} = any (isGalleryIndex ||| isViewerIndex) items | 98 | containsOutputGallery Dir{items} = any (isGalleryIndex ||| isViewerIndex) items |
95 | excludedName = isJust . matchRegex excludeRegex . fromMaybe "" . nodeName | ||
96 | 99 | ||
97 | 100 | ||
98 | compileGallery :: FilePath -> FilePath -> Bool -> IO () | 101 | compileGallery :: FilePath -> FilePath -> Bool -> IO () |
@@ -102,7 +105,9 @@ compileGallery inputDirPath outputDirPath rebuildAll = | |||
102 | let config = compiler fullConfig | 105 | let config = compiler fullConfig |
103 | 106 | ||
104 | inputDir <- readDirectory inputDirPath | 107 | inputDir <- readDirectory inputDirPath |
105 | let sourceFilter = galleryDirFilter (mkRegex $ ignoreFiles config) | 108 | let inclusionPatterns = map Glob.compile $ includeFiles config |
109 | let exclusionPatterns = map Glob.compile $ excludeFiles config | ||
110 | let sourceFilter = galleryDirFilter (inclusionPatterns, exclusionPatterns) | ||
106 | let sourceTree = filterDir sourceFilter inputDir | 111 | let sourceTree = filterDir sourceFilter inputDir |
107 | inputTree <- readInputTree sourceTree | 112 | inputTree <- readInputTree sourceTree |
108 | 113 | ||
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(..)) | |||
34 | 34 | ||
35 | data CompilerConfig = CompilerConfig | 35 | data CompilerConfig = CompilerConfig |
36 | { galleryName :: String | 36 | { galleryName :: String |
37 | , ignoreFiles :: String | 37 | , includeFiles :: [String] |
38 | , excludeFiles :: [String] | ||
38 | , tagsFromDirectories :: Int | 39 | , tagsFromDirectories :: Int |
39 | , thumbnailMaxResolution :: Resolution | 40 | , thumbnailMaxResolution :: Resolution |
40 | , pictureMaxResolution :: Maybe Resolution | 41 | , pictureMaxResolution :: Maybe Resolution |
@@ -43,7 +44,8 @@ data CompilerConfig = CompilerConfig | |||
43 | instance FromJSON CompilerConfig where | 44 | instance FromJSON CompilerConfig where |
44 | parseJSON = withObject "CompilerConfig" $ \v -> CompilerConfig | 45 | parseJSON = withObject "CompilerConfig" $ \v -> CompilerConfig |
45 | <$> v .:? "galleryName" .!= "Gallery" | 46 | <$> v .:? "galleryName" .!= "Gallery" |
46 | <*> v .:? "ignoreFiles" .!= ".^" | 47 | <*> v .:? "includeFiles" .!= ["*"] |
48 | <*> v .:? "excludeFiles" .!= [] | ||
47 | <*> v .:? "tagsFromDirectories" .!= 0 | 49 | <*> v .:? "tagsFromDirectories" .!= 0 |
48 | <*> v .:? "thumbnailMaxResolution" .!= (Resolution 400 400) | 50 | <*> v .:? "thumbnailMaxResolution" .!= (Resolution 400 400) |
49 | <*> v .:? "pictureMaxResolution" | 51 | <*> v .:? "pictureMaxResolution" |