mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Merge RDoc 6.0.3 from upstream.
It fixed the several bugs that was found after RDoc 6 releasing. From: SHIBATA Hiroshi <hsbt@ruby-lang.org> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62924 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ee83dc3fe4
commit
98c7058bf7
94 changed files with 983 additions and 466 deletions
|
@ -7,6 +7,18 @@
|
|||
|
||||
module RDoc::Encoding
|
||||
|
||||
HEADER_REGEXP = /^
|
||||
(?:
|
||||
\A\#!.*\n
|
||||
|
|
||||
^\#\s+frozen[-_]string[-_]literal[=:].+\n
|
||||
|
|
||||
^\#[^\n]+\b(?:en)?coding[=:]\s*(?<name>[^\s;]+).*\n
|
||||
|
|
||||
<\?xml[^?]*encoding=(?<quote>["'])(?<name>.*?)\k<quote>.*\n
|
||||
)+
|
||||
/xi # :nodoc:
|
||||
|
||||
##
|
||||
# Reads the contents of +filename+ and handles any encoding directives in
|
||||
# the file.
|
||||
|
@ -18,12 +30,13 @@ module RDoc::Encoding
|
|||
# unknown character in the target encoding will be replaced with '?'
|
||||
|
||||
def self.read_file filename, encoding, force_transcode = false
|
||||
content = open filename, "rb" do |f| f.read end
|
||||
content = File.open filename, "rb" do |f| f.read end
|
||||
content.gsub!("\r\n", "\n") if RUBY_PLATFORM =~ /mswin|mingw/
|
||||
|
||||
utf8 = content.sub!(/\A\xef\xbb\xbf/, '')
|
||||
|
||||
content = RDoc::Encoding.set_encoding content
|
||||
enc = RDoc::Encoding.detect_encoding content
|
||||
content = RDoc::Encoding.change_encoding content, enc if enc
|
||||
|
||||
begin
|
||||
encoding ||= Encoding.default_external
|
||||
|
@ -85,29 +98,22 @@ module RDoc::Encoding
|
|||
end
|
||||
|
||||
##
|
||||
# Sets the encoding of +string+ based on the magic comment
|
||||
# Detects the encoding of +string+ based on the magic comment
|
||||
|
||||
def self.set_encoding string
|
||||
string = remove_frozen_string_literal string
|
||||
def self.detect_encoding string
|
||||
result = HEADER_REGEXP.match string
|
||||
name = result && result[:name]
|
||||
|
||||
string =~ /\A(?:#!.*\n)?(.*\n)/
|
||||
name ? Encoding.find(name) : nil
|
||||
end
|
||||
|
||||
first_line = $1
|
||||
##
|
||||
# Removes magic comments and shebang
|
||||
|
||||
name = case first_line
|
||||
when /^<\?xml[^?]*encoding=(["'])(.*?)\1/ then $2
|
||||
when /\b(?:en)?coding[=:]\s*([^\s;]+)/i then $1
|
||||
else return string
|
||||
end
|
||||
|
||||
string = string.sub first_line, ''
|
||||
|
||||
string = remove_frozen_string_literal string
|
||||
|
||||
enc = Encoding.find name
|
||||
string = RDoc::Encoding.change_encoding string, enc if enc
|
||||
|
||||
string
|
||||
def self.remove_magic_comment string
|
||||
string.sub HEADER_REGEXP do |s|
|
||||
s.gsub(/[^\n]/, '')
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue