coderay 0.7.1.147 → 0.7.2.165

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/bin/coderay +54 -56
  2. data/demo/suite.rb +54 -54
  3. data/lib/coderay.rb +187 -187
  4. data/lib/coderay/duo.rb +29 -29
  5. data/lib/coderay/encoder.rb +173 -173
  6. data/lib/coderay/encoders/_map.rb +8 -8
  7. data/lib/coderay/encoders/count.rb +21 -21
  8. data/lib/coderay/encoders/debug.rb +46 -46
  9. data/lib/coderay/encoders/div.rb +20 -20
  10. data/lib/coderay/encoders/html.rb +249 -245
  11. data/lib/coderay/encoders/html/classes.rb +73 -73
  12. data/lib/coderay/encoders/html/css.rb +65 -65
  13. data/lib/coderay/encoders/html/numerization.rb +122 -122
  14. data/lib/coderay/encoders/html/output.rb +195 -195
  15. data/lib/coderay/encoders/null.rb +26 -26
  16. data/lib/coderay/encoders/page.rb +21 -21
  17. data/lib/coderay/encoders/span.rb +20 -20
  18. data/lib/coderay/encoders/statistic.rb +81 -81
  19. data/lib/coderay/encoders/text.rb +33 -33
  20. data/lib/coderay/encoders/tokens.rb +44 -44
  21. data/lib/coderay/encoders/xml.rb +71 -71
  22. data/lib/coderay/encoders/yaml.rb +22 -22
  23. data/lib/coderay/helpers/filetype.rb +152 -153
  24. data/lib/coderay/helpers/gzip_simple.rb +67 -68
  25. data/lib/coderay/helpers/plugin.rb +297 -297
  26. data/lib/coderay/helpers/word_list.rb +46 -47
  27. data/lib/coderay/scanner.rb +238 -238
  28. data/lib/coderay/scanners/_map.rb +15 -14
  29. data/lib/coderay/scanners/c.rb +163 -155
  30. data/lib/coderay/scanners/delphi.rb +131 -129
  31. data/lib/coderay/scanners/html.rb +174 -167
  32. data/lib/coderay/scanners/nitro_xhtml.rb +130 -0
  33. data/lib/coderay/scanners/plaintext.rb +15 -15
  34. data/lib/coderay/scanners/rhtml.rb +73 -65
  35. data/lib/coderay/scanners/ruby.rb +404 -397
  36. data/lib/coderay/scanners/ruby/patterns.rb +216 -216
  37. data/lib/coderay/scanners/xml.rb +18 -18
  38. data/lib/coderay/style.rb +20 -20
  39. data/lib/coderay/styles/_map.rb +3 -3
  40. data/lib/coderay/styles/cycnus.rb +18 -18
  41. data/lib/coderay/styles/murphy.rb +18 -18
  42. data/lib/coderay/tokens.rb +322 -322
  43. metadata +86 -86
  44. data/lib/coderay/scanners/nitro_html.rb +0 -125
  45. data/lib/coderay/scanners/yaml.rb +0 -85
@@ -1,14 +1,15 @@
1
- module CodeRay
2
- module Scanners
3
-
4
- map :cpp => :c,
5
- :plain => :plaintext,
6
- :pascal => :delphi,
7
- :irb => :ruby,
8
- :xml => :html,
9
- :xhtml => :nitro_html
10
-
11
- default :plain
12
-
13
- end
14
- end
1
+ module CodeRay
2
+ module Scanners
3
+
4
+ map :cpp => :c,
5
+ :plain => :plaintext,
6
+ :pascal => :delphi,
7
+ :irb => :ruby,
8
+ :xml => :html,
9
+ :xhtml => :nitro_xhtml,
10
+ :nitro => :nitro_xhtml
11
+
12
+ default :plain
13
+
14
+ end
15
+ end
@@ -1,155 +1,163 @@
1
- module CodeRay
2
- module Scanners
3
-
4
- class C < Scanner
5
-
6
- register_for :c
7
-
8
- RESERVED_WORDS = [
9
- 'asm', 'break', 'case', 'continue', 'default', 'do', 'else',
10
- 'for', 'goto', 'if', 'return', 'switch', 'while',
11
- 'struct', 'union', 'enum', 'typedef',
12
- 'static', 'register', 'auto', 'extern',
13
- 'sizeof',
14
- 'volatile', 'const', # C89
15
- 'inline', 'restrict', # C99
16
- ]
17
-
18
- PREDEFINED_TYPES = [
19
- 'int', 'long', 'short', 'char', 'void',
20
- 'signed', 'unsigned', 'float', 'double',
21
- 'bool', 'complex', # C99
22
- ]
23
-
24
- PREDEFINED_CONSTANTS = [
25
- 'EOF', 'NULL',
26
- 'true', 'false', # C99
27
- ]
28
-
29
- IDENT_KIND = WordList.new(:ident).
30
- add(RESERVED_WORDS, :reserved).
31
- add(PREDEFINED_TYPES, :pre_type).
32
- add(PREDEFINED_CONSTANTS, :pre_constant)
33
-
34
- ESCAPE = / [rbfnrtv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
35
- UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
36
-
37
- def scan_tokens tokens, options
38
-
39
- state = :initial
40
-
41
- until eos?
42
-
43
- kind = :error
44
- match = nil
45
-
46
- case state
47
-
48
- when :initial
49
-
50
- if scan(/ \s+ | \\\n /x)
51
- kind = :space
52
-
53
- elsif scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
54
- kind = :comment
55
-
56
- elsif match = scan(/ \# \s* if \s* 0 /x)
57
- match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
58
- kind = :comment
59
-
60
- elsif scan(/ [-+*\/=<>?:;,!&^|()\[\]{}~%]+ | \.(?!\d) /x)
61
- kind = :operator
62
-
63
- elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
64
- kind = IDENT_KIND[match]
65
- if kind == :ident and check(/:(?!:)/)
66
- match << scan(/:/)
67
- kind = :label
68
- end
69
-
70
- elsif match = scan(/L?"/)
71
- tokens << [:open, :string]
72
- if match[0] == ?L
73
- tokens << ['L', :modifier]
74
- match = '"'
75
- end
76
- state = :string
77
- kind = :delimiter
78
-
79
- elsif scan(/#\s*(\w*)/)
80
- kind = :preprocessor # FIXME multiline preprocs
81
- state = :include_expected if self[1] == 'include'
82
-
83
- elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
84
- kind = :char
85
-
86
- elsif scan(/0[xX][0-9A-Fa-f]+/)
87
- kind = :hex
88
-
89
- elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
90
- kind = :oct
91
-
92
- elsif scan(/(?:\d+)(?![.eEfF])/)
93
- kind = :integer
94
-
95
- elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
96
- kind = :float
97
-
98
- else
99
- getch
100
- end
101
-
102
- when :string
103
- if scan(/[^\\"]+/)
104
- kind = :content
105
- elsif scan(/"/)
106
- tokens << ['"', :delimiter]
107
- tokens << [:close, :string]
108
- state = :initial
109
- next
110
- elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
111
- kind = :char
112
- elsif scan(/ \\ | $ /x)
113
- kind = :error
114
- state = :initial
115
- else
116
- raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
117
- end
118
-
119
- when :include_expected
120
- if scan(/<[^>\n]+>?|"[^"\n\\]*(?:\\.[^"\n\\]*)*"?/)
121
- kind = :include
122
- state = :initial
123
-
124
- elsif match = scan(/\s+/)
125
- kind = :space
126
- state = :initial if match.index ?\n
127
-
128
- else
129
- getch
130
-
131
- end
132
-
133
- else
134
- raise_inspect 'Unknown state', tokens
135
-
136
- end
137
-
138
- match ||= matched
139
- if $DEBUG and (not kind or kind == :error)
140
- raise_inspect 'Error token %p in line %d' %
141
- [[match, kind], line], tokens
142
- end
143
- raise_inspect 'Empty token', tokens unless match
144
-
145
- tokens << [match, kind]
146
-
147
- end
148
-
149
- tokens
150
- end
151
-
152
- end
153
-
154
- end
155
- end
1
+ module CodeRay
2
+ module Scanners
3
+
4
+ class C < Scanner
5
+
6
+ register_for :c
7
+
8
+ RESERVED_WORDS = [
9
+ 'asm', 'break', 'case', 'continue', 'default', 'do', 'else',
10
+ 'for', 'goto', 'if', 'return', 'switch', 'while',
11
+ 'struct', 'union', 'enum', 'typedef',
12
+ 'static', 'register', 'auto', 'extern',
13
+ 'sizeof',
14
+ 'volatile', 'const', # C89
15
+ 'inline', 'restrict', # C99
16
+ ]
17
+
18
+ PREDEFINED_TYPES = [
19
+ 'int', 'long', 'short', 'char', 'void',
20
+ 'signed', 'unsigned', 'float', 'double',
21
+ 'bool', 'complex', # C99
22
+ ]
23
+
24
+ PREDEFINED_CONSTANTS = [
25
+ 'EOF', 'NULL',
26
+ 'true', 'false', # C99
27
+ ]
28
+
29
+ IDENT_KIND = WordList.new(:ident).
30
+ add(RESERVED_WORDS, :reserved).
31
+ add(PREDEFINED_TYPES, :pre_type).
32
+ add(PREDEFINED_CONSTANTS, :pre_constant)
33
+
34
+ ESCAPE = / [rbfnrtv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
35
+ UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
36
+
37
+ def scan_tokens tokens, options
38
+
39
+ state = :initial
40
+
41
+ until eos?
42
+
43
+ kind = nil
44
+ match = nil
45
+
46
+ case state
47
+
48
+ when :initial
49
+
50
+ if scan(/ \s+ | \\\n /x)
51
+ kind = :space
52
+
53
+ elsif scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
54
+ kind = :comment
55
+
56
+ elsif match = scan(/ \# \s* if \s* 0 /x)
57
+ match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
58
+ kind = :comment
59
+
60
+ elsif scan(/ [-+*\/=<>?:;,!&^|()\[\]{}~%]+ | \.(?!\d) /x)
61
+ kind = :operator
62
+
63
+ elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
64
+ kind = IDENT_KIND[match]
65
+ if kind == :ident and check(/:(?!:)/)
66
+ match << scan(/:/)
67
+ kind = :label
68
+ end
69
+
70
+ elsif match = scan(/L?"/)
71
+ tokens << [:open, :string]
72
+ if match[0] == ?L
73
+ tokens << ['L', :modifier]
74
+ match = '"'
75
+ end
76
+ state = :string
77
+ kind = :delimiter
78
+
79
+ elsif scan(/#\s*(\w*)/)
80
+ kind = :preprocessor # FIXME multiline preprocs
81
+ state = :include_expected if self[1] == 'include'
82
+
83
+ elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
84
+ kind = :char
85
+
86
+ elsif scan(/0[xX][0-9A-Fa-f]+/)
87
+ kind = :hex
88
+
89
+ elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
90
+ kind = :oct
91
+
92
+ elsif scan(/(?:\d+)(?![.eEfF])/)
93
+ kind = :integer
94
+
95
+ elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
96
+ kind = :float
97
+
98
+ else
99
+ getch
100
+ kind = :error
101
+
102
+ end
103
+
104
+ when :string
105
+ if scan(/[^\\\n"]+/)
106
+ kind = :content
107
+ elsif scan(/"/)
108
+ tokens << ['"', :delimiter]
109
+ tokens << [:close, :string]
110
+ state = :initial
111
+ next
112
+ elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
113
+ kind = :char
114
+ elsif scan(/ \\ | $ /x)
115
+ tokens << [:close, :string]
116
+ kind = :error
117
+ state = :initial
118
+ else
119
+ raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
120
+ end
121
+
122
+ when :include_expected
123
+ if scan(/<[^>\n]+>?|"[^"\n\\]*(?:\\.[^"\n\\]*)*"?/)
124
+ kind = :include
125
+ state = :initial
126
+
127
+ elsif match = scan(/\s+/)
128
+ kind = :space
129
+ state = :initial if match.index ?\n
130
+
131
+ else
132
+ getch
133
+ kind = :error
134
+
135
+ end
136
+
137
+ else
138
+ raise_inspect 'Unknown state', tokens
139
+
140
+ end
141
+
142
+ match ||= matched
143
+ if $DEBUG and not kind
144
+ raise_inspect 'Error token %p in line %d' %
145
+ [[match, kind], line], tokens
146
+ end
147
+ raise_inspect 'Empty token', tokens unless match
148
+
149
+ tokens << [match, kind]
150
+
151
+ end
152
+
153
+ if state == :string
154
+ tokens << [:close, :string]
155
+ end
156
+
157
+ tokens
158
+ end
159
+
160
+ end
161
+
162
+ end
163
+ end
@@ -1,129 +1,131 @@
1
- module CodeRay
2
- module Scanners
3
-
4
- class Delphi < Scanner
5
-
6
- register_for :delphi
7
-
8
- RESERVED_WORDS = [
9
- 'and', 'array', 'as', 'at', 'asm', 'at', 'begin', 'case', 'class',
10
- 'const', 'constructor', 'destructor', 'dispinterface', 'div', 'do',
11
- 'downto', 'else', 'end', 'except', 'exports', 'file', 'finalization',
12
- 'finally', 'for', 'function', 'goto', 'if', 'implementation', 'in',
13
- 'inherited', 'initialization', 'inline', 'interface', 'is', 'label',
14
- 'library', 'mod', 'nil', 'not', 'object', 'of', 'or', 'out', 'packed',
15
- 'procedure', 'program', 'property', 'raise', 'record', 'repeat',
16
- 'resourcestring', 'set', 'shl', 'shr', 'string', 'then', 'threadvar',
17
- 'to', 'try', 'type', 'unit', 'until', 'uses', 'var', 'while', 'with',
18
- 'xor', 'on'
19
- ]
20
-
21
- DIRECTIVES = [
22
- 'absolute', 'abstract', 'assembler', 'at', 'automated', 'cdecl',
23
- 'contains', 'deprecated', 'dispid', 'dynamic', 'export',
24
- 'external', 'far', 'forward', 'implements', 'local',
25
- 'near', 'nodefault', 'on', 'overload', 'override',
26
- 'package', 'pascal', 'platform', 'private', 'protected', 'public',
27
- 'published', 'read', 'readonly', 'register', 'reintroduce',
28
- 'requires', 'resident', 'safecall', 'stdcall', 'stored', 'varargs',
29
- 'virtual', 'write', 'writeonly'
30
- ]
31
-
32
- IDENT_KIND = CaseIgnoringWordList.new(:ident).
33
- add(RESERVED_WORDS, :reserved).
34
- add(DIRECTIVES, :directive)
35
-
36
- def scan_tokens tokens, options
37
-
38
- state = :initial
39
-
40
- until eos?
41
-
42
- kind = :error
43
- match = nil
44
-
45
- if state == :initial
46
-
47
- if scan(/ \s+ /x)
48
- kind = :space
49
-
50
- elsif scan(%r! \{ \$ [^}]* \}? | \(\* \$ (?: .*? \*\) | .* ) !mx)
51
- kind = :preprocessor
52
-
53
- elsif scan(%r! // [^\n]* | \{ [^}]* \}? | \(\* (?: .*? \*\) | .* ) !mx)
54
- kind = :comment
55
-
56
- elsif scan(/ [-+*\/=<>:;,.@\^|\(\)\[\]]+ /x)
57
- kind = :operator
58
-
59
- elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
60
- kind = IDENT_KIND[match]
61
-
62
- elsif match = scan(/ ' ( [^\n']|'' ) (?:'|$) /x)
63
- tokens << [:open, :char]
64
- tokens << ["'", :delimiter]
65
- tokens << [self[1], :content]
66
- tokens << ["'", :delimiter]
67
- tokens << [:close, :char]
68
- next
69
-
70
- elsif match = scan(/ ' /x)
71
- tokens << [:open, :string]
72
- state = :string
73
- kind = :delimiter
74
-
75
- elsif scan(/ \# (?: \d+ | \$[0-9A-Fa-f]+ ) /x)
76
- kind = :char
77
-
78
- elsif scan(/ \$ [0-9A-Fa-f]+ /x)
79
- kind = :hex
80
-
81
- elsif scan(/ (?: \d+ ) (?![eE]|\.[^.]) /x)
82
- kind = :integer
83
-
84
- elsif scan(/ \d+ (?: \.\d+ (?: [eE][+-]? \d+ )? | [eE][+-]? \d+ ) /x)
85
- kind = :float
86
-
87
- else
88
- getch
89
- end
90
-
91
- elsif state == :string
92
- if scan(/[^\n']+/)
93
- kind = :content
94
- elsif scan(/''/)
95
- kind = :char
96
- elsif scan(/'/)
97
- tokens << ["'", :delimiter]
98
- tokens << [:close, :string]
99
- state = :initial
100
- next
101
- elsif scan(/\n/)
102
- state = :initial
103
- else
104
- raise "else case \' reached; %p not handled." % peek(1), tokens
105
- end
106
-
107
- else
108
- raise 'else-case reached', tokens
109
-
110
- end
111
-
112
- match ||= matched
113
- if $DEBUG and (not kind or kind == :error)
114
- raise_inspect 'Error token %p in line %d' %
115
- [[match, kind], line], tokens
116
- end
117
- raise_inspect 'Empty token', tokens unless match
118
-
119
- tokens << [match, kind]
120
-
121
- end
122
-
123
- tokens
124
- end
125
-
126
- end
127
-
128
- end
129
- end
1
+ module CodeRay
2
+ module Scanners
3
+
4
+ class Delphi < Scanner
5
+
6
+ register_for :delphi
7
+
8
+ RESERVED_WORDS = [
9
+ 'and', 'array', 'as', 'at', 'asm', 'at', 'begin', 'case', 'class',
10
+ 'const', 'constructor', 'destructor', 'dispinterface', 'div', 'do',
11
+ 'downto', 'else', 'end', 'except', 'exports', 'file', 'finalization',
12
+ 'finally', 'for', 'function', 'goto', 'if', 'implementation', 'in',
13
+ 'inherited', 'initialization', 'inline', 'interface', 'is', 'label',
14
+ 'library', 'mod', 'nil', 'not', 'object', 'of', 'or', 'out', 'packed',
15
+ 'procedure', 'program', 'property', 'raise', 'record', 'repeat',
16
+ 'resourcestring', 'set', 'shl', 'shr', 'string', 'then', 'threadvar',
17
+ 'to', 'try', 'type', 'unit', 'until', 'uses', 'var', 'while', 'with',
18
+ 'xor', 'on'
19
+ ]
20
+
21
+ DIRECTIVES = [
22
+ 'absolute', 'abstract', 'assembler', 'at', 'automated', 'cdecl',
23
+ 'contains', 'deprecated', 'dispid', 'dynamic', 'export',
24
+ 'external', 'far', 'forward', 'implements', 'local',
25
+ 'near', 'nodefault', 'on', 'overload', 'override',
26
+ 'package', 'pascal', 'platform', 'private', 'protected', 'public',
27
+ 'published', 'read', 'readonly', 'register', 'reintroduce',
28
+ 'requires', 'resident', 'safecall', 'stdcall', 'stored', 'varargs',
29
+ 'virtual', 'write', 'writeonly'
30
+ ]
31
+
32
+ IDENT_KIND = CaseIgnoringWordList.new(:ident).
33
+ add(RESERVED_WORDS, :reserved).
34
+ add(DIRECTIVES, :directive)
35
+
36
+ def scan_tokens tokens, options
37
+
38
+ state = :initial
39
+
40
+ until eos?
41
+
42
+ kind = nil
43
+ match = nil
44
+
45
+ if state == :initial
46
+
47
+ if scan(/ \s+ /x)
48
+ kind = :space
49
+
50
+ elsif scan(%r! \{ \$ [^}]* \}? | \(\* \$ (?: .*? \*\) | .* ) !mx)
51
+ kind = :preprocessor
52
+
53
+ elsif scan(%r! // [^\n]* | \{ [^}]* \}? | \(\* (?: .*? \*\) | .* ) !mx)
54
+ kind = :comment
55
+
56
+ elsif scan(/ [-+*\/=<>:;,.@\^|\(\)\[\]]+ /x)
57
+ kind = :operator
58
+
59
+ elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
60
+ kind = IDENT_KIND[match]
61
+
62
+ elsif match = scan(/ ' ( [^\n']|'' ) (?:'|$) /x)
63
+ tokens << [:open, :char]
64
+ tokens << ["'", :delimiter]
65
+ tokens << [self[1], :content]
66
+ tokens << ["'", :delimiter]
67
+ tokens << [:close, :char]
68
+ next
69
+
70
+ elsif match = scan(/ ' /x)
71
+ tokens << [:open, :string]
72
+ state = :string
73
+ kind = :delimiter
74
+
75
+ elsif scan(/ \# (?: \d+ | \$[0-9A-Fa-f]+ ) /x)
76
+ kind = :char
77
+
78
+ elsif scan(/ \$ [0-9A-Fa-f]+ /x)
79
+ kind = :hex
80
+
81
+ elsif scan(/ (?: \d+ ) (?![eE]|\.[^.]) /x)
82
+ kind = :integer
83
+
84
+ elsif scan(/ \d+ (?: \.\d+ (?: [eE][+-]? \d+ )? | [eE][+-]? \d+ ) /x)
85
+ kind = :float
86
+
87
+ else
88
+ kind = :error
89
+ getch
90
+
91
+ end
92
+
93
+ elsif state == :string
94
+ if scan(/[^\n']+/)
95
+ kind = :content
96
+ elsif scan(/''/)
97
+ kind = :char
98
+ elsif scan(/'/)
99
+ tokens << ["'", :delimiter]
100
+ tokens << [:close, :string]
101
+ state = :initial
102
+ next
103
+ elsif scan(/\n/)
104
+ state = :initial
105
+ else
106
+ raise "else case \' reached; %p not handled." % peek(1), tokens
107
+ end
108
+
109
+ else
110
+ raise 'else-case reached', tokens
111
+
112
+ end
113
+
114
+ match ||= matched
115
+ if $DEBUG and not kind
116
+ raise_inspect 'Error token %p in line %d' %
117
+ [[match, kind], line], tokens
118
+ end
119
+ raise_inspect 'Empty token', tokens unless match
120
+
121
+ tokens << [match, kind]
122
+
123
+ end
124
+
125
+ tokens
126
+ end
127
+
128
+ end
129
+
130
+ end
131
+ end