From 6691b14cf4e867a9018f38c174fa98f1ada19f82 Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 31 Dec 2019 08:38:15 +0100 Subject: compiler: add option to ignore files matching a regex GitHub: closes #10 --- compiler/src/Compiler.hs | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'compiler/src/Compiler.hs') diff --git a/compiler/src/Compiler.hs b/compiler/src/Compiler.hs index 9572d50..0132b1a 100644 --- a/compiler/src/Compiler.hs +++ b/compiler/src/Compiler.hs @@ -30,6 +30,8 @@ module Compiler import Control.Monad (liftM2) import Data.Function ((&)) import Data.List (any) +import Data.Maybe (isJust) +import Text.Regex (Regex, mkRegex, matchRegex) import System.FilePath (()) import Data.Aeson (ToJSON) @@ -52,6 +54,14 @@ import Processors , skipCached, withCached ) +galleryConf = "gallery.yaml" +indexFile = "index.json" +viewerMainFile = "index.html" +viewerConfFile = "viewer.json" +itemsDir = "items" +thumbnailsDir = "thumbnails" + + writeJSON :: ToJSON a => FileName -> a -> IO () writeJSON outputPath object = do @@ -59,6 +69,28 @@ writeJSON outputPath object = ensureParentDir JSON.encodeFile outputPath object +galleryDirFilter :: Regex -> FSNode -> Bool +galleryDirFilter excludeRegex = + (not . isHidden) + &&& (not . isConfigFile) + &&& (not . containsOutputGallery) + &&& (not . excludedName) + + where + (&&&) = liftM2 (&&) + (|||) = liftM2 (||) + + isConfigFile = (galleryConf ==) . nodeName + + isGalleryIndex = (indexFile ==) + isViewerIndex = (viewerMainFile ==) + containsOutputGallery (File _) = False + containsOutputGallery (Dir _ items) = + any ((isGalleryIndex ||| isViewerIndex) . nodeName) items + + excludedName = isJust . matchRegex excludeRegex . nodeName + + compileGallery :: FilePath -> FilePath -> Bool -> IO () compileGallery inputDirPath outputDirPath rebuildAll = do @@ -66,7 +98,8 @@ compileGallery inputDirPath outputDirPath rebuildAll = let config = compiler fullConfig inputDir <- readDirectory inputDirPath - let sourceTree = filterDir galleryDirFilter inputDir + let sourceFilter = galleryDirFilter (mkRegex $ ignoreFiles config) + let sourceTree = filterDir sourceFilter inputDir inputTree <- readInputTree sourceTree invalidateCache <- isOutdated False inputGalleryConf outputIndex @@ -82,22 +115,10 @@ compileGallery inputDirPath outputDirPath rebuildAll = writeJSON outputViewerConf $ viewer fullConfig where - galleryConf = "gallery.yaml" - indexFile = "index.json" - viewerConfFile = "viewer.json" - itemsDir = "items" - thumbnailsDir = "thumbnails" - inputGalleryConf = inputDirPath galleryConf outputIndex = outputDirPath indexFile outputViewerConf = outputDirPath viewerConfFile - (&&&) = liftM2 (&&) - galleryDirFilter = (not . containsOutputGallery) &&& (not . isConfigFile) &&& (not . isHidden) - isConfigFile = (==) galleryConf . nodeName - containsOutputGallery (File _) = False - containsOutputGallery (Dir _ items) = any ((==) indexFile . nodeName) items - dirProcessor = dirFileProcessor inputDirPath outputDirPath itemsDir itemProcessor maxRes cache = itemFileProcessor maxRes cache inputDirPath outputDirPath itemsDir -- cgit v1.2.3