README.md

Linguist

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 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.

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

Syntax Highlighting

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

We typically run on a prerelease version of Pygments to get early access to new lexers. The lexers.yml file is a dump of the lexers we have available on our server. If there is a new lexer in pygments-main not on the list, open an issue and we'll try to upgrade it soon.

MIME type detection

MIME types are used to set the Content-Type of raw binary blobs which are served from a special raw.github.com domain. However, all text blobs are served as text/plain regardless of their type to ensure they open in the browser rather than downloading.

The MIME type also determines whether a blob is binary or plain text. So if you're seeing a blob that says "View Raw" and it is actually plain text, the mime type and encoding probably needs to be explicitly stated.

Stats

The Language Graph 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.

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.

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.