Module CodeRay::FileType
In: lib/coderay/helpers/file_type.rb

FileType

A simple filetype recognizer.

Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de>

License:LGPL / ask the author
Version:0.1 (2005-09-01)

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

Methods

[]   fetch   shebang  

Constants

UnknownFileType = Class.new Exception
TypeFromExt = { 'c' => :c, 'cfc' => :xml, 'cfm' => :xml, 'css' => :css, 'diff' => :diff, 'dpr' => :delphi, 'gemspec' => :ruby, 'groovy' => :groovy, 'gvy' => :groovy, 'h' => :c, 'htm' => :html, 'html' => :html, 'html.erb' => :rhtml, 'java' => :java, 'js' => :java_script, 'json' => :json, 'mab' => :ruby, 'pas' => :delphi, 'patch' => :diff, 'php' => :php, 'php3' => :php, 'php4' => :php, 'php5' => :php, 'py' => :python, 'py3' => :python, 'pyw' => :python, 'rake' => :ruby, 'raydebug' => :debug, 'rb' => :ruby, 'rbw' => :ruby, 'rhtml' => :rhtml, 'rjs' => :ruby, 'rpdf' => :ruby, 'rxml' => :ruby, 'sch' => :scheme, 'sql' => :sql, 'ss' => :scheme, 'xhtml' => :xhtml, 'xml' => :xml, 'yaml' => :yaml, 'yml' => :yaml, }
TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
TypeFromName = { 'Rakefile' => :ruby, 'Rantfile' => :ruby, }

Public Class methods

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.

[Source]

    # File lib/coderay/helpers/file_type.rb, line 35
35:     def [] filename, read_shebang = false
36:       name = File.basename filename
37:       ext = File.extname(name).sub(/^\./, '')  # from last dot, delete the leading dot
38:       ext2 = filename.to_s[/\.(.*)/, 1]  # from first dot
39: 
40:       type =
41:         TypeFromExt[ext] ||
42:         TypeFromExt[ext.downcase] ||
43:         (TypeFromExt[ext2] if ext2) ||
44:         (TypeFromExt[ext2.downcase] if ext2) ||
45:         TypeFromName[name] ||
46:         TypeFromName[name.downcase]
47:       type ||= shebang(filename) if read_shebang
48: 
49:       type
50:     end

This works like Hash#fetch.

If the filetype cannot be found, the default value is returned.

[Source]

    # File lib/coderay/helpers/file_type.rb, line 70
70:     def fetch filename, default = nil, read_shebang = false
71:       if default and block_given?
72:         warn 'block supersedes default value argument'
73:       end
74: 
75:       unless type = self[filename, read_shebang]
76:         return yield if block_given?
77:         return default if default
78:         raise UnknownFileType, 'Could not determine type of %p.' % filename
79:       end
80:       type
81:     end

[Source]

    # File lib/coderay/helpers/file_type.rb, line 52
52:     def shebang filename
53:       begin
54:         File.open filename, 'r' do |f|
55:           if first_line = f.gets
56:             if type = first_line[TypeFromShebang]
57:               type.to_sym
58:             end
59:           end
60:         end
61:       rescue IOError
62:         nil
63:       end
64:     end

[Validate]