summaryrefslogtreecommitdiff
path: root/lib/coderay/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'lib/coderay/helpers')
-rw-r--r--lib/coderay/helpers/filetype.rb44
-rw-r--r--lib/coderay/helpers/plugin.rb22
2 files changed, 51 insertions, 15 deletions
diff --git a/lib/coderay/helpers/filetype.rb b/lib/coderay/helpers/filetype.rb
index 8e6e16b..bc8ccf2 100644
--- a/lib/coderay/helpers/filetype.rb
+++ b/lib/coderay/helpers/filetype.rb
@@ -6,16 +6,29 @@
#
# Version: 0.1 (2005.september.1)
#
-# ==Documentation
-#
-# TODO
+# == Documentation
#
+# # determine the type of the given
+# lang = FileType[ARGV.first]
+#
+# # return :plaintext if the file type is unknown
+# lang = FileType.fetch ARGV.first, :plaintext
+#
+# # try the shebang line, too
+# lang = FileType.fetch ARGV.first, :plaintext, 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.
+ #
+ # The file itself is only accessed when +read_shebang+ is set to true.
+ # That means you can get filetypes from files that don't exist.
def [] filename, read_shebang = false
name = File.basename filename
ext = File.extname name
@@ -43,6 +56,9 @@ module FileType
end
# This works like Hash#fetch.
+ #
+ # If the filetype cannot be found, the +default+ value
+ # is returned.
def fetch filename, default = nil, read_shebang = false
if default and block_given?
warn 'block supersedes default value argument'
@@ -61,12 +77,17 @@ module FileType
TypeFromExt = {
'rb' => :ruby,
'rbw' => :ruby,
- 'cpp' => :cpp,
+ 'rake' => :ruby,
+ 'cpp' => :c,
'c' => :c,
'h' => :c,
'xml' => :xml,
'htm' => :html,
'html' => :html,
+ 'xhtml' => :xhtml,
+ 'rhtml' => :rhtml,
+ 'yaml' => :yaml,
+ 'yml' => :yaml,
}
TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
@@ -118,6 +139,7 @@ class TC_FileType < Test::Unit::TestCase
assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
+ assert_equal :ruby, FileType['./lib/tasks\repository.rake']
assert_not_equal :ruby, FileType['test_rb']
assert_not_equal :ruby, FileType['Makefile']
assert_not_equal :ruby, FileType['set.rb/set']
@@ -133,6 +155,20 @@ class TC_FileType < Test::Unit::TestCase
assert_not_equal :c, FileType['~/projects/blabla/c']
end
+ def test_html
+ assert_equal :html, FileType['test.htm']
+ assert_equal :xhtml, FileType['test.xhtml']
+ assert_equal :xhtml, FileType['test.html.xhtml']
+ assert_equal :rhtml, FileType['_form.rhtml']
+ end
+
+ def test_yaml
+ assert_equal :yaml, FileType['test.yml']
+ assert_equal :yaml, FileType['test.yaml']
+ assert_equal :yaml, FileType['my.html.yaml']
+ assert_not_equal :yaml, FileType['YAML']
+ end
+
def test_shebang
dir = './test'
if File.directory? dir
diff --git a/lib/coderay/helpers/plugin.rb b/lib/coderay/helpers/plugin.rb
index c55e565..2b47d8e 100644
--- a/lib/coderay/helpers/plugin.rb
+++ b/lib/coderay/helpers/plugin.rb
@@ -157,6 +157,17 @@ module PluginHost
@plugin_hash ||= create_plugin_hash
end
+ # Returns an array of all .rb files in the plugin path.
+ #
+ # The extension .rb is not included.
+ def all_plugin_names
+ Dir[path_to('*')].select do |file|
+ File.basename(file)[/^(?!_)\w+\.rb$/]
+ end.map do |file|
+ File.basename file, '.rb'
+ end
+ end
+
protected
# Created a new plugin list and stores it to @plugin_hash.
def create_plugin_hash
@@ -204,17 +215,6 @@ protected
end
end
- # Returns an array of all .rb files in the plugin path.
- #
- # The extension .rb is not included.
- def all_plugin_names
- Dir[path_to('*')].select do |file|
- File.basename(file)[/^(?!_)\w+\.rb$/]
- end.map do |file|
- File.basename file, '.rb'
- end
- end
-
# Returns the Plugin for +id+.
# Use it like Hash#fetch.
#