aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2020-01-05 20:40:41 +0100
committerpacien2020-01-05 20:40:41 +0100
commit1e3a0e39cb6cdc86a6ba6b570c72c44931cf1c3b (patch)
tree2746d5fe93eb00162b146a054761a2c8e7f76ed2
parent2ad60869c2e8d0846672ccb18b2de99c9cf33671 (diff)
downloadldgallery-1e3a0e39cb6cdc86a6ba6b570c72c44931cf1c3b.tar.gz
compiler: replace file filter with inclusino and exclusion glob lists
GitHub: closes #16
-rw-r--r--compiler/package.yaml2
-rw-r--r--compiler/src/Compiler.hs29
-rw-r--r--compiler/src/Config.hs6
-rw-r--r--example/gallery.yaml7
-rw-r--r--ldgallery.1.md7
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:
26- JuicyPixels 26- JuicyPixels
27- JuicyPixels-extra 27- JuicyPixels-extra
28- parallel-io 28- parallel-io
29- regex-compat 29- Glob
30 30
31default-extensions: 31default-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
24import Control.Monad (liftM2) 24import Control.Monad (liftM2)
25import Data.List (any) 25import Data.List (any)
26import Data.Maybe (isJust, fromMaybe)
27import Text.Regex (Regex, mkRegex, matchRegex)
28import System.FilePath ((</>)) 26import System.FilePath ((</>))
27import qualified System.FilePath.Glob as Glob
29 28
30import Data.Aeson (ToJSON) 29import Data.Aeson (ToJSON)
31import qualified Data.Aeson as JSON 30import 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
76galleryDirFilter :: Regex -> FSNode -> Bool 75galleryDirFilter :: ([Glob.Pattern], [Glob.Pattern]) -> FSNode -> Bool
77galleryDirFilter excludeRegex = 76galleryDirFilter (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
98compileGallery :: FilePath -> FilePath -> Bool -> IO () 101compileGallery :: 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
35data CompilerConfig = CompilerConfig 35data 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
43instance FromJSON CompilerConfig where 44instance 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"
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 @@
1compiler: 1compiler:
2 galleryName: Example gallery 2 galleryName: Example gallery
3 ignoreFiles: .*\.md 3
4 includeFiles:
5 - "*.jpg"
6
7 #excludeFiles:
8 #- "*.md"
4 9
5 tagsFromDirectories: 0 # default 10 tagsFromDirectories: 0 # default
6 11
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
90compiler.galleryName 90compiler.galleryName
91: Name of the gallery. Defaults to "Gallery". 91: Name of the gallery. Defaults to "Gallery".
92 92
93compiler.ignoreFiles 93compiler.includeFiles[]
94: Regular expression matching the name of files to ignore. 94: Glob patterns of file names to include in the gallery. Defaults to ["*"] (matches all file names).
95
96compiler.excludeFiles[]
97: Glob patterns of file names to exclude from the gallery. Defaults to [] (none).
95 98
96compiler.tagsFromDirectories 99compiler.tagsFromDirectories
97: How far to look at parent directories to add implicit tags. Defaults to 0. 100: How far to look at parent directories to add implicit tags. Defaults to 0.