1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

restore extension-checking to cat

This commit is contained in:
Ryan Fitzgerald 2012-01-14 22:06:24 -08:00
parent 94dfec84a3
commit 710a60ebee
4 changed files with 52 additions and 65 deletions

View file

@ -33,17 +33,18 @@ class Pry
# @param [String] fn The name of a file, or "(pry)".
# @param [Symbol] code_type (:ruby) The type of code the file contains.
# @return [Code]
def from_file(fn, code_type=:ruby)
def from_file(fn, code_type=nil)
if fn == Pry.eval_path
f = Pry.line_buffer.drop(1)
else
if File.readable?(fn)
f = File.open(fn, 'r')
code_type = type_from_filename(fn)
else
raise CommandError, "Cannot open #{fn.inspect} for reading."
end
end
new(f, 1, code_type)
new(f, 1, code_type || :ruby)
ensure
f.close if f.respond_to?(:close)
end
@ -61,6 +62,37 @@ class Pry
start_line ||= meth.source_line || 1
new(meth.source, start_line, meth.source_type)
end
protected
# Guess the CodeRay type of a file from its extension, or nil if
# unknown.
#
# @param [String] filename
# @return [Symbol, nil]
def type_from_filename(filename)
map = {
%w(.c .h) => :c,
%w(.cpp .hpp .cc .h cxx) => :cpp,
%w(.rb .ru .irbrc .gemspec .pryrc) => :ruby,
%w(.py) => :python,
%w(.diff) => :diff,
%w(.css) => :css,
%w(.html) => :html,
%w(.yaml .yml) => :yaml,
%w(.xml) => :xml,
%w(.php) => :php,
%w(.js) => :javascript,
%w(.java) => :java,
%w(.rhtml) => :rhtml,
%w(.json) => :json
}
_, type = map.find do |k, _|
k.any? { |ext| ext == File.extname(filename) }
end
type
end
end
attr_accessor :code_type

View file

@ -8,11 +8,11 @@ class Pry
# Open a temp file and yield it to the block, closing it after
# @return [String] The path of the temp file
def temp_file
file = Tempfile.new(['pry', '.rb'])
def temp_file(ext='.rb')
file = Tempfile.new(['pry', ext])
yield file
ensure
file.close(true)
file.close(true) if file
end
def render_output(str, opts={})
@ -61,63 +61,6 @@ class Pry
header << "#{Pry::Helpers::Text.bold("Number of lines:")} #{content.each_line.count.to_s}\n"
end
def file_map
{
[".c", ".h"] => :c,
[".cpp", ".hpp", ".cc", ".h", "cxx"] => :cpp,
[".rb", "Rakefile", ".irbrc", ".gemspec", ".pryrc"] => :ruby,
".py" => :python,
".diff" => :diff,
".css" => :css,
".html" => :html,
[".yaml", ".yml"] => :yaml,
".xml" => :xml,
".php" => :php,
".js" => :javascript,
".java" => :java,
".rhtml" => :rhtml,
".json" => :json
}
end
def syntax_highlight_by_file_type_or_specified(contents, file_name, file_type)
if file_type
language_detected = file_type
else
_, language_detected = file_map.find do |k, v|
Array(k).any? do |matcher|
matcher == File.extname(file_name) || matcher == File.basename(file_name)
end
end
end
if Pry.color
CodeRay.scan(contents, language_detected).term
else
contents
end
end
# convert negative line numbers to positive by wrapping around
# last line (as per array indexing with negative numbers)
def normalized_line_number(line_number, total_lines)
line_number < 0 ? line_number + total_lines : line_number
end
# returns the file content between the lines and the normalized
# start and end line numbers.
def read_between_the_lines(file_name, start_line, end_line)
if file_name == Pry.eval_path
content = Pry.line_buffer.drop(1).join
else
content = File.read(File.expand_path(file_name))
end
lines_array = content.each_line.to_a
[lines_array[start_line..end_line].join, normalized_line_number(start_line, lines_array.size),
normalized_line_number(end_line, lines_array.size)]
end
def process_rdoc(comment, code_type)
comment = comment.dup
comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { Pry.color ? CodeRay.scan($1, code_type).term : $1 }.

View file

@ -148,11 +148,11 @@ end
# Open a temp file and yield it to the block, closing it after
# @return [String] The path of the temp file
def temp_file
file = Tempfile.new('pry')
def temp_file(ext='.rb')
file = Tempfile.new(['pry', ext])
yield file
ensure
file.close(true)
file.close(true) if file
end

View file

@ -11,6 +11,18 @@ describe Pry::Code do
Pry::Code.from_file('(pry)').grep(/:hay_guys/).length.should == 1
end
it 'should default to Ruby' do
temp_file('') do |f|
Pry::Code.from_file(f.path).code_type.should == :ruby
end
end
it 'should check the extension' do
temp_file('.c') do |f|
Pry::Code.from_file(f.path).code_type.should == :c
end
end
it 'should raise an error if the file doesn\'t exist' do
proc do
Pry::Code.from_file('/knalkjsdnalsd/alkjdlkq')