gallery/make-pages/Depend.hs

115 lines
3.4 KiB
Haskell
Raw Normal View History

2020-07-16 10:07:28 -04:00
module Depend
(dependSingle, dependSingle',
dependGallery, dependGallery',
thumbFile, pageFile)
where
2020-07-15 15:31:46 -04:00
import BuilderQQ
import Info hiding (Text)
2020-07-15 14:09:14 -04:00
2020-07-21 19:48:29 -04:00
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Text.Lazy (Text)
2020-07-15 14:09:14 -04:00
import System.FilePath
2020-07-15 14:10:09 -04:00
dependSingle :: FilePath -- ^ yaml file name (relative to data dir!)
-> Info
-> FilePath -- ^ output prefix
-> FilePath -- ^ build dir
-> Bool -- ^ include nsfw?
-> Text
2020-07-16 10:07:28 -04:00
dependSingle yamlDir info prefix build nsfw =
toLazyText $ dependSingle' yamlDir info prefix build nsfw
dependSingle' :: FilePath -> Info -> FilePath -> FilePath -> Bool -> Builder
dependSingle' yamlDir info prefix build nsfw =
[b|$page: $deps $$(MAKEPAGES)|]
2020-07-25 21:03:55 -04:00
where
2021-08-23 10:30:11 -04:00
images = #all if nsfw then #images info else #sfwImages info
2020-07-25 21:03:55 -04:00
2020-08-04 18:52:39 -04:00
paths = map #path images
dls = mapMaybe #download images
extras = #extras info
2020-07-25 21:03:55 -04:00
dir = build </> prefix </> yamlDir
page = dir </> "index.html"
deps = unwords $ map (dir </>) $
2020-08-04 18:52:39 -04:00
thumbFile (thumbnail info) : map pageFile paths ++ paths ++ dls ++ extras
2020-07-16 10:07:28 -04:00
dependGallery :: GalleryInfo
-> FilePath -- ^ index file
2020-07-16 10:07:28 -04:00
-> [(FilePath, Info)] -- ^ relative to data dir
-> FilePath -- ^ build dir
-> FilePath -- ^ data dir
-> FilePath -- ^ tmp dir
-> Text
dependGallery ginfo index infos build data_ tmp =
toLazyText $ dependGallery' ginfo index infos build data_ tmp
2020-07-16 10:07:28 -04:00
dependGallery' :: GalleryInfo -> FilePath -> [(FilePath, Info)]
2020-07-16 10:07:28 -04:00
-> FilePath -> FilePath -> FilePath -> Builder
dependGallery' (GalleryInfo {prefix, filters})
indexFile infos' build data_ tmp = [b|@0
$index: $gallery
2020-07-25 21:03:55 -04:00
$gallery: $pages' $files' $rss $indexFile $$(MAKEPAGES)
$$(call gallery,$indexFile,$prefix)
2020-07-16 10:07:28 -04:00
$rss: $files' $indexFile $$(MAKEPAGES)
$$(call rss,$indexFile,$prefix,$data_)
2020-07-19 12:04:40 -04:00
2020-07-16 10:07:28 -04:00
$rules
$incs
|]
where
2020-07-25 21:03:55 -04:00
infos = filter (matchFilters filters . #second) infos'
files = map #first infos
files' = unwords $ map (data_ </>) files
page d = build </> prefix </> takeDirectory d </> "index.html"
pages = map page files
pages' = unwords pages
index = build </> "index.html"
gallery = build </> prefix </> "index.html"
rss = build </> prefix </> "rss.xml"
rules = makeRules prefix filters build data_ tmp
inc d = tmp </> prefix </> takeDirectory d <.> "mk"
incFiles = unwords $ map inc files
incs = if null infos then "" else [b|include $incFiles|]
2020-07-25 21:03:55 -04:00
2020-07-16 10:07:28 -04:00
makeRules :: FilePath -- ^ prefix
-> GalleryFilters
-> FilePath -- ^ build dir
-> FilePath -- ^ data dir
-> FilePath -- ^ tmp dir
-> Builder
makeRules prefix filters build data_ tmp = [b|@0
$buildPrefix/%/index.html: $data_/%/info.yaml $$(MAKEPAGES)
$$(call single,$data_,$prefix,$flags)
2020-07-16 10:07:28 -04:00
$tmpPrefix/%.mk: $data_/%/info.yaml $$(MAKEPAGES)
$$(call depend-single,$prefix,$build,$data_,$flags)
2020-07-24 09:20:10 -04:00
$buildPrefix/%: $tmp/%
2020-07-31 23:09:23 -04:00
$$(call copy,-l)
2020-07-16 10:07:28 -04:00
$buildPrefix/%: $data_/%
2020-07-24 09:20:10 -04:00
$$(call copy)
2020-07-16 10:07:28 -04:00
|]
where
buildPrefix = build </> prefix
tmpPrefix = tmp </> prefix
flags = filtersToFlags filters
2020-07-16 10:07:28 -04:00
filtersToFlags :: GalleryFilters -> Builder
filtersToFlags (GalleryFilters {nsfw}) =
2020-07-24 19:10:52 -04:00
case nsfw of NoNsfw -> ""; _ -> "-n"
thumbnail :: Info -> FilePath
2020-07-15 14:07:51 -04:00
thumbnail = fromMaybe (error "no thumbnail or sfw images") . #thumb