pass index file to gallery/rss

(instead of trying to escape the strings inside in make & on the command
line)
This commit is contained in:
Rhiannon Morris 2020-08-04 02:25:59 +02:00
parent 161cb48d3a
commit 28fc9db3e0
6 changed files with 63 additions and 67 deletions

View File

@ -139,13 +139,13 @@ $(MAKEPAGES) $(MPFLAGS) depend-single \
endef
# args
# 1. title
# 1. index file
# 2. gallery prefix
# 3. other flags
define gallery
echo "[gallery] "$@
mkdir -p "$(dir $@)"
$(MAKEPAGES) $(MPFLAGS) gallery -t "$(1)" -p "$(2)" $(3) -o "$@" \
$(MAKEPAGES) $(MPFLAGS) gallery -i "$(1)" -p "$(2)" $(3) -o "$@" \
$(filter $(DATADIR)/%/$(INFONAME),$^)
endef
@ -159,21 +159,18 @@ $(MAKEPAGES) $(MPFLAGS) single -D "$(1)" $< -o "$@" $(2)
endef
# args:
# 1. title with commas replaced with $(comma)
# 2. description with commas replaced
# 3. gallery prefix
# 4. data dir
# 5. other flags
# 1. index file
# 2. gallery prefix
# 3. data dir
# 4. other flags
define rss
echo "[rss] "$@
mkdir -p "$(dir $@)"
$(MAKEPAGES) $(MPFLAGS) rss -t "$(1)" -d "$(2)" \
-R "$(ROOT)" -p "$(3)" -D "$(4)" -o "$@" $(5) \
$(MAKEPAGES) $(MPFLAGS) rss -i "$(1)" \
-R "$(ROOT)" -p "$(2)" -D "$(3)" -o "$@" $(4) \
$(filter $(DATADIR)/%/$(INFONAME),$^)
endef
comma := ,
# never delete intermediate files
.SECONDARY:

View File

@ -36,25 +36,26 @@ dependSingle' yamlDir info prefix build nsfw =
thumbFile (thumbnail info) : map pageFile paths ++ paths ++ dls
dependGallery :: GalleryInfo
-> FilePath -- ^ index file
-> [(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 ginfo index infos build data_ tmp =
toLazyText $ dependGallery' ginfo index infos build data_ tmp
dependGallery' :: GalleryInfo -> [(FilePath, Info)]
dependGallery' :: GalleryInfo -> FilePath -> [(FilePath, Info)]
-> FilePath -> FilePath -> FilePath -> Builder
dependGallery' (GalleryInfo {title, desc, prefix, filters})
infos' build data_ tmp = [b|@0
dependGallery' (GalleryInfo {prefix, filters})
indexFile infos' build data_ tmp = [b|@0
$@index: $@gallery
$@gallery: $@pages' $@files' $@rss $$(MAKEPAGES)
$$(call gallery,$title',$@prefix,$flags)
$@gallery: $@pages' $@files' $@rss $@indexFile $$(MAKEPAGES)
$$(call gallery,$@indexFile,$@prefix)
$@rss: $@files' $$(MAKEPAGES)
$$(call rss,$title',$desc',$@prefix,$@data_)
$@rss: $@files' $@indexFile $$(MAKEPAGES)
$$(call rss,$@indexFile,$@prefix,$@data_)
$rules
@ -80,12 +81,6 @@ dependGallery' (GalleryInfo {title, desc, prefix, filters})
incFiles = unwords $ map inc files
incs = if null infos then "" else [b|include $@incFiles|]
flags = filtersToFlags filters
title' = substComma title
desc' = substComma desc
substComma = textMap \case ',' -> "$(comma)"; c -> fromChar c
makeRules :: FilePath -- ^ prefix
-> GalleryFilters
-> FilePath -- ^ build dir

View File

@ -18,15 +18,12 @@ newtype NoThumb = NoThumb FilePath
deriving stock Eq deriving anyclass Exception
instance Show NoThumb where show (NoThumb dir) = "no thumbnail for " ++ dir
make :: Text -- ^ title
-> FilePath -- ^ gallery url prefix
-> Bool -- ^ nsfw is included?
-> [(FilePath, Info)]
-> Lazy.Text
make title prefix nsfw infos = toLazyText $ make' title prefix nsfw infos
make :: GalleryInfo -> [(FilePath, Info)] -> Lazy.Text
make ginfo infos = toLazyText $ make' ginfo infos
make' :: Text -> FilePath -> Bool -> [(FilePath, Info)] -> Builder
make' title prefix nsfw infos = [b|@0
make' :: GalleryInfo -> [(FilePath, Info)] -> Builder
make' (GalleryInfo {title, prefix, filters}) infos = [b|@0
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
@ -77,6 +74,7 @@ make' title prefix nsfw infos = [b|@0
|]
where
items = map (uncurry $ makeYearItems nsfw) infosByYear
infosByYear =
[(the year, infopath) |
infopath@(_, info) <- infos,
@ -84,7 +82,9 @@ make' title prefix nsfw infos = [b|@0
let year = #year info,
then group by Down year using groupBy']
groupBy' f = groupBy ((==) `on` f)
undir = joinPath (replicate (length (splitPath prefix)) "..")
allTags = infos
& concatMap (map (,1) . tagsFor nsfw . #second)
& HashMap.fromListWith (+) & HashMap.toList
@ -92,6 +92,8 @@ make' title prefix nsfw infos = [b|@0
requireFilters = map (uncurry $ makeFilter "require") allTags
excludeFilters = map (uncurry $ makeFilter "exclude") allTags
nsfw = #nsfw filters /= NoNsfw
makeFilter :: Text -> Text -> Int -> Builder
makeFilter prefix tag count = [b|@8
<li>

View File

@ -5,16 +5,18 @@ import Control.Monad
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as ByteString
import Data.List (intersperse)
import qualified Data.List as List
import Data.Text.Lazy (Text)
import Data.Text.Lazy.Builder (toLazyText)
import qualified Data.Text.Lazy.IO as Text
import qualified Data.YAML as YAML
import System.FilePath (makeRelative, takeDirectory, takeFileName)
import System.FilePath.Find (find, always, fileName, (==?))
import System.FilePath.Find (always, fileName, (==?))
import qualified System.FilePath.Find as File
import System.IO (hPrint, stderr)
import Depend
import Info (IndexInfo (..), Info)
import Info hiding (Text)
import Options
import qualified SinglePage
import qualified GalleryPage
@ -52,10 +54,12 @@ main2 (SinglePage {file, dataDir, nsfw, output}) = do
let page = SinglePage.make nsfw dir info
writeOutput output page
main2 (GalleryPage {title, prefix, files, nsfw, output, dataDir}) = do
main2 (GalleryPage {files, prefix, index, output, dataDir}) = do
ginfo <- galleryFromIndex index prefix
printV $ "gallery_info" := ginfo
infos <- mapM (infoYAML dataDir) files
printV $ "infos" := infos
let page = GalleryPage.make title prefix nsfw infos
let page = GalleryPage.make ginfo infos
writeOutput output page
main2 (IndexPage {file, output}) = do
@ -64,11 +68,13 @@ main2 (IndexPage {file, output}) = do
let page = IndexPage.make info
writeOutput output page
main2 (RSS {files, title, desc, root, prefix, output, dataDir}) = do
main2 (RSS {files, root, index, prefix, output, dataDir}) = do
ginfo <- galleryFromIndex index prefix
printV $ "gallery_info" := ginfo
infos <- mapM (infoYAML dataDir) files
printV $ "infos" := infos
let output' = takeFileName <$> output
let rss = RSS.make root title desc prefix output' infos
let rss = RSS.make root ginfo output' infos
writeOutput output rss
main2 (DependSingle {file, nsfw, output, prefix, buildDir, dataDir}) = do
@ -83,15 +89,15 @@ main2 (DependGallery {file, output, buildDir, dataDir, tmpDir, infoName}) = do
IndexInfo {galleries} <- readYAML file
printV $ "galleries" := galleries
infos <- mapM (infoYAML dataDir) =<< findInfos dataDir infoName
printV $ "info files" := infos
let dependGallery0 g = dependGallery' g infos buildDir dataDir tmpDir
printV $ "info_files" := infos
let dependGallery0 g = dependGallery' g file infos buildDir dataDir tmpDir
let deps = toLazyText $ mconcat $ intersperse "\n\n\n" $
map dependGallery0 galleries
writeOutput output deps
main2 (ListTags {nsfw, listUntagged, dataDir, infoName}) = do
infos <- mapM (infoYAML dataDir) =<< findInfos dataDir infoName
printV $ "info files" := infos
printV $ "info_files" := infos
ListTags.run nsfw listUntagged infos
@ -104,11 +110,17 @@ infoYAML dataDir f = do
pure (f', info)
findInfos :: FilePath -> FilePath -> IO [FilePath]
findInfos dataDir infoName = find always (fileName ==? infoName) dataDir
findInfos dataDir infoName = File.find always (fileName ==? infoName) dataDir
readYAML :: YAML.FromYAML a => FilePath -> IO a
readYAML file = ByteString.readFile file >>= decode1Must file
galleryFromIndex :: FilePath -> FilePath -> IO GalleryInfo
galleryFromIndex file prefix = do
IndexInfo {galleries} <- readYAML file
maybe (fail $ "no gallery with prefix " ++ prefix) pure $
List.find (\g -> #prefix g == prefix) galleries
decode1Must :: YAML.FromYAML a => FilePath -> ByteString -> IO a
decode1Must file txt =
case YAML.decode1 txt of

View File

@ -20,8 +20,7 @@ data ModeOptions =
| GalleryPage {
files :: [FilePath],
prefix :: FilePath,
nsfw :: Bool,
title :: Text,
index :: FilePath,
output :: Maybe FilePath,
dataDir :: FilePath
}
@ -31,9 +30,8 @@ data ModeOptions =
}
| RSS {
files :: [FilePath],
title :: Text,
desc :: Text,
root :: Text,
index :: FilePath,
prefix :: FilePath,
output :: Maybe FilePath,
dataDir :: FilePath
@ -92,27 +90,21 @@ optionsParser = globalOpts `info` mainInfo where
gallery = command "gallery" $ galleryOpts `info` galleryInfo
galleryOpts =
GalleryPage <$> files <*> prefix <*> nsfwG <*> title <*> output <*> dataDir
GalleryPage <$> files <*> prefix <*> indexFile <*> output <*> dataDir
prefix = strOption $
short 'p' <> long "prefix" <> metavar "DIR" <>
value "" <>
help "output directory prefix"
indexFile = strOption $
short 'i' <> long "index" <> metavar "FILE" <>
help "path to index file"
files = many $ strArgument $
metavar "FILE..." <> help "yaml files to read"
nsfwG = switch $
short 'n' <> long "nsfw" <>
help "nsfw versions are included"
title = strOption $
short 't' <> long "title" <> metavar "TITLE" <>
help "page title"
galleryInfo = progDesc "generate a gallery page"
rss = command "rss" $ rssOpts `info` rssInfo
rssOpts = RSS <$> files <*> title <*> desc <*> root
rssOpts = RSS <$> files <*> root <*> indexFile
<*> prefix <*> output <*> dataDir
desc = strOption $
short 'd' <> long "desc" <> metavar "DESC" <>
help "gallery description"
root = strOption $
short 'R' <> long "root" <> metavar "URL" <>
help "website root (no trailing slash)"

View File

@ -14,18 +14,16 @@ import System.FilePath (takeDirectory)
make :: Strict.Text -- ^ website root e.g. @https://gallery.niss.website@
-> Strict.Text -- ^ title
-> Strict.Text -- ^ description
-> FilePath -- ^ gallery prefix e.g. @main@
-> GalleryInfo
-> Maybe FilePath -- ^ output filename for self link
-> [(FilePath, Info)]
-> Lazy.Text
make root title desc prefix output infos =
toLazyText $ make' root title desc prefix output infos
make root ginfo output infos =
toLazyText $ make' root ginfo output infos
make' :: Strict.Text -> Strict.Text -> Strict.Text
-> FilePath -> Maybe FilePath -> [(FilePath, Info)] -> Builder
make' root title desc prefix output infos = [b|@0
make' :: Strict.Text -> GalleryInfo
-> Maybe FilePath -> [(FilePath, Info)] -> Builder
make' root (GalleryInfo {title, desc, prefix}) output infos = [b|@0
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>