mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
afb6041104 | ||
|
|
4b28fdbc4d | ||
|
|
4f14db10ea | ||
|
|
98e348ba5f | ||
|
|
a69b20c1a4 | ||
|
|
9275e5240f | ||
|
|
7dcc3b3edf | ||
|
|
6e872c11b6 | ||
|
|
e5b6001759 | ||
|
|
769f1b8658 | ||
|
|
5814b61356 | ||
|
|
f59cf24a82 | ||
|
|
d94bffb198 | ||
|
|
2beb450df6 |
56
README.md
56
README.md
@@ -32,33 +32,57 @@ The Language stats bar that you see on every repository is built by aggregating
|
|||||||
|
|
||||||
The repository stats API, accessed through `#languages`, can be used on a directory:
|
The repository stats API, accessed through `#languages`, can be used on a directory:
|
||||||
|
|
||||||
|
***API UPDATE***
|
||||||
|
|
||||||
|
Since [Version 3.0.0](https://github.com/github/linguist/releases/tag/v3.0.0) Linguist requires a git repository (in the form of a [Rugged::Repository](https://github.com/libgit2/rugged#repositories)) to be passed when initializing `Linguist::Repository`.
|
||||||
|
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
project = Linguist::Repository.from_directory(".")
|
require 'rugged'
|
||||||
project.language.name #=> "Ruby"
|
require 'linguist'
|
||||||
project.languages #=> { "Ruby" => 0.98, "Shell" => 0.02 }
|
|
||||||
|
repo = Rugged::Repository.new('.')
|
||||||
|
project = Linguist::Repository.new(repo, repo.head.target_id)
|
||||||
|
project.language #=> "Ruby"
|
||||||
|
project.languages #=> { "Ruby" => 119387 }
|
||||||
```
|
```
|
||||||
|
|
||||||
These stats are also printed out by the `linguist` binary. You can use the
|
These stats are also printed out by the `linguist` binary. You can use the
|
||||||
`--breakdown` flag, and the binary will also output the breakdown of files by language.
|
`--breakdown` flag, and the binary will also output the breakdown of files by language.
|
||||||
|
|
||||||
You can try running `linguist` on the `lib/` directory in this repository itself:
|
You can try running `linguist` on the root directory in this repository itself:
|
||||||
|
|
||||||
$ bundle exec linguist lib/ --breakdown
|
$ bundle exec linguist --breakdown
|
||||||
|
|
||||||
100.00% Ruby
|
100.00% Ruby
|
||||||
|
|
||||||
Ruby:
|
Ruby:
|
||||||
linguist/blob_helper.rb
|
Gemfile
|
||||||
linguist/classifier.rb
|
Rakefile
|
||||||
linguist/file_blob.rb
|
bin/linguist
|
||||||
linguist/generated.rb
|
github-linguist.gemspec
|
||||||
linguist/heuristics.rb
|
lib/linguist.rb
|
||||||
linguist/language.rb
|
lib/linguist/blob_helper.rb
|
||||||
linguist/md5.rb
|
lib/linguist/classifier.rb
|
||||||
linguist/repository.rb
|
lib/linguist/file_blob.rb
|
||||||
linguist/samples.rb
|
lib/linguist/generated.rb
|
||||||
linguist/tokenizer.rb
|
lib/linguist/heuristics.rb
|
||||||
linguist.rb
|
lib/linguist/language.rb
|
||||||
|
lib/linguist/lazy_blob.rb
|
||||||
|
lib/linguist/md5.rb
|
||||||
|
lib/linguist/repository.rb
|
||||||
|
lib/linguist/samples.rb
|
||||||
|
lib/linguist/tokenizer.rb
|
||||||
|
lib/linguist/version.rb
|
||||||
|
test/test_blob.rb
|
||||||
|
test/test_classifier.rb
|
||||||
|
test/test_heuristics.rb
|
||||||
|
test/test_language.rb
|
||||||
|
test/test_md5.rb
|
||||||
|
test/test_pedantic.rb
|
||||||
|
test/test_repository.rb
|
||||||
|
test/test_samples.rb
|
||||||
|
test/test_tokenizer.rb
|
||||||
|
|
||||||
#### Ignore vendored files
|
#### Ignore vendored files
|
||||||
|
|
||||||
|
|||||||
@@ -321,6 +321,11 @@ module Linguist
|
|||||||
language ? language.lexer : Pygments::Lexer.find_by_name('Text only')
|
language ? language.lexer : Pygments::Lexer.find_by_name('Text only')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Internal: Get the TextMate compatible scope for the blob
|
||||||
|
def tm_scope
|
||||||
|
language && language.tm_scope
|
||||||
|
end
|
||||||
|
|
||||||
# Public: Highlight syntax of blob
|
# Public: Highlight syntax of blob
|
||||||
#
|
#
|
||||||
# options - A Hash of options (defaults to {})
|
# options - A Hash of options (defaults to {})
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ module Linguist
|
|||||||
if languages.all? { |l| ["ECL", "Prolog"].include?(l) }
|
if languages.all? { |l| ["ECL", "Prolog"].include?(l) }
|
||||||
result = disambiguate_ecl(data, languages)
|
result = disambiguate_ecl(data, languages)
|
||||||
end
|
end
|
||||||
|
if languages.all? { |l| ["IDL", "Prolog"].include?(l) }
|
||||||
|
result = disambiguate_pro(data, languages)
|
||||||
|
end
|
||||||
if languages.all? { |l| ["Common Lisp", "OpenCL"].include?(l) }
|
if languages.all? { |l| ["Common Lisp", "OpenCL"].include?(l) }
|
||||||
result = disambiguate_cl(data, languages)
|
result = disambiguate_cl(data, languages)
|
||||||
end
|
end
|
||||||
@@ -51,6 +54,16 @@ module Linguist
|
|||||||
matches
|
matches
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.disambiguate_pro(data, languages)
|
||||||
|
matches = []
|
||||||
|
if (data.include?(":-"))
|
||||||
|
matches << Language["Prolog"]
|
||||||
|
else
|
||||||
|
matches << Language["IDL"]
|
||||||
|
end
|
||||||
|
matches
|
||||||
|
end
|
||||||
|
|
||||||
def self.disambiguate_ts(data, languages)
|
def self.disambiguate_ts(data, languages)
|
||||||
matches = []
|
matches = []
|
||||||
if (data.include?("</translation>"))
|
if (data.include?("</translation>"))
|
||||||
|
|||||||
@@ -290,6 +290,16 @@ module Linguist
|
|||||||
@lexer = Pygments::Lexer.find_by_name(attributes[:lexer] || name) ||
|
@lexer = Pygments::Lexer.find_by_name(attributes[:lexer] || name) ||
|
||||||
raise(ArgumentError, "#{@name} is missing lexer")
|
raise(ArgumentError, "#{@name} is missing lexer")
|
||||||
|
|
||||||
|
@tm_scope = attributes[:tm_scope] || begin
|
||||||
|
context = case @type
|
||||||
|
when :data, :markup, :prose
|
||||||
|
'text'
|
||||||
|
when :programming, nil
|
||||||
|
'source'
|
||||||
|
end
|
||||||
|
"#{context}.#{@name.downcase}"
|
||||||
|
end
|
||||||
|
|
||||||
@ace_mode = attributes[:ace_mode]
|
@ace_mode = attributes[:ace_mode]
|
||||||
@wrap = attributes[:wrap] || false
|
@wrap = attributes[:wrap] || false
|
||||||
|
|
||||||
@@ -363,6 +373,11 @@ module Linguist
|
|||||||
# Returns the Lexer
|
# Returns the Lexer
|
||||||
attr_reader :lexer
|
attr_reader :lexer
|
||||||
|
|
||||||
|
# Public: Get the name of a TextMate-compatible scope
|
||||||
|
#
|
||||||
|
# Returns the scope
|
||||||
|
attr_reader :tm_scope
|
||||||
|
|
||||||
# Public: Get Ace mode
|
# Public: Get Ace mode
|
||||||
#
|
#
|
||||||
# Examples
|
# Examples
|
||||||
@@ -564,6 +579,7 @@ module Linguist
|
|||||||
:type => options['type'],
|
:type => options['type'],
|
||||||
:aliases => options['aliases'],
|
:aliases => options['aliases'],
|
||||||
:lexer => options['lexer'],
|
:lexer => options['lexer'],
|
||||||
|
:tm_scope => options['tm_scope'],
|
||||||
:ace_mode => options['ace_mode'],
|
:ace_mode => options['ace_mode'],
|
||||||
:wrap => options['wrap'],
|
:wrap => options['wrap'],
|
||||||
:group_name => options['group'],
|
:group_name => options['group'],
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ ATS:
|
|||||||
ActionScript:
|
ActionScript:
|
||||||
type: programming
|
type: programming
|
||||||
lexer: ActionScript 3
|
lexer: ActionScript 3
|
||||||
|
tm_scope: source.actionscript.3
|
||||||
color: "#e3491a"
|
color: "#e3491a"
|
||||||
search_term: as3
|
search_term: as3
|
||||||
aliases:
|
aliases:
|
||||||
@@ -282,6 +283,7 @@ C:
|
|||||||
C#:
|
C#:
|
||||||
type: programming
|
type: programming
|
||||||
ace_mode: csharp
|
ace_mode: csharp
|
||||||
|
tm_scope: source.cs
|
||||||
search_term: csharp
|
search_term: csharp
|
||||||
color: "#178600"
|
color: "#178600"
|
||||||
aliases:
|
aliases:
|
||||||
@@ -411,6 +413,7 @@ Clojure:
|
|||||||
|
|
||||||
CoffeeScript:
|
CoffeeScript:
|
||||||
type: programming
|
type: programming
|
||||||
|
tm_scope: source.coffee
|
||||||
ace_mode: coffee
|
ace_mode: coffee
|
||||||
color: "#244776"
|
color: "#244776"
|
||||||
aliases:
|
aliases:
|
||||||
@@ -453,6 +456,7 @@ ColdFusion CFC:
|
|||||||
|
|
||||||
Common Lisp:
|
Common Lisp:
|
||||||
type: programming
|
type: programming
|
||||||
|
tm_scope: source.lisp
|
||||||
color: "#3fb68b"
|
color: "#3fb68b"
|
||||||
aliases:
|
aliases:
|
||||||
- lisp
|
- lisp
|
||||||
@@ -648,6 +652,7 @@ Elm:
|
|||||||
Emacs Lisp:
|
Emacs Lisp:
|
||||||
type: programming
|
type: programming
|
||||||
lexer: Common Lisp
|
lexer: Common Lisp
|
||||||
|
tm_scope: source.lisp
|
||||||
color: "#c065db"
|
color: "#c065db"
|
||||||
aliases:
|
aliases:
|
||||||
- elisp
|
- elisp
|
||||||
@@ -932,6 +937,7 @@ Groovy Server Pages:
|
|||||||
|
|
||||||
HTML:
|
HTML:
|
||||||
type: markup
|
type: markup
|
||||||
|
tm_scope: text.html.basic
|
||||||
ace_mode: html
|
ace_mode: html
|
||||||
aliases:
|
aliases:
|
||||||
- xhtml
|
- xhtml
|
||||||
@@ -1158,6 +1164,7 @@ Java Server Pages:
|
|||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
type: programming
|
type: programming
|
||||||
|
tm_scope: source.js
|
||||||
ace_mode: javascript
|
ace_mode: javascript
|
||||||
color: "#f1e05a"
|
color: "#f1e05a"
|
||||||
aliases:
|
aliases:
|
||||||
@@ -1569,6 +1576,7 @@ ObjDump:
|
|||||||
|
|
||||||
Objective-C:
|
Objective-C:
|
||||||
type: programming
|
type: programming
|
||||||
|
tm_scope: source.objc
|
||||||
color: "#438eff"
|
color: "#438eff"
|
||||||
aliases:
|
aliases:
|
||||||
- obj-c
|
- obj-c
|
||||||
@@ -1669,6 +1677,7 @@ PAWN:
|
|||||||
|
|
||||||
PHP:
|
PHP:
|
||||||
type: programming
|
type: programming
|
||||||
|
tm_scope: text.html.php
|
||||||
ace_mode: php
|
ace_mode: php
|
||||||
color: "#4F5D95"
|
color: "#4F5D95"
|
||||||
extensions:
|
extensions:
|
||||||
@@ -1820,6 +1829,7 @@ Prolog:
|
|||||||
extensions:
|
extensions:
|
||||||
- .pl
|
- .pl
|
||||||
- .ecl
|
- .ecl
|
||||||
|
- .pro
|
||||||
- .prolog
|
- .prolog
|
||||||
|
|
||||||
Propeller Spin:
|
Propeller Spin:
|
||||||
@@ -2089,6 +2099,7 @@ SQF:
|
|||||||
|
|
||||||
SQL:
|
SQL:
|
||||||
type: data
|
type: data
|
||||||
|
tm_scope: source.sql
|
||||||
ace_mode: sql
|
ace_mode: sql
|
||||||
extensions:
|
extensions:
|
||||||
- .sql
|
- .sql
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module Linguist
|
module Linguist
|
||||||
VERSION = "3.2.0"
|
VERSION = "3.3.0"
|
||||||
end
|
end
|
||||||
|
|||||||
68
samples/Prolog/logic-problem.pro
Normal file
68
samples/Prolog/logic-problem.pro
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/**
|
||||||
|
* Question 1.1
|
||||||
|
* combiner(+Buddies, -Pairs)
|
||||||
|
*/
|
||||||
|
combiner([], []).
|
||||||
|
combiner([First|Buddies], Pairs):-
|
||||||
|
make_pairs(First, Buddies, Pairs1),
|
||||||
|
combiner(Buddies, Pairs2),
|
||||||
|
concat(Pairs1, Pairs2, Pairs).
|
||||||
|
|
||||||
|
/**
|
||||||
|
* make_pairs(+Buddy, +Buddies, -Pairs)
|
||||||
|
*/
|
||||||
|
make_pairs(Buddy, [], []).
|
||||||
|
make_pairs(Buddy, [First|Buddies], [(Buddy, First)|Pairs]):-
|
||||||
|
make_pairs(Buddy, Buddies, Pairs).
|
||||||
|
|
||||||
|
/**
|
||||||
|
* concat(+X, +Y, ?T)
|
||||||
|
*/
|
||||||
|
concat([], Y, Y).
|
||||||
|
concat([P|R], Y, [P|T]):-
|
||||||
|
concat(R, Y, T).
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Question 1.2
|
||||||
|
* extraire(+AllPossiblePairs, +NbPairs, -Tp, -RemainingPairs)
|
||||||
|
*/
|
||||||
|
extraire(AllPossiblePairs, 0, [], AllPossiblePairs).
|
||||||
|
extraire([PossiblePair|AllPossiblePairs], NbPairs, [PossiblePair|Tp], NewRemainingPairs):-
|
||||||
|
NbPairs > 0,
|
||||||
|
NewNbPairs is NbPairs - 1,
|
||||||
|
extraire(AllPossiblePairs, NewNbPairs, Tp, RemainingPairs),
|
||||||
|
not(pair_in_array(PossiblePair, Tp)),
|
||||||
|
delete_pair(RemainingPairs, PossiblePair, NewRemainingPairs).
|
||||||
|
extraire([PossiblePair|AllPossiblePairs], NbPairs, Tp, [PossiblePair|RemainingPairs]):-
|
||||||
|
NbPairs > 0,
|
||||||
|
extraire(AllPossiblePairs, NbPairs, Tp, RemainingPairs),
|
||||||
|
pair_in_array(PossiblePair, Tp).
|
||||||
|
|
||||||
|
/**
|
||||||
|
* delete_pair(+Pairs, +Pair, -PairsWithoutPair)
|
||||||
|
*/
|
||||||
|
delete_pair([], _, []).
|
||||||
|
delete_pair([Pair|Pairs], Pair, Pairs):-!.
|
||||||
|
delete_pair([FirstPair|Pairs], Pair, [FirstPair|PairsWithoutPair]):-
|
||||||
|
delete_pair(Pairs, Pair, PairsWithoutPair).
|
||||||
|
|
||||||
|
/**
|
||||||
|
* pair_in_array(+Pair, +Pairs)
|
||||||
|
*/
|
||||||
|
pair_in_array((A, B), [(C, D)|Pairs]):-
|
||||||
|
(A == C ; B == D ; A == D ; B == C),
|
||||||
|
!.
|
||||||
|
pair_in_array(Pair, [FirstPair|Pairs]):-
|
||||||
|
pair_in_array(Pair, Pairs).
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Question 1.3
|
||||||
|
* les_tps(+Buddies, -Tps)
|
||||||
|
*/
|
||||||
|
les_tps(Buddies, Tps):-
|
||||||
|
combiner(Buddies, PossiblePairs),
|
||||||
|
length(Buddies, NbBuddies),
|
||||||
|
NbPairs is integer(NbBuddies / 2),
|
||||||
|
findall(Tp, extraire(PossiblePairs, NbPairs, Tp, _), Tps).
|
||||||
@@ -65,6 +65,18 @@ class TestHeuristcs < Test::Unit::TestCase
|
|||||||
assert_equal Language["ECL"], results.first
|
assert_equal Language["ECL"], results.first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_pro_prolog_by_heuristics
|
||||||
|
languages = ["IDL", "Prolog"]
|
||||||
|
results = Heuristics.disambiguate_pro(fixture("Prolog/logic-problem.pro"), languages)
|
||||||
|
assert_equal Language["Prolog"], results.first
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_pro_idl_by_heuristics
|
||||||
|
languages = ["IDL", "Prolog"]
|
||||||
|
results = Heuristics.disambiguate_pro(fixture("IDL/mg_acosh.pro"), languages)
|
||||||
|
assert_equal Language["IDL"], results.first
|
||||||
|
end
|
||||||
|
|
||||||
def test_ts_typescript_by_heuristics
|
def test_ts_typescript_by_heuristics
|
||||||
languages = ["TypeScript", "XML"]
|
languages = ["TypeScript", "XML"]
|
||||||
results = Heuristics.disambiguate_ts(fixture("TypeScript/classes.ts"), languages)
|
results = Heuristics.disambiguate_ts(fixture("TypeScript/classes.ts"), languages)
|
||||||
|
|||||||
Reference in New Issue
Block a user