Permalink
Browse files

Several creators for FeedItem ?

Hi!

Le Monday 05 January 2009 12:30:17 Lucas Nussbaum, vous avez écrit :
> I think that the best way to deal with that in a backward-compatible way
> would be to provide a "authors" method, that would return an array of
> authors. the "author" method would build a string with the list of
> authors (using "a, b, c and d" format).
> Could you make a patch for that?

Please find enclosed a patch to deal with multiple authors in feed items.
All the tests passed correctly, and the code seems to do what it should, at
least with arXiv atom feeds. Do not hesitate to criticize. I am far from
being a super-coder.

At the moment, only the entries may handle multiple authors. I believe that
the same issue is present for the feed in itself. It may concern also
different tags, such as categories. I could try to make patches for these, if
you wish.

Tell me what you think.

Best regards,

Cédric
  • Loading branch information...
1 parent 50edd06 commit 5df3579275423dcd246427e35f5cf14a29ff9954 @boutil boutil committed with lnussbaum Jan 5, 2009
Showing with 28 additions and 10 deletions.
  1. +26 −8 lib/feedparser/feedparser.rb
  2. +1 −1 lib/feedparser/html-output.rb
  3. +1 −1 lib/feedparser/text-output.rb
View
34 lib/feedparser/feedparser.rb
@@ -119,7 +119,7 @@ def to_s(localtime = true)
# an Item from a feed
class FeedItem
- attr_accessor :title, :link, :content, :date, :creator, :subject,
+ attr_accessor :title, :link, :content, :date, :creators, :subject,
:category, :cacheditem
# The item's enclosures childs. An array of (url, length, type) triplets.
@@ -133,7 +133,8 @@ class FeedItem
def initialize(item = nil, feed = nil)
@xml = item
@feed = feed
- @title, @link, @content, @date, @creator, @subject, @category = nil
+ @title, @link, @content, @date, @subject, @category = nil
+ @creators = []
@enclosures = []
parse(item) if item
end
@@ -142,6 +143,17 @@ def parse(item)
raise "parse() should be implemented by subclasses!"
end
+ def creator
+ case @creators.length
+ when 0
+ return nil
+ when 1
+ return creators[0]
+ else
+ return creators[0...-1].join(", ")+" and "+creators[-1]
+ end
+ end
+
def to_s(localtime = true)
s = "--------------------------------\n" +
"Title: #{@title}\nLink: #{@link}\n"
@@ -150,7 +162,7 @@ def to_s(localtime = true)
else
s += "Date: #{@date.getutc.to_s}\n"
end
- s += "Creator: #{@creator}\n" +
+ s += "Creator: #{creator}\n" +
"Subject: #{@subject}\nCategory: #{@category}\nContent:\n#{content}\n"
if defined?(@enclosures) and @enclosures.length > 0
s2 = "Enclosures:\n"
@@ -204,11 +216,14 @@ def parse(item)
end
end
# Creator
- @creator = @feed.creator
+
if (e = item.elements['dc:creator'] || item.elements['author'] ||
item.elements['rss:author']) && e.text
- @creator = e.text.unescape_html.toUTF8(@feed.encoding).rmWhiteSpace!
+ @creators << e.text.unescape_html.toUTF8(@feed.encoding).rmWhiteSpace!
end
+
+ @creators << @feed.creator if @creators.empty? and @feed.creator
+
# Subject
if (e = item.elements['dc:subject']) && e.text
@subject = e.text.unescape_html.toUTF8(@feed.encoding).rmWhiteSpace!
@@ -266,10 +281,13 @@ def parse(item)
end
end
# Creator
- @creator = @feed.creator
- if (e = item.elements['author/name']) && e.text
- @creator = e.text.unescape_html.toUTF8(@feed.encoding).rmWhiteSpace!
+ item.each_element('author/name') do |e|
+ if e.text
+ @creators << e.text.unescape_html.toUTF8(@feed.encoding).rmWhiteSpace!
+ end
end
+
+ @creators << @feed.creator if @creators.empty? and @feed.creator
end
end
View
2 lib/feedparser/html-output.rb
@@ -116,7 +116,7 @@ def to_html(localtime = true)
s += l % [ 'Date:', @date.getutc.to_s ]
end
end
- s += l % [ 'Author:', @creator.escape_html ] if @creator
+ s += l % [ 'Author:', creator.escape_html ] if creator
s += l % [ 'Subject:', @subject.escape_html ] if @subject
s += l % [ 'Category:', @category.escape_html ] if @category
s += "</table>\n"
View
2 lib/feedparser/text-output.rb
@@ -73,7 +73,7 @@ def to_text(localtime = true, wrapto = false)
s += "\nDate: #{@date.getutc.to_s}"
end
end
- s += "\nAuthor: #{@creator}" if @creator
+ s += "\nAuthor: #{creator}" if creator
s += "\nSubject: #{@subject}" if @subject
s += "\nCategory: #{@category}" if @category
s += "\n\n"

0 comments on commit 5df3579

Please sign in to comment.