gallery/make-pages/Depend.hs

131 lines
4 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
import Data.Maybe (fromMaybe)
import Data.Text.Lazy (Text)
2020-07-16 10:07:28 -04:00
import Data.Text.Lazy.Builder (Builder, toLazyText, fromString)
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
index = dir </> "index.html"
2020-07-15 14:09:14 -04:00
deps = thumbFile (thumbnail info) : map pageFile paths ++ paths
deps' = unwords $ map (dir </>) deps
in
2020-07-16 10:07:28 -04:00
[b|$@index: $@deps'|]
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, prefix, filters}) infos' build data_ tmp =
let infos = filter (matchFilters filters . snd) infos'
files = map fst infos
files' = unwords $ map (data_ </>) files
page d = build </> prefix </> takeDirectory d </> "index.html"
pages = map (page . fst) infos
pages' = unwords pages
path = build </> prefix </> "index.html"
rules = makeRules prefix filters build data_ tmp
inc d = tmp </> prefix </> takeDirectory d <.> "d"
incs = if null infos then "" else
"include " <> fromString (unwords $ map inc files)
index = build </> "index.html"
in [b|@0
$@index: $@path
$@path: $@pages'
$@path: $@files'
echo "[gallery] "$$@
mkdir -p $$(dir $$@)
2020-07-17 06:29:13 -04:00
$$(MAKEPAGES) $$(MPFLAGS) gallery -t "$*title" -o "$$@" \
$$(filter $$(DATADIR)/%/$$(INFONAME),$$^)
2020-07-16 10:07:28 -04:00
$rules
$incs
|]
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
2020-07-17 06:29:13 -04:00
echo "[single] "$$@
2020-07-16 10:07:28 -04:00
mkdir -p $$(dir $$@)
2020-07-17 06:29:13 -04:00
$$(MAKEPAGES) $$(MPFLAGS) single "$$<" -o "$$@" $flags
2020-07-16 10:07:28 -04:00
$@tmpPrefix/%.d: $@data_/%/info.yaml
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 $$@)
convert -resize '$$(MED)x$$(MED)>' "$$<" "$$@"
|]
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
pageFile = addSuffix "_med"