gallery/make-pages/Depend.hs

146 lines
4.6 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-16 10:07:28 -04:00
echo "[gallery] "$$@
mkdir -p $$(dir $$@)
$$(MAKEPAGES) $$(MPFLAGS) gallery \
-p "$@prefix" -t "$*title" $flags -o "$$@" \
2020-07-17 06:29:13 -04:00
$$(filter $$(DATADIR)/%/$$(INFONAME),$$^)
2020-07-16 10:07:28 -04:00
2020-07-19 12:04:40 -04:00
$@rss: $@files' $$(MAKEPAGES)
echo "[rss] "$$@
mkdir -p $$(dir $$@)
$$(MAKEPAGES) $$(MPFLAGS) rss -t "$*title" \
-d "$*description" \
-R "$$(ROOT)" -p "$@prefix" \
-o "$$@" -D "$@data_" \
$$(filter $$(DATADIR)/%/$$(INFONAME),$$^)
2020-07-16 10:07:28 -04:00
$rules
$incs
|]
where
flags = filtersToFlags filters
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-17 06:29:13 -04:00
echo "[single] "$$@
2020-07-16 10:07:28 -04:00
mkdir -p $$(dir $$@)
2020-07-21 18:13:02 -04:00
$$(MAKEPAGES) $$(MPFLAGS) single -D "$@data_" "$$<" -o "$$@" $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-17 06:29:13 -04:00
echo "[deps] "$$@
2020-07-16 10:07:28 -04:00
mkdir -p $$(dir $$@)
2020-07-17 06:29:13 -04:00
$$(MAKEPAGES) $$(MPFLAGS) depend-single $flags \
2020-07-16 10:07:28 -04:00
-o "$$@" -p "$@prefix" -B "$@build" -D "$@data_" $$<
$@buildPrefix/%: $@data_/%
2020-07-17 06:29:13 -04:00
echo "[copy] "$$@
2020-07-16 10:07:28 -04:00
mkdir -p $$(dir $$@)
cp "$$<" "$$@"
$@buildPrefix/%_small.png: $@data_/%.png
2020-07-17 06:29:13 -04:00
echo "[resize] "$$@
2020-07-16 10:07:28 -04:00
mkdir -p $$(dir $$@)
convert -resize '$$(SMALL)x$$(SMALL)^' \
-gravity center -crop 1:1+0 "$$<" "$$@"
$@buildPrefix/%_med.png: $@data_/%.png
2020-07-17 06:29:13 -04:00
echo "[resize] "$$@
2020-07-16 10:07:28 -04:00
mkdir -p $$(dir $$@)
2020-07-18 18:11:42 -04:00
convert -resize '$$(MEDW)x$$(MEDH)>' "$$<" "$$@"
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