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
|
|
|
|
|
2024-08-18 00:22:55 -04:00
|
|
|
import Data.List (sortBy, intersperse)
|
2024-10-21 18:08:30 -04:00
|
|
|
import Data.Maybe (catMaybes, fromMaybe)
|
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)
|
2024-08-18 00:35:06 -04:00
|
|
|
import Control.Monad
|
2020-07-19 12:04:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
make :: Strict.Text -- ^ website root e.g. @https://gallery.niss.website@
|
2024-08-18 00:22:55 -04:00
|
|
|
-> Strict.Text -- ^ website name e.g. @nissart@
|
2020-08-03 20:25:59 -04:00
|
|
|
-> GalleryInfo
|
2020-07-19 12:04:40 -04:00
|
|
|
-> Maybe FilePath -- ^ output filename for self link
|
|
|
|
-> [(FilePath, Info)]
|
|
|
|
-> Lazy.Text
|
2024-08-18 00:22:55 -04:00
|
|
|
make root name ginfo output infos =
|
|
|
|
toLazyText $ make' root name ginfo output infos
|
2020-07-19 12:04:40 -04:00
|
|
|
|
2024-08-18 00:22:55 -04:00
|
|
|
make' :: Strict.Text -> Strict.Text -> GalleryInfo
|
2020-08-03 20:25:59 -04:00
|
|
|
-> Maybe FilePath -> [(FilePath, Info)] -> Builder
|
2024-08-18 01:37:58 -04:00
|
|
|
make' root name ginfo@(GalleryInfo {title, desc, prefix}) output infos = [b|
|
2024-08-18 02:59:29 -04:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
2024-08-21 02:30:51 -04:00
|
|
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"
|
|
|
|
xmlns:dc="http://purl.org/dc/elements/1.1/">
|
2024-08-18 02:59:29 -04:00
|
|
|
<channel>
|
2024-08-21 02:30:51 -04:00
|
|
|
<title><![CDATA[$name—$title]]></title>
|
2024-08-18 02:59:29 -04:00
|
|
|
<link>$link</link>
|
2024-08-21 02:30:51 -04:00
|
|
|
<description><![CDATA[$desc]]></description>
|
2024-08-18 02:59:29 -04:00
|
|
|
$selflink
|
2020-07-19 12:04:40 -04:00
|
|
|
|
2024-08-18 02:59:29 -04:00
|
|
|
$items
|
|
|
|
</channel>
|
|
|
|
</rss>
|
|
|
|
|]
|
2020-07-19 12:04:40 -04:00
|
|
|
where
|
2020-08-30 13:13:40 -04:00
|
|
|
link = [b|$root/$prefix|]
|
2024-07-11 16:00:00 -04:00
|
|
|
nsfw = ginfo.nsfw
|
2024-10-21 18:08:30 -04:00
|
|
|
items = concatMap (uncurry $ makeItems root prefix nsfw) $
|
2024-07-11 16:00:00 -04:00
|
|
|
sortBy (flip (compareFor nsfw `on` snd)) $
|
|
|
|
filter (not . (.unlisted) . snd) infos
|
2020-07-19 12:04:40 -04:00
|
|
|
selflink = case output of
|
|
|
|
Nothing -> ""
|
2024-08-18 02:59:29 -04:00
|
|
|
Just o -> [b|<atom:link href="$link/$o" rel="self" />|]
|
2020-07-19 12:04:40 -04:00
|
|
|
|
2024-10-21 18:08:30 -04:00
|
|
|
makeItems :: Strict.Text -> FilePath -> Bool -> FilePath -> Info -> [Builder]
|
|
|
|
makeItems root prefix nsfw path info =
|
|
|
|
makeItem root prefix nsfw path info Nothing :
|
|
|
|
map (makeItem root prefix nsfw path info . Just . fst)
|
|
|
|
(bigUpdatesFor nsfw info)
|
|
|
|
|
|
|
|
makeItem :: Strict.Text -> FilePath -> Bool -> FilePath ->
|
|
|
|
Info -> Maybe Date -> Builder
|
|
|
|
makeItem root prefix nsfw path info@(Info {title}) date = [b|
|
2020-07-19 12:04:40 -04:00
|
|
|
<item>
|
2024-08-21 02:30:51 -04:00
|
|
|
<title><![CDATA[$title$suffix]]></title>
|
2020-07-19 12:04:40 -04:00
|
|
|
<link>$link</link>
|
2024-10-21 18:08:30 -04:00
|
|
|
<guid>$link$guidSuffix</guid>
|
2024-08-21 02:30:51 -04:00
|
|
|
<dc:creator><![CDATA[$creator]]></dc:creator>
|
2024-10-21 18:08:30 -04:00
|
|
|
<pubDate>$dateStr</pubDate>
|
2024-08-21 02:30:51 -04:00
|
|
|
$body
|
2020-07-19 12:04:40 -04:00
|
|
|
</item>
|
|
|
|
|]
|
|
|
|
where
|
2024-08-18 01:37:58 -04:00
|
|
|
body = [b|
|
2024-08-18 00:22:55 -04:00
|
|
|
<description> <![CDATA[
|
2024-08-18 01:37:58 -04:00
|
|
|
$image
|
|
|
|
$artist
|
|
|
|
$desc
|
2024-08-18 00:22:55 -04:00
|
|
|
]]> </description>
|
2020-07-25 07:58:16 -04:00
|
|
|
|]
|
2024-08-18 00:22:55 -04:00
|
|
|
|
2024-08-18 00:35:06 -04:00
|
|
|
suffix = if null parts then ""
|
|
|
|
else " (" <> mconcat (intersperse ", " parts) <> ")"
|
2024-10-21 18:08:30 -04:00
|
|
|
guidSuffix = maybe "" (("?" <>) . show) date
|
2024-08-18 00:35:06 -04:00
|
|
|
parts = catMaybes [o18, cnt, up]
|
2024-10-21 18:08:30 -04:00
|
|
|
up = "updated" <$ date
|
2024-08-18 00:35:06 -04:00
|
|
|
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|]
|
|
|
|
|
2024-08-21 02:30:51 -04:00
|
|
|
creator = maybe "niss" (.name) info.artist
|
|
|
|
|
2024-10-21 18:08:30 -04:00
|
|
|
dateStr = formatRSS $ fromMaybe info.date date
|
2024-08-18 00:35:06 -04:00
|
|
|
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
|
|
|
|
|
2024-10-21 18:08:30 -04:00
|
|
|
preview = previewImage info
|
2024-10-21 08:38:35 -04:00
|
|
|
image = case preview of
|
|
|
|
Just (PFull img) -> figure (escAttr img.desc) $ pageFile img
|
2024-10-21 08:24:17 -04:00
|
|
|
Just (PThumb th) -> figure "full image hidden" $ thumbFile th
|
2024-08-18 00:35:06 -04:00
|
|
|
Nothing -> ""
|
2024-10-21 08:38:35 -04:00
|
|
|
msg = case preview of
|
|
|
|
Just (PThumb _) -> "<p>(full image hidden; open to see)</p>"
|
|
|
|
_ -> "" :: Text
|
2024-10-21 08:24:17 -04:00
|
|
|
figure alt p = [b|
|
|
|
|
<figure aria-describedby=mainimg>
|
|
|
|
<a href="$link">
|
|
|
|
<img id=mainimg src="$link/$p" alt="$alt" title="$alt">
|
|
|
|
</a>
|
|
|
|
</figure>
|
2024-10-21 08:38:35 -04:00
|
|
|
$msg
|
2024-10-21 08:24:17 -04:00
|
|
|
|]
|
2021-03-07 16:08:44 -05:00
|
|
|
|
|
|
|
makeDesc :: Desc -> Builder
|
|
|
|
makeDesc NoDesc = ""
|
|
|
|
makeDesc (TextDesc txt) = [b|$txt|]
|
2024-08-21 02:30:51 -04:00
|
|
|
makeDesc (LongDesc fs) = [b|<ul>$fields</ul>|] where
|
|
|
|
fields = map mkField fs
|
|
|
|
mkField (DescField {name, text}) = [b|
|
|
|
|
<li> <b>$name</b>:
|
|
|
|
$text
|
|
|
|
|]
|