gallery/make-pages/Depend.hs

126 lines
3.8 KiB
Haskell
Raw Normal View History

2020-07-16 10:07:28 -04:00
module Depend
(dependSingle, dependSingle',
dependGallery, dependGallery',
2022-05-16 04:25:16 -04:00
thumbFile, pageFile, bigFile)
2020-07-16 10:07:28 -04:00
where
2022-01-03 14:45:55 -05:00
import BuilderQQ hiding (CanBuild (..))
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!)
-> FilePath -- ^ index file name
2020-07-15 14:10:09 -04:00
-> Info
-> FilePath -- ^ output prefix
-> FilePath -- ^ build dir
-> Bool -- ^ include nsfw?
-> Text
dependSingle yamlDir indexFile info prefix build nsfw =
toLazyText $ dependSingle' yamlDir indexFile info prefix build nsfw
2020-07-16 10:07:28 -04:00
dependSingle' :: FilePath -> FilePath -> Info -> FilePath -> FilePath -> Bool
-> Builder
dependSingle' yamlDir indexFile info prefix build nsfw =
[b|$page: $deps $indexFile $$(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
2023-01-15 15:39:03 -05:00
paths = map #path images
thumb = thumbFile $ thumbnail info
jxls fs = [base <.> "jxl" | f <- fs,
let (base, ext) = splitExtension f,
ext `elem` imgTypes]
imgTypes = words ".png .jpg .gif"
imgFiles0 = thumb : map pageFile paths ++ map bigFile paths
imgFiles = imgFiles0 ++ jxls imgFiles0
2020-08-04 18:52:39 -04:00
dls = mapMaybe #download images
extras = #extras info
2020-07-25 21:03:55 -04:00
dir = build </> prefix </> yamlDir
page = dir </> "index.html"
2023-01-15 15:39:03 -05:00
deps = unwords $ map (dir </>) $ imgFiles ++ 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 indexFile filters build data_ tmp
2020-07-25 21:03:55 -04:00
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
-> FilePath -- ^ index file
2020-07-16 10:07:28 -04:00
-> GalleryFilters
-> FilePath -- ^ build dir
-> FilePath -- ^ data dir
-> FilePath -- ^ tmp dir
-> Builder
makeRules prefix indexFile filters build data_ tmp = [b|@0
$buildPrefix/%/index.html: $data_/%/info.yaml $$(MAKEPAGES)
$$(call single,$data_,$prefix,$indexFile,$flags)
2020-07-16 10:07:28 -04:00
$tmpPrefix/%.mk: $data_/%/info.yaml $$(MAKEPAGES)
$$(call depend-single,$prefix,$indexFile,$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