96 lines
2.9 KiB
Haskell
96 lines
2.9 KiB
Haskell
module RSS (make, make') where
|
|
|
|
import Date
|
|
import Info
|
|
import BuilderQQ
|
|
|
|
import Data.List (sortBy, intersperse)
|
|
import Data.Maybe (catMaybes)
|
|
import Data.Function (on)
|
|
import qualified Data.Text as Strict
|
|
import qualified Data.Text.Lazy as Lazy
|
|
import System.FilePath (takeDirectory)
|
|
import Control.Monad
|
|
|
|
|
|
make :: Strict.Text -- ^ website root e.g. @https://gallery.niss.website@
|
|
-> Strict.Text -- ^ website name e.g. @nissart@
|
|
-> GalleryInfo
|
|
-> Maybe FilePath -- ^ output filename for self link
|
|
-> [(FilePath, Info)]
|
|
-> Lazy.Text
|
|
make root name ginfo output infos =
|
|
toLazyText $ make' root name ginfo output infos
|
|
|
|
make' :: Strict.Text -> Strict.Text -> GalleryInfo
|
|
-> Maybe FilePath -> [(FilePath, Info)] -> Builder
|
|
make' root name ginfo@(GalleryInfo {title, desc, prefix}) output infos = [b|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
|
<channel>
|
|
<title>$name—$title</title>
|
|
<link>$link</link>
|
|
<description>$desc</description>
|
|
$selflink
|
|
|
|
$items
|
|
</channel>
|
|
</rss>
|
|
|]
|
|
where
|
|
link = [b|$root/$prefix|]
|
|
nsfw = ginfo.nsfw
|
|
items = map (uncurry $ makeItem root prefix nsfw) $
|
|
sortBy (flip (compareFor nsfw `on` snd)) $
|
|
filter (not . (.unlisted) . snd) infos
|
|
selflink = case output of
|
|
Nothing -> ""
|
|
Just o -> [b|<atom:link href="$link/$o" rel="self" />|]
|
|
|
|
makeItem :: Strict.Text -> FilePath -> Bool -> FilePath -> Info -> Builder
|
|
makeItem root prefix nsfw path info@(Info {title}) = [b|
|
|
<item>
|
|
<title>$title$suffix</title>
|
|
<link>$link</link>
|
|
<guid>$link</guid>
|
|
$body
|
|
<pubDate>$date</pubDate>
|
|
</item>
|
|
|]
|
|
where
|
|
body = [b|
|
|
<description> <![CDATA[
|
|
$image
|
|
$artist
|
|
$desc
|
|
]]> </description>
|
|
|]
|
|
|
|
suffix = if null parts then ""
|
|
else " (" <> mconcat (intersperse ", " parts) <> ")"
|
|
parts = catMaybes [o18, cnt, up]
|
|
up = do guard $ hasUpdatesFor nsfw info; Just "updated"
|
|
o18 = do guard $ nsfw && anyNsfw info; Just "🔞"
|
|
cnt = do let len = maybe 0 length $ allImages <$> imagesFor nsfw info
|
|
guard $ len /= 1; Just [b|$len images|]
|
|
|
|
dir = takeDirectory path
|
|
link = [b|$root/$prefix/$dir|]
|
|
|
|
date = formatRSS $ latestDateFor nsfw info
|
|
artist = ifJust info.artist \case
|
|
Artist name Nothing -> [b|<p>by $name|]
|
|
Artist name (Just url) -> [b|<p>by <a href="$url">$name</a>|]
|
|
desc = makeDesc $ descFor nsfw info
|
|
|
|
image = case previewImage info of
|
|
Just (PFull img) -> figure $ pageFile img
|
|
Just (PThumb th) -> figure $ thumbFile th
|
|
Nothing -> ""
|
|
figure p = [b|<figure> <a href="$link"><img src="$link/$p"></a> </figure>|]
|
|
|
|
makeDesc :: Desc -> Builder
|
|
makeDesc NoDesc = ""
|
|
makeDesc (TextDesc txt) = [b|$txt|]
|
|
makeDesc (LongDesc fs) = [b|<dl>$fields</dl>|]
|
|
where fields = map (\(DescField {name, text}) -> [b|<dt>$name <dd>$text|]) fs
|