Finish RSS feed

- Redirect hyphenated tags to spaced if they exist
- Tweak personal index template
This commit is contained in:
Daniel J. Summers 2022-05-13 22:55:25 -04:00
parent e2f94edb9e
commit 13e9919f58
2 changed files with 43 additions and 21 deletions

View File

@ -461,6 +461,7 @@ module Post =
open System.IO
open System.ServiceModel.Syndication
open System.Text.RegularExpressions
open System.Xml
/// Split the "rest" capture for categories and tags into the page number and category/tag URL parts
@ -587,6 +588,13 @@ module Post =
hash.Add ("page_title", $"Posts Tagged “{tag}”{pgTitle}")
hash.Add ("is_tag", true)
return! themedView "index" next ctx hash
// Other systems use hyphens for spaces; redirect if this is an old tag link
| _ ->
let spacedTag = tag.Replace ("-", " ")
match! Data.Post.findPageOfTaggedPosts webLog.id spacedTag pageNbr 1 conn with
| posts when List.length posts > 0 ->
let endUrl = if pageNbr = 1L then "" else $"page/{pageNbr}"
return! redirectTo true $"""/tag/{spacedTag.Replace (" ", "+")}/{endUrl}""" next ctx
| _ -> return! Error.notFound next ctx
}
@ -613,32 +621,41 @@ module Post =
let generateFeed : HttpHandler = fun next ctx -> backgroundTask {
let conn = conn ctx
let webLog = WebLogCache.get ctx
let urlBase = $"https://{webLog.urlBase}/"
// TODO: hard-coded number of items
let! posts = Data.Post.findPageOfPublishedPosts webLog.id 1L 10 conn
let! authors = getAuthors webLog posts conn
let cats = CategoryCache.get ctx
let toItem (post : Post) =
let urlBase = $"https://{webLog.urlBase}/"
let plainText =
Regex.Replace (post.text, "<(.|\n)*?>", "")
|> function
| txt when txt.Length < 255 -> txt
| txt -> $"{txt.Substring (0, 252)}..."
let item = SyndicationItem (
Id = $"{urlBase}{Permalink.toString post.permalink}",
Title = TextSyndicationContent.CreateHtmlContent post.title,
PublishDate = DateTimeOffset post.publishedOn.Value)
PublishDate = DateTimeOffset post.publishedOn.Value,
LastUpdatedTime = DateTimeOffset post.updatedOn,
Content = TextSyndicationContent.CreatePlaintextContent plainText)
item.AddPermalink (Uri item.Id)
let doc = XmlDocument ()
let content = doc.CreateElement ("content", "encoded", "http://purl.org/rss/1.0/modules/content/")
content.InnerText <- post.text
.Replace("src=\"/", $"src=\"{urlBase}")
.Replace ("href=\"/", $"href=\"{urlBase}")
item.ElementExtensions.Add content
let encoded = post.text.Replace("src=\"/", $"src=\"{urlBase}").Replace ("href=\"/", $"href=\"{urlBase}")
item.ElementExtensions.Add ("encoded", "http://purl.org/rss/1.0/modules/content/", encoded)
item.Authors.Add (SyndicationPerson (
Name = (authors |> List.find (fun a -> a.name = WebLogUserId.toString post.authorId)).value))
for catId in post.categoryIds do
[ post.categoryIds
|> List.map (fun catId ->
let cat = cats |> Array.find (fun c -> c.id = CategoryId.toString catId)
item.Categories.Add (SyndicationCategory (cat.name, $"{urlBase}category/{cat.slug}/", cat.name))
for tag in post.tags do
SyndicationCategory (cat.name, $"{urlBase}category/{cat.slug}/", cat.name))
post.tags
|> List.map (fun tag ->
let urlTag = tag.Replace (" ", "+")
item.Categories.Add (SyndicationCategory (tag, $"{urlBase}tag/{urlTag}/", $"{tag} (tag)"))
SyndicationCategory (tag, $"{urlBase}tag/{urlTag}/", $"{tag} (tag)"))
]
|> List.concat
|> List.iter item.Categories.Add
item
@ -648,11 +665,16 @@ module Post =
feed.LastUpdatedTime <- DateTimeOffset <| (List.head posts).updatedOn
feed.Generator <- generator ctx
feed.Items <- posts |> Seq.ofList |> Seq.map toItem
feed.Language <- "en"
feed.Id <- urlBase
feed.Links.Add (SyndicationLink (Uri $"{urlBase}feed.xml", "self", "", "application/rss+xml", 0L))
feed.AttributeExtensions.Add (XmlQualifiedName ("content", "http://www.w3.org/2000/xmlns/"), "http://purl.org/rss/1.0/modules/content/")
feed.ElementExtensions.Add ("link", "", urlBase)
use mem = new MemoryStream ()
use xml = XmlWriter.Create mem
let formatter = Rss20FeedFormatter feed
formatter.WriteTo xml
feed.SaveAsRss20 xml
xml.Close ()
let _ = mem.Seek (0L, SeekOrigin.Begin)

View File

@ -17,10 +17,10 @@
<i class="fa fa-calendar"></i> {{ post.published_on | date: "dddd, MMMM d, yyyy" }}
</span>
<span title="Published At">
<i class="fa fa-clock-o" title="Time"></i> {{ post.published_on | date: "h:mm tt" | downcase }}
<i class="fa fa-clock-o"></i> {{ post.published_on | date: "h:mm tt" | downcase }}
</span>
<span title="Author">
<i class="fa fa-user" title="Author"></i> {{ model.authors | value: post.author_id }}
<i class="fa fa-user"></i> {{ model.authors | value: post.author_id }}
</span>
{% if logged_on %}
<span>