From 9be1ed43c888249563f32a3df394a9841e4f9601 Mon Sep 17 00:00:00 2001 From: kou Date: Fri, 19 Nov 2004 08:25:25 +0000 Subject: * lib/rss/rss.rb (RSS::VERSION): 0.1.0 -> 0.1.1. * lib/rss: #to_s used #tag. * test/rss/test_to_s.rb: added. * lib/rss/maker.rb (RSS::Maker.make): changed API. It's not received modules which is used as the second argument. * lib/rss/xml-stylesheet.rb (RSS::XMLStyleSheet#alternate): changed return value type which is not String but Boolean. * lib/rss/2.0.rb (RSS::Rss::Channel#ttl): changed return value type which is not String but Integer. * lib/rss/0.9.rb (RSS::Rss::Channel): has s and has s. * lib/rss/maker/0.9.rb (RSS::Maker::RSS09::Channel): ditto. * lib/rss/0.9.rb (RSS::Rss::Channel::Item): has s. * lib/rss/maker/2.0.rb (RSS::Maker::Rss20::Channel::Item): ditto. * lib/rss/2.0.rb (RSS::Rss::Channel): has s. * lib/rss/maker/2.0.rb (RSS::Maker::RSS20::Channel): ditto. * lib/rss/trackback.rb: parent element has s. * lib/rss/maker/trackback.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7319 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/rss/rss.rb | 148 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 116 insertions(+), 32 deletions(-) (limited to 'lib/rss/rss.rb') diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb index b6afa24b93..a63bee8a69 100644 --- a/lib/rss/rss.rb +++ b/lib/rss/rss.rb @@ -58,7 +58,7 @@ require "rss/xml-stylesheet" module RSS - VERSION = "0.1.0" + VERSION = "0.1.1" URI = "http://purl.org/rss/1.0/" @@ -164,17 +164,17 @@ EOC end alias_method(:install_have_attribute_element, :install_have_child_element) - def install_have_children_element(name, postfix="s") + def install_have_children_element(name, plural_name=nil) add_have_children_element(name) - def_children_accessor(name, postfix) - install_element(name, postfix) do |n, elem_name| + def_children_accessor(name, plural_name) + install_element(name, "s") do |n, elem_name| <<-EOC - rv = '' + rv = [] @#{n}.each do |x| rv << "\#{x.to_s(convert, indent)}" end - rv + rv.join("\n") EOC end end @@ -289,9 +289,10 @@ EOC end end - def def_children_accessor(accessor_name, postfix="s") + def def_children_accessor(accessor_name, plural_name=nil) + plural_name ||= "#{accessor_name}s" module_eval(<<-EOC, *get_file_and_line_from_caller(2)) - def #{accessor_name}#{postfix} + def #{plural_name} @#{accessor_name} end @@ -314,6 +315,22 @@ EOC EOC end + def def_content_only_to_s + module_eval(<<-EOC, *get_file_and_line_from_caller(2)) + def to_s(convert=true, indent=calc_indent) + if @content + rv = tag(indent) do |next_indent| + h(@content) + end + rv = @converter.convert(rv) if convert and @converter + rv + else + "" + end + end +EOC + end + end class Element @@ -329,7 +346,8 @@ EOC klass.module_eval(<<-EOC) public - @tag_name = name.split(/::/).last.downcase + @tag_name = name.split(/::/).last + @tag_name[0,1] = @tag_name[0,1].downcase @indent_size = name.split(/::/).size - 2 @@must_call_validators = {} @@ -373,6 +391,7 @@ EOC def self.content_setup attr_writer :content convert_attr_reader :content + def_content_only_to_s @@have_content = true end @@ -440,6 +459,14 @@ EOC self.class.tag_name end + def full_name + tag_name + end + + def indent_size + self.class.indent_size + end + def converter=(converter) @converter = converter children.each do |child| @@ -472,6 +499,66 @@ EOC end end + def tag(indent, additional_attrs=[]) + next_indent = indent + INDENT + + attrs = collect_attrs + return "" if attrs.nil? + + attrs += additional_attrs + start_tag = make_start_tag(indent, next_indent, attrs) + + if block_given? + content = yield(next_indent) + else + content = [] + end + + if content.is_a?(String) + content = [content] + start_tag << ">" + end_tag = "" + else + content = content.reject{|x| x.empty?} + if content.empty? + end_tag = "/>" + else + start_tag << ">\n" + end_tag = "\n#{indent}" + end + end + + start_tag + content.join("\n") + end_tag + end + + def make_start_tag(indent, next_indent, attrs) + start_tag = ["#{indent}<#{full_name}"] + unless attrs.empty? + start_tag << attrs.collect do |key, value| + %Q[#{h key}="#{h value}"] + end.join("\n#{next_indent}") + end + start_tag.join(" ") + end + + def collect_attrs + _attrs.collect do |name, required, alias_name| + value = __send__(alias_name || name) + return nil if required and value.nil? + [name, value] + end.reject do |name, value| + value.nil? + end + end + + def tag_name_with_prefix(prefix) + "#{prefix}:#{tag_name}" + end + + def calc_indent + INDENT * (self.class.indent_size) + end + # not String class children. def children [] @@ -507,23 +594,23 @@ EOC end def validate_attribute - _attrs.each do |a_name, required| - if required and send(a_name).nil? - raise MissingAttributeError.new(self.class.tag_name, a_name) + _attrs.each do |a_name, required, alias_name| + if required and __send__(alias_name || a_name).nil? + raise MissingAttributeError.new(tag_name, a_name) end end end def other_element(convert, indent='') - rv = '' + rv = [] private_methods.each do |meth| if /\A([^_]+)_[^_]+_elements?\z/ =~ meth and self.class::NSPOOL.has_key?($1) - res = send(meth, convert) - rv << "#{indent}#{res}\n" if /\A\s*\z/ !~ res + res = __send__(meth, convert) + rv << "#{indent}#{res}" if /\A\s*\z/ !~ res end end - rv + rv.join("\n") end def _validate(tags, model=self.class.model) @@ -626,19 +713,12 @@ EOC rv end - private - def calc_indent - INDENT * (self.class.indent_size) - end - - def remove_empty_newline(string) - string.gsub(/^\s*$(?:\r?\n?)/, '') - end - end module RootElementMixin + include XMLStyleSheetMixin + attr_reader :output_encoding def initialize(rss_version, version=nil, encoding=nil, standalone=nil) @@ -656,23 +736,27 @@ EOC end private + def tag(indent, attrs) + rv = xmldecl + xml_stylesheet_pi + rv << super + rv + end + def xmldecl rv = %Q[' + rv << %Q[ standalone="yes"] if @standalone + rv << "?>\n" rv end - def ns_declaration(indent) - rv = '' - self.class::NSPOOL.each do |prefix, uri| + def ns_declarations + self.class::NSPOOL.collect do |prefix, uri| prefix = ":#{prefix}" unless prefix.empty? - rv << %Q|\n#{indent}xmlns#{prefix}="#{html_escape(uri)}"| + ["xmlns#{prefix}", uri] end - rv end end -- cgit v1.2.3