module CodeRay::FileType
def [] filename, read_shebang = false
The file itself is only accessed when +read_shebang+ is set to true.
+filename+ is a relative or absolute path to a file.
Try to determine the file type of the file.
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
def fetch filename, default = nil, read_shebang = false
If the filetype cannot be found, the +default+ value
This works like Hash#fetch.
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
def shebang filename
def 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