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

ruby-changes:26395

From: drbrain <ko1@a...>
Date: Tue, 18 Dec 2012 17:25:09 +0900 (JST)
Subject: [ruby-changes:26395] drbrain:r38446 (trunk): * lib/rdoc/encoding.rb: Do not remove #! line from document when

drbrain	2012-12-18 17:24:57 +0900 (Tue, 18 Dec 2012)

  New Revision: 38446

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

  Log:
    * lib/rdoc/encoding.rb:  Do not remove #! line from document when
      setting encoding.  This allows ruby executables to be parsed as ruby
      files.
    * test/rdoc/test_rdoc_encoding.rb:  Test for above.
    
    * lib/rdoc/parser.rb:  Set the parser file name of ruby executables
      correctly.
    * test/rdoc/test_rdoc_parser.rb:  Test for above.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38445)
+++ ChangeLog	(revision 38446)
@@ -1,3 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Dec 18 17:24:40 2012  Eric Hodel  <drbrain@s...>
+
+	* lib/rdoc/encoding.rb:  Do not remove #! line from document when
+	  setting encoding.  This allows ruby executables to be parsed as ruby
+	  files.
+	* test/rdoc/test_rdoc_encoding.rb:  Test for above.
+
+	* lib/rdoc/parser.rb:  Set the parser file name of ruby executables
+	  correctly.
+	* test/rdoc/test_rdoc_parser.rb:  Test for above.
+
 Tue Dec 18 16:46:15 2012  Eric Hodel  <drbrain@s...>
 
 	* doc/syntax/literals.rdoc:  Used simplified heredoc example that
Index: lib/rdoc/parser.rb
===================================================================
--- lib/rdoc/parser.rb	(revision 38445)
+++ lib/rdoc/parser.rb	(revision 38446)
@@ -46,6 +46,11 @@ class RDoc::Parser https://github.com/ruby/ruby/blob/trunk/lib/rdoc/parser.rb#L46
   end
 
   ##
+  # The name of the file being parsed
+
+  attr_reader :file_name
+
+  ##
   # Alias an extension to another extension. After this call, files ending
   # "new_ext" will be parsed using the same parser as "old_ext"
 
@@ -182,16 +187,18 @@ class RDoc::Parser https://github.com/ruby/ruby/blob/trunk/lib/rdoc/parser.rb#L187
     parser = use_markup content
 
     unless parser then
+      parse_name = file_name
+
       # If no extension, look for shebang
       if file_name !~ /\.\w+$/ && content =~ %r{\A#!(.+)} then
         shebang = $1
         case shebang
         when %r{env\s+ruby}, %r{/ruby}
-          file_name = "dummy.rb"
+          parse_name = 'dummy.rb'
         end
       end
 
-      parser = can_parse file_name
+      parser = can_parse parse_name
     end
 
     return unless parser
Index: lib/rdoc/encoding.rb
===================================================================
--- lib/rdoc/encoding.rb	(revision 38445)
+++ lib/rdoc/encoding.rb	(revision 38446)
@@ -75,7 +75,9 @@ module RDoc::Encoding https://github.com/ruby/ruby/blob/trunk/lib/rdoc/encoding.rb#L75
   # Sets the encoding of +string+ based on the magic comment
 
   def self.set_encoding string
-    first_line = string[/\A(?:#!.*\n)?.*\n/]
+    string =~ /\A(?:#!.*\n)?(.*\n)/
+
+    first_line = $1
 
     name = case first_line
            when /^<\?xml[^?]*encoding=(["'])(.*?)\1/ then $2
Index: test/rdoc/test_rdoc_parser.rb
===================================================================
--- test/rdoc/test_rdoc_parser.rb	(revision 38445)
+++ test/rdoc/test_rdoc_parser.rb	(revision 38446)
@@ -77,6 +77,19 @@ class TestRDocParser < RDoc::TestCase https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_parser.rb#L77
     end
   end
 
+  def test_class_for_executable
+    temp_dir do
+      content = "#!/usr/bin/env ruby -w\n"
+      open 'app', 'w' do |io| io.write content end
+      app = @store.add_file 'app'
+      parser = @RP.for app, 'app', content, @options, :stats
+
+      assert_kind_of RDoc::Parser::Ruby, parser
+
+      assert_equal 'app', parser.file_name
+    end
+  end
+
   def test_can_parse_modeline
     readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"
 
Index: test/rdoc/test_rdoc_encoding.rb
===================================================================
--- test/rdoc/test_rdoc_encoding.rb	(revision 38445)
+++ test/rdoc/test_rdoc_encoding.rb	(revision 38446)
@@ -164,7 +164,7 @@ class TestRDocEncoding < RDoc::TestCase https://github.com/ruby/ruby/blob/trunk/test/rdoc/test_rdoc_encoding.rb#L164
 
     RDoc::Encoding.set_encoding s
 
-    assert_equal "# more comments", s
+    assert_equal "#!/bin/ruby\n# more comments", s
   end
 
   def test_class_set_encoding_bad

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

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