1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/rss: supported prety print.

* test/rss/test_1.0.rb: added test for calculating default indent size.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7047 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kou 2004-10-16 04:39:58 +00:00
parent 64422ae494
commit 0ebac90b0e
8 changed files with 210 additions and 154 deletions

View file

@ -1,3 +1,8 @@
Sat Oct 16 13:34:56 2004 Kouhei Sutou <kou@cozmixng.org>
* lib/rss: supported prety print.
* test/rss/test_1.0.rb: added test for calculating default indent size.
Sat Oct 16 10:56:36 2004 Nobuyoshi Nakada <nobu@ruby-lang.org> Sat Oct 16 10:56:36 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/mkmf.rb (create_makefile): install-rb is needed for statically * lib/mkmf.rb (create_makefile): install-rb is needed for statically

View file

@ -51,16 +51,18 @@ module RSS
end end
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
rv = <<-EOR rv = <<-EOR
#{xmldecl} #{xmldecl}
#{xml_stylesheet_pi}<rss version="#{@rss_version}"#{ns_declaration}> #{xml_stylesheet_pi}
#{channel_element(false)} #{indent}<rss version="#{@rss_version}"#{ns_declaration(next_indent)}>
#{other_element(false, "\t")} #{channel_element(false, next_indent)}
</rss> #{other_element(false, next_indent)}
#{inent}}</rss>
EOR EOR
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv remove_empty_newline(rv)
end end
private private
@ -135,27 +137,28 @@ EOR
super() super()
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
rv = <<-EOT rv = <<-EOT
<channel> #{indent}<channel>
#{title_element(false)} #{title_element(false, next_indent)}
#{link_element(false)} #{link_element(false, next_indent)}
#{description_element(false)} #{description_element(false, next_indent)}
#{language_element(false)} #{language_element(false, next_indent)}
#{copyright_element(false)} #{copyright_element(false, next_indent)}
#{managingEditor_element(false)} #{managingEditor_element(false, next_indent)}
#{webMaster_element(false)} #{webMaster_element(false, next_indent)}
#{rating_element(false)} #{rating_element(false, next_indent)}
#{pubDate_element(false)} #{pubDate_element(false, next_indent)}
#{lastBuildDate_element(false)} #{lastBuildDate_element(false, next_indent)}
#{docs_element(false)} #{docs_element(false, next_indent)}
#{skipDays_element(false)} #{skipDays_element(false, next_indent)}
#{skipHours_element(false)} #{skipHours_element(false, next_indent)}
#{image_element(false)} #{image_element(false, next_indent)}
#{item_elements(false)} #{item_elements(false, next_indent)}
#{textInput_element(false)} #{textInput_element(false, next_indent)}
#{other_element(false, "\t\t")} #{other_element(false, next_indent)}
</channel> #{indent}</channel>
EOT EOT
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
@ -208,17 +211,18 @@ EOT
install_model(x, "?") install_model(x, "?")
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
rv = <<-EOT rv = <<-EOT
<image> #{indent}<image>
#{url_element(false)} #{url_element(false, next_indent)}
#{title_element(false)} #{title_element(false, next_indent)}
#{link_element(false)} #{link_element(false, next_indent)}
#{width_element(false)} #{width_element(false, next_indent)}
#{height_element(false)} #{height_element(false, next_indent)}
#{description_element(false)} #{description_element(false, next_indent)}
#{other_element(false, "\t\t\t\t")} #{other_element(false, next_indent)}
</image> #{indent}</image>
EOT EOT
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
@ -257,14 +261,15 @@ EOT
@protocol = protocol @protocol = protocol
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
rv = <<-EOT rv = <<-EOT
<cloud #{indent}<cloud
domain="#{h @domain}" #{next_indent}domain="#{h @domain}"
port="#{h @port}" #{next_indent}port="#{h @port}"
path="#{h @path}" #{next_indent}path="#{h @path}"
registerProcedure="#{h @registerProcedure}" #{next_indent}registerProcedure="#{h @registerProcedure}"
protocol="#{h @protocol}"/> #{next_indent}protocol="#{h @protocol}"/>
EOT EOT
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
@ -302,17 +307,18 @@ EOT
install_model(tag, occurs) install_model(tag, occurs)
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
rv = <<-EOT rv = <<-EOT
<item> #{indent}<item>
#{title_element(false)} #{title_element(false, next_indent)}
#{link_element(false)} #{link_element(false, next_indent)}
#{description_element(false)} #{description_element(false, next_indent)}
#{category_element(false)} #{category_element(false, next_indent)}
#{source_element(false)} #{source_element(false, next_indent)}
#{enclosure_element(false)} #{enclosure_element(false, next_indent)}
#{other_element(false, "\t\t\t\t")} #{other_element(false, next_indent)}
</item> #{indent}</item>
EOT EOT
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
@ -350,7 +356,7 @@ EOT
@content = content @content = content
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @url if @url
rv = %Q! <source url="#{@url}">! rv = %Q! <source url="#{@url}">!
rv << %Q!#{@content}</source>! rv << %Q!#{@content}</source>!
@ -393,7 +399,7 @@ EOT
@type = type @type = type
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @url and @length and @type if @url and @length and @type
rv = %Q!<enclosure url="#{h @url}" ! rv = %Q!<enclosure url="#{h @url}" !
rv << %Q!length="#{h @length}" type="#{h @type}"/>! rv << %Q!length="#{h @length}" type="#{h @type}"/>!
@ -433,7 +439,7 @@ EOT
@content = content @content = content
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @domain if @domain
rv = %Q!<category domain="#{h @domain}">! rv = %Q!<category domain="#{h @domain}">!
rv << %Q!#{h @content}</category>! rv << %Q!#{h @content}</category>!
@ -464,15 +470,16 @@ EOT
install_model(x, nil) install_model(x, nil)
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
rv = <<-EOT rv = <<-EOT
<textInput> #{indent}<textInput>
#{title_element(false)} #{title_element(false, next_indent)}
#{description_element(false)} #{description_element(false, next_indent)}
#{name_element(false)} #{name_element(false, next_indent)}
#{link_element(false)} #{link_element(false, next_indent)}
#{other_element(false, "\t\t\t\t")} #{other_element(false, next_indent)}
</textInput> #{indent}</textInput>
EOT EOT
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv

View file

@ -28,7 +28,7 @@ module RSS
end end
TAG_NAME.replace('RDF') @tag_name = 'RDF'
PREFIX = 'rdf' PREFIX = 'rdf'
URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
@ -57,19 +57,21 @@ module RSS
super('1.0', version, encoding, standalone) super('1.0', version, encoding, standalone)
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
rv = <<-EORDF rv = <<-EORDF
#{xmldecl} #{xmldecl}
#{xml_stylesheet_pi}<#{PREFIX}:RDF#{ns_declaration}> #{xml_stylesheet_pi}
#{channel_element(false)} #{indent}<#{PREFIX}:RDF#{ns_declaration(next_indent)}>
#{image_element(false)} #{channel_element(false, next_indent)}
#{item_elements(false)} #{image_element(false, next_indent)}
#{textinput_element(false)} #{item_elements(false, next_indent)}
#{other_element(false, "\t")} #{textinput_element(false, next_indent)}
</#{PREFIX}:RDF> #{other_element(false, next_indent)}
#{indent}</#{PREFIX}:RDF>
EORDF EORDF
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv remove_empty_newline(rv)
end end
private private
@ -105,7 +107,7 @@ EORDF
end end
TAG_NAME.replace('Seq') @tag_name = 'Seq'
install_have_children_element("li") install_have_children_element("li")
@ -116,12 +118,13 @@ EORDF
@li = li @li = li
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
<<-EOT <<-EOT
<#{PREFIX}:Seq> #{indent}<#{PREFIX}:Seq>
#{li_elements(convert, "\t\t\t\t")} #{li_elements(convert, next_indent)}
#{other_element(convert, "\t\t\t\t")} #{other_element(convert, next_indent)}
</#{PREFIX}:Seq> #{indent}</#{PREFIX}:Seq>
EOT EOT
end end
@ -167,9 +170,9 @@ EOT
@resource = resource @resource = resource
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @resource if @resource
rv = %Q!<#{PREFIX}:li resource="#{h @resource}" />\n! rv = %Q!#{indent}<#{PREFIX}:li resource="#{h @resource}" />\n!
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
else else
@ -228,19 +231,20 @@ EOT
@about = about @about = about
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
about = '' about = ''
about << %Q!#{PREFIX}:about="#{h @about}"! if @about about << %Q!#{PREFIX}:about="#{h @about}"! if @about
rv = <<-EOT rv = <<-EOT
<channel #{about}> #{indent}<channel #{about}>
#{title_element(false)} #{title_element(false, next_indent)}
#{link_element(false)} #{link_element(false, next_indent)}
#{description_element(false)} #{description_element(false, next_indent)}
#{image_element(false)} #{image_element(false, next_indent)}
#{items_element(false)} #{items_element(false, next_indent)}
#{textinput_element(false)} #{textinput_element(false, next_indent)}
#{other_element(false, "\t\t")} #{other_element(false, next_indent)}
</channel> #{indent}</channel>
EOT EOT
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
@ -293,9 +297,9 @@ EOT
@resource = resource @resource = resource
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @resource if @resource
rv = %Q!<image #{PREFIX}:resource="#{h @resource}" />! rv = %Q!#{indent}<image #{PREFIX}:resource="#{h @resource}" />!
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
else else
@ -335,9 +339,9 @@ EOT
@resource = resource @resource = resource
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @resource if @resource
rv = %Q|<textinput #{PREFIX}:resource="#{h @resource}" />| rv = %Q|#{indent}<textinput #{PREFIX}:resource="#{h @resource}" />|
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
else else
@ -382,12 +386,13 @@ EOT
@Seq = seq @Seq = seq
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
<<-EOT <<-EOT
<items> #{indent}<items>
#{Seq_element(convert)} #{Seq_element(convert, next_indent)}
#{other_element(convert, "\t\t\t")} #{other_element(convert, next_indent)}
</items> #{indent}</items>
EOT EOT
end end
@ -446,16 +451,17 @@ EOT
@about = about @about = about
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
about = '' about = ''
about << %Q!#{PREFIX}:about="#{h @about}"! if @about about << %Q!#{PREFIX}:about="#{h @about}"! if @about
rv = <<-EOT rv = <<-EOT
<image #{about}> #{indent}<image #{about}>
#{title_element(false)} #{title_element(false, next_indent)}
#{url_element(false)} #{url_element(false, next_indent)}
#{link_element(false)} #{link_element(false, next_indent)}
#{other_element(false, "\t\t")} #{other_element(false, next_indent)}
</image> #{indent}</image>
EOT EOT
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
@ -515,16 +521,17 @@ EOT
@about = about @about = about
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
about = '' about = ''
about << %Q!#{PREFIX}:about="#{h @about}"! if @about about << %Q!#{PREFIX}:about="#{h @about}"! if @about
rv = <<-EOT rv = <<-EOT
<item #{about}> #{indent}<item #{about}>
#{title_element(false)} #{title_element(false, next_indent)}
#{link_element(false)} #{link_element(false, next_indent)}
#{description_element(false)} #{description_element(false, next_indent)}
#{other_element(false, "\t\t")} #{other_element(false, next_indent)}
</item> #{indent}</item>
EOT EOT
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
@ -585,17 +592,18 @@ EOT
@about = about @about = about
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
next_indent = indent + INDENT
about = '' about = ''
about << %Q!#{PREFIX}:about="#{h @about}"! if @about about << %Q!#{PREFIX}:about="#{h @about}"! if @about
rv = <<-EOT rv = <<-EOT
<textinput #{about}> #{indent}<textinput #{about}>
#{title_element(false)} #{title_element(false, next_indent)}
#{description_element(false)} #{description_element(false, next_indent)}
#{name_element(false)} #{name_element(false, next_indent)}
#{link_element(false)} #{link_element(false, next_indent)}
#{other_element(false, "\t\t")} #{other_element(false, next_indent)}
</textinput> #{indent}</textinput>
EOT EOT
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv

View file

@ -23,11 +23,11 @@ module RSS
install_model(x, occurs) install_model(x, occurs)
end end
def other_element(convert, indent='') def other_element(convert, indent)
rv = <<-EOT rv = <<-EOT
#{indent}#{category_element(convert)} #{category_element(convert, indent)}
#{indent}#{generator_element(convert)} #{generator_element(convert, indent)}
#{indent}#{ttl_element(convert)} #{ttl_element(convert, indent)}
EOT EOT
rv << super rv << super
end end
@ -73,12 +73,12 @@ EOT
install_model(x, occurs) install_model(x, occurs)
end end
def other_element(convert, indent='') def other_element(convert, indent)
rv = <<-EOT rv = <<-EOT
#{indent}#{author_element(false)} #{author_element(false, indent)}
#{indent}#{comments_element(false)} #{comments_element(false, indent)}
#{indent}#{pubDate_element(false)} #{pubDate_element(false, indent)}
#{indent}#{guid_element(false)} #{guid_element(false, indent)}
EOT EOT
rv << super rv << super
end end
@ -116,7 +116,7 @@ EOT
@content = content @content = content
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @content if @content
rv = %Q!<guid! rv = %Q!<guid!
rv << %Q! isPermaLink="#{h @isPermaLink}"! if @isPermaLink rv << %Q! isPermaLink="#{h @isPermaLink}"! if @isPermaLink

View file

@ -286,8 +286,8 @@ module RSS
@proc_stack.push(nil) @proc_stack.push(nil)
else else
parent = "ROOT ELEMENT???" parent = "ROOT ELEMENT???"
if current_class.const_defined?("TAG_NAME") if current_class.tag_name
parent = current_class.const_get("TAG_NAME") parent = current_class.tag_name
end end
raise NotExceptedTagError.new(local, parent) raise NotExceptedTagError.new(local, parent)
end end

View file

@ -154,7 +154,7 @@ module RSS
install_element(name) do |n, elem_name| install_element(name) do |n, elem_name|
<<-EOC <<-EOC
if @#{n} if @#{n}
"\#{indent}\#{@#{n}.to_s(convert)}" "\#{@#{n}.to_s(convert, indent)}"
else else
'' ''
end end
@ -171,7 +171,7 @@ EOC
<<-EOC <<-EOC
rv = '' rv = ''
@#{n}.each do |x| @#{n}.each do |x|
rv << "\#{indent}\#{x.to_s(convert)}" rv << "\#{x.to_s(convert, indent)}"
end end
rv rv
EOC EOC
@ -320,14 +320,16 @@ EOC
extend BaseModel extend BaseModel
include Utils include Utils
INDENT = " "
class << self class << self
def inherited(klass) def inherited(klass)
klass.module_eval(<<-EOC) klass.module_eval(<<-EOC)
public public
TAG_NAME = name.split('::').last.downcase @tag_name = name.split(/::/).last.downcase
@indent_size = name.split(/::/).size - 2
@@must_call_validators = {} @@must_call_validators = {}
@ -415,6 +417,14 @@ EOC
self::NSPOOL[prefix] = uri self::NSPOOL[prefix] = uri
end end
def tag_name
@tag_name
end
def indent_size
@indent_size
end
end end
attr_accessor :do_validate attr_accessor :do_validate
@ -426,7 +436,7 @@ EOC
end end
def tag_name def tag_name
self.class::TAG_NAME self.class.tag_name
end end
def converter=(converter) def converter=(converter)
@ -498,7 +508,7 @@ EOC
def validate_attribute def validate_attribute
_attrs.each do |a_name, required| _attrs.each do |a_name, required|
if required and send(a_name).nil? if required and send(a_name).nil?
raise MissingAttributeError.new(self.class::TAG_NAME, a_name) raise MissingAttributeError.new(self.class.tag_name, a_name)
end end
end end
end end
@ -615,6 +625,15 @@ EOC
rv rv
end end
private
def calc_indent
INDENT * (self.class.indent_size)
end
def remove_empty_newline(string)
string.gsub(/^\s*$(?:\r?\n?)/, '')
end
end end
module RootElementMixin module RootElementMixin
@ -646,11 +665,11 @@ EOC
rv rv
end end
def ns_declaration def ns_declaration(indent)
rv = '' rv = ''
self.class::NSPOOL.each do |prefix, uri| self.class::NSPOOL.each do |prefix, uri|
prefix = ":#{prefix}" unless prefix.empty? prefix = ":#{prefix}" unless prefix.empty?
rv << %Q|\n\txmlns#{prefix}="#{html_escape(uri)}"| rv << %Q|\n#{indent}xmlns#{prefix}="#{html_escape(uri)}"|
end end
rv rv
end end

View file

@ -76,9 +76,10 @@ module RSS
@resource = resource @resource = resource
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @resource if @resource
rv = %Q!<#{TRACKBACK_PREFIX}:ping #{::RSS::RDF::PREFIX}:resource="#{h @resource}"/>! rv = %Q[#{indent}<#{TRACKBACK_PREFIX}:ping ]
rv << %Q[#{::RSS::RDF::PREFIX}:resource="#{h @resource}"/>]
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
else else
@ -121,9 +122,10 @@ module RSS
@resource = resource @resource = resource
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @resource if @resource
rv = %Q!<#{TRACKBACK_PREFIX}:about #{::RSS::RDF::PREFIX}:resource="#{h @resource}"/>! rv = %Q[#{indent}<#{TRACKBACK_PREFIX}:about ]
rv << %Q[#{::RSS::RDF::PREFIX}:resource="#{h @resource}"/>]
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
else else
@ -245,9 +247,11 @@ module RSS
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @content if @content
rv = %Q!<#{TRACKBACK_PREFIX}:ping>#{h @content}</#{TRACKBACK_PREFIX}:ping>! rv = %Q[#{indent}<#{TRACKBACK_PREFIX}:ping>]
rv << h(@content)
rv << %Q[</#{TRACKBACK_PREFIX}:ping>]
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
else else
@ -274,9 +278,11 @@ module RSS
end end
def to_s(convert=true) def to_s(convert=true, indent=calc_indent)
if @content if @content
rv = %Q!<#{TRACKBACK_PREFIX}:about>#{h @content}</#{TRACKBACK_PREFIX}:about>! rv = %Q[#{indent}<#{TRACKBACK_PREFIX}:about>]
rv << h(@content)
rv << %Q[</#{TRACKBACK_PREFIX}:about>]
rv = @converter.convert(rv) if convert and @converter rv = @converter.convert(rv) if convert and @converter
rv rv
else else

View file

@ -255,5 +255,16 @@ module RSS
end end
def test_indent_size
assert_equal(0, RDF.indent_size)
assert_equal(1, RDF::Channel.indent_size)
assert_equal(2, RDF::Channel::Image.indent_size)
assert_equal(2, RDF::Channel::Textinput.indent_size)
assert_equal(2, RDF::Channel::Items.indent_size)
assert_equal(1, RDF::Image.indent_size)
assert_equal(1, RDF::Item.indent_size)
assert_equal(1, RDF::Textinput.indent_size)
end
end end
end end