summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Youngman <git@nathany.com>2013-06-23 09:23:54 -0600
committerNathan Youngman <git@nathany.com>2013-06-23 09:23:54 -0600
commite1e6a6af97ef710cfc02a1380180b772f1c34672 (patch)
tree36ca243803e3e27d41c76ac8fd38b2d536177bcd
parent64ca2ae8ad5130bdcf652aa7aa08298de00f20f4 (diff)
downloadcoderay-e1e6a6af97ef710cfc02a1380180b772f1c34672.tar.gz
additional Ruby files types
Ruby seems to have no shortage of these specially named files.
-rw-r--r--lib/coderay/helpers/file_type.rb41
1 files changed, 22 insertions, 19 deletions
diff --git a/lib/coderay/helpers/file_type.rb b/lib/coderay/helpers/file_type.rb
index 6d4fa92..6d928d6 100644
--- a/lib/coderay/helpers/file_type.rb
+++ b/lib/coderay/helpers/file_type.rb
@@ -1,5 +1,5 @@
module CodeRay
-
+
# = FileType
#
# A simple filetype recognizer.
@@ -8,18 +8,18 @@ module CodeRay
#
# # determine the type of the given
# lang = FileType[file_name]
- #
+ #
# # return :text if the file type is unknown
# lang = FileType.fetch file_name, :text
- #
+ #
# # try the shebang line, too
# lang = FileType.fetch file_name, :text, true
module FileType
-
+
UnknownFileType = Class.new Exception
-
+
class << self
-
+
# Try to determine the file type of the file.
#
# +filename+ is a relative or absolute path to a file.
@@ -30,7 +30,7 @@ module CodeRay
name = File.basename filename
ext = File.extname(name).sub(/^\./, '') # from last dot, delete the leading dot
ext2 = filename.to_s[/\.(.*)/, 1] # from first dot
-
+
type =
TypeFromExt[ext] ||
TypeFromExt[ext.downcase] ||
@@ -39,10 +39,10 @@ module CodeRay
TypeFromName[name] ||
TypeFromName[name.downcase]
type ||= shebang(filename) if read_shebang
-
+
type
end
-
+
# This works like Hash#fetch.
#
# If the filetype cannot be found, the +default+ value
@@ -51,7 +51,7 @@ module CodeRay
if default && block_given?
warn 'Block supersedes default value argument; use either.'
end
-
+
if type = self[filename, read_shebang]
type
else
@@ -60,9 +60,9 @@ module CodeRay
raise UnknownFileType, 'Could not determine type of %p.' % filename
end
end
-
+
protected
-
+
def shebang filename
return unless File.exist? filename
File.open filename, 'r' do |f|
@@ -73,9 +73,9 @@ module CodeRay
end
end
end
-
+
end
-
+
TypeFromExt = {
'c' => :c,
'cfc' => :xml,
@@ -116,7 +116,7 @@ module CodeRay
'rhtml' => :erb,
'rjs' => :ruby,
'rpdf' => :ruby,
- 'ru' => :ruby,
+ 'ru' => :ruby, # config.ru
'rxml' => :ruby,
'sass' => :sass,
'sql' => :sql,
@@ -132,16 +132,19 @@ module CodeRay
for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
TypeFromExt[cpp_alias] = :cpp
end
-
+
TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
-
+
TypeFromName = {
'Capfile' => :ruby,
'Rakefile' => :ruby,
'Rantfile' => :ruby,
'Gemfile' => :ruby,
+ 'Guardfile' => :ruby,
+ 'Vagrantfile' => :ruby,
+ 'Appraisals' => :ruby
}
-
+
end
-
+
end