Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Detect Programming Languages by extensionor or #! or content from files and texts, no dependencies

tree: b08a17c021

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.md

LanguageSniffer

Find out what language a given file is.
This is a stripped down version of linguist

  • pure language detection, nothing else ...
  • no dependencies
  • no Github business logic

Language detection

Most languages are detected by their file extension. This is the fastest and most common situation. For script files, which are usually extensionless, we do "deep content inspection"™ and check the shebang of the file. Checking the file's contents may also be used for disambiguating languages. C, C++ and Obj-C all use .h files. Looking for common keywords, we are usually able to guess the correct language.

LanguageSniffer.detect("lib/language_sniffer.rb").language.name #=> "Ruby"

LanguageSniffer.detect("bin/language_sniffer").language.name #=> "Ruby"

You can also just pass :content and :path, no need to have a local file.

LanguageSniffer.detect("xxx/language_sniffer", :content => File.read('bin/language_sniffer)).language.name #=> "Ruby"

See lib/language_sniffer/language.rb and lib/language_sniffer/languages.yml.

Installation

To get it, clone the repo and run Bundler to install its dependencies.

git clone https://github.com/grosser/language_sniffer.git
cd language_sniffer/
bundle install

To run the tests:

bundle exec rake test

Contributing

  1. Fork it.
  2. Create a branch (git checkout -b detect-foo-language)
  3. Make your changes
  4. Run the tests (bundle install then bundle exec rake)
  5. Commit your changes (git commit -am "Added detection for the new Foo language")
  6. Push to the branch (git push origin detect-foo-language)
  7. Create a Pull Request from your branch.
  8. Promote it. Get others to drop in and +1 it.

Author

Original work by Github,
stripping and simplification by Michael Grosser

Something went wrong with that request. Please try again.