gallery/make-pages/Depend.hs

124 lines
3.8 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 =
let dir = build </> prefix </> yamlDir
2020-07-16 05:47:02 -04:00
images = if nsfw then #images info else #sfwImages info
paths = map #path images
2020-07-21 19:48:29 -04:00
dls = mapMaybe #download images
index = dir </> "index.html"
2020-07-21 19:48:29 -04:00
deps = thumbFile (thumbnail info) : map pageFile paths ++ paths ++ dls
2020-07-15 14:09:14 -04:00
deps' = unwords $ map (dir </>) deps
in
2020-07-19 11:56:46 -04:00
[b|$@index: $@deps' $$(MAKEPAGES)|]
2020-07-16 10:07:28 -04:00
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
2020-07-19 12:04:40 -04:00
dependGallery' (GalleryInfo {title, description, prefix, filters})
infos' build data_ tmp =
let infos = filter (matchFilters filters . #second) infos'
files = map #first infos
2020-07-16 10:07:28 -04:00
files' = unwords $ map (data_ </>) files
page d = build </> prefix </> takeDirectory d </> "index.html"
2020-07-19 12:04:40 -04:00
pages = map page files
2020-07-16 10:07:28 -04:00
pages' = unwords pages
path = build </> prefix </> "index.html"
2020-07-19 12:04:40 -04:00
rss = build </> prefix </> "rss.xml"
2020-07-16 10:07:28 -04:00
rules = makeRules prefix filters build data_ tmp
2020-07-18 05:35:42 -04:00
inc d = tmp </> prefix </> takeDirectory d <.> "mk"
2020-07-16 10:07:28 -04:00
incs = if null infos then "" else
"include " <> fromString (unwords $ map inc files)
index = build </> "index.html"
in [b|@0
$@index: $@path
2020-07-19 11:56:46 -04:00
$@path: $@pages' $@files' $@rss $$(MAKEPAGES)
2020-07-24 09:20:10 -04:00
$$(call gallery,$title',$@prefix,$flags)
2020-07-16 10:07:28 -04:00
2020-07-19 12:04:40 -04:00
$@rss: $@files' $$(MAKEPAGES)
2020-07-24 09:20:10 -04:00
$$(call rss,$title',$description',$@prefix,$@data_)
2020-07-19 12:04:40 -04:00
2020-07-16 10:07:28 -04:00
$rules
$incs
|]
where
flags = filtersToFlags filters
2020-07-24 09:20:10 -04:00
title' = substComma title
description' = substComma description
substComma = textMap \case ',' -> "$(comma)"; c -> fromChar c
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
2020-07-19 11:56:46 -04:00
$@buildPrefix/%/index.html: $@data_/%/info.yaml $$(MAKEPAGES)
2020-07-24 09:20:10 -04:00
$$(call single,$@data_,$flags)
2020-07-16 10:07:28 -04:00
2020-07-19 11:56:46 -04:00
$@tmpPrefix/%.mk: $@data_/%/info.yaml $$(MAKEPAGES)
2020-07-24 09:20:10 -04:00
$$(call depend-single,$@prefix,$@build,$@data_,$flags)
$@buildPrefix/%: $@tmp/%
$$(call copy)
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}) =
case nsfw of Just False -> ""; _ -> "-n"
thumbnail :: Info -> FilePath
2020-07-15 14:07:51 -04:00
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
2020-07-18 05:43:10 -04:00
pageFile f
| takeExtension f == ".gif" = f
| otherwise = addSuffix "_med" f