coderay 0.8.357 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/lib/README +4 -3
  2. data/lib/coderay.rb +2 -1
  3. data/lib/coderay/encoder.rb +41 -15
  4. data/lib/coderay/encoders/_map.rb +3 -1
  5. data/lib/coderay/encoders/comment_filter.rb +43 -0
  6. data/lib/coderay/encoders/div.rb +2 -3
  7. data/lib/coderay/encoders/filter.rb +75 -0
  8. data/lib/coderay/encoders/html.rb +20 -3
  9. data/lib/coderay/encoders/html/css.rb +1 -1
  10. data/lib/coderay/encoders/html/numerization.rb +11 -2
  11. data/lib/coderay/encoders/html/output.rb +10 -1
  12. data/lib/coderay/encoders/json.rb +69 -0
  13. data/lib/coderay/encoders/lines_of_code.rb +90 -0
  14. data/lib/coderay/encoders/page.rb +1 -2
  15. data/lib/coderay/encoders/span.rb +2 -3
  16. data/lib/coderay/encoders/term.rb +137 -0
  17. data/lib/coderay/encoders/text.rb +4 -4
  18. data/lib/coderay/encoders/token_class_filter.rb +84 -0
  19. data/lib/coderay/encoders/xml.rb +1 -0
  20. data/lib/coderay/for_redcloth.rb +9 -4
  21. data/lib/coderay/helpers/file_type.rb +54 -15
  22. data/lib/coderay/helpers/plugin.rb +21 -3
  23. data/lib/coderay/helpers/word_list.rb +19 -4
  24. data/lib/coderay/scanner.rb +33 -2
  25. data/lib/coderay/scanners/_map.rb +10 -4
  26. data/lib/coderay/scanners/c.rb +61 -23
  27. data/lib/coderay/scanners/cpp.rb +228 -0
  28. data/lib/coderay/scanners/css.rb +9 -1
  29. data/lib/coderay/scanners/debug.rb +1 -0
  30. data/lib/coderay/scanners/delphi.rb +2 -2
  31. data/lib/coderay/scanners/diff.rb +1 -0
  32. data/lib/coderay/scanners/groovy.rb +263 -0
  33. data/lib/coderay/scanners/html.rb +9 -2
  34. data/lib/coderay/scanners/java.rb +18 -14
  35. data/lib/coderay/scanners/java_script.rb +42 -13
  36. data/lib/coderay/scanners/json.rb +7 -1
  37. data/lib/coderay/scanners/nitro_xhtml.rb +4 -0
  38. data/lib/coderay/scanners/php.rb +526 -0
  39. data/lib/coderay/scanners/plaintext.rb +4 -1
  40. data/lib/coderay/scanners/python.rb +285 -0
  41. data/lib/coderay/scanners/rhtml.rb +3 -0
  42. data/lib/coderay/scanners/ruby.rb +29 -11
  43. data/lib/coderay/scanners/ruby/patterns.rb +26 -20
  44. data/lib/coderay/scanners/scheme.rb +3 -0
  45. data/lib/coderay/scanners/sql.rb +162 -0
  46. data/lib/coderay/scanners/xml.rb +1 -1
  47. data/lib/coderay/scanners/yaml.rb +4 -1
  48. data/lib/coderay/styles/cycnus.rb +11 -7
  49. data/lib/coderay/token_classes.rb +4 -1
  50. data/lib/coderay/tokens.rb +50 -46
  51. metadata +14 -4
  52. data/lib/coderay/encoders/tokens.rb +0 -44
@@ -5,6 +5,9 @@ module CodeRay
5
5
  # Thanks to murphy for putting CodeRay into public.
6
6
  class Scheme < Scanner
7
7
 
8
+ # TODO: function defs
9
+ # TODO: built-in functions
10
+
8
11
  register_for :scheme
9
12
  file_extension 'scm'
10
13
 
@@ -0,0 +1,162 @@
1
+ module CodeRay module Scanners
2
+
3
+ # by Josh Goebel
4
+ class SQL < Scanner
5
+
6
+ register_for :sql
7
+
8
+ RESERVED_WORDS = %w(
9
+ create database table index trigger drop primary key set select
10
+ insert update delete replace into
11
+ on from values before and or if exists case when
12
+ then else as group order by avg where
13
+ join inner outer union engine not
14
+ like end using collate show columns begin
15
+ )
16
+
17
+ PREDEFINED_TYPES = %w(
18
+ char varchar enum binary text tinytext mediumtext
19
+ longtext blob tinyblob mediumblob longblob timestamp
20
+ date time datetime year double decimal float int
21
+ integer tinyint mediumint bigint smallint unsigned bit
22
+ bool boolean hex bin oct
23
+ )
24
+
25
+ PREDEFINED_FUNCTIONS = %w( sum cast abs pi count min max avg )
26
+
27
+ DIRECTIVES = %w( auto_increment unique default charset )
28
+
29
+ PREDEFINED_CONSTANTS = %w( null true false )
30
+
31
+ IDENT_KIND = CaseIgnoringWordList.new(:ident).
32
+ add(RESERVED_WORDS, :reserved).
33
+ add(PREDEFINED_TYPES, :pre_type).
34
+ add(PREDEFINED_CONSTANTS, :pre_constant).
35
+ add(PREDEFINED_FUNCTIONS, :predefined).
36
+ add(DIRECTIVES, :directive)
37
+
38
+ ESCAPE = / [rbfntv\n\\\/'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} | . /mx
39
+ UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
40
+
41
+ STRING_PREFIXES = /[xnb]|_\w+/i
42
+
43
+ def scan_tokens tokens, options
44
+
45
+ state = :initial
46
+ string_type = nil
47
+ string_content = ''
48
+
49
+ until eos?
50
+
51
+ kind = nil
52
+ match = nil
53
+
54
+ if state == :initial
55
+
56
+ if scan(/ \s+ | \\\n /x)
57
+ kind = :space
58
+
59
+ elsif scan(/^(?:--\s?|#).*/)
60
+ kind = :comment
61
+
62
+ elsif scan(%r! /\* (?: .*? \*/ | .* ) !mx)
63
+ kind = :comment
64
+
65
+ elsif scan(/ [-+*\/=<>;,!&^|()\[\]{}~%] | \.(?!\d) /x)
66
+ kind = :operator
67
+
68
+ elsif scan(/(#{STRING_PREFIXES})?([`"'])/o)
69
+ prefix = self[1]
70
+ string_type = self[2]
71
+ tokens << [:open, :string]
72
+ tokens << [prefix, :modifier] if prefix
73
+ match = string_type
74
+ state = :string
75
+ kind = :delimiter
76
+
77
+ elsif match = scan(/ @? [A-Za-z_][A-Za-z_0-9]* /x)
78
+ kind = match[0] == ?@ ? :variable : IDENT_KIND[match.downcase]
79
+
80
+ elsif scan(/0[xX][0-9A-Fa-f]+/)
81
+ kind = :hex
82
+
83
+ elsif scan(/0[0-7]+(?![89.eEfF])/)
84
+ kind = :oct
85
+
86
+ elsif scan(/(?>\d+)(?![.eEfF])/)
87
+ kind = :integer
88
+
89
+ elsif scan(/\d[fF]|\d*\.\d+(?:[eE][+-]?\d+)?|\d+[eE][+-]?\d+/)
90
+ kind = :float
91
+
92
+ else
93
+ getch
94
+ kind = :error
95
+
96
+ end
97
+
98
+ elsif state == :string
99
+ if match = scan(/[^\\"'`]+/)
100
+ string_content << match
101
+ next
102
+ elsif match = scan(/["'`]/)
103
+ if string_type == match
104
+ if peek(1) == string_type # doubling means escape
105
+ string_content << string_type << getch
106
+ next
107
+ end
108
+ unless string_content.empty?
109
+ tokens << [string_content, :content]
110
+ string_content = ''
111
+ end
112
+ tokens << [matched, :delimiter]
113
+ tokens << [:close, :string]
114
+ state = :initial
115
+ string_type = nil
116
+ next
117
+ else
118
+ string_content << match
119
+ end
120
+ next
121
+ elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
122
+ unless string_content.empty?
123
+ tokens << [string_content, :content]
124
+ string_content = ''
125
+ end
126
+ kind = :char
127
+ elsif match = scan(/ \\ . /mox)
128
+ string_content << match
129
+ next
130
+ elsif scan(/ \\ | $ /x)
131
+ unless string_content.empty?
132
+ tokens << [string_content, :content]
133
+ string_content = ''
134
+ end
135
+ kind = :error
136
+ state = :initial
137
+ else
138
+ raise "else case \" reached; %p not handled." % peek(1), tokens
139
+ end
140
+
141
+ else
142
+ raise 'else-case reached', tokens
143
+
144
+ end
145
+
146
+ match ||= matched
147
+ unless kind
148
+ raise_inspect 'Error token %p in line %d' %
149
+ [[match, kind], line], tokens, state
150
+ end
151
+ raise_inspect 'Empty token', tokens unless match
152
+
153
+ tokens << [match, kind]
154
+
155
+ end
156
+ tokens
157
+
158
+ end
159
+
160
+ end
161
+
162
+ end end
@@ -10,7 +10,7 @@ module Scanners
10
10
 
11
11
  register_for :xml
12
12
  file_extension 'xml'
13
-
13
+
14
14
  end
15
15
 
16
16
  end
@@ -9,6 +9,8 @@ module Scanners
9
9
  register_for :yaml
10
10
  file_extension 'yml'
11
11
 
12
+ KINDS_NOT_LOC = :all
13
+
12
14
  def scan_tokens tokens, options
13
15
 
14
16
  value_expected = nil
@@ -60,7 +62,8 @@ module Scanners
60
62
  when match = scan(/[|>][-+]?/)
61
63
  tokens << [:open, :string]
62
64
  tokens << [match, :delimiter]
63
- tokens << [matched, :content] if scan(/(?:\n+ {#{key_indent + 1}}.*)+/)
65
+ string_indent = key_indent || column(pos - match.size - 1)
66
+ tokens << [matched, :content] if scan(/(?:\n+ {#{string_indent + 1}}.*)+/)
64
67
  tokens << [:close, :string]
65
68
  next
66
69
  when match = scan(/(?![!"*&]).+?(?=$|\s+#)/)
@@ -32,6 +32,7 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top }
32
32
  text-align: right;
33
33
  }
34
34
  .CodeRay .line_numbers tt { font-weight: bold }
35
+ .CodeRay .line_numbers .highlighted { color: red }
35
36
  .CodeRay .no { padding: 0px 4px }
36
37
  .CodeRay .code { width: 100% }
37
38
 
@@ -57,9 +58,11 @@ ol.CodeRay li { white-space: pre }
57
58
  .ch .dl { color:#039 }
58
59
 
59
60
  .cl { color:#B06; font-weight:bold }
61
+ .cm { color:#A08; font-weight:bold }
60
62
  .co { color:#036; font-weight:bold }
61
63
  .cr { color:#0A0 }
62
64
  .cv { color:#369 }
65
+ .de { color:#B0B; }
63
66
  .df { color:#099; font-weight:bold }
64
67
  .di { color:#088; font-weight:bold }
65
68
  .dl { color:black }
@@ -69,7 +72,7 @@ ol.CodeRay li { white-space: pre }
69
72
  .e { color:#666; font-weight:bold }
70
73
  .en { color:#800; font-weight:bold }
71
74
  .er { color:#F00; background-color:#FAA }
72
- .ex { color:#F00; font-weight:bold }
75
+ .ex { color:#C00; font-weight:bold }
73
76
  .fl { color:#60E; font-weight:bold }
74
77
  .fu { color:#06B; font-weight:bold }
75
78
  .gv { color:#d70; font-weight:bold }
@@ -77,10 +80,11 @@ ol.CodeRay li { white-space: pre }
77
80
  .i { color:#00D; font-weight:bold }
78
81
  .ic { color:#B44; font-weight:bold }
79
82
 
80
- .il { background: #eee; color: black }
81
- .il .il { background: #ddd }
82
- .il .il .il { background: #ccc }
83
- .il .idl { font-weight: bold; color: #777 }
83
+ .il { background: #ddd; color: black }
84
+ .il .il { background: #ccc }
85
+ .il .il .il { background: #bbb }
86
+ .il .idl { background: #ddd; font-weight: bold; color: #666 }
87
+ .idl { background-color: #bbb; font-weight: bold; color: #666; }
84
88
 
85
89
  .im { color:#f00; }
86
90
  .in { color:#B2B; font-weight:bold }
@@ -93,8 +97,8 @@ ol.CodeRay li { white-space: pre }
93
97
  .pc { color:#038; font-weight:bold }
94
98
  .pd { color:#369; font-weight:bold }
95
99
  .pp { color:#579; }
96
- .ps { color:#00C; font-weight: bold; }
97
- .pt { color:#349; font-weight:bold }
100
+ .ps { color:#00C; font-weight:bold }
101
+ .pt { color:#074; font-weight:bold }
98
102
  .r, .kw { color:#080; font-weight:bold }
99
103
 
100
104
  .ke { color: #808; }
@@ -15,8 +15,10 @@ module CodeRay
15
15
  :class_variable => 'cv',
16
16
  :color => 'cr',
17
17
  :comment => 'c',
18
+ :complex => 'cm',
18
19
  :constant => 'co',
19
20
  :content => 'k',
21
+ :decorator => 'de',
20
22
  :definition => 'df',
21
23
  :delimiter => 'dl',
22
24
  :directive => 'di',
@@ -31,6 +33,7 @@ module CodeRay
31
33
  :function => 'fu',
32
34
  :global_variable => 'gv',
33
35
  :hex => 'hx',
36
+ :imaginary => 'cm',
34
37
  :important => 'im',
35
38
  :include => 'ic',
36
39
  :inline => 'il',
@@ -74,7 +77,7 @@ module CodeRay
74
77
  :space => :NO_HIGHLIGHT, # 'sp'
75
78
  :plain => :NO_HIGHLIGHT,
76
79
  }
77
- ClassOfKind[:procedure] = ClassOfKind[:method] = ClassOfKind[:function]
80
+ ClassOfKind[:method] = ClassOfKind[:function]
78
81
  ClassOfKind[:open] = ClassOfKind[:close] = ClassOfKind[:delimiter]
79
82
  ClassOfKind[:nesting_delimiter] = ClassOfKind[:delimiter]
80
83
  ClassOfKind[:escape] = ClassOfKind[:delimiter]
@@ -14,7 +14,7 @@ module CodeRay
14
14
  #
15
15
  # [:comment, '# It looks like this']
16
16
  # [:float, '3.1415926']
17
- # [:error, '���']
17
+ # [:error, '$^']
18
18
  #
19
19
  # Some scanners also yield some kind of sub-tokens, represented by special
20
20
  # token texts, namely :open and :close .
@@ -46,47 +46,10 @@ module CodeRay
46
46
  #
47
47
  # Tokens' subclass TokenStream allows streaming to save memory.
48
48
  class Tokens < Array
49
-
50
- class << self
51
-
52
- # Convert the token to a string.
53
- #
54
- # This format is used by Encoders.Tokens.
55
- # It can be reverted using read_token.
56
- def write_token text, type
57
- if text.is_a? String
58
- "#{type}\t#{escape(text)}\n"
59
- else
60
- ":#{text}\t#{type}\t\n"
61
- end
62
- end
63
-
64
- # Read a token from the string.
65
- #
66
- # Inversion of write_token.
67
- #
68
- # TODO Test this!
69
- def read_token token
70
- type, text = token.split("\t", 2)
71
- if type[0] == ?:
72
- [text.to_sym, type[1..-1].to_sym]
73
- else
74
- [type.to_sym, unescape(text)]
75
- end
76
- end
77
-
78
- # Escapes a string for use in write_token.
79
- def escape text
80
- text.gsub(/[\n\\]/, '\\\\\&')
81
- end
82
-
83
- # Unescapes a string created by escape.
84
- def unescape text
85
- text.gsub(/\\[\n\\]/) { |m| m[1,1] }
86
- end
87
-
88
- end
89
-
49
+
50
+ # The Scanner instance that created the tokens.
51
+ attr_accessor :scanner
52
+
90
53
  # Whether the object is a TokenStream.
91
54
  #
92
55
  # Returns false.
@@ -146,7 +109,6 @@ module CodeRay
146
109
  encode :text, options
147
110
  end
148
111
 
149
-
150
112
  # Redirects unknown methods to encoder calls.
151
113
  #
152
114
  # For example, if you call +tokens.html+, the HTML encoder
@@ -230,6 +192,8 @@ module CodeRay
230
192
  replace fix
231
193
  end
232
194
 
195
+ # TODO: Scanner#split_into_lines
196
+ #
233
197
  # Makes sure that:
234
198
  # - newlines are single tokens
235
199
  # (which means all other token are single-line)
@@ -380,8 +344,48 @@ module CodeRay
380
344
 
381
345
  end
382
346
 
383
-
384
- # Token name abbreviations
385
- require 'coderay/token_classes'
347
+ end
386
348
 
349
+ if $0 == __FILE__
350
+ $VERBOSE = true
351
+ $: << File.join(File.dirname(__FILE__), '..')
352
+ eval DATA.read, nil, $0, __LINE__ + 4
387
353
  end
354
+
355
+ __END__
356
+ require 'test/unit'
357
+
358
+ class TokensTest < Test::Unit::TestCase
359
+
360
+ def test_creation
361
+ assert CodeRay::Tokens < Array
362
+ tokens = nil
363
+ assert_nothing_raised do
364
+ tokens = CodeRay::Tokens.new
365
+ end
366
+ assert_kind_of Array, tokens
367
+ end
368
+
369
+ def test_adding_tokens
370
+ tokens = CodeRay::Tokens.new
371
+ assert_nothing_raised do
372
+ tokens << ['string', :type]
373
+ tokens << ['()', :operator]
374
+ end
375
+ assert_equal tokens.size, 2
376
+ end
377
+
378
+ def test_dump_undump
379
+ tokens = CodeRay::Tokens.new
380
+ assert_nothing_raised do
381
+ tokens << ['string', :type]
382
+ tokens << ['()', :operator]
383
+ end
384
+ tokens2 = nil
385
+ assert_nothing_raised do
386
+ tokens2 = tokens.dump.undump
387
+ end
388
+ assert_equal tokens, tokens2
389
+ end
390
+
391
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coderay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.357
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - murphy
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-02 00:00:00 +02:00
12
+ date: 2010-01-01 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: " CodeRay is a Ruby library for syntax highlighting.\n I try to make CodeRay easy to use and intuitive, but at the same time\n fully featured, complete, fast and efficient.\n\n Usage is simple:\n require 'coderay'\n code = 'some %q(weird (Ruby) can't shock) me!'\n puts CodeRay.scan(code, :ruby).html\n"
16
+ description: " CodeRay is a Ruby library for syntax highlighting.\n I try to make CodeRay easy to use and intuitive, but at the same time\n fully featured, complete, fast and efficient.\n\n Usage is simple:\n CodeRay.scan(code, :ruby).div\n"
17
17
  email: murphy@rubychan.de
18
18
  executables:
19
19
  - coderay
@@ -27,19 +27,24 @@ files:
27
27
  - ./lib/coderay/duo.rb
28
28
  - ./lib/coderay/encoder.rb
29
29
  - ./lib/coderay/encoders/_map.rb
30
+ - ./lib/coderay/encoders/comment_filter.rb
30
31
  - ./lib/coderay/encoders/count.rb
31
32
  - ./lib/coderay/encoders/debug.rb
32
33
  - ./lib/coderay/encoders/div.rb
34
+ - ./lib/coderay/encoders/filter.rb
33
35
  - ./lib/coderay/encoders/html/css.rb
34
36
  - ./lib/coderay/encoders/html/numerization.rb
35
37
  - ./lib/coderay/encoders/html/output.rb
36
38
  - ./lib/coderay/encoders/html.rb
39
+ - ./lib/coderay/encoders/json.rb
40
+ - ./lib/coderay/encoders/lines_of_code.rb
37
41
  - ./lib/coderay/encoders/null.rb
38
42
  - ./lib/coderay/encoders/page.rb
39
43
  - ./lib/coderay/encoders/span.rb
40
44
  - ./lib/coderay/encoders/statistic.rb
45
+ - ./lib/coderay/encoders/term.rb
41
46
  - ./lib/coderay/encoders/text.rb
42
- - ./lib/coderay/encoders/tokens.rb
47
+ - ./lib/coderay/encoders/token_class_filter.rb
43
48
  - ./lib/coderay/encoders/xml.rb
44
49
  - ./lib/coderay/encoders/yaml.rb
45
50
  - ./lib/coderay/for_redcloth.rb
@@ -50,21 +55,26 @@ files:
50
55
  - ./lib/coderay/scanner.rb
51
56
  - ./lib/coderay/scanners/_map.rb
52
57
  - ./lib/coderay/scanners/c.rb
58
+ - ./lib/coderay/scanners/cpp.rb
53
59
  - ./lib/coderay/scanners/css.rb
54
60
  - ./lib/coderay/scanners/debug.rb
55
61
  - ./lib/coderay/scanners/delphi.rb
56
62
  - ./lib/coderay/scanners/diff.rb
63
+ - ./lib/coderay/scanners/groovy.rb
57
64
  - ./lib/coderay/scanners/html.rb
58
65
  - ./lib/coderay/scanners/java/builtin_types.rb
59
66
  - ./lib/coderay/scanners/java.rb
60
67
  - ./lib/coderay/scanners/java_script.rb
61
68
  - ./lib/coderay/scanners/json.rb
62
69
  - ./lib/coderay/scanners/nitro_xhtml.rb
70
+ - ./lib/coderay/scanners/php.rb
63
71
  - ./lib/coderay/scanners/plaintext.rb
72
+ - ./lib/coderay/scanners/python.rb
64
73
  - ./lib/coderay/scanners/rhtml.rb
65
74
  - ./lib/coderay/scanners/ruby/patterns.rb
66
75
  - ./lib/coderay/scanners/ruby.rb
67
76
  - ./lib/coderay/scanners/scheme.rb
77
+ - ./lib/coderay/scanners/sql.rb
68
78
  - ./lib/coderay/scanners/xml.rb
69
79
  - ./lib/coderay/scanners/yaml.rb
70
80
  - ./lib/coderay/style.rb