From 2707d1db67ed782a0e523679fa960d65a20c6a78 Mon Sep 17 00:00:00 2001
From: Andreas Bolka
Date: Wed, 29 Jun 2011 01:37:30 +0200
Subject: [PATCH] Guess if .r is R or REBOL
.r is the default extension used widely for both, R and REBOL. This
patch attempts to guess if .r is a REBOL file by looking for the
following very common REBOL code fragments:
- `REBOL`
- `: func [`
- `make object! [`
- `context [`
If any of those is found in a .r file, it is identified as a REBOL file.
Otherwise the language for a .r file is R.
Signed-off-by: Andreas Bolka
---
lib/linguist/blob_helper.rb | 16 ++++++++++++++++
test/fixtures/hello-r.R | 4 ++++
test/fixtures/hello-rebol.r | 5 +++++
test/test_blob.rb | 4 ++++
4 files changed, 29 insertions(+)
create mode 100644 test/fixtures/hello-r.R
create mode 100644 test/fixtures/hello-rebol.r
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