Permalink
Browse files

Prevent RDoc from parsing binary files, which would produce garbage o…

…utput.
  • Loading branch information...
1 parent 8f49afa commit a3f78ab0a65e85c7208164ddb4a6e25fa72db8f1 @DesigningPatterns DesigningPatterns committed Aug 28, 2008
Showing with 39 additions and 1 deletion.
  1. +2 −0 History.txt
  2. +2 −0 Manifest.txt
  3. +22 −1 lib/rdoc/parser.rb
  4. BIN test/binary.dat
  5. +13 −0 test/test_rdoc_parser.rb
View
@@ -21,6 +21,8 @@
* Fixed a case where RDoc first would encounter Foo::Bar and then would
encounter class Foo. Previously, RDoc erroneously would have considered
that both a Foo class and a Foo module existed.
+ * Prevented RDoc from trying to parse binary files, which would produce
+ garbage output.
* Fixed some (but not all!) of the issues with RDoc's HTML typesetting:
** RDoc now correctly converts ' characters to apostrophes, opening single
quotes, and closing single quotes in most cases (smart single quotes).
View
@@ -62,13 +62,15 @@ lib/rdoc/ri/writer.rb
lib/rdoc/stats.rb
lib/rdoc/template.rb
lib/rdoc/tokenstream.rb
+test/binary.dat
test/rdoc_markup_to_html_crossref_reference.rb
test/test_rdoc_info_formatting.rb
test/test_rdoc_info_sections.rb
test/test_rdoc_markup.rb
test/test_rdoc_markup_attribute_manager.rb
test/test_rdoc_markup_to_html.rb
test/test_rdoc_markup_to_html_crossref.rb
+test/test_rdoc_parser.rb
test/test_rdoc_parser_c.rb
test/test_rdoc_parser_ruby.rb
test/test_rdoc_ri_attribute_formatter.rb
View
@@ -63,10 +63,31 @@ def self.alias_extension(old_ext, new_ext)
end
##
+ # Shamelessly stolen from the ptools gem (since RDoc cannot depend on
+ # the gem).
+
+ def self.binary?(file)
+ s = (File.read(file, File.stat(file).blksize) || "").split(//)
+ ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
+ end
+ private_class_method :binary?
+
+ ##
# Return a parser that can handle a particular extension
def self.can_parse(file_name)
- RDoc::Parser.parsers.find { |regexp, parser| regexp =~ file_name }.last
+ parser = RDoc::Parser.parsers.find { |regexp, parser| regexp =~ file_name }.last
+
+ #
+ # The default parser should *NOT* parse binary files.
+ #
+ if parser == RDoc::Parser::Simple then
+ if binary? file_name then
+ return nil
+ end
+ end
+
+ return parser
end
##
View
Binary file not shown.
View
@@ -0,0 +1,13 @@
+require 'rdoc/parser'
+
+class TestRDocParser < Test::Unit::TestCase
+ def test_can_parse
+ assert_equal(RDoc::Parser.can_parse(__FILE__), RDoc::Parser::Ruby)
+
+ readme_file_name = File.join(File.dirname(__FILE__), "..", "README.txt")
+ assert_equal(RDoc::Parser.can_parse(readme_file_name), RDoc::Parser::Simple)
+
+ binary_file_name = File.join(File.dirname(__FILE__), "binary.dat")
+ assert_equal(RDoc::Parser.can_parse(binary_file_name), nil)
+ end
+end

0 comments on commit a3f78ab

Please sign in to comment.