From 2ad60869c2e8d0846672ccb18b2de99c9cf33671 Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 5 Jan 2020 19:24:50 +0100 Subject: compiler: add option to add tags from n parent directories GitHub: closes #15 --- compiler/src/Compiler.hs | 2 +- compiler/src/Config.hs | 4 ++-- compiler/src/Resource.hs | 31 +++++++++++++------------------ example/gallery.yaml | 3 ++- ldgallery.1.md | 4 ++-- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/compiler/src/Compiler.hs b/compiler/src/Compiler.hs index d0ec003..fc4e272 100644 --- a/compiler/src/Compiler.hs +++ b/compiler/src/Compiler.hs @@ -111,7 +111,7 @@ compileGallery inputDirPath outputDirPath rebuildAll = let itemProc = itemProcessor (pictureMaxResolution config) cache let thumbnailProc = thumbnailProcessor (thumbnailMaxResolution config) cache - let galleryBuilder = buildGalleryTree itemProc thumbnailProc (implicitDirectoryTag config) + let galleryBuilder = buildGalleryTree itemProc thumbnailProc (tagsFromDirectories config) resources <- galleryBuilder (galleryName config) inputTree galleryCleanupResourceDir resources outputDirPath diff --git a/compiler/src/Config.hs b/compiler/src/Config.hs index b9434ba..20bc3bb 100644 --- a/compiler/src/Config.hs +++ b/compiler/src/Config.hs @@ -35,7 +35,7 @@ import Resource (Resolution(..)) data CompilerConfig = CompilerConfig { galleryName :: String , ignoreFiles :: String - , implicitDirectoryTag :: Bool + , tagsFromDirectories :: Int , thumbnailMaxResolution :: Resolution , pictureMaxResolution :: Maybe Resolution } deriving (Generic, Show) @@ -44,7 +44,7 @@ instance FromJSON CompilerConfig where parseJSON = withObject "CompilerConfig" $ \v -> CompilerConfig <$> v .:? "galleryName" .!= "Gallery" <*> v .:? "ignoreFiles" .!= ".^" - <*> v .:? "implicitDirectoryTag" .!= False + <*> v .:? "tagsFromDirectories" .!= 0 <*> v .:? "thumbnailMaxResolution" .!= (Resolution 400 400) <*> v .:? "pictureMaxResolution" diff --git a/compiler/src/Resource.hs b/compiler/src/Resource.hs index 2019418..261191b 100644 --- a/compiler/src/Resource.hs +++ b/compiler/src/Resource.hs @@ -27,7 +27,7 @@ import Control.Concurrent.ParallelIO.Global (parallel) import Data.List ((\\), sortBy) import Data.Ord (comparing) import Data.Char (toLower) -import Data.Maybe (mapMaybe, fromMaybe, maybeToList) +import Data.Maybe (mapMaybe, fromMaybe) import Data.Function ((&)) import qualified Data.Set as Set @@ -94,15 +94,12 @@ type ThumbnailProcessor = Path -> IO (Maybe Path) buildGalleryTree :: ItemProcessor -> ThumbnailProcessor - -> Bool -> String -> InputTree -> IO GalleryItem -buildGalleryTree processItem processThumbnail addDirTag galleryName inputTree = - mkGalleryItem (Path []) inputTree >>= return . named galleryName + -> Int -> String -> InputTree -> IO GalleryItem +buildGalleryTree processItem processThumbnail tagsFromDirectories galleryName inputTree = + mkGalleryItem (Just galleryName) (Path []) inputTree where - named :: String -> GalleryItem -> GalleryItem - named name item = item { title = name } - - mkGalleryItem :: Path -> InputTree -> IO GalleryItem - mkGalleryItem parents InputFile{path, sidecar} = + mkGalleryItem :: Maybe String -> Path -> InputTree -> IO GalleryItem + mkGalleryItem _ parents InputFile{path, sidecar} = do properties <- processItem path processedThumbnail <- processThumbnail path @@ -110,7 +107,7 @@ buildGalleryTree processItem processThumbnail addDirTag galleryName inputTree = { title = itemTitle , date = optMeta date "" -- TODO: check and normalise dates , description = optMeta description "" - , tags = (optMeta tags []) ++ implicitParentTag parents + , tags = (optMeta tags []) ++ implicitParentTags parents , path = parents Maybe a) -> a -> a optMeta get fallback = fromMaybe fallback $ get sidecar - mkGalleryItem parents InputDir{path, dirThumbnailPath, items} = + mkGalleryItem rootTitle parents InputDir{path, dirThumbnailPath, items} = do processedThumbnail <- maybeThumbnail dirThumbnailPath - processedItems <- parallel $ map (mkGalleryItem itemPath) items + processedItems <- parallel $ map (mkGalleryItem Nothing itemPath) items return GalleryItem { title = itemTitle -- TODO: consider using the most recent item's date? what if empty? , date = "" -- TODO: consider allowing metadata sidecars for directories too , description = "" - , tags = (aggregateChildTags processedItems) ++ implicitParentTag parents + , tags = (aggregateChildTags processedItems) ++ implicitParentTags parents , path = itemPath , thumbnail = processedThumbnail , properties = Directory processedItems } where itemTitle :: String - itemTitle = fromMaybe "" $ fileName path + itemTitle = flip fromMaybe rootTitle (fromMaybe "" $ fileName path) itemPath :: Path itemPath = parents [a] -> [a] unique = Set.toList . Set.fromList - implicitParentTag :: Path -> [Tag] - implicitParentTag parents - | addDirTag = maybeToList $ fileName parents - | otherwise = [] + implicitParentTags :: Path -> [Tag] + implicitParentTags (Path elements) = take tagsFromDirectories elements flattenGalleryTree :: GalleryItem -> [GalleryItem] diff --git a/example/gallery.yaml b/example/gallery.yaml index 53e1448..5da7328 100644 --- a/example/gallery.yaml +++ b/example/gallery.yaml @@ -1,7 +1,8 @@ compiler: galleryName: Example gallery ignoreFiles: .*\.md - implicitDirectoryTag: false # default + + tagsFromDirectories: 0 # default thumbnailMaxResolution: width: 400 # default diff --git a/ldgallery.1.md b/ldgallery.1.md index 99d6ccb..ec685c6 100644 --- a/ldgallery.1.md +++ b/ldgallery.1.md @@ -93,8 +93,8 @@ compiler.galleryName compiler.ignoreFiles : Regular expression matching the name of files to ignore. -compiler.implicitDirectoryTag -: Whether to implicitely tag items with the name of their immediate parent directory. +compiler.tagsFromDirectories +: How far to look at parent directories to add implicit tags. Defaults to 0. compiler.thumbnailMaxResolution.width : Maximum width in pixels of the item thumbnails, 400 by default. -- cgit v1.2.3