diff options
Diffstat (limited to 'compiler/src/Compiler.hs')
-rw-r--r-- | compiler/src/Compiler.hs | 96 |
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 | |||
25 | module Compiler | ||
26 | ( compileGallery | ||
27 | ) where | ||
28 | |||
29 | |||
30 | import Control.Monad | ||
31 | import Data.Function ((&)) | ||
32 | import Data.Ord (comparing) | ||
33 | import Data.List (sortBy, length) | ||
34 | import System.Directory (createDirectoryIfMissing, removePathForcibly) | ||
35 | import System.FilePath (dropFileName, (</>)) | ||
36 | |||
37 | import Data.Aeson (ToJSON) | ||
38 | import qualified Data.Aeson as JSON | ||
39 | |||
40 | import Config | ||
41 | import Files (FileName, readDirectory, localPath, isHidden, nodeName, filterDir, flattenDir, root, (/>), ensureParentDir) | ||
42 | import Input (decodeYamlFile, readInputTree) | ||
43 | import Resource (ResourceTree, buildResourceTree, outputDiff) | ||
44 | import Gallery (buildGalleryTree) | ||
45 | import Processors | ||
46 | |||
47 | |||
48 | itemsDir :: String | ||
49 | itemsDir = "items" | ||
50 | |||
51 | thumbnailsDir :: String | ||
52 | thumbnailsDir = "thumbnails" | ||
53 | |||
54 | |||
55 | compileGallery :: FilePath -> FilePath -> IO () | ||
56 | compileGallery 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 | ||