filetype 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gemtest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Lee Jarvis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ Filetype
2
+ ========
3
+
4
+ Find a file type according to a filename or extension
5
+
6
+ Installation
7
+ ------------
8
+
9
+ ### Rubygems
10
+
11
+ gem install filetype
12
+
13
+ ### GitHub
14
+
15
+ git clone git://github.com/injekt/filetype.git
16
+ gem build filetype.gemspec
17
+ gem install filetype-<version>.gem
18
+
19
+ Usage
20
+ -----
21
+
22
+ Filetype.get('foo.rb') #=> :ruby
23
+ Filetype.all('foo.h') #=> [:c, :cpp, :objc]
24
+
25
+ Custom file types
26
+ -----------------
27
+
28
+ You can of course add your own custom file types
29
+
30
+ Filetype.add(:cool, %w[ cool kl ])
31
+ Filetype.get('hello.kl') #=> :cool
32
+
33
+ Contributing
34
+ ------------
35
+
36
+ The list of file types Filetype supports can be found
37
+ [here](https://github.com/injekt/filetype/blob/master/lib/filetype.rb#L6).
38
+ This list is of course fairly short. Please feel free to add file types!
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ task :test do
2
+ $LOAD_PATH.unshift './lib'
3
+ $LOAD_PATH.unshift './test'
4
+ require 'filetype'
5
+ require 'minitest/autorun'
6
+ begin; require 'turn'; rescue LoadError; end
7
+ Dir.glob("test/**/*_test.rb").each { |test| require "./#{test}" }
8
+ end
9
+
10
+ task :default => :test
data/filetype.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "filetype"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "filetype"
6
+ s.version = Filetype::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Lee Jarvis"]
9
+ s.email = ["lee@jarvis.co"]
10
+ s.homepage = "http://github.com/injekt/filetype"
11
+ s.summary = "Find a files type"
12
+ s.description = "Find a file type according to a filename or extension"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+ end
data/lib/filetype.rb ADDED
@@ -0,0 +1,95 @@
1
+ module Filetype
2
+ module_function
3
+
4
+ VERSION = '0.1.0'
5
+
6
+ FTYPES = {
7
+ :actionscript => %w[ as mxml ],
8
+ :ada => %w[ ada adb ads ],
9
+ :asm => %w[ asm s ],
10
+ :batch => %w[ bat cmd ],
11
+ :c => %w[ c h ],
12
+ :clojure => %w[ clj ],
13
+ :cpp => %w[ cpp cc cxx m hpp hh h hxx ],
14
+ :csharp => %w[ cs ],
15
+ :css => %w[ css ],
16
+ :elisp => %w[ el ],
17
+ :erlang => %w[ erl hrl ],
18
+ :go => %w[ go ],
19
+ :groovy => %w[ groovy gpp grunit gtmpl ],
20
+ :haskell => %w[ hs lhs ],
21
+ :haml => %w[ haml ],
22
+ :html => %w[ html xhtml shtml htm ],
23
+ :java => %w[ java ],
24
+ :js => %w[ js ],
25
+ :lisp => %w[ lisp lsp ],
26
+ :lua => %w[ lua ],
27
+ :make => /\A[Mm]akefile\z/,
28
+ :objc => %w[ m h ],
29
+ :ocaml => %w[ ml mli ],
30
+ :perl => %w[ pl pm t pod ],
31
+ :php => %w[ php phpt phtml ],
32
+ :python => %w[ py pyc ],
33
+ :rackup => %w[ ru ],
34
+ :rake => /\A[Rr]akefile(?:.rb)?\z/,
35
+ :ruby => %w[ rb rhtml erb rake rjs ],
36
+ :sass => %w[ sass ],
37
+ :scala => %w[ scala ],
38
+ :scss => %w[ scss ],
39
+ :scheme => %w[ scm ss ],
40
+ :shell => %w[ sh bash ksh zsh csh ],
41
+ :smalltalk => %w[ st ],
42
+ :sql => %w[ sql ctl ],
43
+ :yaml => %w[ yaml yml ],
44
+ :xml => %w[ xml xsl dtd xslt ],
45
+ }
46
+
47
+ # Fetch a language for this filetype
48
+ #
49
+ # @param [String] fname The file name to check
50
+ # @example
51
+ # Filetype.get('foo.rb') #=> :ruby
52
+ # Filetype.get('Rakefile') #=> :rake
53
+ # @return [Symbol] The language found or nil
54
+ def get(fname)
55
+ FTYPES.each do |ftype, rule|
56
+ case rule
57
+ when Array
58
+ ext = File.extname(fname)[1..-1]
59
+ return ftype if rule.include? ext
60
+ when Regexp
61
+ return ftype if fname.match rule
62
+ when String, Symbol
63
+ return ftype if fname == rule.to_s
64
+ end
65
+ end
66
+ nil
67
+ end
68
+
69
+ # Fetch a list of possible languages which match this filetype
70
+ #
71
+ # @param [String] fname The file name to check
72
+ # @example
73
+ # Filetype.all('foo.h') #=> [:c, :cpp, :objc]
74
+ # @return [Array] The list of languages found
75
+ def all(fname)
76
+ FTYPES.select do |ftype, rule|
77
+ ext = File.extname(fname)[1..-1]
78
+ ftype if rule.is_a?(Array) && rule.include?(ext)
79
+ end.keys
80
+ end
81
+
82
+ # Add a file type and rule
83
+ #
84
+ # @param [Symbol] ftype The file type
85
+ # @param [Object] rule The rule to match
86
+ # @example
87
+ # Filetype.add(:foo, ['foo', 'bar'])
88
+ # Filetype.get('hello.bar') #=> :foo
89
+ #
90
+ # Filetype.add(:bar, /\Ahello/)
91
+ # Filetype.get('hellofoo') #=> :bar
92
+ def add(ftype, rule)
93
+ FTYPES[ftype] = rule
94
+ end
95
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class FileTypeTest < TestCase
4
+
5
+ test 'fetching the language of a file name' do
6
+ assert_equal :ruby, Filetype.get('foo.rb')
7
+ assert_nil Filetype.get('foo.abahaha')
8
+ end
9
+
10
+ test 'fetching multiple languages of a file name' do
11
+ assert_equal [:c, :cpp, :objc], Filetype.all('foo.h')
12
+ assert_empty Filetype.all('foo.aahaha')
13
+ end
14
+
15
+ test 'adding filetypes' do
16
+ Filetype.add(:bar, %w/foo bar/)
17
+ assert_equal :bar, Filetype.get('hello.foo')
18
+ end
19
+
20
+ end
@@ -0,0 +1,14 @@
1
+ class TestCase < MiniTest::Unit::TestCase
2
+ def self.test(name, &block)
3
+ test_name = "test_#{name.gsub(/\s+/, '_')}".to_sym
4
+ defined = instance_method(test_name) rescue false
5
+ raise "#{test_name} is already defined in #{self}" if defined
6
+ if block_given?
7
+ define_method(test_name, &block)
8
+ else
9
+ define_method(test_name) do
10
+ flunk "No implementation provided for #{name}"
11
+ end
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filetype
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Lee Jarvis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-26 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Find a file type according to a filename or extension
18
+ email:
19
+ - lee@jarvis.co
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gemtest
28
+ - .gitignore
29
+ - LICENSE
30
+ - README.md
31
+ - Rakefile
32
+ - filetype.gemspec
33
+ - lib/filetype.rb
34
+ - test/filetype_test.rb
35
+ - test/test_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/injekt/filetype
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.6.2
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Find a files type
64
+ test_files:
65
+ - test/filetype_test.rb
66
+ - test/test_helper.rb