gallery/make-pages/Depend.hs

123 lines
3.7 KiB
Haskell

module Depend
(dependSingle, dependSingle',
dependGallery, dependGallery',
thumbFile, pageFile, bigFile)
where
import BuilderQQ hiding (CanBuild (..))
import Info hiding (Text)
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Text.Lazy (Text)
import Data.Foldable
import System.FilePath
dependSingle :: FilePath -- ^ yaml file name (relative to data dir!)
-> FilePath -- ^ index file name
-> 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
dependSingle' :: FilePath -> FilePath -> Info -> FilePath -> FilePath -> Bool
-> Builder
dependSingle' yamlDir indexFile info prefix build nsfw =
[b|$page: $deps $indexFile $$(MAKEPAGES)|]
where
images =
maybe [] (toList . #all) $
if nsfw then Just $ #images info else #sfwImages info
paths = map #path images
dls = mapMaybe #download images
extras = #extras info
dir = build </> prefix </> yamlDir
page = dir </> "index.html"
deps = unwords $ map (dir </>) $
thumbFile (thumbnail info) :
map pageFile paths ++
map bigFile paths ++
dls ++ extras
dependGallery :: GalleryInfo
-> FilePath -- ^ index file
-> [(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
dependGallery' :: GalleryInfo -> FilePath -> [(FilePath, Info)]
-> FilePath -> FilePath -> FilePath -> Builder
dependGallery' (GalleryInfo {prefix, filters})
indexFile infos' build data_ tmp = [b|@0
$index: $gallery
$gallery: $pages' $files' $rss $indexFile $$(MAKEPAGES)
$$(call gallery,$indexFile,$prefix)
$rss: $files' $indexFile $$(MAKEPAGES)
$$(call rss,$indexFile,$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 indexFile 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|]
makeRules :: FilePath -- ^ prefix
-> FilePath -- ^ index file
-> 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)
$tmpPrefix/%.mk: $data_/%/info.yaml $$(MAKEPAGES)
$$(call depend-single,$prefix,$indexFile,$build,$data_,$flags)
$buildPrefix/%: $tmp/%
$$(call copy,-l)
$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