aboutsummaryrefslogtreecommitdiff
path: root/compiler
diff options
context:
space:
mode:
authorpacien2020-06-13 03:41:39 +0200
committerpacien2020-06-16 18:23:01 +0200
commitce2210e6deff1d981186b6d7ddb1176f27e41f49 (patch)
tree63d83660e733f16ef18d48debbc97c091c492c1f /compiler
parent34b90f08a21fbe3f1928e16a8ea48f1fc7453e4e (diff)
downloadldgallery-ce2210e6deff1d981186b6d7ddb1176f27e41f49.tar.gz
compiler: make GalleryIndex loadable from JSON
Diffstat (limited to 'compiler')
-rw-r--r--compiler/src/Compiler.hs4
-rw-r--r--compiler/src/Config.hs2
-rw-r--r--compiler/src/Files.hs14
-rw-r--r--compiler/src/Resource.hs28
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 ((</>))
29import qualified System.FilePath.Glob as Glob 29import qualified System.FilePath.Glob as Glob
30import System.Directory (canonicalizePath) 30import System.Directory (canonicalizePath)
31 31
32import Data.Aeson (ToJSON) 32import Data.Aeson (ToJSON, FromJSON)
33import qualified Data.Aeson as JSON 33import qualified Data.Aeson as JSON
34 34
35import Config 35import Config
@@ -64,7 +64,7 @@ thumbnailsDir = "thumbnails"
64data GalleryIndex = GalleryIndex 64data 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
70writeJSON :: ToJSON a => FileName -> a -> IO () 70writeJSON :: 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
84data ViewerConfig = ViewerConfig 84data 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
89viewerConfig :: GalleryConfig -> ViewerConfig 89viewerConfig :: GalleryConfig -> ViewerConfig
90viewerConfig GalleryConfig{galleryTitle, tagCategories} = ViewerConfig galleryTitle tagCategories 90viewerConfig 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
31import Data.List (isPrefixOf, length, sortOn) 31import Data.List (isPrefixOf, length, sortOn)
32import Data.Function ((&)) 32import Data.Function ((&))
33import Data.Functor ((<&>)) 33import Data.Functor ((<&>))
34import Data.Text (pack) 34import Data.Text (pack, unpack)
35import Data.Aeson (ToJSON) 35import Data.Aeson (ToJSON, FromJSON)
36import qualified Data.Aeson as JSON 36import qualified Data.Aeson as JSON
37 37
38import System.Directory 38import System.Directory
@@ -59,8 +59,11 @@ newtype Path = Path [FileName] deriving Show
59instance ToJSON Path where 59instance ToJSON Path where
60 toJSON = JSON.String . pack . webPath 60 toJSON = JSON.String . pack . webPath
61 61
62instance FromJSON Path where
63 parseJSON = JSON.withText "Path" (return . fromWebPath . unpack)
64
62instance Eq Path where 65instance 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
95webPath :: Path -> WebPath 98webPath :: Path -> WebPath
96webPath (Path path) = System.FilePath.Posix.joinPath $ reverse path 99webPath (Path path) = System.FilePath.Posix.joinPath $ reverse path
97 100
101fromWebPath :: WebPath -> Path
102fromWebPath = Path . reverse . System.FilePath.Posix.splitDirectories
103
98 104
99data FSNode = 105data 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)
31import Data.Function ((&)) 31import Data.Function ((&))
32import Data.Functor ((<&>)) 32import Data.Functor ((<&>))
33import qualified Data.Set as Set 33import qualified Data.Set as Set
34import Data.Text (pack) 34import Data.Text (pack, unpack, breakOn)
35import Data.Time.Clock (UTCTime) 35import Data.Time.Clock (UTCTime)
36import Data.Time.LocalTime (ZonedTime, utc, utcToZonedTime, zonedTimeToUTC) 36import Data.Time.LocalTime (ZonedTime, utc, utcToZonedTime, zonedTimeToUTC)
37import Data.Time.Format (formatTime, defaultTimeLocale) 37import Data.Time.Format (formatTime, parseTimeM, defaultTimeLocale)
38import Safe.Foldable (maximumByMay) 38import Safe.Foldable (maximumByMay)
39 39
40import GHC.Generics (Generic) 40import GHC.Generics (Generic)
41import Data.Aeson (ToJSON, genericToJSON, genericToEncoding) 41import Data.Aeson (ToJSON, FromJSON, genericToJSON, genericToEncoding, genericParseJSON)
42import qualified Data.Aeson as JSON 42import qualified Data.Aeson as JSON
43 43
44import Files 44import 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
73instance 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
74data GalleryItemProps = 81data 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
97instance FromJSON GalleryItemProps where
98 parseJSON = genericParseJSON encodingOptions
99
90 100
91data Thumbnail = Thumbnail 101data 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
96instance ToJSON Thumbnail where
97 toJSON = genericToJSON encodingOptions
98 toEncoding = genericToEncoding encodingOptions
99 105
100 106
101data GalleryItem = GalleryItem 107data 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
111instance ToJSON GalleryItem where
112 toJSON = genericToJSON encodingOptions
113 toEncoding = genericToEncoding encodingOptions
114 116
115 117
116type ItemProcessor = Path -> IO GalleryItemProps 118type ItemProcessor = Path -> IO GalleryItemProps