aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/Input.hs
blob: 6ed7471ba476d77bff30935ecd8fc101a5ad08a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- ldgallery - A static generator which turns a collection of tagged
--             pictures into a searchable web gallery.
--
-- Copyright (C) 2019-2020  Pacien TRAN-GIRARD
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program.  If not, see <https://www.gnu.org/licenses/>.

module Input
  ( decodeYamlFile
  , Sidecar(..)
  , InputTree(..), readInputTree, filterInputTree
  ) where


import GHC.Generics (Generic)
import Control.Exception (Exception, AssertionFailed(..), throw, throwIO)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Function ((&))
import Data.Maybe (catMaybes)
import Data.Bool (bool)
import Data.List (find)
import Data.Time.Clock (UTCTime)
import Data.Time.LocalTime (ZonedTime)
import Data.Yaml (ParseException, decodeFileEither)
import Data.Aeson (FromJSON)
import System.FilePath (isExtensionOf, dropExtension)
import System.Directory (doesFileExist, getModificationTime)

import Files


data LoadException = LoadException String ParseException deriving Show
instance Exception LoadException

decodeYamlFile :: (MonadIO m, FromJSON a) => FileName -> m a
decodeYamlFile path =
  liftIO $ Data.Yaml.decodeFileEither path
  >>= either (throwIO . LoadException path) return


-- | Tree representing the input from the input directory.
data InputTree =
    InputFile
      { path :: Path
      , modTime :: UTCTime
      , sidecar :: Sidecar }
    | InputDir
      { path :: Path
      , modTime :: UTCTime
      , sidecar :: Sidecar
      , dirThumbnailPath :: Maybe Path
      , items :: [InputTree] }
  deriving Show

data Sidecar = Sidecar
  { title :: Maybe String
  , datetime :: Maybe ZonedTime
  , description :: Maybe String
  , tags :: Maybe [String]
  } deriving (Generic, FromJSON, Show)

emptySidecar :: Sidecar
emptySidecar = Sidecar
  { title = Nothing
  , datetime = Nothing
  , description = Nothing
  , tags = Nothing }

sidecarExt :: String
sidecarExt = "yaml"

dirPropFile :: String
dirPropFile = "_directory"

dirSidecar :: Path
dirSidecar = Path [dirPropFile] <.> sidecarExt

readSidecarFile :: FilePath -> IO Sidecar
readSidecarFile filepath =
  doesFileExist filepath
  >>= bool (return Nothing) (decodeYamlFile filepath)
  >>= return . maybe emptySidecar id


readInputTree :: AnchoredFSNode -> IO InputTree
readInputTree (AnchoredFSNode _ File{}) =
  throw $ AssertionFailed "Input directory is a file"
readInputTree (AnchoredFSNode anchor root@Dir{}) = mkDirNode root
  where
    mkInputNode :: FSNode -> IO (Maybe InputTree)
    mkInputNode file@File{path}
      | (not $ isSidecar file) && (not $ isThumbnail file) =
        do
          sidecar <- readSidecarFile $ localPath (anchor /> path <.> sidecarExt)
          modTime <- getModificationTime $ localPath (anchor /> path)
          return $ Just $ InputFile path modTime sidecar
    mkInputNode File{} = return Nothing
    mkInputNode dir@Dir{} = mkDirNode dir >>= return . Just

    mkDirNode :: FSNode -> IO InputTree
    mkDirNode File{} = throw $ AssertionFailed "Input directory is a file"
    mkDirNode Dir{path, items} =
      do
        dirItems <- mapM mkInputNode items
        modTime <- getModificationTime $ localPath (anchor /> path)
        sidecar <- readSidecarFile $ localPath (anchor /> path </> dirSidecar)
        return $ InputDir path modTime sidecar (findThumbnail items) (catMaybes dirItems)

    isSidecar :: FSNode -> Bool
    isSidecar Dir{} = False
    isSidecar File{path} =
      fileName path
      & (maybe False $ isExtensionOf sidecarExt)

    isThumbnail :: FSNode -> Bool
    isThumbnail Dir{} = False
    isThumbnail File{path} =
      fileName path
      & fmap dropExtension
      & (maybe False (dirPropFile ==))

    findThumbnail :: [FSNode] -> Maybe Path
    findThumbnail = (fmap Files.path) . (find isThumbnail)

-- | Filters an InputTree. The root is always returned.
filterInputTree :: (InputTree -> Bool) -> InputTree -> InputTree
filterInputTree cond = filterNode
  where
    filterNode :: InputTree -> InputTree
    filterNode inputFile@InputFile{} = inputFile
    filterNode inputDir@InputDir{items} =
        filter cond items
      & map filterNode
      & \curatedItems -> inputDir { items = curatedItems } :: InputTree