aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/Compiler.hs
diff options
context:
space:
mode:
authorpacien2020-02-26 22:13:00 +0100
committerNotkea2020-02-27 21:54:32 +0100
commitf09e9d9fa29284bd9ae872efe5ba1d526e349011 (patch)
tree50d523ffb4f2d6e4b1d09eb2edd9f099c9b20048 /compiler/src/Compiler.hs
parentc7fa5bd40d0e5c9ea50190a90a0ccfee8ad96c25 (diff)
downloadldgallery-f09e9d9fa29284bd9ae872efe5ba1d526e349011.tar.gz
compiler: add tag inclusion and exclusion globs
GitHub: closes #30
Diffstat (limited to 'compiler/src/Compiler.hs')
-rw-r--r--compiler/src/Compiler.hs30
1 files changed, 22 insertions, 8 deletions
diff --git a/compiler/src/Compiler.hs b/compiler/src/Compiler.hs
index 51f5065..fa405a2 100644
--- a/compiler/src/Compiler.hs
+++ b/compiler/src/Compiler.hs
@@ -25,6 +25,7 @@ module Compiler
25import GHC.Generics (Generic) 25import GHC.Generics (Generic)
26import Control.Monad (liftM2, when) 26import Control.Monad (liftM2, when)
27import Data.List (any) 27import Data.List (any)
28import Data.Maybe (fromMaybe)
28import System.FilePath ((</>)) 29import System.FilePath ((</>))
29import qualified System.FilePath.Glob as Glob 30import qualified System.FilePath.Glob as Glob
30import System.Directory (canonicalizePath) 31import System.Directory (canonicalizePath)
@@ -33,7 +34,7 @@ import Data.Aeson (ToJSON)
33import qualified Data.Aeson as JSON 34import qualified Data.Aeson as JSON
34 35
35import Config 36import Config
36import Input (readInputTree) 37import Input (InputTree, readInputTree, filterInputTree, sidecar, tags)
37import Resource (GalleryItem, buildGalleryTree, galleryCleanupResourceDir) 38import Resource (GalleryItem, buildGalleryTree, galleryCleanupResourceDir)
38import Files 39import Files
39 ( FileName 40 ( FileName
@@ -74,6 +75,15 @@ writeJSON outputPath object =
74 ensureParentDir JSON.encodeFile outputPath object 75 ensureParentDir JSON.encodeFile outputPath object
75 76
76 77
78(&&&) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
79(&&&) = liftM2 (&&)
80
81(|||) :: (a -> Bool) -> (a -> Bool) -> a -> Bool
82(|||) = liftM2 (||)
83
84anyPattern :: [String] -> String -> Bool
85anyPattern patterns string = any (flip Glob.match string) (map Glob.compile patterns)
86
77galleryDirFilter :: GalleryConfig -> [FilePath] -> FSNode -> Bool 87galleryDirFilter :: GalleryConfig -> [FilePath] -> FSNode -> Bool
78galleryDirFilter config excludedCanonicalDirs = 88galleryDirFilter config excludedCanonicalDirs =
79 (not . isHidden) 89 (not . isHidden)
@@ -84,9 +94,6 @@ galleryDirFilter config excludedCanonicalDirs =
84 (matchesFile $ anyPattern $ excludedFiles config))) 94 (matchesFile $ anyPattern $ excludedFiles config)))
85 95
86 where 96 where
87 (&&&) = liftM2 (&&)
88 (|||) = liftM2 (||)
89
90 matchesDir :: (FileName -> Bool) -> FSNode -> Bool 97 matchesDir :: (FileName -> Bool) -> FSNode -> Bool
91 matchesDir cond dir@Dir{} = maybe False cond $ nodeName dir 98 matchesDir cond dir@Dir{} = maybe False cond $ nodeName dir
92 matchesDir _ File{} = False 99 matchesDir _ File{} = False
@@ -95,13 +102,19 @@ galleryDirFilter config excludedCanonicalDirs =
95 matchesFile cond file@File{} = maybe False cond $ nodeName file 102 matchesFile cond file@File{} = maybe False cond $ nodeName file
96 matchesFile _ Dir{} = False 103 matchesFile _ Dir{} = False
97 104
98 anyPattern :: [String] -> FileName -> Bool
99 anyPattern patterns filename = any (flip Glob.match filename) (map Glob.compile patterns)
100
101 isExcludedDir :: FSNode -> Bool 105 isExcludedDir :: FSNode -> Bool
102 isExcludedDir Dir{canonicalPath} = any (canonicalPath ==) excludedCanonicalDirs 106 isExcludedDir Dir{canonicalPath} = any (canonicalPath ==) excludedCanonicalDirs
103 isExcludedDir File{} = False 107 isExcludedDir File{} = False
104 108
109inputTreeFilter :: GalleryConfig -> InputTree -> Bool
110inputTreeFilter GalleryConfig{includedTags, excludedTags} =
111 (hasTagMatching $ anyPattern includedTags)
112 &&& (not . (hasTagMatching $ anyPattern excludedTags))
113
114 where
115 hasTagMatching :: (String -> Bool) -> InputTree -> Bool
116 hasTagMatching cond = (any cond) . (fromMaybe [""] . tags) . sidecar
117
105 118
106compileGallery :: FilePath -> FilePath -> FilePath -> FilePath -> [FilePath] -> Bool -> Bool -> IO () 119compileGallery :: FilePath -> FilePath -> FilePath -> FilePath -> [FilePath] -> Bool -> Bool -> IO ()
107compileGallery configPath inputDirPath outputDirPath outputIndexPath excludedDirs rebuildAll cleanOutput = 120compileGallery configPath inputDirPath outputDirPath outputIndexPath excludedDirs rebuildAll cleanOutput =
@@ -113,12 +126,13 @@ compileGallery configPath inputDirPath outputDirPath outputIndexPath excludedDir
113 let sourceFilter = galleryDirFilter config excludedCanonicalDirs 126 let sourceFilter = galleryDirFilter config excludedCanonicalDirs
114 let sourceTree = filterDir sourceFilter inputDir 127 let sourceTree = filterDir sourceFilter inputDir
115 inputTree <- readInputTree sourceTree 128 inputTree <- readInputTree sourceTree
129 let curatedInputTree = filterInputTree (inputTreeFilter config) inputTree
116 130
117 let cache = if rebuildAll then skipCached else withCached 131 let cache = if rebuildAll then skipCached else withCached
118 let itemProc = itemProcessor config cache 132 let itemProc = itemProcessor config cache
119 let thumbnailProc = thumbnailProcessor config cache 133 let thumbnailProc = thumbnailProcessor config cache
120 let galleryBuilder = buildGalleryTree itemProc thumbnailProc (tagsFromDirectories config) 134 let galleryBuilder = buildGalleryTree itemProc thumbnailProc (tagsFromDirectories config)
121 resources <- galleryBuilder inputTree 135 resources <- galleryBuilder curatedInputTree
122 136
123 when cleanOutput $ galleryCleanupResourceDir resources outputDirPath 137 when cleanOutput $ galleryCleanupResourceDir resources outputDirPath
124 writeJSON (outputGalleryIndex outputIndexPath) $ GalleryIndex (viewerConfig config) resources 138 writeJSON (outputGalleryIndex outputIndexPath) $ GalleryIndex (viewerConfig config) resources