Skip to content

Commit

Permalink
Don't create an empty <summary> in Atom feeds (gorilla#83)
Browse files Browse the repository at this point in the history
When creating an AtomEntry from an Item, only set Summary if
item.Description is non-empty, like we already do for Content.

Currently we always emit a `<summary>` element, even if the item has a
non-empty `<content>` element. Having an empty `<summary>` in this case
may
be confusing for feed consumers.

The Atom RFC explicitly says that a `<summary>` is not required in
general.
(There are a couple special cases where it is required, but they aren't
relevant here.)

https://tools.ietf.org/html/rfc4287#section-4.1.1.1

Fixes gorilla#82

Co-authored-by: Corey Daley <cdaley@redhat.com>
  • Loading branch information
2 people authored and Bios-Marcel committed Apr 27, 2024
1 parent 4aff1cb commit 4af5d3a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
8 changes: 5 additions & 3 deletions atom.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ type Atom struct {

func newAtomEntry(i *Item) *AtomEntry {
id := i.Id
// assume the description is html
s := &AtomSummary{Content: i.Description, Type: "html"}

if len(id) == 0 {
// if there's no id set, try to create one, either from data or just a uuid
Expand Down Expand Up @@ -119,7 +117,11 @@ func newAtomEntry(i *Item) *AtomEntry {
Links: []AtomLink{{Href: i.Link.Href, Rel: link_rel, Type: i.Link.Type}},
Id: id,
Updated: anyTimeFormat(time.RFC3339, i.Updated, i.Created),
Summary: s,
}

// if there's a description, assume it's html
if len(i.Description) > 0 {
x.Summary = &AtomSummary{Content: i.Description, Type: "html"}
}

// if there's a content, assume it's html
Expand Down
32 changes: 27 additions & 5 deletions feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ var atomOutput = `<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.
<link href="http://example.com/strings" rel="alternate"></link>
<summary type="html">How to use things like %s, %v, %d, etc.</summary>
</entry>
<entry>
<title>Go Proverb #1</title>
<updated>2013-01-16T21:52:35-05:00</updated>
<id>tag:go-proverbs.github.io,2013-01-16:/</id>
<content type="html">Don&#39;t communicate by sharing memory, share memory by communicating.</content>
<link href="https://go-proverbs.github.io/" rel="alternate"></link>
</entry>
</feed>`

var rssOutput = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
Expand Down Expand Up @@ -103,6 +110,13 @@ var rssOutput = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:
<description>How to use things like %s, %v, %d, etc.</description>
<pubDate>Wed, 16 Jan 2013 21:52:35 -0500</pubDate>
</item>
<item>
<title>Go Proverb #1</title>
<link>https://go-proverbs.github.io/</link>
<description></description>
<content:encoded><![CDATA[Don't communicate by sharing memory, share memory by communicating.]]></content:encoded>
<pubDate>Wed, 16 Jan 2013 21:52:35 -0500</pubDate>
</item>
</channel>
</rss>`

Expand Down Expand Up @@ -154,6 +168,13 @@ var jsonOutput = `{
"title": "String formatting in Go",
"summary": "How to use things like %s, %v, %d, etc.",
"date_published": "2013-01-16T21:52:35-05:00"
},
{
"id": "",
"url": "https://go-proverbs.github.io/",
"title": "Go Proverb #1",
"content_html": "Don't communicate by sharing memory, share memory by communicating.",
"date_published": "2013-01-16T21:52:35-05:00"
}
]
}`
Expand Down Expand Up @@ -209,6 +230,12 @@ func TestFeed(t *testing.T) {
Link: &Link{Href: "http://example.com/strings"},
Description: "How to use things like %s, %v, %d, etc.",
Created: now,
},
{
Title: "Go Proverb #1",
Link: &Link{Href: "https://go-proverbs.github.io/"},
Content: "Don't communicate by sharing memory, share memory by communicating.",
Created: now,
}}

atom, err := feed.ToAtom()
Expand Down Expand Up @@ -273,35 +300,30 @@ var atomOutputSorted = `<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http:
<updated>2013-01-18T21:52:35-05:00</updated>
<id>tag:jmoiron.net,2013-01-18:/blog/limiting-concurrency-in-go/</id>
<link href="http://jmoiron.net/blog/limiting-concurrency-in-go/" rel="alternate"></link>
<summary type="html"></summary>
</entry>
<entry>
<title>Logic-less Template Redux</title>
<updated>2013-01-17T21:52:35-05:00</updated>
<id>tag:jmoiron.net,2013-01-17:/blog/logicless-template-redux/</id>
<link href="http://jmoiron.net/blog/logicless-template-redux/" rel="alternate"></link>
<summary type="html"></summary>
</entry>
<entry>
<title>Idiomatic Code Reuse in Go</title>
<updated>2013-01-17T09:52:35-05:00</updated>
<id>tag:jmoiron.net,2013-01-17:/blog/idiomatic-code-reuse-in-go/</id>
<link href="http://jmoiron.net/blog/idiomatic-code-reuse-in-go/" rel="alternate"></link>
<summary type="html"></summary>
</entry>
<entry>
<title>Never Gonna Give You Up Mp3</title>
<updated>2013-01-17T07:52:35-05:00</updated>
<id>tag:example.com,2013-01-17:/RickRoll.mp3</id>
<link href="http://example.com/RickRoll.mp3" rel="alternate"></link>
<summary type="html"></summary>
</entry>
<entry>
<title>String formatting in Go</title>
<updated>2013-01-16T21:52:35-05:00</updated>
<id>tag:example.com,2013-01-16:/strings</id>
<link href="http://example.com/strings" rel="alternate"></link>
<summary type="html"></summary>
</entry>
</feed>`

Expand Down

0 comments on commit 4af5d3a

Please sign in to comment.