mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/rss, test/rss: backported from trunk. (2005-11-16 - now)
* lib/rss/taxonomy.rb: implemented taxonomy module. * test/rss/test_taxonomy.rb: added tests for taxonomy support. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10288 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5a6f02677b
commit
201028438d
3 changed files with 196 additions and 39 deletions
|
@ -13,6 +13,9 @@ Sat Jun 17 09:03:47 2006 Kouhei Sutou <kou@cozmixng.org>
|
||||||
|
|
||||||
* lib/rss/1.0.rb: added rdf:Bag.
|
* lib/rss/1.0.rb: added rdf:Bag.
|
||||||
|
|
||||||
|
* lib/rss/taxonomy.rb: implemented taxonomy module.
|
||||||
|
* test/rss/test_taxonomy.rb: added tests for taxonomy support.
|
||||||
|
|
||||||
Sat Jun 17 02:01:00 2006 Tanaka Akira <akr@m17n.org>
|
Sat Jun 17 02:01:00 2006 Tanaka Akira <akr@m17n.org>
|
||||||
|
|
||||||
* lib/pp.rb (Kernel#pretty_inspect): defined for pretty printed
|
* lib/pp.rb (Kernel#pretty_inspect): defined for pretty printed
|
||||||
|
|
|
@ -1,32 +1,198 @@
|
||||||
# Experimental
|
|
||||||
|
|
||||||
require "rss/1.0"
|
require "rss/1.0"
|
||||||
|
require "rss/dublincore"
|
||||||
|
|
||||||
module RSS
|
module RSS
|
||||||
|
|
||||||
TAXO_PREFIX = "taxo"
|
TAXO_PREFIX = "taxo"
|
||||||
TAXO_NS = "http://purl.org/rss/1.0/modules/taxonomy/"
|
TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/"
|
||||||
|
|
||||||
Element.install_ns(TAXO_PREFIX, TAXO_NS)
|
RDF.install_ns(TAXO_PREFIX, TAXO_URI)
|
||||||
|
|
||||||
TAXO_ELEMENTS = []
|
TAXO_ELEMENTS = []
|
||||||
|
|
||||||
%w(link).each do |name|
|
%w(link).each do |name|
|
||||||
full_name = "#{TAXO_PREFIX}_#{name}"
|
full_name = "#{TAXO_PREFIX}_#{name}"
|
||||||
BaseListener.install_get_text_element(TAXO_NS, name, "#{full_name}=")
|
BaseListener.install_get_text_element(TAXO_URI, name, "#{full_name}=")
|
||||||
TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{name}"
|
TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
%w(topic topics).each do |name|
|
||||||
|
class_name = Utils.to_class_name(name)
|
||||||
|
BaseListener.install_class_name(TAXO_URI, name, "Taxo#{class_name}")
|
||||||
|
TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{name}"
|
||||||
|
end
|
||||||
|
|
||||||
|
module TaxoTopicsModel
|
||||||
|
extend BaseModel
|
||||||
|
|
||||||
module TaxonomyModel
|
def self.append_features(klass)
|
||||||
attr_writer(*%w(title description creator subject publisher
|
super
|
||||||
contributor date format identifier source
|
|
||||||
language relation coverage rights
|
var_name = "#{TAXO_PREFIX}_topics"
|
||||||
).collect{|name| "#{TAXO_PREFIX}_#{name}"})
|
klass.install_have_child_element(var_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def taxo_validate(tags)
|
||||||
|
found_topics = false
|
||||||
|
tags.each do |tag|
|
||||||
|
if tag == "topics"
|
||||||
|
if found_topics
|
||||||
|
raise TooMuchTagError.new(tag, tag_name)
|
||||||
|
else
|
||||||
|
found_topics = true
|
||||||
|
end
|
||||||
|
else
|
||||||
|
raise UnknownTagError.new(tag, TAXO_URI)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TaxoTopics < Element
|
||||||
|
include RSS10
|
||||||
|
|
||||||
|
Bag = ::RSS::RDF::Bag
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def required_prefix
|
||||||
|
TAXO_PREFIX
|
||||||
|
end
|
||||||
|
|
||||||
|
def required_uri
|
||||||
|
TAXO_URI
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag_name = "topics"
|
||||||
|
|
||||||
|
install_have_child_element("Bag")
|
||||||
|
|
||||||
|
install_must_call_validator('rdf', ::RSS::RDF::URI)
|
||||||
|
|
||||||
|
def initialize(bag=Bag.new)
|
||||||
|
super()
|
||||||
|
@Bag = bag
|
||||||
|
end
|
||||||
|
|
||||||
|
def full_name
|
||||||
|
tag_name_with_prefix(TAXO_PREFIX)
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s(need_convert=true, indent=calc_indent)
|
||||||
|
rv = tag(indent) do |next_indent|
|
||||||
|
[
|
||||||
|
Bag_element(need_convert, next_indent),
|
||||||
|
other_element(need_convert, next_indent),
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def children
|
||||||
|
[@Bag]
|
||||||
|
end
|
||||||
|
|
||||||
|
def _tags
|
||||||
|
rv = []
|
||||||
|
rv << [::RSS::RDF::URI, 'Bag'] unless @Bag.nil?
|
||||||
|
rv
|
||||||
|
end
|
||||||
|
|
||||||
|
def rdf_validate(tags)
|
||||||
|
_validate(tags, [["Bag", nil]])
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Channel; extend TaxonomyModel; end
|
module TaxoTopicModel
|
||||||
class Item; extend TaxonomyModel; end
|
extend BaseModel
|
||||||
class Image; extend TaxonomyModel; end
|
|
||||||
class TextInput; extend TaxonomyModel; end
|
def self.append_features(klass)
|
||||||
|
super
|
||||||
|
var_name = "#{TAXO_PREFIX}_topic"
|
||||||
|
klass.install_have_children_element(var_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def taxo_validate(tags)
|
||||||
|
tags.each do |tag|
|
||||||
|
if tag != "topic"
|
||||||
|
raise UnknownTagError.new(tag, TAXO_URI)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TaxoTopic < Element
|
||||||
|
include RSS10
|
||||||
|
|
||||||
|
include DublinCoreModel
|
||||||
|
include TaxoTopicsModel
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def required_prefix
|
||||||
|
TAXO_PREFIX
|
||||||
|
end
|
||||||
|
|
||||||
|
def required_uri
|
||||||
|
TAXO_URI
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@tag_name = "topic"
|
||||||
|
|
||||||
|
install_get_attribute("about", ::RSS::RDF::URI, true)
|
||||||
|
install_text_element("#{TAXO_PREFIX}_link")
|
||||||
|
|
||||||
|
def initialize(about=nil)
|
||||||
|
super()
|
||||||
|
@about = about
|
||||||
|
end
|
||||||
|
|
||||||
|
def full_name
|
||||||
|
tag_name_with_prefix(TAXO_PREFIX)
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s(need_convert=true, indent=calc_indent)
|
||||||
|
rv = tag(indent) do |next_indent|
|
||||||
|
[
|
||||||
|
link_element(need_convert, next_indent),
|
||||||
|
other_element(need_convert, next_indent),
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def taxo_validate(tags)
|
||||||
|
elements = %w(link topics)
|
||||||
|
counter = {}
|
||||||
|
|
||||||
|
tags.each do |tag|
|
||||||
|
if elements.include?(tag)
|
||||||
|
counter[tag] ||= 0
|
||||||
|
counter[tag] += 1
|
||||||
|
raise TooMuchTagError.new(tag, tag_name) if counter[tag] > 1
|
||||||
|
else
|
||||||
|
raise UnknownTagError.new(tag, TAXO_URI)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def children
|
||||||
|
[@taxo_link, @taxo_topics]
|
||||||
|
end
|
||||||
|
|
||||||
|
def _tags
|
||||||
|
rv = []
|
||||||
|
rv << [TAXO_URI, "link"] unless @taxo_link.nil?
|
||||||
|
rv << [TAXO_URI, "topics"] unless @taxo_topics.nil?
|
||||||
|
rv
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class RDF
|
||||||
|
include TaxoTopicModel
|
||||||
|
class Channel
|
||||||
|
include TaxoTopicsModel
|
||||||
|
end
|
||||||
|
class Item; include TaxoTopicsModel; end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,7 +20,7 @@ module RSS
|
||||||
@dc_prefix => @dc_uri,
|
@dc_prefix => @dc_uri,
|
||||||
}
|
}
|
||||||
|
|
||||||
@topics_parents = %w(channel item)
|
@parents = %w(channel item)
|
||||||
|
|
||||||
@topics_lis = [
|
@topics_lis = [
|
||||||
"http://meerkat.oreillynet.com/?c=cat23",
|
"http://meerkat.oreillynet.com/?c=cat23",
|
||||||
|
@ -66,28 +66,28 @@ module RSS
|
||||||
info.each do |name, value|
|
info.each do |name, value|
|
||||||
case name
|
case name
|
||||||
when :topics
|
when :topics
|
||||||
rv << " <#{@prefix}:topics>\n"
|
rv << "<#{@prefix}:topics>\n"
|
||||||
rv << " <rdf:Bag>\n"
|
rv << " <rdf:Bag>\n"
|
||||||
value.each do |li|
|
value.each do |li|
|
||||||
resource = CGI.escapeHTML(li)
|
resource = CGI.escapeHTML(li)
|
||||||
rv << " <rdf:li resource=\"#{resource}\"/>\n"
|
rv << " <rdf:li resource=\"#{resource}\"/>\n"
|
||||||
end
|
end
|
||||||
rv << " </rdf:Bag>\n"
|
rv << " </rdf:Bag>\n"
|
||||||
rv << " </#{@prefix}:topics>\n"
|
rv << "</#{@prefix}:topics>"
|
||||||
else
|
else
|
||||||
prefix = (name == :link ? @prefix : @dc_prefix)
|
prefix = (name == :link ? @prefix : @dc_prefix)
|
||||||
rv << " <#{prefix}:#{name}>#{value}</#{prefix}:#{name}>\n"
|
rv << " <#{prefix}:#{name}>#{value}</#{prefix}:#{name}>\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rv << "</#{@prefix}:topic>"
|
rv << "</#{@prefix}:topic>"
|
||||||
end
|
end.join("\n")
|
||||||
|
|
||||||
@rss_source = make_RDF(<<-EOR, @ns)
|
@rss_source = make_RDF(<<-EOR, @ns)
|
||||||
#{make_channel(@topics_node)}
|
#{make_channel(@topics_node)}
|
||||||
#{make_image()}
|
#{make_image()}
|
||||||
#{make_item(@topics_node)}
|
#{make_item(@topics_node)}
|
||||||
#{make_textinput()}
|
#{make_textinput()}
|
||||||
#{@topic_nodes.join("\n")}
|
#{@topic_nodes}
|
||||||
EOR
|
EOR
|
||||||
|
|
||||||
@rss = Parser.parse(@rss_source)
|
@rss = Parser.parse(@rss_source)
|
||||||
|
@ -117,7 +117,6 @@ EOR
|
||||||
topics = @rss.channel.taxo_topics
|
topics = @rss.channel.taxo_topics
|
||||||
assert_equal(@topics_lis.sort,
|
assert_equal(@topics_lis.sort,
|
||||||
topics.Bag.lis.collect {|li| li.resource}.sort)
|
topics.Bag.lis.collect {|li| li.resource}.sort)
|
||||||
assert_equal(@topics_lis.sort, topics.resources.sort)
|
|
||||||
|
|
||||||
assert_equal(@rss.taxo_topics.first, @rss.taxo_topic)
|
assert_equal(@rss.taxo_topics.first, @rss.taxo_topic)
|
||||||
|
|
||||||
|
@ -129,7 +128,8 @@ EOR
|
||||||
assert_equal(value, topic.about)
|
assert_equal(value, topic.about)
|
||||||
assert_equal(value, topic.taxo_link)
|
assert_equal(value, topic.taxo_link)
|
||||||
when :topics
|
when :topics
|
||||||
assert_equal(value.sort, topic.taxo_topics.resources.sort)
|
assert_equal(value.sort,
|
||||||
|
topic.taxo_topics.Bag.lis.collect {|li| li.resource}.sort)
|
||||||
else
|
else
|
||||||
assert_equal(value, topic.__send__("dc_#{name}"))
|
assert_equal(value, topic.__send__("dc_#{name}"))
|
||||||
end
|
end
|
||||||
|
@ -138,21 +138,9 @@ EOR
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_to_s
|
def test_to_s
|
||||||
@topics_parents.each do |parent|
|
@parents.each do |parent|
|
||||||
meth = "taxo_topics_element"
|
meth = "taxo_topics_element"
|
||||||
assert_equal(@topics_node, @rss.__send__(parent).funcall(meth))
|
assert_equal(@topics_node, @rss.__send__(parent).__send__(meth))
|
||||||
end
|
|
||||||
|
|
||||||
@topic_nodes.each_with_index do |node, i|
|
|
||||||
expected = REXML::Document.new(node).root
|
|
||||||
actual = REXML::Document.new(@rss.taxo_topics[i].to_s(true, "")).root
|
|
||||||
expected_elems = expected.reject {|x| x.is_a?(REXML::Text)}
|
|
||||||
actual_elems = actual.reject {|x| x.is_a?(REXML::Text)}
|
|
||||||
expected_elems.sort! {|x, y| x.name <=> y.name}
|
|
||||||
actual_elems.sort! {|x, y| x.name <=> y.name}
|
|
||||||
assert_equal(expected_elems.collect {|x| x.to_s},
|
|
||||||
actual_elems.collect {|x| x.to_s})
|
|
||||||
assert_equal(expected.attributes.sort, actual.attributes.sort)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue