Module: Filetype

Defined in:
lib/filetype.rb

Constant Summary collapse

VERSION =
'0.3.0'
FTYPES =
{
  :actionscript => %w[ as mxml ],
  :ada          => %w[ ada adb ads ],
  :asm          => %w[ asm s ],
  :batch        => %w[ bat cmd ],
  :bundler      => /\A[Gg]emfile(?:\.lock)?\z/,
  :bzip         => %w[ bzip2 bzip ],
  :c            => %w[ c h ],
  :clojure      => %w[ clj ],
  :cpp          => %w[ cpp cc cxx m hpp hh h hxx ],
  :csharp       => %w[ cs ],
  :css          => %w[ css ],
  :diff         => %w[ diff patch ],
  :elisp        => %w[ el ],
  :erb          => %w[ rhtml erb ],
  :erlang       => %w[ erl hrl ],
  :exe          => %w[ exe ],
  :forth        => %w[ 4th ],
  :fsharp       => %w[ fs ],
  :gem          => %w[ gem ],
  :go           => %w[ go ],
  :groovy       => %w[ groovy gpp grunit gtmpl ],
  :gzip         => %w[ gzip gz ],
  :haskell      => %w[ hs lhs ],
  :haml         => %w[ haml ],
  :html         => %w[ html xhtml shtml htm ],
  :java         => %w[ java jar ],
  :js           => %w[ js ],
  :lisp         => %w[ lisp lsp ],
  :lua          => %w[ lua ],
  :make         => /\A[Mm]akefile\z/,
  :objc         => %w[ m h ],
  :ocaml        => %w[ ml mli ],
  :pcap         => %w[ pcap ],
  :perl         => %w[ pl pm t pod ],
  :pgp          => %w[ asc pgp gpg ],
  :php          => %w[ php phpt phtml ],
  :python       => %w[ py pyc ],
  :rackup       => %w[ ru ],
  :rake         => /\A[Rr]akefile(?:.rb)?\z/,
  :rar          => %w[ rar ],
  :ruby         => %w[ rb rake gemspec rjs ],
  :sass         => %w[ sass ],
  :scala        => %w[ scala ],
  :scss         => %w[ scss ],
  :scheme       => %w[ scm ss ],
  :shell        => %w[ sh bash ksh zsh csh ],
  :smalltalk    => %w[ st ],
  :sql          => %w[ sql ctl ],
  :tar          => %w[ tar ],
  :text         => %w[ txt ],
  :yaml         => %w[ yaml yml ],
  :xml          => %w[ xml dtd ],
  :xsl          => %w[ xsl xslt],
  :zip          => %w[ zip ]
}

Class Method Summary collapse

Class Method Details

.add(ftype, rule) ⇒ Object

Add a file type and rule

Examples:

Filetype.add(:foo, ['foo', 'bar'])
Filetype.get('hello.bar') #=> :foo

Filetype.add(:bar, /\Ahello/)
Filetype.get('hellofoo') #=> :bar

Parameters:

  • ftype (Symbol)

    The file type

  • rule (Object)

    The rule to match



106
107
108
# File 'lib/filetype.rb', line 106

def add(ftype, rule)
  FTYPES[ftype] = rule
end

.all(fname) ⇒ Array

Fetch a list of possible languages which match this filetype

Examples:

Filetype.all('foo.h') #=> [:c, :cpp, :objc]

Parameters:

  • fname (String)

    The file name to check

Returns:

  • (Array)

    The list of languages found



90
91
92
93
94
# File 'lib/filetype.rb', line 90

def all(fname)
  FTYPES.select do |ftype, rule|
    ftype if rule.is_a?(Array) && rule.include?(ext(fname))
  end.keys
end

.get(fname) ⇒ Symbol

Fetch a language for this filetype

Examples:

Filetype.get('foo.rb')   #=> :ruby
Filetype.get('Rakefile') #=> :rake

Parameters:

  • fname (String)

    The file name to check

Returns:

  • (Symbol)

    The language found or nil



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/filetype.rb', line 70

def get(fname)
  FTYPES.each do |ftype, rule|
    case rule
    when Array
      return ftype if rule.include? ext(fname)
    when Regexp
      return ftype if fname.match rule
    when String, Symbol
      return ftype if fname == rule.to_s
    end
  end
  nil
end