Add document for lib/rss/atom.rb

[fix GH-1520]

Patch by Tsehau Chao. Thanks!!!


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57428 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kou 2017-01-26 12:47:51 +00:00
parent 8fa99e6458
commit fdbb4bb3e2

View File

@ -37,10 +37,12 @@ module RSS
end end
klass.class_eval do klass.class_eval do
class << self class << self
# Returns the Atom URI W3C Namespace
def required_uri def required_uri
URI URI
end end
# Returns true
def need_parent? def need_parent?
true true
end end
@ -92,9 +94,11 @@ module RSS
end end
# The TextConstruct module is used to define a Text construct Atom element, # The TextConstruct module is used to define a Text construct Atom element,
# which is used to store small quantities of human-readable text # which is used to store small quantities of human-readable text.
# #
# The TextConstruct has a type attribute, e.g. text, html, xhtml # The TextConstruct has a type attribute, e.g. text, html, xhtml
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#text.constructs
module TextConstruct module TextConstruct
def self.append_features(klass) def self.append_features(klass)
super super
@ -122,6 +126,7 @@ module RSS
attr_writer :xhtml attr_writer :xhtml
# Returns or builds the XHTML content.
def xhtml def xhtml
return @xhtml if @xhtml.nil? return @xhtml if @xhtml.nil?
if @xhtml.is_a?(XML::Element) and if @xhtml.is_a?(XML::Element) and
@ -135,11 +140,13 @@ module RSS
{"xmlns" => XHTML_URI}, children) {"xmlns" => XHTML_URI}, children)
end end
# Returns true if type is "xhtml" # Returns true if type is "xhtml".
def have_xml_content? def have_xml_content?
@type == "xhtml" @type == "xhtml"
end end
# Raises a MissingTagError or NotExpectedTagError
# if the element is not properly formatted.
def atom_validate(ignore_unknown_element, tags, uri) def atom_validate(ignore_unknown_element, tags, uri)
if have_xml_content? if have_xml_content?
if @xhtml.nil? if @xhtml.nil?
@ -163,10 +170,12 @@ module RSS
end end
end end
# The PersonConstruct module is used to define a Person Atom element that can be # The PersonConstruct module is used to define a person Atom element that can be
# used to describe a person, corporation, or similar entity # used to describe a person, corporation or similar entity.
# #
# The PersonConstruct has a Name, Uri, and Email child elements # The PersonConstruct has a Name, Uri and Email child elements.
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#atomPersonConstruct
module PersonConstruct module PersonConstruct
# Adds attributes for name, uri, and email to the +klass+ # Adds attributes for name, uri, and email to the +klass+
@ -187,19 +196,25 @@ module RSS
target.__send__("new_#{self.class.name.split(/::/).last.downcase}") target.__send__("new_#{self.class.name.split(/::/).last.downcase}")
end end
# The name of the person or entity # The name of the person or entity.
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.name
class Name < RSS::Element class Name < RSS::Element
include CommonModel include CommonModel
include ContentModel include ContentModel
end end
# The URI of the person or entity # The URI of the person or entity.
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.uri
class Uri < RSS::Element class Uri < RSS::Element
include CommonModel include CommonModel
include URIContentModel include URIContentModel
end end
# The email of the person or entity # The email of the person or entity.
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.email
class Email < RSS::Element class Email < RSS::Element
include CommonModel include CommonModel
include ContentModel include ContentModel
@ -245,9 +260,28 @@ module RSS
end end
end end
# Atom feed element # Defines the top-level element of an Atom Feed Document.
# It consists of a number of children Entry elements,
# and has the following attributes:
# #
# A Feed has several metadata attributes in addition to a number of Entry child elements # * author
# * categories
# * category
# * content
# * contributor
# * entries (aliased as items)
# * entry
# * generator
# * icon
# * id
# * link
# * logo
# * rights
# * subtitle
# * title
# * updated
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.feed
class Feed < RSS::Element class Feed < RSS::Element
include RootElementMixin include RootElementMixin
include CommonModel include CommonModel
@ -322,11 +356,23 @@ module RSS
end end
end end
# PersonConstruct that contains information regarding the author
# of a Feed or Entry.
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.author
class Author < RSS::Element class Author < RSS::Element
include CommonModel include CommonModel
include PersonConstruct include PersonConstruct
end end
# Contains information about a category associated with a Feed or Entry.
# It has the following attributes:
#
# * term
# * scheme
# * label
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.category
class Category < RSS::Element class Category < RSS::Element
include CommonModel include CommonModel
@ -344,11 +390,18 @@ module RSS
end end
end end
# PersonConstruct that contains information regarding the
# contributors of a Feed or Entry.
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.contributor
class Contributor < RSS::Element class Contributor < RSS::Element
include CommonModel include CommonModel
include PersonConstruct include PersonConstruct
end end
# Contains information on the agent used to generate the feed.
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.generator
class Generator < RSS::Element class Generator < RSS::Element
include CommonModel include CommonModel
include ContentModel include ContentModel
@ -369,32 +422,34 @@ module RSS
end end
end end
# Atom Icon element # Defines an image that provides a visual identification for a eed.
# The image should have an aspect ratio of 1:1.
# #
# Image that provides a visual identification for the Feed. Image should have an aspect # Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.icon
# ratio of 1:1
class Icon < RSS::Element class Icon < RSS::Element
include CommonModel include CommonModel
include URIContentModel include URIContentModel
end end
# Atom ID element # Defines the Universally Unique Identifier (UUID) for a Feed or Entry.
# #
# Universally Unique Identifier (UUID) for the Feed # Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.id
class Id < RSS::Element class Id < RSS::Element
include CommonModel include CommonModel
include URIContentModel include URIContentModel
end end
# Defines an Atom Link element # Defines a reference to a Web resource. It has the following
# attributes:
# #
# A Link has the following attributes:
# * href # * href
# * rel # * rel
# * type # * type
# * hreflang # * hreflang
# * title # * title
# * length # * length
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.link
class Link < RSS::Element class Link < RSS::Element
include CommonModel include CommonModel
@ -415,10 +470,10 @@ module RSS
end end
end end
# Atom Logo element # Defines an image that provides a visual identification for the Feed.
# The image should have an aspect ratio of 2:1 (horizontal:vertical).
# #
# Image that provides a visual identification for the Feed. Image should have an aspect # Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.logo
# ratio of 2:1 (horizontal:vertical)
class Logo < RSS::Element class Logo < RSS::Element
include CommonModel include CommonModel
include URIContentModel include URIContentModel
@ -433,40 +488,60 @@ module RSS
end end
end end
# Atom Rights element # TextConstruct that contains copyright information regarding
# the content in an Entry or Feed. It should not be used to
# convey machine readable licensing information.
# #
# TextConstruct that contains copyright information regarding the content in an Entry or Feed # Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.rights
class Rights < RSS::Element class Rights < RSS::Element
include CommonModel include CommonModel
include TextConstruct include TextConstruct
end end
# Atom Subtitle element # TextConstruct that conveys a description or subtitle for a Feed.
# #
# TextConstruct that conveys a description or subtitle for a Feed # Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.subtitle
class Subtitle < RSS::Element class Subtitle < RSS::Element
include CommonModel include CommonModel
include TextConstruct include TextConstruct
end end
# Atom Title element # TextConstruct that conveys a description or title for a Feed or Entry.
# #
# TextConstruct that conveys a description or title for a feed or Entry # Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.title
class Title < RSS::Element class Title < RSS::Element
include CommonModel include CommonModel
include TextConstruct include TextConstruct
end end
# Atom Updated element # DateConstruct indicating the most recent time when a Feed or
# Entry was modified in a way the publisher considers
# significant.
# #
# DateConstruct indicating the most recent time when an Entry or Feed was modified # Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.updated
# in a way the publisher considers significant
class Updated < RSS::Element class Updated < RSS::Element
include CommonModel include CommonModel
include DateConstruct include DateConstruct
end end
# Defines a child Atom Entry element for an Atom Feed # Defines a child Atom Entry element of an Atom Feed element.
# It has the following attributes:
#
# * author
# * category
# * categories
# * content
# * contributor
# * id
# * link
# * published
# * rights
# * source
# * summary
# * title
# * updated
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.entry
class Entry < RSS::Element class Entry < RSS::Element
include CommonModel include CommonModel
include DuplicateLinkChecker include DuplicateLinkChecker
@ -490,9 +565,11 @@ module RSS
tag, URI, occurs, tag, *args) tag, URI, occurs, tag, *args)
end end
# Returns whether any of the following are true # Returns whether any of the following are true:
#
# * There are any authors in the feed # * There are any authors in the feed
# * If the parent element has an author and the +check_parent+ parameter was given. # * If the parent element has an author and the +check_parent+
# parameter was given.
# * There is a source element that has an author # * There is a source element that has an author
def have_author?(check_parent=true) def have_author?(check_parent=true)
authors.any? {|author| !author.to_s.empty?} or authors.any? {|author| !author.to_s.empty?} or
@ -520,9 +597,18 @@ module RSS
items.new_item items.new_item
end end
# Feed::Author
Author = Feed::Author Author = Feed::Author
# Feed::Category
Category = Feed::Category Category = Feed::Category
# Contains or links to the content of the Entry.
# It has the following attributes:
#
# * type
# * src
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.content
class Content < RSS::Element class Content < RSS::Element
include CommonModel include CommonModel
@ -546,11 +632,15 @@ module RSS
content_setup content_setup
add_need_initialize_variable("xml") add_need_initialize_variable("xml")
# Returns the element content in XML.
attr_writer :xml attr_writer :xml
# Returns true if the element has inline XML content.
def have_xml_content? def have_xml_content?
inline_xhtml? or inline_other_xml? inline_xhtml? or inline_other_xml?
end end
# Returns or builds the element content in XML.
def xml def xml
return @xml unless inline_xhtml? return @xml unless inline_xhtml?
return @xml if @xml.nil? return @xml if @xml.nil?
@ -565,6 +655,7 @@ module RSS
{"xmlns" => XHTML_URI}, children) {"xmlns" => XHTML_URI}, children)
end end
# Returns the element content in XHTML.
def xhtml def xhtml
if inline_xhtml? if inline_xhtml?
xml xml
@ -573,6 +664,9 @@ module RSS
end end
end end
# Raises a MissingAttributeError, NotAvailableValueError,
# MissingTagError or NotExpectedTagError if the element is
# not properly formatted.
def atom_validate(ignore_unknown_element, tags, uri) def atom_validate(ignore_unknown_element, tags, uri)
if out_of_line? if out_of_line?
raise MissingAttributeError.new(tag_name, "type") if @type.nil? raise MissingAttributeError.new(tag_name, "type") if @type.nil?
@ -589,19 +683,27 @@ module RSS
end end
end end
# Returns true if the element contains inline content
# that has a text or HTML media type, or no media type at all.
def inline_text? def inline_text?
!out_of_line? and [nil, "text", "html"].include?(@type) !out_of_line? and [nil, "text", "html"].include?(@type)
end end
# Returns true if the element contains inline content that
# has a HTML media type.
def inline_html? def inline_html?
return false if out_of_line? return false if out_of_line?
@type == "html" or mime_split == ["text", "html"] @type == "html" or mime_split == ["text", "html"]
end end
# Returns true if the element contains inline content that
# has a XHTML media type.
def inline_xhtml? def inline_xhtml?
!out_of_line? and @type == "xhtml" !out_of_line? and @type == "xhtml"
end end
# Returns true if the element contains inline content that
# has a MIME media type.
def inline_other? def inline_other?
return false if out_of_line? return false if out_of_line?
media_type, subtype = mime_split media_type, subtype = mime_split
@ -609,6 +711,8 @@ module RSS
true true
end end
# Returns true if the element contains inline content that
# has a text media type.
def inline_other_text? def inline_other_text?
return false unless inline_other? return false unless inline_other?
return false if inline_other_xml? return false if inline_other_xml?
@ -618,6 +722,8 @@ module RSS
false false
end end
# Returns true if the element contains inline content that
# has a XML media type.
def inline_other_xml? def inline_other_xml?
return false unless inline_other? return false unless inline_other?
@ -632,14 +738,18 @@ module RSS
false false
end end
# Returns true if the element contains inline content
# encoded in base64.
def inline_other_base64? def inline_other_base64?
inline_other? and !inline_other_text? and !inline_other_xml? inline_other? and !inline_other_text? and !inline_other_xml?
end end
# Returns true if the element contains linked content.
def out_of_line? def out_of_line?
not @src.nil? not @src.nil?
end end
# Splits the type attribute into an array, e.g. ["text", "xml"]
def mime_split def mime_split
media_type = subtype = nil media_type = subtype = nil
if /\A\s*([a-z]+)\/([a-z\+]+)\s*(?:;.*)?\z/i =~ @type.to_s if /\A\s*([a-z]+)\/([a-z\+]+)\s*(?:;.*)?\z/i =~ @type.to_s
@ -649,6 +759,7 @@ module RSS
[media_type, subtype] [media_type, subtype]
end end
# Returns true if the content needs to be encoded in base64.
def need_base64_encode? def need_base64_encode?
inline_other_base64? inline_other_base64?
end end
@ -659,17 +770,43 @@ module RSS
end end
end end
# Feed::Contributor
Contributor = Feed::Contributor Contributor = Feed::Contributor
# Feed::Id
Id = Feed::Id Id = Feed::Id
# Feed::Link
Link = Feed::Link Link = Feed::Link
# DateConstruct that usually indicates the time of the initial
# creation of an Entry.
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.published
class Published < RSS::Element class Published < RSS::Element
include CommonModel include CommonModel
include DateConstruct include DateConstruct
end end
# Feed::Rights
Rights = Feed::Rights Rights = Feed::Rights
# Defines a Atom Source element. It has the following attributes:
#
# * author
# * category
# * categories
# * content
# * contributor
# * generator
# * icon
# * id
# * link
# * logo
# * rights
# * subtitle
# * title
# * updated
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.source
class Source < RSS::Element class Source < RSS::Element
include CommonModel include CommonModel
@ -692,36 +829,71 @@ module RSS
tag, URI, occurs, tag, *args) tag, URI, occurs, tag, *args)
end end
# Returns true if the Source element has an author.
def have_author? def have_author?
!author.to_s.empty? !author.to_s.empty?
end end
# Feed::Author
Author = Feed::Author Author = Feed::Author
# Feed::Category
Category = Feed::Category Category = Feed::Category
# Feed::Contributor
Contributor = Feed::Contributor Contributor = Feed::Contributor
# Feed::Generator
Generator = Feed::Generator Generator = Feed::Generator
# Feed::Icon
Icon = Feed::Icon Icon = Feed::Icon
# Feed::Id
Id = Feed::Id Id = Feed::Id
# Feed::Link
Link = Feed::Link Link = Feed::Link
# Feed::Logo
Logo = Feed::Logo Logo = Feed::Logo
# Feed::Rights
Rights = Feed::Rights Rights = Feed::Rights
# Feed::Subtitle
Subtitle = Feed::Subtitle Subtitle = Feed::Subtitle
# Feed::Title
Title = Feed::Title Title = Feed::Title
# Feed::Updated
Updated = Feed::Updated Updated = Feed::Updated
end end
# TextConstruct that describes a summary of the Entry.
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.summary
class Summary < RSS::Element class Summary < RSS::Element
include CommonModel include CommonModel
include TextConstruct include TextConstruct
end end
# Feed::Title
Title = Feed::Title Title = Feed::Title
# Feed::Updated
Updated = Feed::Updated Updated = Feed::Updated
end end
end end
# Defines a top-level Atom Entry element # Defines a top-level Atom Entry element,
# used as the document element of a stand-alone Atom Entry Document.
# It has the following attributes:
# #
# * author
# * category
# * categories
# * content
# * contributor
# * id
# * link
# * published
# * rights
# * source
# * summary
# * title
# * updated
#
# Reference: https://validator.w3.org/feed/docs/rfc4287.html#element.entry]
class Entry < RSS::Element class Entry < RSS::Element
include RootElementMixin include RootElementMixin
include CommonModel include CommonModel
@ -746,25 +918,26 @@ module RSS
tag, URI, occurs, tag, *args) tag, URI, occurs, tag, *args)
end end
# Creates a new Atom Entry element # Creates a new Atom Entry element.
def initialize(version=nil, encoding=nil, standalone=nil) def initialize(version=nil, encoding=nil, standalone=nil)
super("1.0", version, encoding, standalone) super("1.0", version, encoding, standalone)
@feed_type = "atom" @feed_type = "atom"
@feed_subtype = "entry" @feed_subtype = "entry"
end end
# Returns the Entry in an array # Returns the Entry in an array.
def items def items
[self] [self]
end end
# sets up the +maker+ for constructing Entry elements # Sets up the +maker+ for constructing Entry elements.
def setup_maker(maker) def setup_maker(maker)
maker = maker.maker if maker.respond_to?("maker") maker = maker.maker if maker.respond_to?("maker")
super(maker) super(maker)
end end
# Returns where there are any authors present or there is a source with an author # Returns where there are any authors present or there is a
# source with an author.
def have_author? def have_author?
authors.any? {|author| !author.to_s.empty?} or authors.any? {|author| !author.to_s.empty?} or
(source and source.have_author?) (source and source.have_author?)
@ -786,17 +959,29 @@ module RSS
maker.items.new_item maker.items.new_item
end end
# Feed::Entry::Author
Author = Feed::Entry::Author Author = Feed::Entry::Author
# Feed::Entry::Category
Category = Feed::Entry::Category Category = Feed::Entry::Category
# Feed::Entry::Content
Content = Feed::Entry::Content Content = Feed::Entry::Content
# Feed::Entry::Contributor
Contributor = Feed::Entry::Contributor Contributor = Feed::Entry::Contributor
# Feed::Entry::Id
Id = Feed::Entry::Id Id = Feed::Entry::Id
# Feed::Entry::Link
Link = Feed::Entry::Link Link = Feed::Entry::Link
# Feed::Entry::Published
Published = Feed::Entry::Published Published = Feed::Entry::Published
# Feed::Entry::Rights
Rights = Feed::Entry::Rights Rights = Feed::Entry::Rights
# Feed::Entry::Source
Source = Feed::Entry::Source Source = Feed::Entry::Source
# Feed::Entry::Summary
Summary = Feed::Entry::Summary Summary = Feed::Entry::Summary
# Feed::Entry::Title
Title = Feed::Entry::Title Title = Feed::Entry::Title
# Feed::Entry::Updated
Updated = Feed::Entry::Updated Updated = Feed::Entry::Updated
end end
end end