From c0306157d9dbb12447ba5288b8a2db73b2beef11 Mon Sep 17 00:00:00 2001 From: kou Date: Fri, 13 Feb 2004 11:02:03 +0000 Subject: [PATCH] * test/rss/test_trackback.rb: added tests for TrackBack with RSS 2.0. * test/rss/common.rb: added methods make RSS 2.0. * lib/rss/trackback.rb: TrackBack API is decided. * lib/rss/rss.rb: RSS::VERSION 0.0.7 -> 0.0.8 * lib/rss/parser.rb, lib/rss/rss.rb: replaced $DEBUG by RSS::DEBUG. * lib/rss/2.0.rb: removed RSS 2.0 URI. Because RSS 2.0 doesn't have URI. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 16 ++++++++ lib/rss/2.0.rb | 41 ++++++++++---------- lib/rss/parser.rb | 4 +- lib/rss/rss.rb | 14 ++++--- lib/rss/trackback.rb | 78 +++++++++++++++++++++++++++++++++++--- test/rss/common.rb | 45 ++++++++++++++++++++++ test/rss/test_trackback.rb | 33 +++++++++++++--- 7 files changed, 193 insertions(+), 38 deletions(-) diff --git a/ChangeLog b/ChangeLog index bc9fc787c7..7194b15b3c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +Fri Feb 13 19:57:01 2004 Kouhei Sutou + + * test/rss/test_trackback.rb: added tests for TrackBack with RSS + 2.0. + + * test/rss/common.rb: added methods make RSS 2.0. + + * lib/rss/trackback.rb: TrackBack API is decided. + + * lib/rss/rss.rb: RSS::VERSION 0.0.7 -> 0.0.8. + + * lib/rss/parser.rb, lib/rss/rss.rb: replaced $DEBUG by RSS::DEBUG. + + * lib/rss/2.0.rb: removed RSS 2.0 URI. Because RSS 2.0 doesn't + have URI. + Fri Feb 13 14:41:00 2004 Hidetoshi NAGAI * ext/tk/lib/tk.rb: en-bugged at last commit (Feb 11 23:24:22 2004) diff --git a/lib/rss/2.0.rb b/lib/rss/2.0.rb index 799e6b788f..de48c20e3c 100644 --- a/lib/rss/2.0.rb +++ b/lib/rss/2.0.rb @@ -4,19 +4,19 @@ module RSS class Rss - URI = "http://backend.userland.com/rss2" +# URI = "http://backend.userland.com/rss2" - install_ns('', URI) +# install_ns('', URI) - def self.required_uri - URI - end +# def self.required_uri +# URI +# end class Channel - def self.required_uri - URI - end +# def self.required_uri +# URI +# end %w(generator ttl).each do |x| install_text_element(x) @@ -42,15 +42,15 @@ EOT end Category = Item::Category - def Category.required_uri - URI - end +# def Category.required_uri +# URI +# end class Item - def self.required_uri - URI - end +# def self.required_uri +# URI +# end [ ["pubDate", '?'], @@ -78,9 +78,9 @@ EOT include RSS09 - def self.required_uri - URI - end +# def self.required_uri +# URI +# end [ ["isPermaLink", nil, false] @@ -124,14 +124,15 @@ EOT end RSS09::ELEMENTS.each do |x| - BaseListener.install_get_text_element(x, Rss::URI, "#{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) +# check_ns(tag_name, prefix, ns, Rss::URI) @rss = Rss.new(attrs['version'], @version, @encoding, @standalone) @last_element = @rss @@ -139,7 +140,7 @@ EOT @rss.validate_for_stream(tags) if @do_validate } end - + end end diff --git a/lib/rss/parser.rb b/lib/rss/parser.rb index 6434206ae3..aadd9658cf 100644 --- a/lib/rss/parser.rb +++ b/lib/rss/parser.rb @@ -222,7 +222,7 @@ module RSS end def tag_end(name) - if $DEBUG + if DEBUG p "end tag #{name}" p @tag_stack end @@ -336,7 +336,7 @@ module RSS @last_element.send(setter, next_element) @last_element = next_element @proc_stack.push Proc.new { |text, tags| - p(@last_element.class) if $DEBUG + p(@last_element.class) if DEBUG @last_element.content = text if klass.have_content? @last_element.validate_for_stream(tags) if @do_validate @last_element = previous diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb index 2dfc0d1ecc..d84b51472d 100644 --- a/lib/rss/rss.rb +++ b/lib/rss/rss.rb @@ -5,7 +5,9 @@ require "rss/converter" module RSS - VERSION = "0.0.7" + VERSION = "0.0.8" + + DEBUG = false class Error < StandardError; end @@ -363,6 +365,7 @@ EOC def initialize(do_validate=true) @converter = nil + @output_encoding = nil @do_validate = do_validate initialize_variables end @@ -393,6 +396,7 @@ EOC instance_eval("@#{variable_name} = nil") end initialize_have_children_elements + @content = "" if self.class.have_content? end def initialize_have_children_elements @@ -423,7 +427,7 @@ EOC end must_call_validators = self.class::must_call_validators tags = tag_filter(tags.dup) - p tags if $DEBUG + p tags if DEBUG self.class::NSPOOL.each do |prefix, uri| if tags.has_key?(uri) and !must_call_validators.has_key?(uri) meth = "#{prefix}_validate" @@ -463,7 +467,7 @@ EOC model.each_with_index do |elem, i| - if $DEBUG + if DEBUG p "before" p tags p elem @@ -478,7 +482,7 @@ EOC end end - if $DEBUG + if DEBUG p "mid" p count end @@ -523,7 +527,7 @@ EOC end end - if $DEBUG + if DEBUG p "after" p not_shift p do_redo diff --git a/lib/rss/trackback.rb b/lib/rss/trackback.rb index 5c7ca777bd..493e742453 100644 --- a/lib/rss/trackback.rb +++ b/lib/rss/trackback.rb @@ -1,6 +1,3 @@ -# ATTENSION: -# TrackBack handling API MUST be CHANGED!!!! - require 'rss/1.0' require 'rss/2.0' @@ -154,13 +151,82 @@ module RSS unless klass.class == Module %w(ping).each do |x| - klass.install_have_child_element("#{TRACKBACK_PREFIX}_#{x}") + var_name = "#{TRACKBACK_PREFIX}_#{x}" + klass.install_have_child_element(var_name) + klass.module_eval(<<-EOC) + alias _#{var_name} #{var_name} + def #{var_name} + @#{var_name} and @#{var_name}.content + end + + alias _#{var_name}= #{var_name}= + def #{var_name}=(content) + @#{var_name} = new_with_content_if_need(#{x.capitalize}, content) + end + EOC end - %w(about).each do |x| - klass.install_have_children_element("#{TRACKBACK_PREFIX}_#{x}") + [%w(about s)].each do |x, postfix| + var_name = "#{TRACKBACK_PREFIX}_#{x}" + klass.install_have_children_element(var_name) + klass.module_eval(<<-EOC) + alias _#{var_name}#{postfix} #{var_name}#{postfix} + def #{var_name}#{postfix} + @#{var_name}.collect {|x| x.content} + end + + alias _#{var_name} #{var_name} + def #{var_name}(*args) + if args.empty? + @#{var_name}.first and @#{var_name}.first.content + else + ret = @#{var_name}.send("[]", *args) + if ret.is_a?(Array) + ret.collect {|x| x.content} + else + ret.content + end + end + end + + alias _#{var_name}= #{var_name}= + alias _set_#{var_name} set_#{var_name} + def #{var_name}=(*args) + if args.size == 1 + item = new_with_content_if_need(#{x.capitalize}, args[0]) + @#{var_name}.push(item) + else + new_val = args.last + if new_val.is_a?(Array) + new_val = new_value.collect do |val| + new_with_content_if_need(#{x.capitalize}, val) + end + else + new_val = new_with_content_if_need(#{x.capitalize}, new_val) + end + @#{var_name}.send("[]=", *(args[0..-2] + [new_val])) + end + end + alias set_#{var_name} #{var_name}= + EOC end end + + private + def new_with_content(klass, content) + obj = klass.new + obj.content = content + obj + end + + def new_with_content_if_need(klass, content) + if content.is_a?(klass) + content + else + new_with_content(klass, content) + end + end + end class Ping < Element diff --git a/test/rss/common.rb b/test/rss/common.rb index d41d79e2db..64bf0100fc 100644 --- a/test/rss/common.rb +++ b/test/rss/common.rb @@ -99,4 +99,49 @@ EOI EOT end + + def make_Rss2(content=nil, xmlns=[]) + <<-EORSS +#{make_xmldecl} + +#{block_given? ? yield : content} + +EORSS + end + + def make_channel2(content=nil) + <<-EOC + + #{TITLE_VALUE} + #{LINK_VALUE} + #{DESCRIPTION_VALUE} + + + #{RDF_RESOURCE} + #{TITLE_VALUE} + #{LINK_VALUE} + + +#{RESOURCES.collect do |res| '' + res + '' end.join("\n")} + + + #{RDF_RESOURCE} + + +#{block_given? ? yield : content} + +EOC + end + + def make_item2(content=nil) + <<-EOI + + #{TITLE_VALUE} + #{LINK_VALUE} + #{DESCRIPTION_VALUE} +#{block_given? ? yield : content} + +EOI + end end diff --git a/test/rss/test_trackback.rb b/test/rss/test_trackback.rb index 85ab533617..936f3683f0 100644 --- a/test/rss/test_trackback.rb +++ b/test/rss/test_trackback.rb @@ -5,6 +5,7 @@ require "cgi-lib" require "rexml/document" require "rss/1.0" +require "rss/2.0" require "rss/trackback" require "common" @@ -26,6 +27,10 @@ class TestTrackBack < Test::Unit::TestCase "<#{@prefix}:#{name} rdf:resource=\"#{CGI.escapeHTML(value.to_s)}\"/>" end.join("\n") + @content_nodes2 = @elems.collect do |name, value| + "<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}" + end.join("\n") + @rss_source = make_RDF(<<-EOR, {@prefix => @uri}) #{make_channel()} #{make_image()} @@ -34,6 +39,14 @@ class TestTrackBack < Test::Unit::TestCase EOR @rss = Parser.parse(@rss_source) + + @rss2_source = make_Rss2(nil, {@prefix => @uri}) do + make_channel2(nil) do + make_item2(@content_nodes2) + end + end + + @rss2 = Parser.parse(@rss2_source, false) end def test_parser @@ -76,11 +89,21 @@ EOR @elems.each do |name, value| @parents.each do |parent| - elem = @rss.send(parent).send("#{RSS::TRACKBACK_PREFIX}_#{name}") - meth = "resource" - assert_equal(value, elem.send(meth)) - elem.send("#{meth}=", new_value[name].to_s) - assert_equal(new_value[name], elem.send(meth)) + accessor = "#{RSS::TRACKBACK_PREFIX}_#{name}" + target_accessor = "resource" + target = @rss.send(parent).send(accessor) + target2 = @rss2.channel.send(parent, -1) + assert_equal(value, target.send(target_accessor)) + assert_equal(value, target2.send(accessor)) + target.send("#{target_accessor}=", new_value[name].to_s) + if name == :about + # abount is zero or more + target2.send("#{accessor}=", 0, new_value[name].to_s) + else + target2.send("#{accessor}=", new_value[name].to_s) + end + assert_equal(new_value[name], target.send(target_accessor)) + assert_equal(new_value[name], target2.send(accessor)) end end