mirror of
https://github.com/ruby/ruby.git
synced 2025-08-23 13:04:13 +02:00
* lib/rss/{rss,parser,0.9,1.0,2.0}.rb: supported RSS 0.9x/2.0
validation and validation which disregard order of elements. * test/rss/test_parser.rb: added tests for RSS 0.9x/2.0 validation. * test/rss/{test_trackback,rss-testcase}.rb: fixed no good method name. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6590 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
9800838ff0
commit
e289fcf81a
9 changed files with 446 additions and 113 deletions
166
lib/rss/0.9.rb
166
lib/rss/0.9.rb
|
@ -5,6 +5,12 @@ module RSS
|
|||
module RSS09
|
||||
NSPOOL = {}
|
||||
ELEMENTS = []
|
||||
|
||||
def self.append_features(klass)
|
||||
super
|
||||
|
||||
klass.install_must_call_validator('', nil)
|
||||
end
|
||||
end
|
||||
|
||||
class Rss < Element
|
||||
|
@ -62,22 +68,34 @@ EOR
|
|||
[@channel]
|
||||
end
|
||||
|
||||
def _tags
|
||||
[
|
||||
[nil, 'channel'],
|
||||
].delete_if {|x| send(x[1]).nil?}
|
||||
end
|
||||
|
||||
def _attrs
|
||||
[
|
||||
["version", true],
|
||||
]
|
||||
end
|
||||
|
||||
class Channel < Element
|
||||
|
||||
include RSS09
|
||||
|
||||
[
|
||||
["title", nil],
|
||||
["link", nil],
|
||||
["description", nil],
|
||||
["language", nil],
|
||||
["copyright", "?"],
|
||||
["managingEditor", "?"],
|
||||
["webMaster", "?"],
|
||||
["rating", "?"],
|
||||
["docs", "?"],
|
||||
["skipDays", "?"],
|
||||
["skipHours", "?"],
|
||||
["link", nil],
|
||||
["description", nil],
|
||||
["language", nil],
|
||||
["copyright", "?"],
|
||||
["managingEditor", "?"],
|
||||
["webMaster", "?"],
|
||||
["rating", "?"],
|
||||
["docs", "?"],
|
||||
["skipDays", "?"],
|
||||
["skipHours", "?"],
|
||||
].each do |x, occurs|
|
||||
install_text_element(x)
|
||||
install_model(x, occurs)
|
||||
|
@ -85,7 +103,7 @@ EOR
|
|||
|
||||
[
|
||||
["pubDate", "?"],
|
||||
["lastBuildDate", "?"],
|
||||
["lastBuildDate", "?"],
|
||||
].each do |x, occurs|
|
||||
install_date_element(x, 'rfc822')
|
||||
install_model(x, occurs)
|
||||
|
@ -93,13 +111,19 @@ EOR
|
|||
|
||||
[
|
||||
["image", nil],
|
||||
["textInput", "?"],
|
||||
["cloud", "?"]
|
||||
["textInput", "?"],
|
||||
].each do |x, occurs|
|
||||
install_have_child_element(x)
|
||||
install_model(x, occurs)
|
||||
end
|
||||
|
||||
[
|
||||
["cloud", "?"]
|
||||
].each do |x, occurs|
|
||||
install_have_attribute_element(x)
|
||||
install_model(x, occurs)
|
||||
end
|
||||
|
||||
[
|
||||
["item", "*"]
|
||||
].each do |x, occurs|
|
||||
|
@ -142,12 +166,46 @@ EOT
|
|||
[@image, @textInput, @cloud, *@item]
|
||||
end
|
||||
|
||||
def _tags
|
||||
rv = [
|
||||
"title",
|
||||
"link",
|
||||
"description",
|
||||
"language",
|
||||
"copyright",
|
||||
"managingEditor",
|
||||
"webMaster",
|
||||
"rating",
|
||||
"docs",
|
||||
"skipDays",
|
||||
"skipHours",
|
||||
"image",
|
||||
"textInput",
|
||||
"cloud",
|
||||
].delete_if do |x|
|
||||
send(x).nil?
|
||||
end.collect do |elem|
|
||||
[nil, elem]
|
||||
end
|
||||
|
||||
@item.each do
|
||||
rv << [nil, "item"]
|
||||
end
|
||||
|
||||
rv
|
||||
end
|
||||
|
||||
class Image < Element
|
||||
|
||||
include RSS09
|
||||
|
||||
%w(url title link width height description).each do |x|
|
||||
%w(url title link).each do |x|
|
||||
install_text_element(x)
|
||||
install_model(x, nil)
|
||||
end
|
||||
%w(width height description).each do |x|
|
||||
install_text_element(x)
|
||||
install_model(x, "?")
|
||||
end
|
||||
|
||||
def to_s(convert=true)
|
||||
|
@ -166,6 +224,14 @@ EOT
|
|||
rv
|
||||
end
|
||||
|
||||
private
|
||||
def _tags
|
||||
%w(url title link width height description).delete_if do |x|
|
||||
send(x).nil?
|
||||
end.collect do |elem|
|
||||
[nil, elem]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Cloud < Element
|
||||
|
@ -173,15 +239,24 @@ EOT
|
|||
include RSS09
|
||||
|
||||
[
|
||||
["domain", nil, false],
|
||||
["port", nil, false],
|
||||
["path", nil, false],
|
||||
["registerProcedure", nil, false],
|
||||
["protocol", nil ,false],
|
||||
["domain", nil, true],
|
||||
["port", nil, true],
|
||||
["path", nil, true],
|
||||
["registerProcedure", nil, true],
|
||||
["protocol", nil ,true],
|
||||
].each do |name, uri, required|
|
||||
install_get_attribute(name, uri, required)
|
||||
end
|
||||
|
||||
def initialize(domain, port, path, rp, protocol)
|
||||
super()
|
||||
@domain = domain
|
||||
@port = port
|
||||
@path = path
|
||||
@registerProcedure = rp
|
||||
@protocol = protocol
|
||||
end
|
||||
|
||||
def to_s(convert=true)
|
||||
rv = <<-EOT
|
||||
<cloud
|
||||
|
@ -195,13 +270,20 @@ EOT
|
|||
rv
|
||||
end
|
||||
|
||||
private
|
||||
def _attrs
|
||||
%w(domain port path registerProcedure protocol).collect do |attr|
|
||||
[attr, true]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Item < Element
|
||||
|
||||
include RSS09
|
||||
|
||||
%w(title link description author comments).each do |x|
|
||||
%w(title link description).each do |x|
|
||||
install_text_element(x)
|
||||
end
|
||||
|
||||
|
@ -213,8 +295,6 @@ EOT
|
|||
["title", '?'],
|
||||
["link", '?'],
|
||||
["description", '?'],
|
||||
["author", '?'],
|
||||
["comments", '?'],
|
||||
["category", '?'],
|
||||
["source", '?'],
|
||||
["enclosure", '?'],
|
||||
|
@ -228,11 +308,9 @@ EOT
|
|||
#{title_element(false)}
|
||||
#{link_element(false)}
|
||||
#{description_element(false)}
|
||||
#{author_element(false)}
|
||||
#{category_element(false)}
|
||||
#{comments_element(false)}
|
||||
#{enclosure_element(false)}
|
||||
#{source_element(false)}
|
||||
#{enclosure_element(false)}
|
||||
#{other_element(false, "\t\t\t\t")}
|
||||
</item>
|
||||
EOT
|
||||
|
@ -240,6 +318,20 @@ EOT
|
|||
rv
|
||||
end
|
||||
|
||||
private
|
||||
def children
|
||||
[@category, @source, @enclosure,].compact
|
||||
end
|
||||
|
||||
def _tags
|
||||
%w(title link description author comments category
|
||||
source enclosure).delete_if do |x|
|
||||
send(x).nil?
|
||||
end.collect do |x|
|
||||
[nil, x]
|
||||
end
|
||||
end
|
||||
|
||||
class Source < Element
|
||||
|
||||
include RSS09
|
||||
|
@ -249,7 +341,7 @@ EOT
|
|||
].each do |name, uri, required|
|
||||
install_get_attribute(name, uri, required)
|
||||
end
|
||||
|
||||
|
||||
content_setup
|
||||
|
||||
def initialize(url=nil, content=nil)
|
||||
|
@ -270,6 +362,10 @@ EOT
|
|||
end
|
||||
|
||||
private
|
||||
def _tags
|
||||
[]
|
||||
end
|
||||
|
||||
def _attrs
|
||||
[
|
||||
["url", true]
|
||||
|
@ -284,8 +380,8 @@ EOT
|
|||
|
||||
[
|
||||
["url", nil, true],
|
||||
["length", nil, true],
|
||||
["type", nil, true],
|
||||
["length", nil, true],
|
||||
["type", nil, true],
|
||||
].each do |name, uri, required|
|
||||
install_get_attribute(name, uri, required)
|
||||
end
|
||||
|
@ -312,8 +408,8 @@ EOT
|
|||
def _attrs
|
||||
[
|
||||
["url", true],
|
||||
["length", true],
|
||||
["type", true],
|
||||
["length", true],
|
||||
["type", true],
|
||||
]
|
||||
end
|
||||
|
||||
|
@ -365,6 +461,7 @@ EOT
|
|||
|
||||
%w(title description name link).each do |x|
|
||||
install_text_element(x)
|
||||
install_model(x, nil)
|
||||
end
|
||||
|
||||
def to_s(convert=true)
|
||||
|
@ -381,6 +478,14 @@ EOT
|
|||
rv
|
||||
end
|
||||
|
||||
private
|
||||
def _tags
|
||||
%w(title description name link).each do |x|
|
||||
send(x).nil?
|
||||
end.collect do |elem|
|
||||
[nil, elem]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -397,6 +502,7 @@ EOT
|
|||
check_ns(tag_name, prefix, ns, nil)
|
||||
|
||||
@rss = Rss.new(attrs['version'], @version, @encoding, @standalone)
|
||||
@rss.do_validate = @do_validate
|
||||
@rss.xml_stylesheets = @xml_stylesheets
|
||||
@last_element = @rss
|
||||
@proc_stack.push Proc.new { |text, tags|
|
||||
|
|
|
@ -5,6 +5,13 @@ module RSS
|
|||
module RSS10
|
||||
NSPOOL = {}
|
||||
ELEMENTS = []
|
||||
|
||||
def self.append_features(klass)
|
||||
super
|
||||
|
||||
klass.install_must_call_validator('', ::RSS::URI)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class RDF < Element
|
||||
|
|
|
@ -4,30 +4,21 @@ module RSS
|
|||
|
||||
class Rss
|
||||
|
||||
# URI = "http://backend.userland.com/rss2"
|
||||
|
||||
# install_ns('', URI)
|
||||
|
||||
# def self.required_uri
|
||||
# URI
|
||||
# end
|
||||
|
||||
class Channel
|
||||
|
||||
# def self.required_uri
|
||||
# URI
|
||||
# end
|
||||
|
||||
%w(generator ttl).each do |x|
|
||||
install_text_element(x)
|
||||
install_model(x, '?')
|
||||
end
|
||||
|
||||
%w(category).each do |x|
|
||||
install_have_child_element(x)
|
||||
install_model(x, '?')
|
||||
end
|
||||
|
||||
[
|
||||
["image", "?"],
|
||||
["language", "?"],
|
||||
].each do |x, occurs|
|
||||
install_model(x, occurs)
|
||||
end
|
||||
|
@ -40,17 +31,33 @@ module RSS
|
|||
EOT
|
||||
rv << super
|
||||
end
|
||||
|
||||
private
|
||||
alias children09 children
|
||||
def children
|
||||
children09 + [@category].compact
|
||||
end
|
||||
|
||||
alias _tags09 _tags
|
||||
def _tags
|
||||
%w(generator ttl category).delete_if do |x|
|
||||
send(x).nil?
|
||||
end.collect do |elem|
|
||||
[nil, elem]
|
||||
end + _tags09
|
||||
end
|
||||
|
||||
Category = Item::Category
|
||||
# def Category.required_uri
|
||||
# URI
|
||||
# end
|
||||
|
||||
class Item
|
||||
|
||||
# def self.required_uri
|
||||
# URI
|
||||
# end
|
||||
[
|
||||
["comments", "?"],
|
||||
["author", "?"],
|
||||
].each do |x, occurs|
|
||||
install_text_element(x)
|
||||
install_model(x, occurs)
|
||||
end
|
||||
|
||||
[
|
||||
["pubDate", '?'],
|
||||
|
@ -68,20 +75,33 @@ EOT
|
|||
|
||||
def other_element(convert, indent='')
|
||||
rv = <<-EOT
|
||||
#{indent}#{author_element(false)}
|
||||
#{indent}#{comments_element(false)}
|
||||
#{indent}#{pubDate_element(false)}
|
||||
#{indent}#{guid_element(false)}
|
||||
EOT
|
||||
rv << super
|
||||
end
|
||||
|
||||
private
|
||||
alias children09 children
|
||||
def children
|
||||
children09 + [@guid].compact
|
||||
end
|
||||
|
||||
alias _tags09 _tags
|
||||
def _tags
|
||||
%w(comments author pubDate guid).delete_if do |x|
|
||||
send(x).nil?
|
||||
end.collect do |elem|
|
||||
[nil, elem]
|
||||
end + _tags09
|
||||
end
|
||||
|
||||
class Guid < Element
|
||||
|
||||
include RSS09
|
||||
|
||||
# def self.required_uri
|
||||
# URI
|
||||
# end
|
||||
|
||||
[
|
||||
["isPermaLink", nil, false]
|
||||
].each do |name, uri, required|
|
||||
|
@ -124,24 +144,7 @@ EOT
|
|||
end
|
||||
|
||||
RSS09::ELEMENTS.each do |x|
|
||||
# BaseListener.install_get_text_element(x, Rss::URI, "#{x}=")
|
||||
BaseListener.install_get_text_element(x, nil, "#{x}=")
|
||||
end
|
||||
|
||||
module ListenerMixin
|
||||
private
|
||||
alias start_rss09 start_rss
|
||||
def start_rss(tag_name, prefix, attrs, ns)
|
||||
# check_ns(tag_name, prefix, ns, Rss::URI)
|
||||
|
||||
@rss = Rss.new(attrs['version'], @version, @encoding, @standalone)
|
||||
@rss.xml_stylesheets = @xml_stylesheets
|
||||
@last_element = @rss
|
||||
@proc_stack.push Proc.new { |text, tags|
|
||||
@rss.validate_for_stream(tags) if @do_validate
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -124,11 +124,22 @@ module RSS
|
|||
class << self
|
||||
|
||||
@@setter = {}
|
||||
@@registered_uris = {}
|
||||
|
||||
def install_setter(uri, tag_name, setter)
|
||||
@@setter[uri] = {} unless @@setter.has_key?(uri)
|
||||
@@setter[uri][tag_name] = setter
|
||||
end
|
||||
|
||||
def register_uri(name, uri)
|
||||
@@registered_uris[name] = {} unless @@registered_uris.has_key?(name)
|
||||
@@registered_uris[name][uri] = nil
|
||||
end
|
||||
|
||||
def uri_registered?(name, uri)
|
||||
@@registered_uris[name].has_key?(uri)
|
||||
end
|
||||
|
||||
def setter(uri, tag_name)
|
||||
begin
|
||||
@@setter[uri][tag_name]
|
||||
|
@ -147,27 +158,32 @@ module RSS
|
|||
|
||||
def install_get_text_element(name, uri, setter)
|
||||
install_setter(uri, name, setter)
|
||||
def_get_text_element(name, *get_file_and_line_from_caller(1))
|
||||
def_get_text_element(uri, name, *get_file_and_line_from_caller(1))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def def_get_text_element(name, file, line)
|
||||
def def_get_text_element(uri, name, file, line)
|
||||
register_uri(name, uri)
|
||||
unless private_instance_methods(false).include?("start_#{name}")
|
||||
module_eval(<<-EOT, file, line)
|
||||
def start_#{name}(name, prefix, attrs, ns)
|
||||
uri = ns[prefix]
|
||||
if @do_validate
|
||||
tags = self.class.available_tags(uri)
|
||||
unless tags.include?(name)
|
||||
raise UnknownTagError.new(name, uri)
|
||||
if self.class.uri_registered?(#{name.inspect}, uri)
|
||||
if @do_validate
|
||||
tags = self.class.available_tags(uri)
|
||||
unless tags.include?(name)
|
||||
raise UnknownTagError.new(name, uri)
|
||||
end
|
||||
end
|
||||
start_get_text_element(name, prefix, ns, uri)
|
||||
else
|
||||
start_else_element(name, prefix, attrs, ns)
|
||||
end
|
||||
start_get_text_element(name, prefix, ns, uri)
|
||||
end
|
||||
EOT
|
||||
send("private", "start_#{name}")
|
||||
end
|
||||
send("private", "start_#{name}")
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -35,6 +35,20 @@ class Time
|
|||
end
|
||||
end
|
||||
|
||||
module Enumerable
|
||||
unless instance_methods.include?("sort_by")
|
||||
def sort_by
|
||||
collect do |x|
|
||||
[yield(x), x]
|
||||
end.sort do |x, y|
|
||||
x[0] <=> y[0]
|
||||
end.collect! do |x|
|
||||
x[1]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require "English"
|
||||
require "rss/utils"
|
||||
require "rss/converter"
|
||||
|
@ -42,7 +56,9 @@ require "rss/xml-stylesheet"
|
|||
|
||||
module RSS
|
||||
|
||||
VERSION = "0.0.8"
|
||||
VERSION = "0.0.9"
|
||||
|
||||
URI = "http://purl.org/rss/1.0/"
|
||||
|
||||
DEBUG = false
|
||||
|
||||
|
@ -298,8 +314,6 @@ EOC
|
|||
|
||||
end
|
||||
|
||||
URI = "http://purl.org/rss/1.0/"
|
||||
|
||||
class Element
|
||||
|
||||
extend BaseModel
|
||||
|
@ -314,7 +328,7 @@ EOC
|
|||
TAG_NAME = name.split('::').last.downcase
|
||||
|
||||
|
||||
@@must_call_validators = {::RSS::URI => ''}
|
||||
@@must_call_validators = {}
|
||||
|
||||
def self.must_call_validators
|
||||
@@must_call_validators
|
||||
|
@ -427,6 +441,7 @@ EOC
|
|||
end
|
||||
|
||||
def validate_for_stream(tags)
|
||||
validate_attribute
|
||||
__validate(tags, false)
|
||||
end
|
||||
|
||||
|
@ -504,6 +519,11 @@ EOC
|
|||
do_redo = false
|
||||
not_shift = false
|
||||
tag = nil
|
||||
element_names = model.collect {|elem| elem[0]}
|
||||
if tags
|
||||
tags_size = tags.size
|
||||
tags = tags.sort_by {|x| element_names.index(x) || tags_size}
|
||||
end
|
||||
|
||||
model.each_with_index do |elem, i|
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue