V2 #1

Merged
danieljsummers merged 102 commits from v2 into main 2022-06-23 00:35:12 +00:00
2 changed files with 43 additions and 21 deletions
Showing only changes of commit 13e9919f58 - Show all commits

View File

@ -461,6 +461,7 @@ module Post =
open System.IO open System.IO
open System.ServiceModel.Syndication open System.ServiceModel.Syndication
open System.Text.RegularExpressions
open System.Xml open System.Xml
/// Split the "rest" capture for categories and tags into the page number and category/tag URL parts /// Split the "rest" capture for categories and tags into the page number and category/tag URL parts
@ -587,7 +588,14 @@ module Post =
hash.Add ("page_title", $"Posts Tagged “{tag}”{pgTitle}") hash.Add ("page_title", $"Posts Tagged “{tag}”{pgTitle}")
hash.Add ("is_tag", true) hash.Add ("is_tag", true)
return! themedView "index" next ctx hash return! themedView "index" next ctx hash
| _ -> return! Error.notFound next ctx // 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
} }
// GET / // GET /
@ -613,32 +621,41 @@ module Post =
let generateFeed : HttpHandler = fun next ctx -> backgroundTask { let generateFeed : HttpHandler = fun next ctx -> backgroundTask {
let conn = conn ctx let conn = conn ctx
let webLog = WebLogCache.get ctx let webLog = WebLogCache.get ctx
let urlBase = $"https://{webLog.urlBase}/"
// TODO: hard-coded number of items // TODO: hard-coded number of items
let! posts = Data.Post.findPageOfPublishedPosts webLog.id 1L 10 conn let! posts = Data.Post.findPageOfPublishedPosts webLog.id 1L 10 conn
let! authors = getAuthors webLog posts conn let! authors = getAuthors webLog posts conn
let cats = CategoryCache.get ctx let cats = CategoryCache.get ctx
let toItem (post : Post) = 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 ( let item = SyndicationItem (
Id = $"{urlBase}{Permalink.toString post.permalink}", Id = $"{urlBase}{Permalink.toString post.permalink}",
Title = TextSyndicationContent.CreateHtmlContent post.title, 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) item.AddPermalink (Uri item.Id)
let doc = XmlDocument ()
let content = doc.CreateElement ("content", "encoded", "http://purl.org/rss/1.0/modules/content/") let encoded = post.text.Replace("src=\"/", $"src=\"{urlBase}").Replace ("href=\"/", $"href=\"{urlBase}")
content.InnerText <- post.text item.ElementExtensions.Add ("encoded", "http://purl.org/rss/1.0/modules/content/", encoded)
.Replace("src=\"/", $"src=\"{urlBase}")
.Replace ("href=\"/", $"href=\"{urlBase}")
item.ElementExtensions.Add content
item.Authors.Add (SyndicationPerson ( item.Authors.Add (SyndicationPerson (
Name = (authors |> List.find (fun a -> a.name = WebLogUserId.toString post.authorId)).value)) Name = (authors |> List.find (fun a -> a.name = WebLogUserId.toString post.authorId)).value))
for catId in post.categoryIds do [ post.categoryIds
let cat = cats |> Array.find (fun c -> c.id = CategoryId.toString catId) |> List.map (fun catId ->
item.Categories.Add (SyndicationCategory (cat.name, $"{urlBase}category/{cat.slug}/", cat.name)) let cat = cats |> Array.find (fun c -> c.id = CategoryId.toString catId)
for tag in post.tags do SyndicationCategory (cat.name, $"{urlBase}category/{cat.slug}/", cat.name))
let urlTag = tag.Replace (" ", "+") post.tags
item.Categories.Add (SyndicationCategory (tag, $"{urlBase}tag/{urlTag}/", $"{tag} (tag)")) |> List.map (fun tag ->
let urlTag = tag.Replace (" ", "+")
SyndicationCategory (tag, $"{urlBase}tag/{urlTag}/", $"{tag} (tag)"))
]
|> List.concat
|> List.iter item.Categories.Add
item item
@ -648,11 +665,16 @@ module Post =
feed.LastUpdatedTime <- DateTimeOffset <| (List.head posts).updatedOn feed.LastUpdatedTime <- DateTimeOffset <| (List.head posts).updatedOn
feed.Generator <- generator ctx feed.Generator <- generator ctx
feed.Items <- posts |> Seq.ofList |> Seq.map toItem 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 mem = new MemoryStream ()
use xml = XmlWriter.Create mem use xml = XmlWriter.Create mem
let formatter = Rss20FeedFormatter feed feed.SaveAsRss20 xml
formatter.WriteTo xml
xml.Close () xml.Close ()
let _ = mem.Seek (0L, SeekOrigin.Begin) 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" }} <i class="fa fa-calendar"></i> {{ post.published_on | date: "dddd, MMMM d, yyyy" }}
</span> </span>
<span title="Published At"> <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>
<span title="Author"> <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> </span>
{% if logged_on %} {% if logged_on %}
<span> <span>