filetype 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +8 -0
  2. data/lib/filetype.rb +27 -8
  3. data/test/filetype_test.rb +93 -2
  4. metadata +2 -2
data/README.md CHANGED
@@ -30,6 +30,14 @@ You can of course add your own custom file types
30
30
  Filetype.add(:cool, %w[ cool kl ])
31
31
  Filetype.get('hello.kl') #=> :cool
32
32
 
33
+ Simple
34
+ ------
35
+
36
+ You may notice how basic Filetype is, all it's doing is checking the file name
37
+ or file extension. Well, that's its intention, it was built for nothing more.
38
+ If you want more advanced information, check out the
39
+ [mime-types](http://mime-types.rubyforge.org/) library.
40
+
33
41
  Contributing
34
42
  ------------
35
43
 
data/lib/filetype.rb CHANGED
@@ -1,38 +1,48 @@
1
1
  module Filetype
2
2
  module_function
3
3
 
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
 
6
6
  FTYPES = {
7
7
  :actionscript => %w[ as mxml ],
8
8
  :ada => %w[ ada adb ads ],
9
9
  :asm => %w[ asm s ],
10
10
  :batch => %w[ bat cmd ],
11
+ :bundler => /\A[Gg]emfile(?:\.lock)?\z/,
12
+ :bzip => %w[ bzip2 bzip ],
11
13
  :c => %w[ c h ],
12
14
  :clojure => %w[ clj ],
13
15
  :cpp => %w[ cpp cc cxx m hpp hh h hxx ],
14
16
  :csharp => %w[ cs ],
15
17
  :css => %w[ css ],
18
+ :diff => %w[ diff patch ],
16
19
  :elisp => %w[ el ],
20
+ :erb => %w[ rhtml erb ],
17
21
  :erlang => %w[ erl hrl ],
22
+ :exe => %w[ exe ],
23
+ :gem => %w[ gem ],
18
24
  :go => %w[ go ],
19
25
  :groovy => %w[ groovy gpp grunit gtmpl ],
26
+ :gzip => %w[ gzip gz ],
20
27
  :haskell => %w[ hs lhs ],
21
28
  :haml => %w[ haml ],
22
29
  :html => %w[ html xhtml shtml htm ],
23
- :java => %w[ java ],
30
+ :java => %w[ java jar ],
24
31
  :js => %w[ js ],
25
32
  :lisp => %w[ lisp lsp ],
26
33
  :lua => %w[ lua ],
27
34
  :make => /\A[Mm]akefile\z/,
28
35
  :objc => %w[ m h ],
29
36
  :ocaml => %w[ ml mli ],
37
+ :pcap => %w[ pcap ],
30
38
  :perl => %w[ pl pm t pod ],
39
+ :pgp => %w[ asc pgp gpg ],
31
40
  :php => %w[ php phpt phtml ],
32
41
  :python => %w[ py pyc ],
33
42
  :rackup => %w[ ru ],
34
43
  :rake => /\A[Rr]akefile(?:.rb)?\z/,
35
- :ruby => %w[ rb rhtml erb rake rjs ],
44
+ :rar => %w[ rar ],
45
+ :ruby => %w[ rb rake gemspec rjs ],
36
46
  :sass => %w[ sass ],
37
47
  :scala => %w[ scala ],
38
48
  :scss => %w[ scss ],
@@ -40,8 +50,12 @@ module Filetype
40
50
  :shell => %w[ sh bash ksh zsh csh ],
41
51
  :smalltalk => %w[ st ],
42
52
  :sql => %w[ sql ctl ],
53
+ :tar => %w[ tar ],
54
+ :text => %w[ txt ],
43
55
  :yaml => %w[ yaml yml ],
44
- :xml => %w[ xml xsl dtd xslt ],
56
+ :xml => %w[ xml dtd ],
57
+ :xsl => %w[ xsl xslt],
58
+ :zip => %w[ zip ]
45
59
  }
46
60
 
47
61
  # Fetch a language for this filetype
@@ -55,8 +69,7 @@ module Filetype
55
69
  FTYPES.each do |ftype, rule|
56
70
  case rule
57
71
  when Array
58
- ext = File.extname(fname)[1..-1]
59
- return ftype if rule.include? ext
72
+ return ftype if rule.include? ext(fname)
60
73
  when Regexp
61
74
  return ftype if fname.match rule
62
75
  when String, Symbol
@@ -74,8 +87,7 @@ module Filetype
74
87
  # @return [Array] The list of languages found
75
88
  def all(fname)
76
89
  FTYPES.select do |ftype, rule|
77
- ext = File.extname(fname)[1..-1]
78
- ftype if rule.is_a?(Array) && rule.include?(ext)
90
+ ftype if rule.is_a?(Array) && rule.include?(ext(fname))
79
91
  end.keys
80
92
  end
81
93
 
@@ -92,4 +104,11 @@ module Filetype
92
104
  def add(ftype, rule)
93
105
  FTYPES[ftype] = rule
94
106
  end
107
+
108
+ private
109
+
110
+ def self.ext(fname)
111
+ ext = File.extname(fname)[1..-1]
112
+ ext.downcase if ext
113
+ end
95
114
  end
@@ -2,9 +2,100 @@ require 'test_helper'
2
2
 
3
3
  class FileTypeTest < TestCase
4
4
 
5
- test 'fetching the language of a file name' do
5
+ test 'testing all languages with all extensions' do
6
+ assert_equal :bundler, Filetype.get('Gemfile')
7
+ assert_equal :bundler, Filetype.get('gemfile') # ?
8
+ assert_equal :bundler, Filetype.get('Gemfile.lock') # ?
9
+ assert_equal :bundler, Filetype.get('gemfile.lock') # ?
10
+ assert_equal :make, Filetype.get('Makefile')
11
+ assert_equal :make, Filetype.get('makefile')
12
+ assert_equal :rake, Filetype.get('Rakefile')
13
+ assert_equal :rake, Filetype.get('rakefile')
14
+ assert_equal :rake, Filetype.get('Rakefile.rb')
15
+
16
+ assert_equal :actionscript, Filetype.get('foo.as')
17
+ assert_equal :actionscript, Filetype.get('foo.AS')
18
+ assert_equal :actionscript, Filetype.get('foo.mxml')
19
+ assert_equal :ada, Filetype.get('foo.ada')
20
+ assert_equal :ada, Filetype.get('foo.adb')
21
+ assert_equal :ada, Filetype.get('foo.ads')
22
+ assert_equal :asm, Filetype.get('foo.asm')
23
+ assert_equal :asm, Filetype.get('foo.s')
24
+ assert_equal :batch, Filetype.get('foo.bat')
25
+ assert_equal :batch, Filetype.get('foo.cmd')
26
+ assert_equal :c, Filetype.get('foo.c')
27
+ assert_equal :c, Filetype.get('foo.h')
28
+ assert_equal :clojure, Filetype.get('foo.clj')
29
+ assert_equal :cpp, Filetype.get('foo.cpp')
30
+ assert_equal :cpp, Filetype.get('foo.cc')
31
+ assert_equal :cpp, Filetype.get('foo.cxx')
32
+ assert_equal :cpp, Filetype.get('foo.m')
33
+ assert_equal :cpp, Filetype.get('foo.hpp')
34
+ assert_equal :cpp, Filetype.get('foo.hh')
35
+ # assert_equal :cpp, Filetype.get('foo.h') # DUPLICATE
36
+ assert_equal :cpp, Filetype.get('foo.hxx')
37
+ assert_equal :csharp, Filetype.get('foo.cs')
38
+ assert_equal :css, Filetype.get('foo.css')
39
+ assert_equal :elisp, Filetype.get('foo.el')
40
+ assert_equal :erb, Filetype.get('foo.rhtml')
41
+ assert_equal :erb, Filetype.get('foo.erb')
42
+ assert_equal :erlang, Filetype.get('foo.erl')
43
+ assert_equal :erlang, Filetype.get('foo.hrl')
44
+ assert_equal :go, Filetype.get('foo.go')
45
+ assert_equal :groovy, Filetype.get('foo.groovy')
46
+ assert_equal :groovy, Filetype.get('foo.gpp')
47
+ assert_equal :groovy, Filetype.get('foo.grunit')
48
+ assert_equal :groovy, Filetype.get('foo.gtmpl')
49
+ assert_equal :haskell, Filetype.get('foo.hs')
50
+ assert_equal :haskell, Filetype.get('foo.lhs')
51
+ assert_equal :haml, Filetype.get('foo.haml')
52
+ assert_equal :html, Filetype.get('foo.html')
53
+ assert_equal :html, Filetype.get('foo.xhtml')
54
+ assert_equal :html, Filetype.get('foo.shtml')
55
+ assert_equal :html, Filetype.get('foo.htm')
56
+ assert_equal :java, Filetype.get('foo.java')
57
+ assert_equal :java, Filetype.get('foo.jar')
58
+ assert_equal :js, Filetype.get('foo.js')
59
+ assert_equal :lisp, Filetype.get('foo.lisp')
60
+ assert_equal :lisp, Filetype.get('foo.lsp')
61
+ assert_equal :lua, Filetype.get('foo.lua')
62
+ # assert_equal :objc, Filetype.get('foo.m') # DUPLICATE
63
+ # assert_equal :objc, Filetype.get('foo.h') # DUPLICATE
64
+ assert_equal :ocaml, Filetype.get('foo.ml')
65
+ assert_equal :ocaml, Filetype.get('foo.mli')
66
+ assert_equal :perl, Filetype.get('foo.pl')
67
+ assert_equal :perl, Filetype.get('foo.pm')
68
+ assert_equal :perl, Filetype.get('foo.t')
69
+ assert_equal :perl, Filetype.get('foo.pod')
70
+ assert_equal :php, Filetype.get('foo.php')
71
+ assert_equal :php, Filetype.get('foo.phpt')
72
+ assert_equal :php, Filetype.get('foo.phtml')
73
+ assert_equal :python, Filetype.get('foo.py')
74
+ assert_equal :python, Filetype.get('foo.pyc')
75
+ assert_equal :rackup, Filetype.get('foo.ru')
6
76
  assert_equal :ruby, Filetype.get('foo.rb')
7
- assert_nil Filetype.get('foo.abahaha')
77
+ assert_equal :ruby, Filetype.get('foo.rake')
78
+ assert_equal :ruby, Filetype.get('foo.rjs')
79
+ assert_equal :sass, Filetype.get('foo.sass')
80
+ assert_equal :scala, Filetype.get('foo.scala')
81
+ assert_equal :scss, Filetype.get('foo.scss')
82
+ assert_equal :scheme, Filetype.get('foo.scm')
83
+ assert_equal :scheme, Filetype.get('foo.ss')
84
+ assert_equal :shell, Filetype.get('foo.sh')
85
+ assert_equal :shell, Filetype.get('foo.bash')
86
+ assert_equal :shell, Filetype.get('foo.ksh')
87
+ assert_equal :shell, Filetype.get('foo.zsh')
88
+ assert_equal :shell, Filetype.get('foo.csh')
89
+ assert_equal :smalltalk, Filetype.get('foo.st')
90
+ assert_equal :sql, Filetype.get('foo.sql')
91
+ assert_equal :sql, Filetype.get('foo.ctl')
92
+ assert_equal :text, Filetype.get('foo.txt')
93
+ assert_equal :yaml, Filetype.get('foo.yaml')
94
+ assert_equal :yaml, Filetype.get('foo.yml')
95
+ assert_equal :xml, Filetype.get('foo.xml')
96
+ assert_equal :xml, Filetype.get('foo.dtd')
97
+ assert_equal :xsl, Filetype.get('foo.xslt')
98
+ assert_equal :xsl, Filetype.get('foo.xsl')
8
99
  end
9
100
 
10
101
  test 'fetching multiple languages of a file name' do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: filetype
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Lee Jarvis
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-26 00:00:00 +01:00
13
+ date: 2011-04-28 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16