gallery/make-pages/RSS.hs

103 lines
2.9 KiB
Haskell
Raw Normal View History

2020-07-19 12:04:40 -04:00
module RSS (make, make') where
2020-09-25 17:08:44 -04:00
import Date
2020-07-19 12:04:40 -04:00
import Info
import BuilderQQ
import Data.List (sortBy, intersperse)
import Data.Maybe (catMaybes)
2020-11-16 17:30:56 -05:00
import Data.Function (on)
2020-07-19 12:04:40 -04:00
import qualified Data.Text as Strict
import qualified Data.Text.Lazy as Lazy
import System.FilePath (takeDirectory)
make :: Strict.Text -- ^ website root e.g. @https://gallery.niss.website@
-> Strict.Text -- ^ website name e.g. @nissart@
-> GalleryInfo
2020-07-19 12:04:40 -04:00
-> Maybe FilePath -- ^ output filename for self link
-> [(FilePath, Info)]
-> Lazy.Text
make root name ginfo output infos =
toLazyText $ make' root name ginfo output infos
2020-07-19 12:04:40 -04:00
make' :: Strict.Text -> Strict.Text -> GalleryInfo
-> Maybe FilePath -> [(FilePath, Info)] -> Builder
make' root name ginfo@(GalleryInfo {title, desc, prefix}) output infos = [b|@0
2020-07-19 12:04:40 -04:00
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
2020-07-19 12:04:40 -04:00
<channel>
<title>$name$title</title>
2020-07-19 12:04:40 -04:00
<link>$link</link>
<description>$desc</description>
2020-07-19 12:04:40 -04:00
$selflink
$4.items
</channel>
</rss>
|]
where
link = [b|$root/$prefix|]
nsfw = ginfo.nsfw
2021-03-07 16:07:02 -05:00
items = map (uncurry $ makeItem root prefix nsfw) $
sortBy (flip (compareFor nsfw `on` snd)) $
filter (not . (.unlisted) . snd) infos
2020-07-19 12:04:40 -04:00
selflink = case output of
Nothing -> ""
Just o -> [b|<link href="$link/$o" rel="self" />|]
2020-07-19 12:04:40 -04:00
2021-03-07 16:07:02 -05:00
makeItem :: Strict.Text -> FilePath -> Bool -> FilePath -> Info -> Builder
2022-01-04 14:13:09 -05:00
makeItem root prefix nsfw path i@(Info {title, artist}) = [b|@4
2020-07-19 12:04:40 -04:00
<item>
<title>$title$suf</title>
2020-07-19 12:04:40 -04:00
<link>$link</link>
<guid>$link</guid>
$body
2022-01-04 14:13:09 -05:00
<pubDate>$date</pubDate>
2020-07-19 12:04:40 -04:00
</item>
|]
where
suf = let parts = catMaybes [o18, cnt, up] in
if null parts then ""
else " (" <> mconcat (intersperse ", " parts) <> ")"
up = if hasUpdatesFor nsfw i then Just "updated" else Nothing
o18 = if nsfw && anyNsfw i then Just "🔞" else Nothing
cnt = let len = maybe 0 length $ allImages <$> imagesFor nsfw i in
if len == 1 then Nothing else Just [b|$len images|]
2020-07-19 12:04:40 -04:00
dir = takeDirectory path
link = [b|$root/$prefix/$dir|]
date = formatRSS $ latestDateFor nsfw i
2020-07-25 07:58:16 -04:00
artist' = ifJust artist \case
Artist {name, url = Nothing} -> [b|<p>by $name|]
Artist {name, url = Just url} -> [b|<p>by <a href="$url">$name</a>|]
2021-03-07 16:07:02 -05:00
desc = descFor nsfw i
desc' = makeDesc desc
body = [b|@6
<description> <![CDATA[
$8.image
$8.artist'
$8.desc'
]]> </description>
2020-07-25 07:58:16 -04:00
|]
image = case previewImage i of
Just (PFull img) -> go $ pageFile img
Just (PThumb th) -> go $ thumbFile th
Nothing -> ""
where go p = [b|@0
<figure>
<a href="$link"><img src="$link/$p"></a>
</figure>
|]
makeDesc :: Desc -> Builder
makeDesc NoDesc = ""
makeDesc (TextDesc txt) = [b|$txt|]
2022-01-04 14:13:32 -05:00
makeDesc (LongDesc fs) = [b|<dl>$fields</dl>|]
where
fields = map makeField fs
2022-01-04 14:13:32 -05:00
makeField (DescField {name, text}) = [b|<dt>$name <dd>$text|]