summaryrefslogtreecommitdiff
path: root/sample
diff options
context:
space:
mode:
authorkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-06-17 01:37:08 +0000
committerkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-06-17 01:37:08 +0000
commitfaeff623e09cb77c23a93b56001b51b043b5aa6a (patch)
treee9fb92ee7006fa7d105d055510238effea9a63ec /sample
parent02cce7ed8273bab55081357c57f24a8265a6f32a (diff)
* lib/rss/rss.rb (Hash#merge, Enumerable#sort_by): removed.
* lib/rss/rss.rb (RSS::RootElementMixin#to_xml): added. [ruby-talk:197284] We can convert RSS version easily like the following: rss10 = RSS::Parser.parse(File.read("1.0.rdf")) File.open("2.0.rss", "w") {|f| f.print(rss10.to_xml("2.0"))} * test/rss/test_1.0.rb: added #to_xml test. * test/rss/test_2.0.rb: ditto. * test/rss/rss-testcase.rb: added some helper methods that generates sample RSS 2.0. * sample/rss/convert.rb: added a sample script to convert RSS format. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10301 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample')
-rwxr-xr-xsample/rss/convert.rb69
1 files changed, 69 insertions, 0 deletions
diff --git a/sample/rss/convert.rb b/sample/rss/convert.rb
new file mode 100755
index 0000000000..394b13e8eb
--- /dev/null
+++ b/sample/rss/convert.rb
@@ -0,0 +1,69 @@
+#!/usr/bin/env ruby
+
+require "rss"
+
+feeds = []
+verbose = false
+encoding = "UTF-8"
+to_version = "1.0"
+
+def error(exception)
+ mark = "=" * 20
+ mark = "#{mark} error #{mark}"
+ STDERR.puts mark
+ STDERR.puts exception.class
+ STDERR.puts exception.message
+ STDERR.puts exception.backtrace
+ STDERR.puts mark
+end
+
+before_time = Time.now
+ARGV.each do |fname|
+ case fname
+ when '-v'
+ verbose = true
+ next
+ when /^-t(0\.91|1\.0|2\.0)$/
+ to_version = $1
+ next
+ end
+ rss = nil
+ f = File.read(fname)
+ begin
+ ## do validate parse
+ rss = RSS::Parser.parse(f)
+ rescue RSS::InvalidRSSError
+ error($!) if verbose
+ ## do non validate parse for invalid RSS 1.0
+ begin
+ rss = RSS::Parser.parse(f, false)
+ rescue RSS::Error
+ ## invalid RSS.
+ error($!) if verbose
+ end
+ rescue RSS::Error
+ error($!) if verbose
+ end
+ if rss.nil?
+ STDERR.puts "#{fname} does not include RSS 1.0 or 0.9x/2.0"
+ else
+ begin
+ rss.output_encoding = encoding
+ rescue RSS::UnknownConversionMethodError
+ error($!) if verbose
+ end
+ feeds << [fname, rss]
+ end
+end
+processing_time = Time.now - before_time
+
+feeds.each do |fname, rss|
+ converted_rss = rss.to_xml(to_version)
+ output_name = fname.sub(/(\.[^\.]+)$/, "-#{to_version}\\1")
+ File.open(output_name, "w") do |output|
+ output.print(converted_rss)
+ end
+end
+
+STDERR.puts "Used XML parser: #{RSS::Parser.default_parser}"
+STDERR.puts "Processing time: #{processing_time}s"