diff options
author | pacien | 2020-06-13 03:41:39 +0200 |
---|---|---|
committer | pacien | 2020-06-16 18:23:01 +0200 |
commit | ce2210e6deff1d981186b6d7ddb1176f27e41f49 (patch) | |
tree | 63d83660e733f16ef18d48debbc97c091c492c1f | |
parent | 34b90f08a21fbe3f1928e16a8ea48f1fc7453e4e (diff) | |
download | ldgallery-ce2210e6deff1d981186b6d7ddb1176f27e41f49.tar.gz |
compiler: make GalleryIndex loadable from JSON
-rw-r--r-- | compiler/src/Compiler.hs | 4 | ||||
-rw-r--r-- | compiler/src/Config.hs | 2 | ||||
-rw-r--r-- | compiler/src/Files.hs | 14 | ||||
-rw-r--r-- | compiler/src/Resource.hs | 28 |
4 files changed, 28 insertions, 20 deletions
diff --git a/compiler/src/Compiler.hs b/compiler/src/Compiler.hs index 2bb27f9..5a7632d 100644 --- a/compiler/src/Compiler.hs +++ b/compiler/src/Compiler.hs | |||
@@ -29,7 +29,7 @@ import System.FilePath ((</>)) | |||
29 | import qualified System.FilePath.Glob as Glob | 29 | import qualified System.FilePath.Glob as Glob |
30 | import System.Directory (canonicalizePath) | 30 | import System.Directory (canonicalizePath) |
31 | 31 | ||
32 | import Data.Aeson (ToJSON) | 32 | import Data.Aeson (ToJSON, FromJSON) |
33 | import qualified Data.Aeson as JSON | 33 | import qualified Data.Aeson as JSON |
34 | 34 | ||
35 | import Config | 35 | import Config |
@@ -64,7 +64,7 @@ thumbnailsDir = "thumbnails" | |||
64 | data GalleryIndex = GalleryIndex | 64 | data GalleryIndex = GalleryIndex |
65 | { properties :: ViewerConfig | 65 | { properties :: ViewerConfig |
66 | , tree :: GalleryItem | 66 | , tree :: GalleryItem |
67 | } deriving (Generic, Show, ToJSON) | 67 | } deriving (Generic, Show, ToJSON, FromJSON) |
68 | 68 | ||
69 | 69 | ||
70 | writeJSON :: ToJSON a => FileName -> a -> IO () | 70 | writeJSON :: ToJSON a => FileName -> a -> IO () |
diff --git a/compiler/src/Config.hs b/compiler/src/Config.hs index 3c38a17..afcfb36 100644 --- a/compiler/src/Config.hs +++ b/compiler/src/Config.hs | |||
@@ -84,7 +84,7 @@ readConfig = decodeYamlFile | |||
84 | data ViewerConfig = ViewerConfig | 84 | data ViewerConfig = ViewerConfig |
85 | { galleryTitle :: String | 85 | { galleryTitle :: String |
86 | , tagCategories :: [String] | 86 | , tagCategories :: [String] |
87 | } deriving (Generic, ToJSON, Show) | 87 | } deriving (Generic, ToJSON, FromJSON, Show) |
88 | 88 | ||
89 | viewerConfig :: GalleryConfig -> ViewerConfig | 89 | viewerConfig :: GalleryConfig -> ViewerConfig |
90 | viewerConfig GalleryConfig{galleryTitle, tagCategories} = ViewerConfig galleryTitle tagCategories | 90 | viewerConfig GalleryConfig{galleryTitle, tagCategories} = ViewerConfig galleryTitle tagCategories |
diff --git a/compiler/src/Files.hs b/compiler/src/Files.hs index 1f14e7f..023546b 100644 --- a/compiler/src/Files.hs +++ b/compiler/src/Files.hs | |||
@@ -20,7 +20,7 @@ module Files | |||
20 | ( FileName, LocalPath, WebPath, Path(..) | 20 | ( FileName, LocalPath, WebPath, Path(..) |
21 | , (</>), (</), (/>), (<.>) | 21 | , (</>), (</), (/>), (<.>) |
22 | , fileName, subPaths, pathLength | 22 | , fileName, subPaths, pathLength |
23 | , localPath, webPath | 23 | , localPath, webPath, fromWebPath |
24 | , FSNode(..), AnchoredFSNode(..) | 24 | , FSNode(..), AnchoredFSNode(..) |
25 | , nodeName, isHidden, flattenDir, filterDir | 25 | , nodeName, isHidden, flattenDir, filterDir |
26 | , readDirectory, copyTo | 26 | , readDirectory, copyTo |
@@ -31,8 +31,8 @@ module Files | |||
31 | import Data.List (isPrefixOf, length, sortOn) | 31 | import Data.List (isPrefixOf, length, sortOn) |
32 | import Data.Function ((&)) | 32 | import Data.Function ((&)) |
33 | import Data.Functor ((<&>)) | 33 | import Data.Functor ((<&>)) |
34 | import Data.Text (pack) | 34 | import Data.Text (pack, unpack) |
35 | import Data.Aeson (ToJSON) | 35 | import Data.Aeson (ToJSON, FromJSON) |
36 | import qualified Data.Aeson as JSON | 36 | import qualified Data.Aeson as JSON |
37 | 37 | ||
38 | import System.Directory | 38 | import System.Directory |
@@ -59,8 +59,11 @@ newtype Path = Path [FileName] deriving Show | |||
59 | instance ToJSON Path where | 59 | instance ToJSON Path where |
60 | toJSON = JSON.String . pack . webPath | 60 | toJSON = JSON.String . pack . webPath |
61 | 61 | ||
62 | instance FromJSON Path where | ||
63 | parseJSON = JSON.withText "Path" (return . fromWebPath . unpack) | ||
64 | |||
62 | instance Eq Path where | 65 | instance Eq Path where |
63 | (Path left) == (Path right) = left == right | 66 | left == right = webPath left == webPath right |
64 | 67 | ||
65 | (</>) :: Path -> Path -> Path | 68 | (</>) :: Path -> Path -> Path |
66 | (Path l) </> (Path r) = Path (r ++ l) | 69 | (Path l) </> (Path r) = Path (r ++ l) |
@@ -95,6 +98,9 @@ localPath (Path path) = System.FilePath.joinPath $ reverse path | |||
95 | webPath :: Path -> WebPath | 98 | webPath :: Path -> WebPath |
96 | webPath (Path path) = System.FilePath.Posix.joinPath $ reverse path | 99 | webPath (Path path) = System.FilePath.Posix.joinPath $ reverse path |
97 | 100 | ||
101 | fromWebPath :: WebPath -> Path | ||
102 | fromWebPath = Path . reverse . System.FilePath.Posix.splitDirectories | ||
103 | |||
98 | 104 | ||
99 | data FSNode = | 105 | data FSNode = |
100 | File | 106 | File |
diff --git a/compiler/src/Resource.hs b/compiler/src/Resource.hs index 607c7f6..fa139e0 100644 --- a/compiler/src/Resource.hs +++ b/compiler/src/Resource.hs | |||
@@ -31,14 +31,14 @@ import Data.Maybe (mapMaybe, fromMaybe) | |||
31 | import Data.Function ((&)) | 31 | import Data.Function ((&)) |
32 | import Data.Functor ((<&>)) | 32 | import Data.Functor ((<&>)) |
33 | import qualified Data.Set as Set | 33 | import qualified Data.Set as Set |
34 | import Data.Text (pack) | 34 | import Data.Text (pack, unpack, breakOn) |
35 | import Data.Time.Clock (UTCTime) | 35 | import Data.Time.Clock (UTCTime) |
36 | import Data.Time.LocalTime (ZonedTime, utc, utcToZonedTime, zonedTimeToUTC) | 36 | import Data.Time.LocalTime (ZonedTime, utc, utcToZonedTime, zonedTimeToUTC) |
37 | import Data.Time.Format (formatTime, defaultTimeLocale) | 37 | import Data.Time.Format (formatTime, parseTimeM, defaultTimeLocale) |
38 | import Safe.Foldable (maximumByMay) | 38 | import Safe.Foldable (maximumByMay) |
39 | 39 | ||
40 | import GHC.Generics (Generic) | 40 | import GHC.Generics (Generic) |
41 | import Data.Aeson (ToJSON, genericToJSON, genericToEncoding) | 41 | import Data.Aeson (ToJSON, FromJSON, genericToJSON, genericToEncoding, genericParseJSON) |
42 | import qualified Data.Aeson as JSON | 42 | import qualified Data.Aeson as JSON |
43 | 43 | ||
44 | import Files | 44 | import Files |
@@ -70,6 +70,13 @@ instance ToJSON Resource where | |||
70 | where | 70 | where |
71 | timestamp = formatTime defaultTimeLocale "%s" modTime | 71 | timestamp = formatTime defaultTimeLocale "%s" modTime |
72 | 72 | ||
73 | instance FromJSON Resource where | ||
74 | parseJSON = JSON.withText "Resource" (unpackRes . breakOn "?") | ||
75 | where | ||
76 | unpackRes (resPathStr, modTimeStr) = | ||
77 | Resource (fromWebPath $ unpack resPathStr) | ||
78 | <$> parseTimeM True defaultTimeLocale "?%s" (unpack modTimeStr) | ||
79 | |||
73 | 80 | ||
74 | data GalleryItemProps = | 81 | data GalleryItemProps = |
75 | Directory { items :: [GalleryItem] } | 82 | Directory { items :: [GalleryItem] } |
@@ -87,15 +94,14 @@ instance ToJSON GalleryItemProps where | |||
87 | toJSON = genericToJSON encodingOptions | 94 | toJSON = genericToJSON encodingOptions |
88 | toEncoding = genericToEncoding encodingOptions | 95 | toEncoding = genericToEncoding encodingOptions |
89 | 96 | ||
97 | instance FromJSON GalleryItemProps where | ||
98 | parseJSON = genericParseJSON encodingOptions | ||
99 | |||
90 | 100 | ||
91 | data Thumbnail = Thumbnail | 101 | data Thumbnail = Thumbnail |
92 | { resource :: Resource | 102 | { resource :: Resource |
93 | , resolution :: Resolution | 103 | , resolution :: Resolution |
94 | } deriving (Generic, Show) | 104 | } deriving (Generic, Show, ToJSON, FromJSON) |
95 | |||
96 | instance ToJSON Thumbnail where | ||
97 | toJSON = genericToJSON encodingOptions | ||
98 | toEncoding = genericToEncoding encodingOptions | ||
99 | 105 | ||
100 | 106 | ||
101 | data GalleryItem = GalleryItem | 107 | data GalleryItem = GalleryItem |
@@ -106,11 +112,7 @@ data GalleryItem = GalleryItem | |||
106 | , path :: Path | 112 | , path :: Path |
107 | , thumbnail :: Maybe Thumbnail | 113 | , thumbnail :: Maybe Thumbnail |
108 | , properties :: GalleryItemProps | 114 | , properties :: GalleryItemProps |
109 | } deriving (Generic, Show) | 115 | } deriving (Generic, Show, ToJSON, FromJSON) |
110 | |||
111 | instance ToJSON GalleryItem where | ||
112 | toJSON = genericToJSON encodingOptions | ||
113 | toEncoding = genericToEncoding encodingOptions | ||
114 | 116 | ||
115 | 117 | ||
116 | type ItemProcessor = Path -> IO GalleryItemProps | 118 | type ItemProcessor = Path -> IO GalleryItemProps |