From 966a25465aab5c2972e6c453f631a15fc2223256 Mon Sep 17 00:00:00 2001 From: kou Date: Sat, 17 Mar 2007 10:13:25 +0000 Subject: * lib/rss, test/rss: - supported Atom. - bumped version 0.1.6 to 0.1.7. * sample/rss/convert.rb: added new sample. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12087 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/rss/1.0.rb | 486 +++++++++++++-------------------------------------------- 1 file changed, 110 insertions(+), 376 deletions(-) (limited to 'lib/rss/1.0.rb') diff --git a/lib/rss/1.0.rb b/lib/rss/1.0.rb index 36d6f5df87..f04e61c5eb 100644 --- a/lib/rss/1.0.rb +++ b/lib/rss/1.0.rb @@ -38,62 +38,22 @@ module RSS [ ["channel", nil], ["image", "?"], - ["item", "+"], + ["item", "+", :children], ["textinput", "?"], - ].each do |tag, occurs| - install_model(tag, occurs) + ].each do |tag, occurs, type| + type ||= :child + __send__("install_have_#{type}_element", tag, ::RSS::URI, occurs) end - %w(channel image textinput).each do |name| - install_have_child_element(name) - end - - install_have_children_element("item") - - attr_accessor :rss_version, :version, :encoding, :standalone - + alias_method(:rss_version, :feed_version) def initialize(version=nil, encoding=nil, standalone=nil) super('1.0', version, encoding, standalone) + @feed_type = "rss" end def full_name tag_name_with_prefix(PREFIX) end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent, ns_declarations) do |next_indent| - [ - channel_element(false, next_indent), - image_element(false, next_indent), - item_elements(false, next_indent), - textinput_element(false, next_indent), - other_element(false, next_indent), - ] - end - rv = convert(rv) if need_convert - rv - end - - private - def rdf_validate(tags) - _validate(tags, []) - end - - def children - [@channel, @image, @textinput, *@item] - end - - def _tags - rv = [ - [::RSS::URI, "channel"], - [::RSS::URI, "image"], - ].delete_if {|uri, name| __send__(name).nil?} - @item.each do |item| - rv << [::RSS::URI, "item"] - end - rv << [::RSS::URI, "textinput"] if @textinput - rv - end class Li < Element @@ -106,32 +66,23 @@ module RSS end [ - ["resource", [URI, nil], true] + ["resource", [URI, ""], true] ].each do |name, uri, required| install_get_attribute(name, uri, required) end - def initialize(resource=nil) - super() - self.resource = resource + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.resource = args[0] + end end def full_name tag_name_with_prefix(PREFIX) end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) - rv = convert(rv) if need_convert - rv - end - - private - def _attrs - [ - ["resource", true] - ] - end end class Seq < Element @@ -148,21 +99,15 @@ module RSS @tag_name = 'Seq' - install_have_children_element("li") - + install_have_children_element("li", URI, "*") install_must_call_validator('rdf', ::RSS::RDF::URI) - def initialize(li=[]) - super() - @li = li - end - - def to_s(need_convert=true, indent=calc_indent) - tag(indent) do |next_indent| - [ - li_elements(need_convert, next_indent), - other_element(need_convert, next_indent), - ] + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + @li = args[0] if args[0] end end @@ -175,23 +120,6 @@ module RSS target << li.resource end end - - private - def children - @li - end - - def rdf_validate(tags) - _validate(tags, [["li", '*']]) - end - - def _tags - rv = [] - @li.each do |li| - rv << [URI, "li"] - end - rv - end end class Bag < Element @@ -208,21 +136,15 @@ module RSS @tag_name = 'Bag' - install_have_children_element("li") - - install_must_call_validator('rdf', ::RSS::RDF::URI) - - def initialize(li=[]) - super() - @li = li - end + install_have_children_element("li", URI, "*") + install_must_call_validator('rdf', URI) - def to_s(need_convert=true, indent=calc_indent) - tag(indent) do |next_indent| - [ - li_elements(need_convert, next_indent), - other_element(need_convert, next_indent), - ] + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + @li = args[0] if args[0] end end @@ -235,23 +157,6 @@ module RSS target << li.resource end end - - private - def children - @li - end - - def rdf_validate(tags) - _validate(tags, [["li", '*']]) - end - - def _tags - rv = [] - @li.each do |li| - rv << [URI, "li"] - end - rv - end end class Channel < Element @@ -269,73 +174,31 @@ module RSS [ ["about", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) - end - - %w(title link description).each do |name| - install_text_element(name) + install_get_attribute(name, uri, required, nil, nil, + "#{PREFIX}:#{name}") end - %w(image items textinput).each do |name| - install_have_child_element(name) - end - [ - ['title', nil], - ['link', nil], - ['description', nil], - ['image', '?'], - ['items', nil], - ['textinput', '?'], - ].each do |tag, occurs| - install_model(tag, occurs) - end - - def initialize(about=nil) - super() - self.about = about - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - link_element(false, next_indent), - description_element(false, next_indent), - image_element(false, next_indent), - items_element(false, next_indent), - textinput_element(false, next_indent), - other_element(false, next_indent), - ] + ['title', nil, :text], + ['link', nil, :text], + ['description', nil, :text], + ['image', '?', :have_child], + ['items', nil, :have_child], + ['textinput', '?', :have_child], + ].each do |tag, occurs, type| + __send__("install_#{type}_element", tag, ::RSS::URI, occurs) + end + + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] end - rv = convert(rv) if need_convert - rv end private - def children - [@image, @items, @textinput] - end - - def _tags - [ - [::RSS::URI, 'title'], - [::RSS::URI, 'link'], - [::RSS::URI, 'description'], - [::RSS::URI, 'image'], - [::RSS::URI, 'items'], - [::RSS::URI, 'textinput'], - ].delete_if do |uri, name| - __send__(name).nil? - end - end - - def _attrs - [ - ["#{PREFIX}:about", true, "about"] - ] - end - def maker_target(maker) maker.channel end @@ -359,25 +222,17 @@ module RSS [ ["resource", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) + install_get_attribute(name, uri, required, nil, nil, + "#{PREFIX}:#{name}") end - def initialize(resource=nil) - super() - self.resource = resource - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) - rv = convert(rv) if need_convert - rv - end - - private - def _attrs - [ - ["#{PREFIX}:resource", true, "resource"] - ] + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.resource = args[0] + end end end @@ -396,25 +251,17 @@ module RSS [ ["resource", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) + install_get_attribute(name, uri, required, nil, nil, + "#{PREFIX}:#{name}") end - def initialize(resource=nil) - super() - self.resource = resource - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) - rv = convert(rv) if need_convert - rv - end - - private - def _attrs - [ - ["#{PREFIX}:resource", true, "resource"] - ] + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.resource = args[0] + end end end @@ -432,22 +279,17 @@ module RSS end - install_have_child_element("Seq") - - install_must_call_validator('rdf', ::RSS::RDF::URI) + install_have_child_element("Seq", URI, nil) + install_must_call_validator('rdf', URI) - def initialize(seq=Seq.new) - super() - @Seq = seq - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - Seq_element(need_convert, next_indent), - other_element(need_convert, next_indent), - ] + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.Seq = args[0] end + self.Seq ||= Seq.new end def resources @@ -459,21 +301,6 @@ module RSS [] end end - - private - def children - [@Seq] - end - - def _tags - rv = [] - rv << [URI, 'Seq'] unless @Seq.nil? - rv - end - - def rdf_validate(tags) - _validate(tags, [["Seq", nil]]) - end end end @@ -488,60 +315,28 @@ module RSS end end - + [ ["about", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) + install_get_attribute(name, uri, required, nil, nil, + "#{PREFIX}:#{name}") end %w(title url link).each do |name| - install_text_element(name) - end - - [ - ['title', nil], - ['url', nil], - ['link', nil], - ].each do |tag, occurs| - install_model(tag, occurs) + install_text_element(name, ::RSS::URI, nil) end - def initialize(about=nil) - super() - self.about = about - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - url_element(false, next_indent), - link_element(false, next_indent), - other_element(false, next_indent), - ] + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] end - rv = convert(rv) if need_convert - rv end private - def _tags - [ - [::RSS::URI, 'title'], - [::RSS::URI, 'url'], - [::RSS::URI, 'link'], - ].delete_if do |uri, name| - __send__(name).nil? - end - end - - def _attrs - [ - ["#{PREFIX}:about", true, "about"] - ] - end - def maker_target(maker) maker.image end @@ -559,14 +354,12 @@ module RSS end + [ ["about", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) - end - - %w(title link description).each do |name| - install_text_element(name) + install_get_attribute(name, uri, required, nil, nil, + "#{PREFIX}:#{name}") end [ @@ -574,44 +367,19 @@ module RSS ["link", nil], ["description", "?"], ].each do |tag, occurs| - install_model(tag, occurs) + install_text_element(tag, ::RSS::URI, occurs) end - def initialize(about=nil) - super() - self.about = about - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - link_element(false, next_indent), - description_element(false, next_indent), - other_element(false, next_indent), - ] - end - rv = convert(rv) if need_convert - rv - end - - private - def _tags - [ - [::RSS::URI, 'title'], - [::RSS::URI, 'link'], - [::RSS::URI, 'description'], - ].delete_if do |uri, name| - __send__(name).nil? + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] end end - def _attrs - [ - ["#{PREFIX}:about", true, "about"] - ] - end - + private def maker_target(items) if items.respond_to?("items") # For backward compatibility @@ -636,59 +404,24 @@ module RSS [ ["about", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) + install_get_attribute(name, uri, required, nil, nil, + "#{PREFIX}:#{name}") end %w(title description name link).each do |name| - install_text_element(name) - end - - [ - ["title", nil], - ["description", nil], - ["name", nil], - ["link", nil], - ].each do |tag, occurs| - install_model(tag, occurs) - end - - def initialize(about=nil) - super() - self.about = about + install_text_element(name, ::RSS::URI, nil) end - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - description_element(false, next_indent), - name_element(false, next_indent), - link_element(false, next_indent), - other_element(false, next_indent), - ] + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] end - rv = convert(rv) if need_convert - rv end private - def _tags - [ - [::RSS::URI, 'title'], - [::RSS::URI, 'description'], - [::RSS::URI, 'name'], - [::RSS::URI, 'link'], - ].delete_if do |uri, name| - __send__(name).nil? - end - end - - def _attrs - [ - ["#{PREFIX}:about", true, "about"] - ] - end - def maker_target(maker) maker.textinput end @@ -697,21 +430,22 @@ module RSS end RSS10::ELEMENTS.each do |name| - BaseListener.install_get_text_element(URI, name, "#{name}=") + BaseListener.install_get_text_element(URI, name, name) end module ListenerMixin private - def start_RDF(tag_name, prefix, attrs, ns) + def initial_start_RDF(tag_name, prefix, attrs, ns) check_ns(tag_name, prefix, ns, RDF::URI) @rss = RDF.new(@version, @encoding, @standalone) @rss.do_validate = @do_validate @rss.xml_stylesheets = @xml_stylesheets @last_element = @rss - @proc_stack.push Proc.new { |text, tags| - @rss.validate_for_stream(tags) if @do_validate - } + pr = Proc.new do |text, tags| + @rss.validate_for_stream(tags, @ignore_unknown_element) if @do_validate + end + @proc_stack.push(pr) end end -- cgit v1.2.3