gallery/make-pages/Depend.hs

130 lines
4 KiB
Haskell

module Depend
(dependSingle, dependSingle',
dependGallery, dependGallery',
thumbFile, pageFile)
where
import BuilderQQ
import Info hiding (Text)
import Data.Maybe (fromMaybe)
import Data.Text.Lazy (Text)
import Data.Text.Lazy.Builder (Builder, toLazyText, fromString)
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 =
let dir = build </> prefix </> yamlDir
images = if nsfw then #images info else #sfwImages info
paths = map #path images
index = dir </> "index.html"
deps = thumbFile (thumbnail info) : map pageFile paths ++ paths
deps' = unwords $ map (dir </>) deps
in
[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 $$@)
$$(MAKEPAGES) $$(MPFLAGS) gallery -t "$*title" -o "$$@" \
$$(filter $$(DATADIR)/%/$$(INFONAME),$$^)
$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
echo "[single] "$$@
mkdir -p $$(dir $$@)
$$(MAKEPAGES) $$(MPFLAGS) single "$$<" -o "$$@" $flags
$@tmpPrefix/%.d: $@data_/%/info.yaml
echo "[deps] "$$@
mkdir -p $$(dir $$@)
$$(MAKEPAGES) $$(MPFLAGS) depend-single $flags \
-o "$$@" -p "$@prefix" -B "$@build" -D "$@data_" $$<
$@buildPrefix/%: $@data_/%
echo "[copy] "$$@
mkdir -p $$(dir $$@)
cp "$$<" "$$@"
$@buildPrefix/%_small.png: $@data_/%.png
echo "[resize] "$$@
mkdir -p $$(dir $$@)
convert -resize '$$(SMALL)x$$(SMALL)^' \
-gravity center -crop 1:1+0 "$$<" "$$@"
$@buildPrefix/%_med.png: $@data_/%.png
echo "[resize] "$$@
mkdir -p $$(dir $$@)
convert -resize '$$(MED)x$$(MED)>' "$$<" "$$@"
|]
where
buildPrefix = build </> prefix
tmpPrefix = tmp </> prefix
flags = filtersToFlags filters
filtersToFlags :: GalleryFilters -> Builder
filtersToFlags (GalleryFilters {nsfw}) =
case nsfw of Just False -> ""; _ -> "-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 = addSuffix "_med"