From c0306157d9dbb12447ba5288b8a2db73b2beef11 Mon Sep 17 00:00:00 2001 From: kou Date: Fri, 13 Feb 2004 11:02:03 +0000 Subject: * 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 --- lib/rss/2.0.rb | 41 +++++++++++++-------------- lib/rss/parser.rb | 4 +-- lib/rss/rss.rb | 14 ++++++---- lib/rss/trackback.rb | 78 ++++++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 104 insertions(+), 33 deletions(-) (limited to 'lib/rss') 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 -- cgit v1.2.3