rouge 2.2.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -4
  3. data/lib/rouge.rb +9 -7
  4. data/lib/rouge/cli.rb +36 -2
  5. data/lib/rouge/guessers/disambiguation.rb +88 -0
  6. data/lib/rouge/guessers/glob_mapping.rb +3 -6
  7. data/lib/rouge/guessers/modeline.rb +4 -3
  8. data/lib/rouge/guessers/source.rb +6 -16
  9. data/lib/rouge/guessers/util.rb +32 -0
  10. data/lib/rouge/lexer.rb +20 -11
  11. data/lib/rouge/lexers/apiblueprint.rb +0 -4
  12. data/lib/rouge/lexers/awk.rb +2 -2
  13. data/lib/rouge/lexers/biml.rb +2 -2
  14. data/lib/rouge/lexers/c.rb +0 -5
  15. data/lib/rouge/lexers/coffeescript.rb +2 -2
  16. data/lib/rouge/lexers/coq.rb +0 -4
  17. data/lib/rouge/lexers/diff.rb +4 -4
  18. data/lib/rouge/lexers/digdag.rb +0 -4
  19. data/lib/rouge/lexers/erb.rb +0 -4
  20. data/lib/rouge/lexers/erlang.rb +0 -4
  21. data/lib/rouge/lexers/factor.rb +2 -2
  22. data/lib/rouge/lexers/gherkin.rb +2 -2
  23. data/lib/rouge/lexers/go.rb +0 -4
  24. data/lib/rouge/lexers/groovy.rb +2 -2
  25. data/lib/rouge/lexers/haml.rb +0 -4
  26. data/lib/rouge/lexers/haskell.rb +4 -4
  27. data/lib/rouge/lexers/html.rb +3 -3
  28. data/lib/rouge/lexers/idlang.rb +0 -6
  29. data/lib/rouge/lexers/ini.rb +0 -4
  30. data/lib/rouge/lexers/io.rb +2 -2
  31. data/lib/rouge/lexers/javascript.rb +1 -1
  32. data/lib/rouge/lexers/julia.rb +2 -2
  33. data/lib/rouge/lexers/kotlin.rb +9 -14
  34. data/lib/rouge/lexers/lasso.rb +3 -6
  35. data/lib/rouge/lexers/llvm.rb +0 -4
  36. data/lib/rouge/lexers/lua.rb +2 -2
  37. data/lib/rouge/lexers/make.rb +0 -4
  38. data/lib/rouge/lexers/matlab.rb +0 -4
  39. data/lib/rouge/lexers/moonscript.rb +2 -2
  40. data/lib/rouge/lexers/mosel.rb +3 -3
  41. data/lib/rouge/lexers/nasm.rb +0 -5
  42. data/lib/rouge/lexers/objective_c.rb +0 -14
  43. data/lib/rouge/lexers/perl.rb +2 -3
  44. data/lib/rouge/lexers/php.rb +2 -4
  45. data/lib/rouge/lexers/plist.rb +0 -4
  46. data/lib/rouge/lexers/praat.rb +2 -2
  47. data/lib/rouge/lexers/prolog.rb +0 -5
  48. data/lib/rouge/lexers/properties.rb +0 -4
  49. data/lib/rouge/lexers/puppet.rb +3 -3
  50. data/lib/rouge/lexers/python.rb +2 -2
  51. data/lib/rouge/lexers/q.rb +0 -4
  52. data/lib/rouge/lexers/r.rb +2 -2
  53. data/lib/rouge/lexers/racket.rb +5 -4
  54. data/lib/rouge/lexers/ruby.rb +2 -2
  55. data/lib/rouge/lexers/rust.rb +2 -2
  56. data/lib/rouge/lexers/sed.rb +2 -2
  57. data/lib/rouge/lexers/shell.rb +2 -2
  58. data/lib/rouge/lexers/smarty.rb +0 -11
  59. data/lib/rouge/lexers/sml.rb +0 -4
  60. data/lib/rouge/lexers/tap.rb +0 -4
  61. data/lib/rouge/lexers/tcl.rb +4 -4
  62. data/lib/rouge/lexers/tex.rb +2 -2
  63. data/lib/rouge/lexers/toml.rb +0 -4
  64. data/lib/rouge/lexers/tulip.rb +2 -3
  65. data/lib/rouge/lexers/turtle.rb +5 -14
  66. data/lib/rouge/lexers/vue.rb +0 -4
  67. data/lib/rouge/lexers/wollok.rb +0 -4
  68. data/lib/rouge/lexers/xml.rb +4 -6
  69. data/lib/rouge/lexers/yaml.rb +2 -2
  70. data/lib/rouge/version.rb +1 -1
  71. data/rouge.gemspec +1 -0
  72. metadata +6 -4
@@ -55,11 +55,6 @@ module Rouge
55
55
  )
56
56
  end
57
57
 
58
- # high priority for filename matches
59
- def self.analyze_text(*)
60
- 0.3
61
- end
62
-
63
58
  def self.builtins
64
59
  @builtins ||= []
65
60
  end
@@ -11,8 +11,8 @@ module Rouge
11
11
  title "CoffeeScript"
12
12
  desc 'The Coffeescript programming language (coffeescript.org)'
13
13
 
14
- def self.analyze_text(text)
15
- return 1 if text.shebang? 'coffee'
14
+ def self.detect?(text)
15
+ return true if text.shebang? 'coffee'
16
16
  end
17
17
 
18
18
  def self.keywords
@@ -8,10 +8,6 @@ module Rouge
8
8
  tag 'coq'
9
9
  mimetypes 'text/x-coq'
10
10
 
11
- def self.analyze_text(text)
12
- return 0.3 if text.include? "Require"
13
- end
14
-
15
11
  def self.gallina
16
12
  @gallina ||= Set.new %w(
17
13
  as fun if in let match then else return end Type Set Prop
@@ -9,10 +9,10 @@ module Rouge
9
9
  filenames '*.diff', '*.patch'
10
10
  mimetypes 'text/x-diff', 'text/x-patch'
11
11
 
12
- def self.analyze_text(text)
13
- return 1 if text.start_with?('Index: ')
14
- return 1 if text.start_with?('diff ')
15
- return 0.9 if text.start_with?('--- ')
12
+ def self.detect?(text)
13
+ return true if text.start_with?('Index: ')
14
+ return true if text =~ %r(\Adiff[^\n]*?\ba/[^\n]*\bb/)
15
+ return true if text =~ /(---|[+][+][+]).*?\n(---|[+][+][+])/
16
16
  end
17
17
 
18
18
  state :root do
@@ -11,10 +11,6 @@ module Rouge
11
11
 
12
12
  mimetypes 'application/x-digdag'
13
13
 
14
- def self.analyze_text(text)
15
- # disable YAML.analyze_text
16
- end
17
-
18
14
  # http://docs.digdag.io/operators.html
19
15
  # as of digdag v0.9.10
20
16
  KEYWORD_PATTERN = Regexp.union(%w(
@@ -11,10 +11,6 @@ module Rouge
11
11
 
12
12
  filenames '*.erb', '*.erubis', '*.rhtml', '*.eruby'
13
13
 
14
- def self.analyze_text(text)
15
- return 0.4 if text =~ /<%.*%>/
16
- end
17
-
18
14
  def initialize(opts={})
19
15
  @ruby_lexer = Ruby.new(opts)
20
16
 
@@ -11,10 +11,6 @@ module Rouge
11
11
 
12
12
  mimetypes 'text/x-erlang', 'application/x-erlang'
13
13
 
14
- def self.analyze_text(text)
15
- return 0.3 if text =~ /^-module[(]\w+[)][.]/
16
- end
17
-
18
14
  keywords = %w(
19
15
  after begin case catch cond end fun if
20
16
  let of query receive try when
@@ -9,8 +9,8 @@ module Rouge
9
9
  filenames '*.factor'
10
10
  mimetypes 'text/x-factor'
11
11
 
12
- def self.analyze_text(text)
13
- return 1 if text.shebang? 'factor'
12
+ def self.detect?(text)
13
+ return true if text.shebang? 'factor'
14
14
  end
15
15
 
16
16
  def self.builtins
@@ -12,8 +12,8 @@ module Rouge
12
12
  filenames '*.feature'
13
13
  mimetypes 'text/x-gherkin'
14
14
 
15
- def self.analyze_text(text)
16
- return 1 if text.shebang? 'cucumber'
15
+ def self.detect?(text)
16
+ return true if text.shebang? 'cucumber'
17
17
  end
18
18
 
19
19
  # self-modifying method that loads the keywords file
@@ -11,10 +11,6 @@ module Rouge
11
11
 
12
12
  mimetypes 'text/x-go', 'application/x-go'
13
13
 
14
- def self.analyze_text(text)
15
- return 0
16
- end
17
-
18
14
  # Characters
19
15
 
20
16
  WHITE_SPACE = /[\s\t\r\n]+/
@@ -11,8 +11,8 @@ module Rouge
11
11
 
12
12
  ws = %r((?:\s|//.*?\n|/[*].*?[*]/)+)
13
13
 
14
- def self.analyze_text(text)
15
- return 1 if text.shebang?(/groovy/)
14
+ def self.detect?(text)
15
+ return true if text.shebang?(/groovy/)
16
16
  end
17
17
 
18
18
  def self.keywords
@@ -16,10 +16,6 @@ module Rouge
16
16
  filenames '*.haml'
17
17
  mimetypes 'text/x-haml'
18
18
 
19
- def self.analyze_text(text)
20
- return 0.1 if text.start_with? '!!!'
21
- end
22
-
23
19
  option 'filters[filter_name]', 'Mapping of lexers to use for haml :filters'
24
20
  attr_reader :filters
25
21
  # @option opts :filters
@@ -11,8 +11,8 @@ module Rouge
11
11
  filenames '*.hs'
12
12
  mimetypes 'text/x-haskell'
13
13
 
14
- def self.analyze_text(text)
15
- return 1 if text.shebang?('runhaskell')
14
+ def self.detect?(text)
15
+ return true if text.shebang?('runhaskell')
16
16
  end
17
17
 
18
18
  reserved = %w(
@@ -148,7 +148,7 @@ module Rouge
148
148
  state :character do
149
149
  rule /\\/ do
150
150
  token Str::Escape
151
- push :character_end
151
+ goto :character_end
152
152
  push :escape
153
153
  end
154
154
 
@@ -174,7 +174,7 @@ module Rouge
174
174
  rule /\^[\]\[A-Z@\^_]/, Str::Escape, :pop!
175
175
  rule /#{ascii.join('|')}/, Str::Escape, :pop!
176
176
  rule /o[0-7]+/i, Str::Escape, :pop!
177
- rule /x[\da-f]/i, Str::Escape, :pop!
177
+ rule /x[\da-f]+/i, Str::Escape, :pop!
178
178
  rule /\d+/, Str::Escape, :pop!
179
179
  rule /\s+\\/, Str::Escape, :pop!
180
180
  end
@@ -9,9 +9,9 @@ module Rouge
9
9
  filenames '*.htm', '*.html', '*.xhtml'
10
10
  mimetypes 'text/html', 'application/xhtml+xml'
11
11
 
12
- def self.analyze_text(text)
13
- return 1 if text.doctype?(/\bhtml\b/i)
14
- return 1 if text =~ /<\s*html\b/
12
+ def self.detect?(text)
13
+ return true if text.doctype?(/\bhtml\b/i)
14
+ return true if text =~ /<\s*html\b/
15
15
  end
16
16
 
17
17
  start do
@@ -10,12 +10,6 @@ module Rouge
10
10
  tag 'idlang'
11
11
  filenames '*.idl'
12
12
 
13
- def self.analyze_text(text)
14
- # Does there exist a statement that starts with 'pro' or
15
- # 'function'?
16
- return 0.4 if text =~ /^\s+(pro|function)\z/
17
- end
18
-
19
13
  name = /[_A-Z]\w*/i
20
14
  kind_param = /(\d+|#{name})/
21
15
  exponent = /[dDeE][+-]\d+/
@@ -11,10 +11,6 @@ module Rouge
11
11
  filenames '*.ini', '*.INI', '*.gitconfig'
12
12
  mimetypes 'text/x-ini'
13
13
 
14
- def self.analyze_text(text)
15
- return 0.1 if text =~ /\A\[[\w\-.]+\]\s*[\w\-]+=\w+/
16
- end
17
-
18
14
  identifier = /[\w\-.]+/
19
15
 
20
16
  state :basic do
@@ -9,8 +9,8 @@ module Rouge
9
9
  mimetypes 'text/x-iosrc'
10
10
  filenames '*.io'
11
11
 
12
- def self.analyze_text(text)
13
- return 1 if text.shebang? 'io'
12
+ def self.detect?(text)
13
+ return true if text.shebang? 'io'
14
14
  end
15
15
 
16
16
  def self.constants
@@ -18,7 +18,7 @@ module Rouge
18
18
  mimetypes 'application/javascript', 'application/x-javascript',
19
19
  'text/javascript', 'text/x-javascript'
20
20
 
21
- def self.analyze_text(text)
21
+ def self.detect?(text)
22
22
  return 1 if text.shebang?('node')
23
23
  return 1 if text.shebang?('jsc')
24
24
  # TODO: rhino, spidermonkey, etc
@@ -10,8 +10,8 @@ module Rouge
10
10
  filenames '*.jl'
11
11
  mimetypes 'text/x-julia', 'application/x-julia'
12
12
 
13
- def self.analyze_text(text)
14
- 1 if text.shebang? 'julia'
13
+ def self.detect?(text)
14
+ return true if text.shebang? 'julia'
15
15
  end
16
16
 
17
17
  BUILTINS = /\b(?:
@@ -3,8 +3,10 @@
3
3
  module Rouge
4
4
  module Lexers
5
5
  class Kotlin < RegexLexer
6
+ # https://kotlinlang.org/docs/reference/grammar.html
7
+
6
8
  title "Kotlin"
7
- desc "Kotlin <http://kotlinlang.org>"
9
+ desc "Kotlin Programming Language (http://kotlinlang.org)"
8
10
 
9
11
  tag 'kotlin'
10
12
  filenames '*.kt'
@@ -41,26 +43,23 @@ module Rouge
41
43
  rule %r'"(\\\\|\\"|[^"\n])*["\n]'m, Str
42
44
  rule %r"'\\.'|'[^\\]'", Str::Char
43
45
  rule %r"[0-9](\.[0-9]+)?([eE][+-][0-9]+)?[flFL]?|0[xX][0-9a-fA-F]+[Ll]?", Num
44
- rule %r'(companion)(\s+)(object)' do
46
+ rule %r'\b(companion)(\s+)(object)\b' do
45
47
  groups Keyword, Text, Keyword
46
48
  end
47
- rule %r'(class|data\s+class|interface|object)(\s+)' do
49
+ rule %r'\b(class|data\s+class|interface|object)(\s+)' do
48
50
  groups Keyword::Declaration, Text
49
51
  push :class
50
52
  end
51
- rule %r'(package|import)(\s+)' do
53
+ rule %r'\b(package|import)(\s+)' do
52
54
  groups Keyword, Text
53
55
  push :package
54
56
  end
55
- rule %r'(val|var)(\s+)' do
57
+ rule %r'\b(val|var)(\s+)' do
56
58
  groups Keyword::Declaration, Text
57
59
  push :property
58
60
  end
59
- rule %r'(fun)(\s+)' do
60
- groups Keyword, Text
61
- push :function
62
- end
63
- rule /(?:#{keywords.join('|')})\b/, Keyword
61
+ rule %r/\bfun\b/, Keyword
62
+ rule /\b(?:#{keywords.join('|')})\b/, Keyword
64
63
  rule id, Name
65
64
  end
66
65
 
@@ -75,10 +74,6 @@ module Rouge
75
74
  state :property do
76
75
  rule id, Name::Property, :pop!
77
76
  end
78
-
79
- state :function do
80
- rule id, Name::Function, :pop!
81
- end
82
77
  end
83
78
  end
84
79
  end
@@ -14,12 +14,9 @@ module Rouge
14
14
 
15
15
  option :start_inline, 'Whether to start inline instead of requiring <?lasso or ['
16
16
 
17
- def self.analyze_text(text)
18
- rv = 0.0
19
- rv += 1 if text.shebang?('lasso9')
20
- rv += 0.4 if text =~ /<\?(lasso(script)?|=)|no_square_brackets|noprocess/i
21
- rv += 0.2 if text =~ /define(_tag|_type|\s)/i
22
- return rv
17
+ def self.detect?(text)
18
+ return true if text.shebang?('lasso9')
19
+ return true if text =~ /\A.*?<\?(lasso(script)?|=)/
23
20
  end
24
21
 
25
22
  def initialize(*)
@@ -10,10 +10,6 @@ module Rouge
10
10
  filenames '*.ll'
11
11
  mimetypes 'text/x-llvm'
12
12
 
13
- def self.analyze_text(text)
14
- return 0.1 if text =~ /\A%\w+\s=\s/
15
- end
16
-
17
13
  string = /"[^"]*?"/
18
14
  identifier = /([-a-zA-Z$._][-a-zA-Z$._0-9]*|#{string})/
19
15
 
@@ -19,8 +19,8 @@ module Rouge
19
19
  super(opts)
20
20
  end
21
21
 
22
- def self.analyze_text(text)
23
- return 1 if text.shebang? 'lua'
22
+ def self.detect?(text)
23
+ return true if text.shebang? 'lua'
24
24
  end
25
25
 
26
26
  def self.builtins
@@ -10,10 +10,6 @@ module Rouge
10
10
  filenames '*.make', 'Makefile', 'makefile', 'Makefile.*', 'GNUmakefile'
11
11
  mimetypes 'text/x-makefile'
12
12
 
13
- def self.analyze_text(text)
14
- return 0.6 if text =~ /^\.PHONY:/
15
- end
16
-
17
13
  bsd_special = %w(
18
14
  include undef error warning if else elif endif for endfor
19
15
  )
@@ -10,10 +10,6 @@ module Rouge
10
10
  filenames '*.m'
11
11
  mimetypes 'text/x-matlab', 'application/x-matlab'
12
12
 
13
- def self.analyze_text(text)
14
- return 0.4 if text =~ /^\s*% / # % comments are a dead giveaway
15
- end
16
-
17
13
  def self.keywords
18
14
  @keywords = Set.new %w(
19
15
  break case catch classdef continue else elseif end for function
@@ -22,8 +22,8 @@ module Rouge
22
22
  @disabled_modules = list_option(:disabled_modules)
23
23
  end
24
24
 
25
- def self.analyze_text(text)
26
- return 1 if text.shebang? 'moon'
25
+ def self.detect?(text)
26
+ return true if text.shebang? 'moon'
27
27
  end
28
28
 
29
29
  def builtins
@@ -11,9 +11,9 @@ module Rouge
11
11
  filenames '*.mos'
12
12
 
13
13
  mimetypes 'text/x-mosel'
14
-
15
- def self.analyze_text(text)
16
- return 1 if text =~ /^\s*(model|package)\s+/
14
+
15
+ def self.detect?(text)
16
+ return true if text =~ /^\s*(model|package)\s+/
17
17
  end
18
18
 
19
19
  id = /[a-zA-Z_][a-zA-Z0-9_]*/
@@ -106,11 +106,6 @@ module Rouge
106
106
  )
107
107
  end
108
108
 
109
- # high priority for filename matches
110
- def self.analyze_text(*)
111
- 0.3
112
- end
113
-
114
109
  def self.builtins
115
110
  @builtins ||= []
116
111
  end
@@ -29,20 +29,6 @@ module Rouge
29
29
  @builtins ||= %w(YES NO nil)
30
30
  end
31
31
 
32
- def self.analyze_text(text)
33
- return 1 if text =~ /@(end|implementation|protocol|property)\b/
34
-
35
- id = /[a-z$_][a-z0-9$_]*/i
36
- return 0.4 if text =~ %r(
37
- \[ \s* #{id} \s+
38
- (?:
39
- #{id} \s* \]
40
- | #{id}? :
41
- )
42
- )x
43
- return 0.4 if text.include? '@"'
44
- end
45
-
46
32
  id = /[a-z$_][a-z0-9$_]*/i
47
33
 
48
34
  prepend :statements do
@@ -12,9 +12,8 @@ module Rouge
12
12
  filenames '*.pl', '*.pm'
13
13
  mimetypes 'text/x-perl', 'application/x-perl'
14
14
 
15
- def self.analyze_text(text)
16
- return 1 if text.shebang? 'perl'
17
- return 0.4 if text.include? 'my $'
15
+ def self.detect?(text)
16
+ return true if text.shebang? 'perl'
18
17
  end
19
18
 
20
19
  keywords = %w(
@@ -75,10 +75,8 @@ module Rouge
75
75
  )
76
76
  end
77
77
 
78
- def self.analyze_text(text)
79
- return 1 if text.shebang?('php')
80
- return 0.3 if /<\?(?!xml)/ =~ text
81
- 0
78
+ def self.detect?(text)
79
+ return true if text.shebang?('php')
82
80
  end
83
81
 
84
82
  state :root do