coderay 1.0.0 → 1.0.0.598.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. data/FOLDERS +49 -0
  2. data/Rakefile +6 -5
  3. data/bin/coderay +74 -190
  4. data/bin/coderay_stylesheet +4 -0
  5. data/{README_INDEX.rdoc → lib/README} +20 -10
  6. data/lib/coderay.rb +60 -62
  7. data/lib/coderay/duo.rb +55 -2
  8. data/lib/coderay/encoder.rb +39 -52
  9. data/lib/coderay/encoders/_map.rb +7 -11
  10. data/lib/coderay/encoders/comment_filter.rb +61 -0
  11. data/lib/coderay/encoders/count.rb +26 -11
  12. data/lib/coderay/encoders/debug.rb +60 -11
  13. data/lib/coderay/encoders/div.rb +8 -9
  14. data/lib/coderay/encoders/filter.rb +52 -12
  15. data/lib/coderay/encoders/html.rb +113 -106
  16. data/lib/coderay/encoders/html/css.rb +7 -2
  17. data/lib/coderay/encoders/html/numbering.rb +27 -24
  18. data/lib/coderay/encoders/html/output.rb +58 -15
  19. data/lib/coderay/encoders/json.rb +44 -37
  20. data/lib/coderay/encoders/lines_of_code.rb +56 -9
  21. data/lib/coderay/encoders/null.rb +13 -6
  22. data/lib/coderay/encoders/page.rb +8 -8
  23. data/lib/coderay/encoders/span.rb +9 -10
  24. data/lib/coderay/encoders/statistic.rb +114 -51
  25. data/lib/coderay/encoders/terminal.rb +10 -7
  26. data/lib/coderay/encoders/text.rb +36 -17
  27. data/lib/coderay/encoders/token_kind_filter.rb +58 -1
  28. data/lib/coderay/encoders/xml.rb +11 -13
  29. data/lib/coderay/encoders/yaml.rb +14 -16
  30. data/lib/coderay/for_redcloth.rb +1 -1
  31. data/lib/coderay/helpers/file_type.rb +240 -125
  32. data/lib/coderay/helpers/gzip_simple.rb +123 -0
  33. data/lib/coderay/helpers/plugin.rb +307 -241
  34. data/lib/coderay/helpers/word_list.rb +126 -65
  35. data/lib/coderay/scanner.rb +103 -153
  36. data/lib/coderay/scanners/_map.rb +16 -18
  37. data/lib/coderay/scanners/c.rb +13 -13
  38. data/lib/coderay/scanners/cpp.rb +6 -6
  39. data/lib/coderay/scanners/css.rb +48 -47
  40. data/lib/coderay/scanners/debug.rb +55 -9
  41. data/lib/coderay/scanners/delphi.rb +4 -4
  42. data/lib/coderay/scanners/diff.rb +25 -43
  43. data/lib/coderay/scanners/groovy.rb +2 -2
  44. data/lib/coderay/scanners/html.rb +30 -107
  45. data/lib/coderay/scanners/java.rb +5 -6
  46. data/lib/coderay/scanners/java/builtin_types.rb +0 -2
  47. data/lib/coderay/scanners/java_script.rb +6 -6
  48. data/lib/coderay/scanners/json.rb +6 -7
  49. data/lib/coderay/scanners/nitro_xhtml.rb +136 -0
  50. data/lib/coderay/scanners/php.rb +12 -13
  51. data/lib/coderay/scanners/plaintext.rb +26 -0
  52. data/lib/coderay/scanners/python.rb +4 -4
  53. data/lib/coderay/scanners/{erb.rb → rhtml.rb} +11 -19
  54. data/lib/coderay/scanners/ruby.rb +208 -219
  55. data/lib/coderay/scanners/ruby/patterns.rb +85 -18
  56. data/lib/coderay/scanners/scheme.rb +136 -0
  57. data/lib/coderay/scanners/sql.rb +22 -29
  58. data/lib/coderay/scanners/yaml.rb +10 -11
  59. data/lib/coderay/styles/_map.rb +2 -2
  60. data/lib/coderay/styles/alpha.rb +104 -102
  61. data/lib/coderay/styles/cycnus.rb +143 -0
  62. data/lib/coderay/styles/murphy.rb +123 -0
  63. data/lib/coderay/token_kinds.rb +86 -87
  64. data/lib/coderay/tokens.rb +169 -26
  65. data/test/functional/basic.rb +14 -200
  66. data/test/functional/examples.rb +14 -20
  67. data/test/functional/for_redcloth.rb +8 -15
  68. data/test/functional/load_plugin_scanner.rb +11 -0
  69. data/test/functional/suite.rb +6 -9
  70. data/test/functional/vhdl.rb +126 -0
  71. data/test/functional/word_list.rb +79 -0
  72. metadata +129 -107
  73. data/lib/coderay/helpers/gzip.rb +0 -41
  74. data/lib/coderay/scanners/clojure.rb +0 -217
  75. data/lib/coderay/scanners/haml.rb +0 -168
  76. data/lib/coderay/scanners/ruby/string_state.rb +0 -71
  77. data/lib/coderay/scanners/text.rb +0 -26
  78. data/lib/coderay/tokens_proxy.rb +0 -55
  79. data/lib/coderay/version.rb +0 -3
@@ -1,24 +1,22 @@
1
1
  module CodeRay
2
2
  module Scanners
3
-
3
+
4
4
  map \
5
- :'c++' => :cpp,
6
- :cplusplus => :cpp,
7
- :ecmascript => :java_script,
5
+ :cplusplus => :cpp,
6
+ :'c++' => :cpp,
7
+ :ecmascript => :java_script,
8
8
  :ecma_script => :java_script,
9
- :rhtml => :erb,
10
- :eruby => :erb,
11
- :irb => :ruby,
12
- :javascript => :java_script,
13
- :js => :java_script,
14
- :pascal => :delphi,
15
- :patch => :diff,
16
- :plain => :text,
17
- :plaintext => :text,
18
- :xhtml => :html,
19
- :yml => :yaml
20
-
21
- default :text
22
-
9
+ :irb => :ruby,
10
+ :javascript => :java_script,
11
+ :js => :java_script,
12
+ :nitro => :nitro_xhtml,
13
+ :pascal => :delphi,
14
+ :patch => :diff,
15
+ :plain => :plaintext,
16
+ :xhtml => :html,
17
+ :yml => :yaml
18
+
19
+ default :plain
20
+
23
21
  end
24
22
  end
@@ -7,7 +7,7 @@ module Scanners
7
7
  register_for :c
8
8
  file_extension 'c'
9
9
 
10
- KEYWORDS = [
10
+ RESERVED_WORDS = [
11
11
  'asm', 'break', 'case', 'continue', 'default', 'do',
12
12
  'else', 'enum', 'for', 'goto', 'if', 'return',
13
13
  'sizeof', 'struct', 'switch', 'typedef', 'union', 'while',
@@ -31,10 +31,10 @@ module Scanners
31
31
  ] # :nodoc:
32
32
 
33
33
  IDENT_KIND = WordList.new(:ident).
34
- add(KEYWORDS, :keyword).
35
- add(PREDEFINED_TYPES, :predefined_type).
34
+ add(RESERVED_WORDS, :reserved).
35
+ add(PREDEFINED_TYPES, :pre_type).
36
36
  add(DIRECTIVES, :directive).
37
- add(PREDEFINED_CONSTANTS, :predefined_constant) # :nodoc:
37
+ add(PREDEFINED_CONSTANTS, :pre_constant) # :nodoc:
38
38
 
39
39
  ESCAPE = / [rbfntv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x # :nodoc:
40
40
  UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x # :nodoc:
@@ -65,6 +65,10 @@ module Scanners
65
65
  elsif match = scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
66
66
  encoder.text_token match, :comment
67
67
 
68
+ elsif match = scan(/ \# \s* if \s* 0 /x)
69
+ match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
70
+ encoder.text_token match, :comment
71
+
68
72
  elsif match = scan(/ [-+*=<>?:;,!&^|()\[\]{}~%]+ | \/=? | \.(?!\d) /x)
69
73
  label_expected = match =~ /[;\{\}]/
70
74
  if case_expected
@@ -80,7 +84,7 @@ module Scanners
80
84
  match << matched
81
85
  else
82
86
  label_expected = false
83
- if kind == :keyword
87
+ if kind == :reserved
84
88
  case match
85
89
  when 'case', 'default'
86
90
  case_expected = true
@@ -89,6 +93,9 @@ module Scanners
89
93
  end
90
94
  encoder.text_token match, kind
91
95
 
96
+ elsif match = scan(/\$/)
97
+ encoder.text_token match, :ident
98
+
92
99
  elsif match = scan(/L?"/)
93
100
  encoder.begin_group :string
94
101
  if match[0] == ?L
@@ -98,10 +105,6 @@ module Scanners
98
105
  encoder.text_token match, :delimiter
99
106
  state = :string
100
107
 
101
- elsif match = scan(/ \# \s* if \s* 0 /x)
102
- match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
103
- encoder.text_token match, :comment
104
-
105
108
  elsif match = scan(/#[ \t]*(\w*)/)
106
109
  encoder.text_token match, :preprocessor
107
110
  in_preproc_line = true
@@ -112,16 +115,13 @@ module Scanners
112
115
  label_expected = false
113
116
  encoder.text_token match, :char
114
117
 
115
- elsif match = scan(/\$/)
116
- encoder.text_token match, :ident
117
-
118
118
  elsif match = scan(/0[xX][0-9A-Fa-f]+/)
119
119
  label_expected = false
120
120
  encoder.text_token match, :hex
121
121
 
122
122
  elsif match = scan(/(?:0[0-7]+)(?![89.eEfF])/)
123
123
  label_expected = false
124
- encoder.text_token match, :octal
124
+ encoder.text_token match, :oct
125
125
 
126
126
  elsif match = scan(/(?:\d+)(?![.eEfF])L?L?/)
127
127
  label_expected = false
@@ -11,7 +11,7 @@ module Scanners
11
11
  title 'C++'
12
12
 
13
13
  #-- http://www.cppreference.com/wiki/keywords/start
14
- KEYWORDS = [
14
+ RESERVED_WORDS = [
15
15
  'and', 'and_eq', 'asm', 'bitand', 'bitor', 'break',
16
16
  'case', 'catch', 'class', 'compl', 'const_cast',
17
17
  'continue', 'default', 'delete', 'do', 'dynamic_cast', 'else',
@@ -40,11 +40,11 @@ module Scanners
40
40
  ] # :nodoc:
41
41
 
42
42
  IDENT_KIND = WordList.new(:ident).
43
- add(KEYWORDS, :keyword).
44
- add(PREDEFINED_TYPES, :predefined_type).
43
+ add(RESERVED_WORDS, :reserved).
44
+ add(PREDEFINED_TYPES, :pre_type).
45
45
  add(PREDEFINED_VARIABLES, :local_variable).
46
46
  add(DIRECTIVES, :directive).
47
- add(PREDEFINED_CONSTANTS, :predefined_constant) # :nodoc:
47
+ add(PREDEFINED_CONSTANTS, :pre_constant) # :nodoc:
48
48
 
49
49
  ESCAPE = / [rbfntv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x # :nodoc:
50
50
  UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x # :nodoc:
@@ -94,7 +94,7 @@ module Scanners
94
94
  match << matched
95
95
  else
96
96
  label_expected = false
97
- if kind == :keyword
97
+ if kind == :reserved
98
98
  case match
99
99
  when 'class'
100
100
  state = :class_name_expected
@@ -133,7 +133,7 @@ module Scanners
133
133
 
134
134
  elsif match = scan(/(?:0[0-7]+)(?![89.eEfF])/)
135
135
  label_expected = false
136
- encoder.text_token match, :octal
136
+ encoder.text_token match, :oct
137
137
 
138
138
  elsif match = scan(/(?:\d+)(?![.eEfF])L?L?/)
139
139
  label_expected = false
@@ -2,46 +2,47 @@ module CodeRay
2
2
  module Scanners
3
3
 
4
4
  class CSS < Scanner
5
-
5
+
6
6
  register_for :css
7
-
7
+
8
8
  KINDS_NOT_LOC = [
9
9
  :comment,
10
10
  :class, :pseudo_class, :type,
11
11
  :constant, :directive,
12
- :key, :value, :operator, :color, :float, :string,
12
+ :key, :value, :operator, :color, :float,
13
13
  :error, :important,
14
14
  ] # :nodoc:
15
15
 
16
16
  module RE # :nodoc:
17
+ NonASCII = /[\x80-\xFF]/
17
18
  Hex = /[0-9a-fA-F]/
18
19
  Unicode = /\\#{Hex}{1,6}(?:\r\n|\s)?/ # differs from standard because it allows uppercase hex too
19
20
  Escape = /#{Unicode}|\\[^\r\n\f0-9a-fA-F]/
20
- NMChar = /[-_a-zA-Z0-9]|#{Escape}/
21
- NMStart = /[_a-zA-Z]|#{Escape}/
21
+ NMChar = /[-_a-zA-Z0-9]|#{NonASCII}|#{Escape}/
22
+ NMStart = /[_a-zA-Z]|#{NonASCII}|#{Escape}/
22
23
  NL = /\r\n|\r|\n|\f/
23
- String1 = /"(?:[^\n\r\f\\"]|\\#{NL}|#{Escape})*"?/ # TODO: buggy regexp
24
- String2 = /'(?:[^\n\r\f\\']|\\#{NL}|#{Escape})*'?/ # TODO: buggy regexp
24
+ String1 = /"(?:[^\n\r\f\\"]|\\#{NL}|#{Escape})*"?/ # FIXME: buggy regexp
25
+ String2 = /'(?:[^\n\r\f\\']|\\#{NL}|#{Escape})*'?/ # FIXME: buggy regexp
25
26
  String = /#{String1}|#{String2}/
26
-
27
+
27
28
  HexColor = /#(?:#{Hex}{6}|#{Hex}{3})/
28
29
  Color = /#{HexColor}/
29
-
30
+
30
31
  Num = /-?(?:[0-9]+|[0-9]*\.[0-9]+)/
31
32
  Name = /#{NMChar}+/
32
33
  Ident = /-?#{NMStart}#{NMChar}*/
33
34
  AtKeyword = /@#{Ident}/
34
35
  Percentage = /#{Num}%/
35
-
36
+
36
37
  reldimensions = %w[em ex px]
37
38
  absdimensions = %w[in cm mm pt pc]
38
39
  Unit = Regexp.union(*(reldimensions + absdimensions))
39
-
40
+
40
41
  Dimension = /#{Num}#{Unit}/
41
-
42
+
42
43
  Comment = %r! /\* (?: .*? \*/ | .* ) !mx
43
44
  Function = /(?:url|alpha|attr|counters?)\((?:[^)\n\r\f]|\\\))*\)?/
44
-
45
+
45
46
  Id = /##{Name}/
46
47
  Class = /\.#{Name}/
47
48
  PseudoClass = /:#{Name}/
@@ -54,36 +55,30 @@ module Scanners
54
55
 
55
56
  value_expected = nil
56
57
  states = [:initial]
57
-
58
+
58
59
  until eos?
59
-
60
+
60
61
  if match = scan(/\s+/)
61
62
  encoder.text_token match, :space
62
-
63
+
63
64
  elsif case states.last
64
65
  when :initial, :media
65
66
  if match = scan(/(?>#{RE::Ident})(?!\()|\*/ox)
66
67
  encoder.text_token match, :type
67
- next
68
68
  elsif match = scan(RE::Class)
69
69
  encoder.text_token match, :class
70
- next
71
70
  elsif match = scan(RE::Id)
72
71
  encoder.text_token match, :constant
73
- next
74
72
  elsif match = scan(RE::PseudoClass)
75
73
  encoder.text_token match, :pseudo_class
76
- next
77
74
  elsif match = scan(RE::AttributeSelector)
78
75
  # TODO: Improve highlighting inside of attribute selectors.
79
76
  encoder.text_token match[0,1], :operator
80
77
  encoder.text_token match[1..-2], :attribute_name if match.size > 2
81
78
  encoder.text_token match[-1,1], :operator if match[-1] == ?]
82
- next
83
79
  elsif match = scan(/@media/)
84
80
  encoder.text_token match, :directive
85
81
  states.push :media_before_name
86
- next
87
82
  end
88
83
 
89
84
  when :block
@@ -93,38 +88,44 @@ module Scanners
93
88
  else
94
89
  encoder.text_token match, :key
95
90
  end
96
- next
97
91
  end
98
-
92
+
99
93
  when :media_before_name
100
94
  if match = scan(RE::Ident)
101
95
  encoder.text_token match, :type
102
96
  states[-1] = :media_after_name
103
- next
104
97
  end
105
98
 
106
99
  when :media_after_name
107
100
  if match = scan(/\{/)
108
101
  encoder.text_token match, :operator
109
102
  states[-1] = :media
110
- next
111
103
  end
112
104
 
105
+ when :comment
106
+ if match = scan(/(?:[^*\s]|\*(?!\/))+/)
107
+ encoder.text_token match, :comment
108
+ elsif match = scan(/\*\//)
109
+ encoder.text_token match, :comment
110
+ states.pop
111
+ elsif match = scan(/\s+/)
112
+ encoder.text_token match, :space
113
+ end
114
+
113
115
  else
114
- #:nocov:
115
116
  raise_inspect 'Unknown state', encoder
116
- #:nocov:
117
-
117
+
118
118
  end
119
-
120
- elsif match = scan(/\/\*(?:.*?\*\/|\z)/m)
119
+
120
+ elsif match = scan(/\/\*/)
121
121
  encoder.text_token match, :comment
122
-
122
+ states.push :comment
123
+
123
124
  elsif match = scan(/\{/)
124
125
  value_expected = false
125
126
  encoder.text_token match, :operator
126
127
  states.push :block
127
-
128
+
128
129
  elsif match = scan(/\}/)
129
130
  value_expected = false
130
131
  if states.last == :block || states.last == :media
@@ -133,14 +134,14 @@ module Scanners
133
134
  else
134
135
  encoder.text_token match, :error
135
136
  end
136
-
137
+
137
138
  elsif match = scan(/#{RE::String}/o)
138
139
  encoder.begin_group :string
139
140
  encoder.text_token match[0, 1], :delimiter
140
141
  encoder.text_token match[1..-2], :content if match.size > 2
141
142
  encoder.text_token match[-1, 1], :delimiter if match.size >= 2
142
143
  encoder.end_group :string
143
-
144
+
144
145
  elsif match = scan(/#{RE::Function}/o)
145
146
  encoder.begin_group :string
146
147
  start = match[/^\w+\(/]
@@ -152,22 +153,22 @@ module Scanners
152
153
  encoder.text_token match[start.size..-1], :content
153
154
  end
154
155
  encoder.end_group :string
155
-
156
+
156
157
  elsif match = scan(/(?: #{RE::Dimension} | #{RE::Percentage} | #{RE::Num} )/ox)
157
158
  encoder.text_token match, :float
158
-
159
+
159
160
  elsif match = scan(/#{RE::Color}/o)
160
161
  encoder.text_token match, :color
161
-
162
+
162
163
  elsif match = scan(/! *important/)
163
164
  encoder.text_token match, :important
164
-
165
+
165
166
  elsif match = scan(/(?:rgb|hsl)a?\([^()\n]*\)?/)
166
167
  encoder.text_token match, :color
167
-
168
+
168
169
  elsif match = scan(RE::AtKeyword)
169
170
  encoder.text_token match, :directive
170
-
171
+
171
172
  elsif match = scan(/ [+>:;,.=()\/] /x)
172
173
  if match == ':'
173
174
  value_expected = true
@@ -175,18 +176,18 @@ module Scanners
175
176
  value_expected = false
176
177
  end
177
178
  encoder.text_token match, :operator
178
-
179
+
179
180
  else
180
181
  encoder.text_token getch, :error
181
-
182
+
182
183
  end
183
-
184
+
184
185
  end
185
-
186
+
186
187
  encoder
187
188
  end
188
-
189
+
189
190
  end
190
-
191
+
191
192
  end
192
193
  end
@@ -1,29 +1,30 @@
1
+ ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
1
2
  module CodeRay
2
3
  module Scanners
3
-
4
+
4
5
  # = Debug Scanner
5
6
  #
6
7
  # Interprets the output of the Encoders::Debug encoder.
7
8
  class Debug < Scanner
8
-
9
+
9
10
  register_for :debug
10
11
  title 'CodeRay Token Dump Import'
11
12
 
12
13
  protected
13
14
 
14
15
  def scan_tokens encoder, options
15
-
16
+
16
17
  opened_tokens = []
17
-
18
+
18
19
  until eos?
19
-
20
+
20
21
  if match = scan(/\s+/)
21
22
  encoder.text_token match, :space
22
23
 
23
24
  elsif match = scan(/ (\w+) \( ( [^\)\\]* ( \\. [^\)\\]* )* ) \)? /x)
24
25
  kind = self[1].to_sym
25
- match = self[2].gsub(/\\(.)/m, '\1')
26
- unless TokenKinds.has_key? kind
26
+ match = self[2].gsub(/\\(.)/, '\1')
27
+ unless Tokens::AbbreviationForKind.has_key? kind
27
28
  kind = :error
28
29
  match = matched
29
30
  end
@@ -58,8 +59,53 @@ module Scanners
58
59
 
59
60
  encoder
60
61
  end
61
-
62
+
62
63
  end
63
-
64
+
65
+ end
64
66
  end
67
+
68
+ if $0 == __FILE__
69
+ $VERBOSE = true
70
+ $: << File.join(File.dirname(__FILE__), '..')
71
+ eval DATA.read, nil, $0, __LINE__ + 4
72
+ end
73
+
74
+ __END__
75
+ require 'test/unit'
76
+
77
+ class DebugScannerTest < Test::Unit::TestCase
78
+
79
+ def test_creation
80
+ assert CodeRay::Scanners::Debug < CodeRay::Scanners::Scanner
81
+ debug = nil
82
+ assert_nothing_raised do
83
+ debug = CodeRay.scanner :debug
84
+ end
85
+ assert_kind_of CodeRay::Scanners::Scanner, debug
86
+ end
87
+
88
+ TEST_INPUT = <<-'DEBUG'.chomp
89
+ integer(10)operator((\\\))string<content(test)>test[
90
+
91
+
92
+ method([])]
93
+ DEBUG
94
+ TEST_OUTPUT = CodeRay::Tokens[
95
+ ['10', :integer],
96
+ ['(\\)', :operator],
97
+ [:begin_group, :string],
98
+ ['test', :content],
99
+ [:end_group, :string],
100
+ [:begin_line, :test],
101
+ ["\n\n \t \n", :space],
102
+ ["[]", :method],
103
+ [:end_line, :test],
104
+ ].flatten
105
+
106
+ def test_filtering_text_tokens
107
+ assert_equal TEST_OUTPUT, CodeRay::Scanners::Debug.new.tokenize(TEST_INPUT)
108
+ assert_equal TEST_OUTPUT, CodeRay.scan(TEST_INPUT, :debug)
109
+ end
110
+
65
111
  end