From 0ebac90b0ebb07ad2aa99080c8509559a7ddc74d Mon Sep 17 00:00:00 2001 From: kou Date: Sat, 16 Oct 2004 04:39:58 +0000 Subject: [PATCH] * 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 --- ChangeLog | 5 ++ lib/rss/0.9.rb | 139 +++++++++++++++++++++++-------------------- lib/rss/1.0.rb | 126 +++++++++++++++++++++------------------ lib/rss/2.0.rb | 20 +++---- lib/rss/parser.rb | 4 +- lib/rss/rss.rb | 35 ++++++++--- lib/rss/trackback.rb | 22 ++++--- test/rss/test_1.0.rb | 13 +++- 8 files changed, 210 insertions(+), 154 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6d4714a710..1b55edaac5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat Oct 16 13:34:56 2004 Kouhei Sutou + + * 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 * lib/mkmf.rb (create_makefile): install-rb is needed for statically diff --git a/lib/rss/0.9.rb b/lib/rss/0.9.rb index c95c47e974..8486c4fc1f 100644 --- a/lib/rss/0.9.rb +++ b/lib/rss/0.9.rb @@ -51,16 +51,18 @@ module RSS end end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT rv = <<-EOR #{xmldecl} -#{xml_stylesheet_pi} -#{channel_element(false)} -#{other_element(false, "\t")} - +#{xml_stylesheet_pi} +#{indent} +#{channel_element(false, next_indent)} +#{other_element(false, next_indent)} +#{inent}} EOR - rv = @converter.convert(rv) if convert and @converter - rv + rv = @converter.convert(rv) if convert and @converter + remove_empty_newline(rv) end private @@ -135,30 +137,31 @@ EOR super() end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT rv = <<-EOT - - #{title_element(false)} - #{link_element(false)} - #{description_element(false)} - #{language_element(false)} - #{copyright_element(false)} - #{managingEditor_element(false)} - #{webMaster_element(false)} - #{rating_element(false)} - #{pubDate_element(false)} - #{lastBuildDate_element(false)} - #{docs_element(false)} - #{skipDays_element(false)} - #{skipHours_element(false)} - #{image_element(false)} -#{item_elements(false)} - #{textInput_element(false)} -#{other_element(false, "\t\t")} - +#{indent} +#{title_element(false, next_indent)} +#{link_element(false, next_indent)} +#{description_element(false, next_indent)} +#{language_element(false, next_indent)} +#{copyright_element(false, next_indent)} +#{managingEditor_element(false, next_indent)} +#{webMaster_element(false, next_indent)} +#{rating_element(false, next_indent)} +#{pubDate_element(false, next_indent)} +#{lastBuildDate_element(false, next_indent)} +#{docs_element(false, next_indent)} +#{skipDays_element(false, next_indent)} +#{skipHours_element(false, next_indent)} +#{image_element(false, next_indent)} +#{item_elements(false, next_indent)} +#{textInput_element(false, next_indent)} +#{other_element(false, next_indent)} +#{indent} EOT rv = @converter.convert(rv) if convert and @converter - rv + rv end private @@ -208,17 +211,18 @@ EOT install_model(x, "?") end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT rv = <<-EOT - - #{url_element(false)} - #{title_element(false)} - #{link_element(false)} - #{width_element(false)} - #{height_element(false)} - #{description_element(false)} -#{other_element(false, "\t\t\t\t")} - +#{indent} +#{url_element(false, next_indent)} +#{title_element(false, next_indent)} +#{link_element(false, next_indent)} +#{width_element(false, next_indent)} +#{height_element(false, next_indent)} +#{description_element(false, next_indent)} +#{other_element(false, next_indent)} +#{indent} EOT rv = @converter.convert(rv) if convert and @converter rv @@ -257,14 +261,15 @@ EOT @protocol = protocol end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT rv = <<-EOT - +#{indent} EOT rv = @converter.convert(rv) if convert and @converter rv @@ -302,17 +307,18 @@ EOT install_model(tag, occurs) end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT rv = <<-EOT - - #{title_element(false)} - #{link_element(false)} - #{description_element(false)} - #{category_element(false)} - #{source_element(false)} - #{enclosure_element(false)} -#{other_element(false, "\t\t\t\t")} - +#{indent} +#{title_element(false, next_indent)} +#{link_element(false, next_indent)} +#{description_element(false, next_indent)} +#{category_element(false, next_indent)} +#{source_element(false, next_indent)} +#{enclosure_element(false, next_indent)} +#{other_element(false, next_indent)} +#{indent} EOT rv = @converter.convert(rv) if convert and @converter rv @@ -350,7 +356,7 @@ EOT @content = content end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) if @url rv = %Q! ! rv << %Q!#{@content}! @@ -393,7 +399,7 @@ EOT @type = type end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) if @url and @length and @type rv = %Q!! @@ -433,7 +439,7 @@ EOT @content = content end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) if @domain rv = %Q!! rv << %Q!#{h @content}! @@ -464,15 +470,16 @@ EOT install_model(x, nil) end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT rv = <<-EOT - - #{title_element(false)} - #{description_element(false)} - #{name_element(false)} - #{link_element(false)} -#{other_element(false, "\t\t\t\t")} - +#{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)} +#{indent} EOT rv = @converter.convert(rv) if convert and @converter rv diff --git a/lib/rss/1.0.rb b/lib/rss/1.0.rb index be141d73d8..999301c595 100644 --- a/lib/rss/1.0.rb +++ b/lib/rss/1.0.rb @@ -28,7 +28,7 @@ module RSS end - TAG_NAME.replace('RDF') + @tag_name = 'RDF' PREFIX = 'rdf' URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" @@ -57,19 +57,21 @@ module RSS super('1.0', version, encoding, standalone) end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT rv = <<-EORDF #{xmldecl} -#{xml_stylesheet_pi}<#{PREFIX}:RDF#{ns_declaration}> -#{channel_element(false)} -#{image_element(false)} -#{item_elements(false)} -#{textinput_element(false)} -#{other_element(false, "\t")} - +#{xml_stylesheet_pi} +#{indent}<#{PREFIX}:RDF#{ns_declaration(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)} +#{indent} EORDF rv = @converter.convert(rv) if convert and @converter - rv + remove_empty_newline(rv) end private @@ -105,7 +107,7 @@ EORDF end - TAG_NAME.replace('Seq') + @tag_name = 'Seq' install_have_children_element("li") @@ -116,12 +118,13 @@ EORDF @li = li end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT <<-EOT - <#{PREFIX}:Seq> -#{li_elements(convert, "\t\t\t\t")} -#{other_element(convert, "\t\t\t\t")} - +#{indent}<#{PREFIX}:Seq> +#{li_elements(convert, next_indent)} +#{other_element(convert, next_indent)} +#{indent} EOT end @@ -167,9 +170,9 @@ EOT @resource = resource end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) 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 else @@ -228,19 +231,20 @@ EOT @about = about end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT about = '' about << %Q!#{PREFIX}:about="#{h @about}"! if @about rv = <<-EOT - - #{title_element(false)} - #{link_element(false)} - #{description_element(false)} - #{image_element(false)} -#{items_element(false)} - #{textinput_element(false)} -#{other_element(false, "\t\t")} - +#{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)} +#{indent} EOT rv = @converter.convert(rv) if convert and @converter rv @@ -293,9 +297,9 @@ EOT @resource = resource end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) if @resource - rv = %Q!! + rv = %Q!#{indent}! rv = @converter.convert(rv) if convert and @converter rv else @@ -335,9 +339,9 @@ EOT @resource = resource end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) if @resource - rv = %Q|| + rv = %Q|#{indent}| rv = @converter.convert(rv) if convert and @converter rv else @@ -382,12 +386,13 @@ EOT @Seq = seq end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT <<-EOT - -#{Seq_element(convert)} -#{other_element(convert, "\t\t\t")} - +#{indent} +#{Seq_element(convert, next_indent)} +#{other_element(convert, next_indent)} +#{indent} EOT end @@ -446,16 +451,17 @@ EOT @about = about end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT about = '' about << %Q!#{PREFIX}:about="#{h @about}"! if @about rv = <<-EOT - - #{title_element(false)} - #{url_element(false)} - #{link_element(false)} -#{other_element(false, "\t\t")} - +#{indent} +#{title_element(false, next_indent)} +#{url_element(false, next_indent)} +#{link_element(false, next_indent)} +#{other_element(false, next_indent)} +#{indent} EOT rv = @converter.convert(rv) if convert and @converter rv @@ -515,16 +521,17 @@ EOT @about = about end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT about = '' about << %Q!#{PREFIX}:about="#{h @about}"! if @about rv = <<-EOT - - #{title_element(false)} - #{link_element(false)} - #{description_element(false)} -#{other_element(false, "\t\t")} - +#{indent} +#{title_element(false, next_indent)} +#{link_element(false, next_indent)} +#{description_element(false, next_indent)} +#{other_element(false, next_indent)} +#{indent} EOT rv = @converter.convert(rv) if convert and @converter rv @@ -585,17 +592,18 @@ EOT @about = about end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) + next_indent = indent + INDENT about = '' about << %Q!#{PREFIX}:about="#{h @about}"! if @about rv = <<-EOT - - #{title_element(false)} - #{description_element(false)} - #{name_element(false)} - #{link_element(false)} -#{other_element(false, "\t\t")} - +#{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)} +#{indent} EOT rv = @converter.convert(rv) if convert and @converter rv diff --git a/lib/rss/2.0.rb b/lib/rss/2.0.rb index c6c6db58a7..c4d055e984 100644 --- a/lib/rss/2.0.rb +++ b/lib/rss/2.0.rb @@ -23,11 +23,11 @@ module RSS install_model(x, occurs) end - def other_element(convert, indent='') + def other_element(convert, indent) rv = <<-EOT -#{indent}#{category_element(convert)} -#{indent}#{generator_element(convert)} -#{indent}#{ttl_element(convert)} +#{category_element(convert, indent)} +#{generator_element(convert, indent)} +#{ttl_element(convert, indent)} EOT rv << super end @@ -73,12 +73,12 @@ EOT install_model(x, occurs) end - def other_element(convert, indent='') + def other_element(convert, indent) rv = <<-EOT -#{indent}#{author_element(false)} -#{indent}#{comments_element(false)} -#{indent}#{pubDate_element(false)} -#{indent}#{guid_element(false)} +#{author_element(false, indent)} +#{comments_element(false, indent)} +#{pubDate_element(false, indent)} +#{guid_element(false, indent)} EOT rv << super end @@ -116,7 +116,7 @@ EOT @content = content end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) if @content rv = %Q!! + rv = %Q[#{indent}<#{TRACKBACK_PREFIX}:ping ] + rv << %Q[#{::RSS::RDF::PREFIX}:resource="#{h @resource}"/>] rv = @converter.convert(rv) if convert and @converter rv else @@ -121,9 +122,10 @@ module RSS @resource = resource end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) 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 else @@ -245,9 +247,11 @@ module RSS end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) if @content - rv = %Q!<#{TRACKBACK_PREFIX}:ping>#{h @content}! + rv = %Q[#{indent}<#{TRACKBACK_PREFIX}:ping>] + rv << h(@content) + rv << %Q[] rv = @converter.convert(rv) if convert and @converter rv else @@ -274,9 +278,11 @@ module RSS end - def to_s(convert=true) + def to_s(convert=true, indent=calc_indent) if @content - rv = %Q!<#{TRACKBACK_PREFIX}:about>#{h @content}! + rv = %Q[#{indent}<#{TRACKBACK_PREFIX}:about>] + rv << h(@content) + rv << %Q[] rv = @converter.convert(rv) if convert and @converter rv else diff --git a/test/rss/test_1.0.rb b/test/rss/test_1.0.rb index 6ed4d90b15..0ea3e884d8 100644 --- a/test/rss/test_1.0.rb +++ b/test/rss/test_1.0.rb @@ -254,6 +254,17 @@ 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