summaryrefslogtreecommitdiff
path: root/lib/rss/rss.rb
diff options
context:
space:
mode:
authorkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-10-16 04:39:58 +0000
committerkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-10-16 04:39:58 +0000
commit6b988d9fab9c4c3b713fbcfee3c96633350a1731 (patch)
tree0411eee34ca110036f249f0da0f84e9130c82923 /lib/rss/rss.rb
parenta7ceaf92356eb918cd058f11330cb5a4840f21bd (diff)
* 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/branches/ruby_1_8@7047 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rss/rss.rb')
-rw-r--r--lib/rss/rss.rb35
1 files changed, 27 insertions, 8 deletions
diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb
index 13eaf7c505..24ba0eb170 100644
--- a/lib/rss/rss.rb
+++ b/lib/rss/rss.rb
@@ -154,7 +154,7 @@ module RSS
install_element(name) do |n, elem_name|
<<-EOC
if @#{n}
- "\#{indent}\#{@#{n}.to_s(convert)}"
+ "\#{@#{n}.to_s(convert, indent)}"
else
''
end
@@ -171,7 +171,7 @@ EOC
<<-EOC
rv = ''
@#{n}.each do |x|
- rv << "\#{indent}\#{x.to_s(convert)}"
+ rv << "\#{x.to_s(convert, indent)}"
end
rv
EOC
@@ -320,14 +320,16 @@ EOC
extend BaseModel
include Utils
+ INDENT = " "
+
class << self
def inherited(klass)
klass.module_eval(<<-EOC)
public
- TAG_NAME = name.split('::').last.downcase
-
+ @tag_name = name.split(/::/).last.downcase
+ @indent_size = name.split(/::/).size - 2
@@must_call_validators = {}
@@ -415,6 +417,14 @@ EOC
self::NSPOOL[prefix] = uri
end
+ def tag_name
+ @tag_name
+ end
+
+ def indent_size
+ @indent_size
+ end
+
end
attr_accessor :do_validate
@@ -426,7 +436,7 @@ EOC
end
def tag_name
- self.class::TAG_NAME
+ self.class.tag_name
end
def converter=(converter)
@@ -498,7 +508,7 @@ EOC
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)
+ raise MissingAttributeError.new(self.class.tag_name, a_name)
end
end
end
@@ -615,6 +625,15 @@ 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
@@ -646,11 +665,11 @@ EOC
rv
end
- def ns_declaration
+ def ns_declaration(indent)
rv = ''
self.class::NSPOOL.each do |prefix, uri|
prefix = ":#{prefix}" unless prefix.empty?
- rv << %Q|\n\txmlns#{prefix}="#{html_escape(uri)}"|
+ rv << %Q|\n#{indent}xmlns#{prefix}="#{html_escape(uri)}"|
end
rv
end