From dff59f6c5df7d995d02e8173a8ce42ff9c552e88 Mon Sep 17 00:00:00 2001 From: drbrain Date: Tue, 18 Dec 2012 08:24:57 +0000 Subject: [PATCH] * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38446 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++++++++++ lib/rdoc/encoding.rb | 4 +++- lib/rdoc/parser.rb | 11 +++++++++-- test/rdoc/test_rdoc_encoding.rb | 2 +- test/rdoc/test_rdoc_parser.rb | 13 +++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 07e8a67fa6..c28469867d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Tue Dec 18 17:24:40 2012 Eric Hodel + + * 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 * doc/syntax/literals.rdoc: Used simplified heredoc example that diff --git a/lib/rdoc/encoding.rb b/lib/rdoc/encoding.rb index 0b1ec6728e..9fe3539412 100644 --- a/lib/rdoc/encoding.rb +++ b/lib/rdoc/encoding.rb @@ -75,7 +75,9 @@ module RDoc::Encoding # 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 diff --git a/lib/rdoc/parser.rb b/lib/rdoc/parser.rb index 685736fd28..506b5e7d1b 100644 --- a/lib/rdoc/parser.rb +++ b/lib/rdoc/parser.rb @@ -45,6 +45,11 @@ class RDoc::Parser 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 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 diff --git a/test/rdoc/test_rdoc_encoding.rb b/test/rdoc/test_rdoc_encoding.rb index 8b482b67f2..89277585ec 100644 --- a/test/rdoc/test_rdoc_encoding.rb +++ b/test/rdoc/test_rdoc_encoding.rb @@ -164,7 +164,7 @@ class TestRDocEncoding < RDoc::TestCase 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 diff --git a/test/rdoc/test_rdoc_parser.rb b/test/rdoc/test_rdoc_parser.rb index 1058323457..5f3b786314 100644 --- a/test/rdoc/test_rdoc_parser.rb +++ b/test/rdoc/test_rdoc_parser.rb @@ -77,6 +77,19 @@ class TestRDocParser < RDoc::TestCase 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.#{$$}"