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 Records ()
|
|
|
|
|
2020-11-16 17:30:56 -05:00
|
|
|
import Data.List (sortBy)
|
2020-07-25 07:58:16 -04:00
|
|
|
import Data.Maybe (isJust)
|
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@
|
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
|
2020-08-03 20:25:59 -04:00
|
|
|
make root ginfo output infos =
|
|
|
|
toLazyText $ make' root ginfo output infos
|
2020-07-19 12:04:40 -04:00
|
|
|
|
2020-08-03 20:25:59 -04:00
|
|
|
make' :: Strict.Text -> GalleryInfo
|
|
|
|
-> Maybe FilePath -> [(FilePath, Info)] -> Builder
|
2020-11-16 17:30:56 -05:00
|
|
|
make' root ginfo@(GalleryInfo {title, desc, prefix}) output infos = [b|@0
|
2020-07-19 12:04:40 -04:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
2020-07-25 07:58:16 -04:00
|
|
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
2020-07-19 12:04:40 -04:00
|
|
|
<channel>
|
2020-08-30 13:13:40 -04:00
|
|
|
<title>$title</title>
|
2020-07-19 12:04:40 -04:00
|
|
|
<link>$link</link>
|
2020-08-30 13:13:40 -04:00
|
|
|
<description>$desc</description>
|
2020-07-19 12:04:40 -04:00
|
|
|
$selflink
|
|
|
|
|
|
|
|
$4.items
|
|
|
|
</channel>
|
|
|
|
</rss>
|
|
|
|
|]
|
|
|
|
where
|
2020-08-30 13:13:40 -04:00
|
|
|
link = [b|$root/$prefix|]
|
2020-11-16 17:30:56 -05:00
|
|
|
nsfw = #nsfw ginfo
|
2021-03-07 16:07:02 -05:00
|
|
|
items = map (uncurry $ makeItem root prefix nsfw) $
|
2021-10-02 17:00:33 -04:00
|
|
|
sortBy (flip (compareFor nsfw `on` #second)) $
|
|
|
|
filter (not . #unlisted . snd) infos
|
2020-07-19 12:04:40 -04:00
|
|
|
selflink = case output of
|
|
|
|
Nothing -> ""
|
2020-08-30 13:13:40 -04:00
|
|
|
Just o -> [b|<atom: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
|
|
|
|
makeItem root prefix nsfw path i@(Info {title, date, artist}) = [b|@4
|
2020-07-19 12:04:40 -04:00
|
|
|
<item>
|
2020-08-30 13:13:40 -04:00
|
|
|
<title>$title</title>
|
2020-07-19 12:04:40 -04:00
|
|
|
<link>$link</link>
|
|
|
|
<guid>$link</guid>
|
2020-07-25 07:58:16 -04:00
|
|
|
$descArtist'
|
2020-07-19 12:04:40 -04:00
|
|
|
<pubDate>$date'</pubDate>
|
|
|
|
</item>
|
|
|
|
|]
|
|
|
|
where
|
|
|
|
dir = takeDirectory path
|
2020-08-30 13:13:40 -04:00
|
|
|
link = [b|$root/$prefix/$dir|]
|
2020-07-25 07:58:16 -04:00
|
|
|
artist' = ifJust artist \case
|
2020-08-30 13:13:40 -04:00
|
|
|
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
|
2021-03-07 16:08:44 -05:00
|
|
|
desc' = makeDesc desc
|
|
|
|
descArtist' = if #exists desc || isJust artist then [b|@6
|
2020-07-25 07:58:16 -04:00
|
|
|
<description>
|
|
|
|
<![CDATA[
|
2021-03-07 16:07:02 -05:00
|
|
|
$10.desc'
|
|
|
|
$10.artist'
|
2020-07-25 07:58:16 -04:00
|
|
|
]]>
|
|
|
|
</description>
|
|
|
|
|]
|
|
|
|
else ""
|
2020-09-25 17:08:44 -04:00
|
|
|
date' = formatRSS date
|
2021-03-07 16:08:44 -05:00
|
|
|
|
|
|
|
makeDesc :: Desc -> Builder
|
|
|
|
makeDesc NoDesc = ""
|
|
|
|
makeDesc (TextDesc txt) = [b|$txt|]
|
|
|
|
makeDesc (LongDesc fs) = [b|$fields|]
|
|
|
|
where
|
|
|
|
fields = map makeField fs
|
|
|
|
makeField (DescField {name, text}) = [b|<b>$name</b>: $text|]
|