Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 117 lines (109 sloc) 3.386 kB
759f909 initial commit
Lee Jarvis authored
1 module Filetype
ec87186 add "Filetype.all" method
Lee Jarvis authored
2 module_function
3
30998c2 bump version 0.3.0
Lee Jarvis authored
4 VERSION = '0.3.0'
bde1b68 added VERSION constant
Lee Jarvis authored
5
759f909 initial commit
Lee Jarvis authored
6 FTYPES = {
7 :actionscript => %w[ as mxml ],
8 :ada => %w[ ada adb ads ],
9 :asm => %w[ asm s ],
10 :batch => %w[ bat cmd ],
009bf18 @RyanScottLewis gemfile.lock
RyanScottLewis authored
11 :bundler => /\A[Gg]emfile(?:\.lock)?\z/,
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
12 :bzip => %w[ bzip2 bzip ],
759f909 initial commit
Lee Jarvis authored
13 :c => %w[ c h ],
14 :clojure => %w[ clj ],
15 :cpp => %w[ cpp cc cxx m hpp hh h hxx ],
16 :csharp => %w[ cs ],
17 :css => %w[ css ],
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
18 :diff => %w[ diff patch ],
759f909 initial commit
Lee Jarvis authored
19 :elisp => %w[ el ],
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
20 :erb => %w[ rhtml erb ],
759f909 initial commit
Lee Jarvis authored
21 :erlang => %w[ erl hrl ],
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
22 :exe => %w[ exe ],
0856066 @blatyo Add fourth and f# language file types.
blatyo authored
23 :forth => %w[ 4th ],
24 :fsharp => %w[ fs ],
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
25 :gem => %w[ gem ],
759f909 initial commit
Lee Jarvis authored
26 :go => %w[ go ],
27 :groovy => %w[ groovy gpp grunit gtmpl ],
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
28 :gzip => %w[ gzip gz ],
759f909 initial commit
Lee Jarvis authored
29 :haskell => %w[ hs lhs ],
30 :haml => %w[ haml ],
31 :html => %w[ html xhtml shtml htm ],
dff3ee8 added jar to java
Lee Jarvis authored
32 :java => %w[ java jar ],
759f909 initial commit
Lee Jarvis authored
33 :js => %w[ js ],
34 :lisp => %w[ lisp lsp ],
35 :lua => %w[ lua ],
36 :make => /\A[Mm]akefile\z/,
37 :objc => %w[ m h ],
38 :ocaml => %w[ ml mli ],
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
39 :pcap => %w[ pcap ],
759f909 initial commit
Lee Jarvis authored
40 :perl => %w[ pl pm t pod ],
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
41 :pgp => %w[ asc pgp gpg ],
759f909 initial commit
Lee Jarvis authored
42 :php => %w[ php phpt phtml ],
43 :python => %w[ py pyc ],
44 :rackup => %w[ ru ],
45 :rake => /\A[Rr]akefile(?:.rb)?\z/,
966d2cc this should be an array
Lee Jarvis authored
46 :rar => %w[ rar ],
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
47 :ruby => %w[ rb rake gemspec rjs ],
759f909 initial commit
Lee Jarvis authored
48 :sass => %w[ sass ],
49 :scala => %w[ scala ],
50 :scss => %w[ scss ],
51 :scheme => %w[ scm ss ],
52 :shell => %w[ sh bash ksh zsh csh ],
53 :smalltalk => %w[ st ],
54 :sql => %w[ sql ctl ],
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
55 :tar => %w[ tar ],
ff815c1 added .txt extension
Lee Jarvis authored
56 :text => %w[ txt ],
759f909 initial commit
Lee Jarvis authored
57 :yaml => %w[ yaml yml ],
1400281 @postmodern Split apart some file-types and added more file-types:
postmodern authored
58 :xml => %w[ xml dtd ],
59 :xsl => %w[ xsl xslt],
60 :zip => %w[ zip ]
759f909 initial commit
Lee Jarvis authored
61 }
62
6cfe399 added more docs for get
Lee Jarvis authored
63 # Fetch a language for this filetype
64 #
ec87186 add "Filetype.all" method
Lee Jarvis authored
65 # @param [String] fname The file name to check
6cfe399 added more docs for get
Lee Jarvis authored
66 # @example
67 # Filetype.get('foo.rb') #=> :ruby
68 # Filetype.get('Rakefile') #=> :rake
ec87186 add "Filetype.all" method
Lee Jarvis authored
69 # @return [Symbol] The language found or nil
70 def get(fname)
759f909 initial commit
Lee Jarvis authored
71 FTYPES.each do |ftype, rule|
72 case rule
73 when Array
f15d784 added ext method and downcase extension before checking
Lee Jarvis authored
74 return ftype if rule.include? ext(fname)
759f909 initial commit
Lee Jarvis authored
75 when Regexp
76 return ftype if fname.match rule
f1370a4 added String/Symbol rule support
Lee Jarvis authored
77 when String, Symbol
78 return ftype if fname == rule.to_s
759f909 initial commit
Lee Jarvis authored
79 end
80 end
b9d39ca ensure get returns nil if nothing is found + tests
Lee Jarvis authored
81 nil
759f909 initial commit
Lee Jarvis authored
82 end
ec87186 add "Filetype.all" method
Lee Jarvis authored
83
84 # Fetch a list of possible languages which match this filetype
85 #
86 # @param [String] fname The file name to check
87 # @example
037a788 no need for p in this example
Lee Jarvis authored
88 # Filetype.all('foo.h') #=> [:c, :cpp, :objc]
ec87186 add "Filetype.all" method
Lee Jarvis authored
89 # @return [Array] The list of languages found
90 def all(fname)
91 FTYPES.select do |ftype, rule|
f15d784 added ext method and downcase extension before checking
Lee Jarvis authored
92 ftype if rule.is_a?(Array) && rule.include?(ext(fname))
ec87186 add "Filetype.all" method
Lee Jarvis authored
93 end.keys
94 end
e150d84 add Filetype.add method
Lee Jarvis authored
95
96 # Add a file type and rule
97 #
98 # @param [Symbol] ftype The file type
99 # @param [Object] rule The rule to match
100 # @example
101 # Filetype.add(:foo, ['foo', 'bar'])
102 # Filetype.get('hello.bar') #=> :foo
103 #
104 # Filetype.add(:bar, /\Ahello/)
105 # Filetype.get('hellofoo') #=> :bar
106 def add(ftype, rule)
107 FTYPES[ftype] = rule
108 end
f15d784 added ext method and downcase extension before checking
Lee Jarvis authored
109
110 private
111
112 def self.ext(fname)
113 ext = File.extname(fname)[1..-1]
114 ext.downcase if ext
115 end
759f909 initial commit
Lee Jarvis authored
116 end
Something went wrong with that request. Please try again.