Module: CodeRay::FileType

### Home Classes Methods

### In Files

- lib/coderay/helpers/file_type.rb

### Methods

- ::[]
- ::fetch
- ::type_from_shebang

### Class/Module Index

Quicksearch

- CodeRay
- CodeRay::Duo
- CodeRay::Encoders
- CodeRay::Encoders::CommentFilter
- CodeRay::Encoders::Count
- CodeRay::Encoders::Debug
- CodeRay::Encoders::DebugLint
- CodeRay::Encoders::Div
- CodeRay::Encoders::Encoder
- CodeRay::Encoders::Filter
- CodeRay::Encoders::HTML
- CodeRay::Encoders::HTML::Output
- CodeRay::Encoders::JSON
- CodeRay::Encoders::LinesOfCode
- CodeRay::Encoders::Lint
- CodeRay::Encoders::Null
- CodeRay::Encoders::Page
- CodeRay::Encoders::Span
- CodeRay::Encoders::Statistic
- CodeRay::Encoders::Terminal
- CodeRay::Encoders::Text
- CodeRay::Encoders::TokenKindFilter
- CodeRay::Encoders::XML
- CodeRay::Encoders::YAML
- CodeRay::FileType
- CodeRay::ForRedCloth
- CodeRay::Plugin
- CodeRay::PluginHost
- CodeRay::Scanners
- CodeRay::Scanners::C
- CodeRay::Scanners::CPlusPlus
- CodeRay::Scanners::CSS
- CodeRay::Scanners::Clojure
- CodeRay::Scanners::Debug
- CodeRay::Scanners::Delphi
- CodeRay::Scanners::Diff
- CodeRay::Scanners::ERB
- CodeRay::Scanners::Go
- CodeRay::Scanners::Groovy
- CodeRay::Scanners::HAML
- CodeRay::Scanners::HTML
- CodeRay::Scanners::JSON
- CodeRay::Scanners::Java
- CodeRay::Scanners::JavaScript
- CodeRay::Scanners::Python
- CodeRay::Scanners::Raydebug
- CodeRay::Scanners::Ruby
- CodeRay::Scanners::SQL
- CodeRay::Scanners::Sass
- CodeRay::Scanners::Scanner
- CodeRay::Scanners::Taskpaper
- CodeRay::Scanners::Text
- CodeRay::Scanners::XML
- CodeRay::Scanners::YAML
- CodeRay::Styles
- CodeRay::Styles::Alpha
- CodeRay::Styles::Style
- CodeRay::Tokens
- CodeRay::TokensProxy
- CodeRay::WordList
- CodeRay::WordList::CaseIgnoring

No matching classes.

# CodeRay::FileType

# FileType

A simple filetype recognizer.

## Usage

    # determine the type of the given
    lang = FileType[file_name]

    # return :text if the file type is unknown
    lang = FileType.fetch file_name, :text

    # try the shebang line, too
    lang = FileType.fetch file_name, :text, true

### Constants

TypeFromExt
TypeFromName
TypeFromShebang
UnknownFileType

### Public Class Methods

[](filename, read_shebang = false) click to toggle source

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.

    # File lib/coderay/helpers/file_type.rb, line 29
    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 ||= type_from_shebang(filename) if read_shebang

      type
    end

fetch(filename, default = nil, read_shebang = false) click to toggle source

This works like Hash#fetch.

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

    # File lib/coderay/helpers/file_type.rb, line 50
    def fetch filename, default = nil, read_shebang = false
      if default && block_given?
        warn 'Block supersedes default value argument; use either.'
      end

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

### Protected Class Methods

type_from_shebang(filename) click to toggle source

    # File lib/coderay/helpers/file_type.rb, line 66
    def type_from_shebang filename
      return unless File.exist? filename
      File.open filename, 'r' do |f|
        if first_line = f.gets
          if type = first_line[TypeFromShebang]
            type.to_sym
          end
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.