[前][次][番号順一覧][スレッド一覧]

ruby-changes:26078

From: drbrain <ko1@a...>
Date: Sun, 2 Dec 2012 07:25:41 +0900 (JST)
Subject: [ruby-changes:26078] drbrain:r38135 (trunk): * lib/rdoc/parser.rb: Parse files with a -*- rdoc -*- modeline

drbrain	2012-12-02 07:24:53 +0900 (Sun, 02 Dec 2012)

  New Revision: 38135

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38135

  Log:
    * lib/rdoc/parser.rb:  Parse files with a -*- rdoc -*- modeline
    * test/rdoc/test_rdoc_parser.rb:  Test for above

  Modified files:
    trunk/ChangeLog
    trunk/lib/rdoc/parser.rb
    trunk/test/rdoc/test_rdoc_parser.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38134)
+++ ChangeLog	(revision 38135)
@@ -1,3 +1,8 @@
+Sun Dec  2 07:24:23 2012  Eric Hodel  <drbrain@s...>
+
+	* lib/rdoc/parser.rb:  Parse files with a -*- rdoc -*- modeline
+	* test/rdoc/test_rdoc_parser.rb:  Test for above
+
 Sun Dec  2 06:02:00 2012  KOSAKI Motohiro  <kosaki.motohiro@g...>
 
 	* gc.h (SET_MACHINE_STACK_END): add volatile for preventing
Index: lib/rdoc/parser.rb
===================================================================
--- lib/rdoc/parser.rb	(revision 38134)
+++ lib/rdoc/parser.rb	(revision 38135)
@@ -138,13 +138,34 @@
     # The default parser must not parse binary files
     ext_name = File.extname file_name
     return parser if ext_name.empty?
-    return if parser == RDoc::Parser::Simple and ext_name !~ /txt|rdoc/
+    if parser == RDoc::Parser::Simple and ext_name !~ /txt|rdoc/ then
+      case check_modeline file_name
+      when 'rdoc' then # continue
+      else return nil
+      end
+    end
 
     parser
   rescue Errno::EACCES
   end
 
   ##
+  # Returns the file type from the modeline in +file_name+
+
+  def self.check_modeline file_name
+    line = open file_name do |io|
+      io.gets
+    end
+
+    line =~ /-\*-(.*?)-\*-/
+
+    return nil unless type = $1
+
+    type.strip.downcase
+  rescue ArgumentError # invalid byte sequence, etc.
+  end
+
+  ##
   # Finds and instantiates the correct parser for the given +file_name+ and
   # +content+.
 
Index: test/rdoc/test_rdoc_parser.rb
===================================================================
--- test/rdoc/test_rdoc_parser.rb	(revision 38134)
+++ test/rdoc/test_rdoc_parser.rb	(revision 38135)
@@ -75,6 +75,20 @@
     end
   end
 
+  def test_can_parse_modeline
+    readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"
+
+    open readme_ext, 'w' do |io|
+      io.puts "# README.EXT -  -*- rdoc -*- created at: Mon Aug 7 16:45:54 JST 1995"
+      io.puts
+      io.puts "This document explains how to make extension libraries for Ruby."
+    end
+
+    assert_equal RDoc::Parser::Simple, @RP.can_parse(readme_ext)
+  ensure
+    File.unlink readme_ext
+  end
+
   ##
   # Selenium hides a .jar file using a .txt extension.
 
@@ -83,6 +97,32 @@
     assert_nil @RP.can_parse(hidden_zip)
   end
 
+  def test_check_modeline
+    readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"
+
+    open readme_ext, 'w' do |io|
+      io.puts "# README.EXT -  -*- RDoc -*- created at: Mon Aug 7 16:45:54 JST 1995"
+      io.puts
+      io.puts "This document explains how to make extension libraries for Ruby."
+    end
+
+    assert_equal 'rdoc', @RP.check_modeline(readme_ext)
+  ensure
+    File.unlink readme_ext
+  end
+
+  def test_check_modeline_no_modeline
+    readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"
+
+    open readme_ext, 'w' do |io|
+      io.puts "This document explains how to make extension libraries for Ruby."
+    end
+
+    assert_nil @RP.check_modeline(readme_ext)
+  ensure
+    File.unlink readme_ext
+  end
+
   def test_class_for_binary
     rp = @RP.dup
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]