Coder Social home page Coder Social logo

linguist's Introduction

Linguist

We use this library at GitHub to detect blob languages, highlight code, ignore binary files, suppress generated files in diffs and generate language breakdown graphs.

Features

Language detection

Linguist defines the list of all languages known to GitHub in a yaml file. In order for a file to be highlighted, a language and lexer must be defined there.

Most languages are detected by their file extension. This is the fastest and most common situation.

For disambiguating between files with common extensions, we use a Bayesian classifier. For an example, this helps us tell the difference between .h files which could be either C, C++, or Obj-C.

In the actual GitHub app we deal with Grit::Blob objects. For testing, there is a simple FileBlob API.

Linguist::FileBlob.new("lib/linguist.rb").language.name #=> "Ruby"

Linguist::FileBlob.new("bin/linguist").language.name #=> "Ruby"

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

Syntax Highlighting

The actual syntax highlighting is handled by our Pygments wrapper, pygments.rb. It also provides a Lexer abstraction that determines which highlighter should be used on a file.

We typically run on a pre-release version of Pygments, pygments.rb, to get early access to new lexers. The languages.yml file is a dump of the lexers we have available on our server.

Stats

The Language Graph you see on every repository is built by aggregating the languages of all repo's blobs. The top language in the graph determines the project's primary language. Collectively, these stats make up the Top Languages page.

The repository stats API can be used on a directory:

project = Linguist::Repository.from_directory(".")
project.language.name  #=> "Ruby"
project.languages      #=> { "Ruby" => 0.98, "Shell" => 0.02 }

These stats are also printed out by the binary. Try running linguist on itself:

$ bundle exec linguist lib/
100%  Ruby

Ignore vendored files

Checking other code into your git repo is a common practice. But this often inflates your project's language stats and may even cause your project to be labeled as another language. We are able to identify some of these files and directories and exclude them.

Linguist::FileBlob.new("vendor/plugins/foo.rb").vendored? # => true

See Linguist::BlobHelper#vendored? and lib/linguist/vendor.yml.

Generated file detection

Not all plain text files are true source files. Generated files like minified js and compiled CoffeeScript can be detected and excluded from language stats. As an extra bonus, these files are suppressed in diffs.

Linguist::FileBlob.new("underscore.min.js").generated? # => true

See Linguist::Generated#generated?.

Installation

github.com is usually running the latest version of the github-linguist gem that is released on RubyGems.org.

But for development you are going to want to checkout out the source. To get it, clone the repo and run Bundler to install its dependencies.

git clone https://github.com/github/linguist.git
cd linguist/
bundle install

To run the tests:

bundle exec rake test

Contributing

The majority of patches won't need to touch any Ruby code at all. The master language list is just a configuration file.

We try to only add languages once they have some usage on GitHub, so please note in-the-wild usage examples in your pull request.

Almost all bug fixes or new language additions should come with some additional code samples. Just drop them under samples/ in the correct subdirectory and our test suite will automatically test them. In most cases you shouldn't need to add any new assertions.

Testing

Sometimes getting the tests running can be too much work, especially if you don't have much Ruby experience. It's okay, be lazy and let our build bot Travis run the tests for you. Just open a pull request and the bot will start cranking away.

Here's our current build status, which is hopefully green: Build Status

linguist's People

Contributors

abevoelker avatar anaisbetts avatar bfontaine avatar brianmario avatar calavera avatar chochos avatar dveeden avatar haileys avatar horndude77 avatar jaxzin avatar josegonzalez avatar josh avatar kevinsawicki avatar leafo avatar lparenteau avatar matkoniecz avatar mislav avatar rdeltour avatar reas avatar rtomayko avatar stevepiercy avatar stuarthalloway avatar tclem avatar tmm1 avatar tnm avatar tucnak avatar uberi avatar upsuper avatar vmg avatar ziaw avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.