From 1e57d76eadb2192be2b3d9343d4ddfeccc996bcb Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 27 Dec 2019 13:38:47 +0100 Subject: compiler: exclude output dir from input --- compiler/app/Main.hs | 21 ++++++--- compiler/src/Compiler.hs | 59 +++++++++++++++---------- example/.gitignore | 3 +- example/gallery.yaml | 6 ++- example/index.json | 111 ----------------------------------------------- 5 files changed, 56 insertions(+), 144 deletions(-) delete mode 100644 example/index.json diff --git a/compiler/app/Main.hs b/compiler/app/Main.hs index be57c82..d9b019a 100644 --- a/compiler/app/Main.hs +++ b/compiler/app/Main.hs @@ -29,29 +29,38 @@ import Compiler data Args = Args { inputDir :: String - , outputDir :: String } + , outputDir :: String + , rebuild :: Bool } args :: Parser Args args = Args <$> strOption ( long "input" <> short 'i' - <> metavar "INPUT DIR" + <> metavar "SOURCE DIR" + <> value "./" + <> showDefault <> help "Gallery source directory" ) <*> strOption ( long "output" <> short 'o' <> metavar "OUTPUT DIR" - <> help "Generated gallery output path, outside of the input directory" ) + <> value "./out" + <> showDefault + <> help "Generated gallery output path" ) + <*> switch + ( long "rebuild" + <> short 'r' + <> help "Invalidate cache and recompile everything" ) main :: IO () main = do options <- execParser opts - compileGallery (inputDir options) (outputDir options) + compileGallery (inputDir options) (outputDir options) (rebuild options) where opts = info (args <**> helper) ( fullDesc - <> progDesc "Compile a picture gallery" - <> header "ldgallery - A static generator which turns a collection of tagged pictures into a searchable web gallery.") + <> progDesc "Compile a gallery" + <> header "ldgallery - a static gallery generator with tags" ) diff --git a/compiler/src/Compiler.hs b/compiler/src/Compiler.hs index 854fd03..2584570 100644 --- a/compiler/src/Compiler.hs +++ b/compiler/src/Compiler.hs @@ -27,31 +27,33 @@ module Compiler ) where -import Control.Monad +import Control.Monad (liftM2) import Data.Function ((&)) +import Data.List (any) import System.FilePath (()) import Data.Aeson (ToJSON) import qualified Data.Aeson as JSON import Config +import Input (decodeYamlFile, readInputTree) +import Resource (ResourceTree, buildResourceTree, cleanupResourceDir) +import Gallery (buildGalleryTree) import Files ( FileName + , FSNode(..) , readDirectory - , localPath , isHidden , nodeName , filterDir - , flattenDir - , root - , (/>) , ensureParentDir , isOutdated ) - -import Input (decodeYamlFile, readInputTree) -import Resource (ResourceTree, buildResourceTree, cleanupResourceDir) -import Gallery (buildGalleryTree) import Processors + ( dirFileProcessor + , itemFileProcessor + , thumbnailFileProcessor + , skipCached + , withCached ) writeJSON :: ToJSON a => FileName -> a -> IO () @@ -61,26 +63,21 @@ writeJSON outputPath object = ensureParentDir JSON.encodeFile outputPath object -compileGallery :: FilePath -> FilePath -> IO () -compileGallery inputDirPath outputDirPath = +compileGallery :: FilePath -> FilePath -> Bool -> IO () +compileGallery inputDirPath outputDirPath rebuildAll = do fullConfig <- readConfig inputGalleryConf let config = compiler fullConfig - -- TODO: exclude output dir if it's under the input dir inputDir <- readDirectory inputDirPath - - let isGalleryFile = \n -> nodeName n == galleryConf - let galleryTree = filterDir (liftM2 (&&) (not . isGalleryFile) (not . isHidden)) inputDir - - inputTree <- readInputTree galleryTree + let sourceTree = filterDir galleryDirFilter inputDir + inputTree <- readInputTree sourceTree invalidateCache <- isOutdated inputGalleryConf outputIndex - let cache = if invalidateCache then skipCached else withCached - let dirProc = dirFileProcessor inputDirPath outputDirPath itemsDir - let itemProc = itemFileProcessor (pictureMaxResolution config) cache inputDirPath outputDirPath itemsDir - let thumbnailProc = thumbnailFileProcessor (thumbnailResolution config) cache inputDirPath outputDirPath thumbnailsDir - resourceTree <- buildResourceTree dirProc itemProc thumbnailProc inputTree + let cache = if invalidateCache || rebuildAll then skipCached else withCached + let itemProc = itemProcessor (pictureMaxResolution config) cache + let thumbnailProc = thumbnailProcessor (thumbnailResolution config) cache + resourceTree <- buildResourceTree dirProcessor itemProc thumbnailProc inputTree cleanupResourceDir resourceTree outputDirPath @@ -92,9 +89,23 @@ compileGallery inputDirPath outputDirPath = where galleryConf = "gallery.yaml" + indexFile = "index.json" + viewerConfFile = "viewer.json" itemsDir = "items" thumbnailsDir = "thumbnails" inputGalleryConf = inputDirPath galleryConf - outputIndex = outputDirPath "index.json" - outputViewerConf = outputDirPath "viewer.json" + 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 + thumbnailProcessor thumbRes cache = + thumbnailFileProcessor thumbRes cache inputDirPath outputDirPath thumbnailsDir diff --git a/example/.gitignore b/example/.gitignore index f1212bc..1fcb152 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -1,2 +1 @@ -items -thumbnails +out diff --git a/example/gallery.yaml b/example/gallery.yaml index fc4bdf9..0a16e59 100644 --- a/example/gallery.yaml +++ b/example/gallery.yaml @@ -1,2 +1,6 @@ -compiler: {} +compiler: + pictureMaxResolution: + width: 1024 + height: 768 + viewer: {} diff --git a/example/index.json b/example/index.json deleted file mode 100644 index d693f7b..0000000 --- a/example/index.json +++ /dev/null @@ -1,111 +0,0 @@ -{ - "_comment": "reference gallery index file, manually aggregated", - - "title": "", - "date": "", - "description": "", - "tags": [ - "photographer.nphilou", - "location.germany.berlin", - "books", - "book-shop", - "location.switzerland.ormont-dessus", - "glacier3000", - "time.day", - "weather.foggy", - "forest", - "trees", - "catwalk", - "mountain" - ], - "path": "/", - "thumbnail": null, - "properties": { - "type": "directory", - "items": [ - { - "title": "Book shop", - "date": "2016:12:19T16:48:50+02:00", - "description": "© Philippe NGUYEN", - "tags": [ - "photographer.nphilou", - "location.germany.berlin", - "books", - "book-shop" - ], - "path": "/items/_DSC8808-1.jpg", - "thumbnail": "/thumbnails/_DSC8808-1.jpg", - "properties": { - "type": "image", - "filesize": 987, - "resolution": { "width": 3840, "height": 2160 } - } - }, - { - "title": "Glacier 3000", - "date": "", - "description": "", - "tags": [ - "photographer.nphilou", - "location.switzerland.ormont-dessus", - "glacier3000", - "time.day", - "weather.foggy", - "forest", - "trees", - "catwalk", - "mountain" - ], - "path": "/items/Glacier 3000", - "thumbnail": null, - "properties": { - "type": "directory", - "items": [ - { - "title": "Fog 3000", - "date": "2019:08:10T14:38:47+01:00", - "description": "© Philippe NGUYEN", - "tags": [ - "photographer.nphilou", - "location.switzerland.ormont-dessus", - "glacier3000", - "time.day", - "weather.foggy", - "forest", - "trees" - ], - "path": "/items/Glacier 3000/_DSC5475.jpg", - "thumbnail": "/thumbnails/Glacier 3000/_DSC5475.jpg", - "properties": { - "type": "image", - "filesize": 355, - "resolution": { "width": 3840, "height": 2160 } - } - }, - { - "title": "Foggy catwalk", - "date": " 2019:08:10T16:00:45+01:00", - "description": "© Philippe NGUYEN", - "tags": [ - "photographer.nphilou", - "location.switzerland.ormont-dessus", - "glacier3000", - "time.day", - "weather.foggy", - "catwalk", - "mountain" - ], - "path": "/items/Glacier 3000/_DSC5542.jpg", - "thumbnail": "/thumbnails/Glacier 3000/_DSC5542.jpg", - "properties": { - "type": "image", - "filesize": 447, - "resolution": { "width": 3840, "height": 2160 } - } - } - ] - } - } - ] - } -} -- cgit v1.2.3