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
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.
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
- Fork it.
- Create a branch (
git checkout -b detect-foo-language
) - Make your changes
- Run the tests (
bundle install
thenbundle exec rake
) - Commit your changes (
git commit -am "Added detection for the new Foo language"
) - Push to the branch (
git push origin detect-foo-language
) - Create a Pull Request from your branch.
- Promote it. Get others to drop in and +1 it.
Original work by Github,
stripping and simplification by Michael Grosser