Module: CodeRay::FileType

Defined in:
vendor/plugins/coderay-0.9.2/lib/coderay/helpers/file_type.rb

Overview

FileType

A simple filetype recognizer.

Copyright © 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

Constant Summary

UnknownFileType =
Class.new Exception
TypeFromExt =
{
  'c' => :c,
  'css' => :css,
  'diff' => :diff,
  'dpr' => :delphi,
  '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,
  '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,
}

Class Method Summary

Class Method Details

+ (Object) [](filename, read_shebang = false)

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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/helpers/file_type.rb', line 35

def [] filename, read_shebang = false
  name = File.basename filename
  ext = File.extname(name).sub(/^\./, '')  # from last dot, delete the leading dot
  ext2 = filename.to_s[/\.(.*)/, 1]  # from first dot

  type =
    TypeFromExt[ext] ||
    TypeFromExt[ext.downcase] ||
    (TypeFromExt[ext2] if ext2) ||
    (TypeFromExt[ext2.downcase] if ext2) ||
    TypeFromName[name] ||
    TypeFromName[name.downcase]
  type ||= shebang(filename) if read_shebang

  type
end

+ (Object) fetch(filename, default = nil, read_shebang = false)

This works like Hash#fetch.

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



70
71
72
73
74
75
76
77
78
79
80
81
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/helpers/file_type.rb', line 70

def fetch filename, default = nil, read_shebang = false
  if default and block_given?
    warn 'block supersedes default value argument'
  end

  unless type = self[filename, read_shebang]
    return yield if block_given?
    return default if default
    raise UnknownFileType, 'Could not determine type of %p.' % filename
  end
  type
end

+ (Object) shebang(filename)



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'vendor/plugins/coderay-0.9.2/lib/coderay/helpers/file_type.rb', line 52

def shebang filename
  begin
    File.open filename, 'r' do |f|
      if first_line = f.gets
        if type = first_line[TypeFromShebang]
          type.to_sym
        end
      end
    end
  rescue IOError
    nil
  end
end