diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index 844ac03a..97b6fa06 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -259,6 +259,9 @@ module Linguist # If its a header file (.h) try to guess the language header_language || + # If it's a .r file, try to guess the language + r_language || + # See if there is a Language for the extension pathname.language || @@ -288,6 +291,19 @@ module Linguist end end + # Internal: Guess language of .r files. + # + # Returns a Language. + def r_language + return unless extname == '.r' + + if lines.grep(/(rebol|(:\s+func|make\s+object!|^\s*context)\s*\[)/i).any? + Language['Rebol'] + else + Language['R'] + end + end + # Internal: Extract the script name from the shebang line # # Requires Blob#data diff --git a/test/fixtures/hello-r.R b/test/fixtures/hello-r.R new file mode 100644 index 00000000..30d715c3 --- /dev/null +++ b/test/fixtures/hello-r.R @@ -0,0 +1,4 @@ +hello <- function() { + print("hello, world!") +} +hello() diff --git a/test/fixtures/hello-rebol.r b/test/fixtures/hello-rebol.r new file mode 100644 index 00000000..4c1b7983 --- /dev/null +++ b/test/fixtures/hello-rebol.r @@ -0,0 +1,5 @@ +REBOL [] +hello: func [] [ + print "hello, world!" +] +hello diff --git a/test/test_blob.rb b/test/test_blob.rb index 62844590..bcc63a98 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -224,6 +224,10 @@ class TestBlob < Test::Unit::TestCase assert_equal Language['Ruby'], blob("wrong_shebang.rb").language assert_nil blob("octocat.png").language + # .r disambiguation + assert_equal Language['R'], blob("hello-r.R").language + assert_equal Language['Rebol'], blob("hello-rebol.r").language + # ML assert_equal Language['OCaml'], blob("Foo.ml").language assert_equal Language['Standard ML'], blob("Foo.sig").language