gallery/make-pages/Depend.hs
2021-04-16 23:48:55 +02:00

131 lines
3.7 KiB
Haskell

module Depend
(dependSingle, dependSingle',
dependGallery, dependGallery',
thumbFile, pageFile)
where
import BuilderQQ
import Info hiding (Text)
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Text.Lazy (Text)
import System.FilePath
dependSingle :: FilePath -- ^ yaml file name (relative to data dir!)
-> Info
-> FilePath -- ^ output prefix
-> FilePath -- ^ build dir
-> Bool -- ^ include nsfw?
-> Text
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)|]
where
images = if nsfw then #images info else #sfwImages info
paths = map #path images
dls = mapMaybe #download images
dir = build </> prefix </> yamlDir
page = dir </> "index.html"
deps = unwords $ map (dir </>) $
thumbFile (thumbnail info) : map pageFile paths ++ paths ++ dls
dependGallery :: GalleryInfo
-> [(FilePath, Info)] -- ^ relative to data dir
-> FilePath -- ^ build dir
-> FilePath -- ^ data dir
-> FilePath -- ^ tmp dir
-> Text
dependGallery ginfo infos build data_ tmp =
toLazyText $ dependGallery' ginfo infos build data_ tmp
dependGallery' :: GalleryInfo -> [(FilePath, Info)]
-> FilePath -> FilePath -> FilePath -> Builder
dependGallery' (GalleryInfo {title, desc, prefix, filters})
infos' build data_ tmp = [b|@0
$@index: $@gallery
$@gallery: $@pages' $@files' $@rss $$(MAKEPAGES)
$$(call gallery,$title',$@prefix,$flags)
$@rss: $@files' $$(MAKEPAGES)
$$(call rss,$title',$desc',$@prefix,$@data_)
$rules
$incs
|]
where
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|]
flags = filtersToFlags filters
title' = substComma title
desc' = substComma desc
substComma = textMap \case ',' -> "$(comma)"; c -> fromChar c
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_,$flags)
$@tmpPrefix/%.mk: $@data_/%/info.yaml $$(MAKEPAGES)
$$(call depend-single,$@prefix,$@build,$@data_,$flags)
$@buildPrefix/%: $@tmp/%
$$(call copy)
$@buildPrefix/%: $@data_/%
$$(call copy)
|]
where
buildPrefix = build </> prefix
tmpPrefix = tmp </> prefix
flags = filtersToFlags filters
filtersToFlags :: GalleryFilters -> Builder
filtersToFlags (GalleryFilters {nsfw}) =
case nsfw of NoNsfw -> ""; _ -> "-n"
thumbnail :: Info -> FilePath
thumbnail = fromMaybe (error "no thumbnail or sfw images") . #thumb
addSuffix :: String -> FilePath -> FilePath
addSuffix suf path =
let (pre, ext) = splitExtension path in
pre ++ suf ++ ext
thumbFile :: FilePath -> FilePath
thumbFile = addSuffix "_small"
pageFile :: FilePath -> FilePath
pageFile f
| takeExtension f == ".gif" = f
| otherwise = addSuffix "_med" f