aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/Compiler.hs
diff options
context:
space:
mode:
authorpacien2019-12-27 10:08:19 +0100
committerpacien2019-12-27 10:08:19 +0100
commiteb7a652b2244ffa4dd5ba2440b7879127e7c6078 (patch)
tree71ab010b20a0f8d9f4a99179b68a7a12c081531d /compiler/src/Compiler.hs
parentaead07929e6ed13375b86539b1679a88993c9cf5 (diff)
downloadldgallery-eb7a652b2244ffa4dd5ba2440b7879127e7c6078.tar.gz
compiler: implement resource processing
but break directory cleanup
Diffstat (limited to 'compiler/src/Compiler.hs')
-rw-r--r--compiler/src/Compiler.hs96
1 files changed, 96 insertions, 0 deletions
diff --git a/compiler/src/Compiler.hs b/compiler/src/Compiler.hs
new file mode 100644
index 0000000..9767394
--- /dev/null
+++ b/compiler/src/Compiler.hs
@@ -0,0 +1,96 @@
1-- ldgallery - A static generator which turns a collection of tagged
2-- pictures into a searchable web gallery.
3--
4-- Copyright (C) 2019 Pacien TRAN-GIRARD
5--
6-- This program is free software: you can redistribute it and/or modify
7-- it under the terms of the GNU Affero General Public License as
8-- published by the Free Software Foundation, either version 3 of the
9-- License, or (at your option) any later version.
10--
11-- This program is distributed in the hope that it will be useful,
12-- but WITHOUT ANY WARRANTY; without even the implied warranty of
13-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-- GNU Affero General Public License for more details.
15--
16-- You should have received a copy of the GNU Affero General Public License
17-- along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19{-# LANGUAGE
20 DuplicateRecordFields
21 , DeriveGeneric
22 , DeriveAnyClass
23#-}
24
25module Compiler
26 ( compileGallery
27 ) where
28
29
30import Control.Monad
31import Data.Function ((&))
32import Data.Ord (comparing)
33import Data.List (sortBy, length)
34import System.Directory (createDirectoryIfMissing, removePathForcibly)
35import System.FilePath (dropFileName, (</>))
36
37import Data.Aeson (ToJSON)
38import qualified Data.Aeson as JSON
39
40import Config
41import Files (FileName, readDirectory, localPath, isHidden, nodeName, filterDir, flattenDir, root, (/>), ensureParentDir)
42import Input (decodeYamlFile, readInputTree)
43import Resource (ResourceTree, buildResourceTree, outputDiff)
44import Gallery (buildGalleryTree)
45import Processors
46
47
48itemsDir :: String
49itemsDir = "items"
50
51thumbnailsDir :: String
52thumbnailsDir = "thumbnails"
53
54
55compileGallery :: FilePath -> FilePath -> IO ()
56compileGallery inputDirPath outputDirPath =
57 do
58 config <- readConfig (inputDirPath </> "gallery.yaml")
59 inputDir <- readDirectory inputDirPath
60
61 let isGalleryFile = \n -> nodeName n == "gallery.yaml"
62 let galleryTree = filterDir (liftM2 (&&) (not . isGalleryFile) (not . isHidden)) inputDir
63
64 inputTree <- readInputTree galleryTree
65
66 let dirProc = dirFileProcessor inputDirPath outputDirPath itemsDir
67 let itemProc = itemFileProcessor Nothing skipCached inputDirPath outputDirPath itemsDir
68 let thumbnailProc = thumbnailFileProcessor (Resolution 150 50) skipCached inputDirPath outputDirPath thumbnailsDir
69 resourceTree <- buildResourceTree dirProc itemProc thumbnailProc inputTree
70
71 putStrLn "\nRESOURCE TREE"
72 putStrLn (show resourceTree)
73
74 --cleanup resourceTree outputDirPath
75
76 buildGalleryTree resourceTree
77 & ensureParentDir JSON.encodeFile (outputDirPath </> "index.json")
78
79 viewer config
80 & ensureParentDir JSON.encodeFile (outputDirPath </> "viewer.json")
81
82 where
83 -- TODO: delete all files, then only non-empty dirs
84 cleanup :: ResourceTree -> FileName -> IO ()
85 cleanup resourceTree outputDir =
86 readDirectory outputDir
87 >>= return . outputDiff resourceTree . root
88 >>= return . sortBy (flip $ comparing length) -- nested files before dirs
89 >>= return . map (localPath . (/>) outputDir)
90 >>= mapM_ remove
91
92 remove :: FileName -> IO ()
93 remove path =
94 do
95 putStrLn $ "Removing: " ++ path
96 removePathForcibly path