diff options
author | pacien | 2020-06-16 23:30:32 +0200 |
---|---|---|
committer | pacien | 2020-06-16 23:41:45 +0200 |
commit | e27f9a220fd8597266d52934bcb06dbe1681b338 (patch) | |
tree | fe6a85b55c252193b7cb7680556ea5fc3ff2131b /compiler/src/Input.hs | |
parent | 52abb806a3bde6eb69d64564d971efae2cbfda24 (diff) | |
download | ldgallery-e27f9a220fd8597266d52934bcb06dbe1681b338.tar.gz |
compiler: allow setting thumbnails for all items
Not only for directories.
GitHub: closes #224
Diffstat (limited to 'compiler/src/Input.hs')
-rw-r--r-- | compiler/src/Input.hs | 60 |
1 files changed, 37 insertions, 23 deletions
diff --git a/compiler/src/Input.hs b/compiler/src/Input.hs index 2480f5b..48931ec 100644 --- a/compiler/src/Input.hs +++ b/compiler/src/Input.hs | |||
@@ -30,11 +30,12 @@ import Data.Function ((&)) | |||
30 | import Data.Functor ((<&>)) | 30 | import Data.Functor ((<&>)) |
31 | import Data.Maybe (catMaybes, fromMaybe) | 31 | import Data.Maybe (catMaybes, fromMaybe) |
32 | import Data.Bool (bool) | 32 | import Data.Bool (bool) |
33 | import Data.List (find) | 33 | import Data.List (find, isSuffixOf) |
34 | import Data.Time.Clock (UTCTime) | 34 | import Data.Time.Clock (UTCTime) |
35 | import Data.Time.LocalTime (ZonedTime) | 35 | import Data.Time.LocalTime (ZonedTime) |
36 | import Data.Yaml (ParseException, decodeFileEither) | 36 | import Data.Yaml (ParseException, decodeFileEither) |
37 | import Data.Aeson (FromJSON) | 37 | import Data.Aeson (FromJSON) |
38 | import qualified Data.Map.Strict as Map | ||
38 | import System.FilePath (isExtensionOf, dropExtension) | 39 | import System.FilePath (isExtensionOf, dropExtension) |
39 | import System.Directory (doesFileExist, getModificationTime) | 40 | import System.Directory (doesFileExist, getModificationTime) |
40 | 41 | ||
@@ -55,12 +56,13 @@ data InputTree = | |||
55 | InputFile | 56 | InputFile |
56 | { path :: Path | 57 | { path :: Path |
57 | , modTime :: UTCTime | 58 | , modTime :: UTCTime |
58 | , sidecar :: Sidecar } | 59 | , sidecar :: Sidecar |
60 | , thumbnailPath :: Maybe Path } | ||
59 | | InputDir | 61 | | InputDir |
60 | { path :: Path | 62 | { path :: Path |
61 | , modTime :: UTCTime | 63 | , modTime :: UTCTime |
62 | , sidecar :: Sidecar | 64 | , sidecar :: Sidecar |
63 | , dirThumbnailPath :: Maybe Path | 65 | , thumbnailPath :: Maybe Path |
64 | , items :: [InputTree] } | 66 | , items :: [InputTree] } |
65 | deriving Show | 67 | deriving Show |
66 | 68 | ||
@@ -81,6 +83,9 @@ emptySidecar = Sidecar | |||
81 | sidecarExt :: String | 83 | sidecarExt :: String |
82 | sidecarExt = "yaml" | 84 | sidecarExt = "yaml" |
83 | 85 | ||
86 | thumbnailSuffix :: String | ||
87 | thumbnailSuffix = "_thumbnail" | ||
88 | |||
84 | dirPropFile :: String | 89 | dirPropFile :: String |
85 | dirPropFile = "_directory" | 90 | dirPropFile = "_directory" |
86 | 91 | ||
@@ -99,40 +104,49 @@ readInputTree (AnchoredFSNode _ File{}) = | |||
99 | throw $ AssertionFailed "Input directory is a file" | 104 | throw $ AssertionFailed "Input directory is a file" |
100 | readInputTree (AnchoredFSNode anchor root@Dir{}) = mkDirNode root | 105 | readInputTree (AnchoredFSNode anchor root@Dir{}) = mkDirNode root |
101 | where | 106 | where |
102 | mkInputNode :: FSNode -> IO (Maybe InputTree) | 107 | mkInputNode :: Map.Map FileName FSNode -> FSNode -> IO (Maybe InputTree) |
103 | mkInputNode file@File{path} | 108 | mkInputNode dir file@File{path} | not (isSidecar file) && not (isThumbnail file) = |
104 | | not (isSidecar file) && not (isThumbnail file) = | 109 | do |
105 | do | 110 | sidecar <- readSidecarFile $ localPath (anchor /> path <.> sidecarExt) |
106 | sidecar <- readSidecarFile $ localPath (anchor /> path <.> sidecarExt) | 111 | modTime <- getModificationTime $ localPath (anchor /> path) |
107 | modTime <- getModificationTime $ localPath (anchor /> path) | 112 | let thumbnail = findFileThumbnail (fromMaybe "" $ fileName path) dir |
108 | return $ Just $ InputFile path modTime sidecar | 113 | return $ Just $ InputFile path modTime sidecar thumbnail |
109 | mkInputNode File{} = return Nothing | 114 | mkInputNode _ File{} = return Nothing |
110 | mkInputNode dir@Dir{} = Just <$> mkDirNode dir | 115 | mkInputNode _ dir@Dir{} = Just <$> mkDirNode dir |
111 | 116 | ||
112 | mkDirNode :: FSNode -> IO InputTree | 117 | mkDirNode :: FSNode -> IO InputTree |
113 | mkDirNode File{} = throw $ AssertionFailed "Input directory is a file" | 118 | mkDirNode File{} = throw $ AssertionFailed "Input directory is a file" |
114 | mkDirNode Dir{path, items} = | 119 | mkDirNode Dir{path, items} = |
115 | do | 120 | do |
116 | dirItems <- mapM mkInputNode items | 121 | dirItems <- mapM (mkInputNode $ Map.fromList (map withBaseName items)) items |
117 | modTime <- getModificationTime $ localPath (anchor /> path) | 122 | modTime <- getModificationTime $ localPath (anchor /> path) |
118 | sidecar <- readSidecarFile $ localPath (anchor /> path </> dirSidecar) | 123 | sidecar <- readSidecarFile $ localPath (anchor /> path </> dirSidecar) |
119 | return $ InputDir path modTime sidecar (findThumbnail items) (catMaybes dirItems) | 124 | return $ InputDir path modTime sidecar (findDirThumbnail items) (catMaybes dirItems) |
125 | |||
126 | withBaseName :: FSNode -> (FileName, FSNode) | ||
127 | withBaseName node = (fromMaybe "" $ baseName $ Files.path node, node) | ||
128 | |||
129 | findFileThumbnail :: FileName -> Map.Map FileName FSNode -> Maybe Path | ||
130 | findFileThumbnail name dict = Files.path <$> Map.lookup (name ++ thumbnailSuffix) dict | ||
120 | 131 | ||
121 | isSidecar :: FSNode -> Bool | 132 | isSidecar :: FSNode -> Bool |
122 | isSidecar Dir{} = False | 133 | isSidecar Dir{} = False |
123 | isSidecar File{path} = | 134 | isSidecar File{path} = fileName path & maybe False (isExtensionOf sidecarExt) |
124 | fileName path | 135 | |
125 | & maybe False (isExtensionOf sidecarExt) | 136 | baseName :: Path -> Maybe FileName |
137 | baseName = fmap dropExtension . fileName | ||
126 | 138 | ||
127 | isThumbnail :: FSNode -> Bool | 139 | isThumbnail :: FSNode -> Bool |
128 | isThumbnail Dir{} = False | 140 | isThumbnail Dir{} = False |
129 | isThumbnail File{path} = | 141 | isThumbnail File{path} = baseName path & maybe False (thumbnailSuffix `isSuffixOf`) |
130 | fileName path | 142 | |
131 | & fmap dropExtension | 143 | isDirThumbnail :: FSNode -> Bool |
132 | & maybe False (dirPropFile ==) | 144 | isDirThumbnail Dir{} = False |
145 | isDirThumbnail File{path} = baseName path & (== Just thumbnailSuffix) | ||
146 | |||
147 | findDirThumbnail :: [FSNode] -> Maybe Path | ||
148 | findDirThumbnail = fmap Files.path . find isDirThumbnail | ||
133 | 149 | ||
134 | findThumbnail :: [FSNode] -> Maybe Path | ||
135 | findThumbnail = fmap Files.path . find isThumbnail | ||
136 | 150 | ||
137 | -- | Filters an InputTree. The root is always returned. | 151 | -- | Filters an InputTree. The root is always returned. |
138 | filterInputTree :: (InputTree -> Bool) -> InputTree -> InputTree | 152 | filterInputTree :: (InputTree -> Bool) -> InputTree -> InputTree |