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,46 +1,46 @@
1
- module CodeRay
2
- module Encoders
3
-
4
- # = Debug Encoder
5
- #
6
- # Fast encoder producing simple debug output.
7
- #
8
- # It is readable and diff-able and is used for testing.
9
- #
10
- # You cannot fully restore the tokens information from the
11
- # output, because consecutive :space tokens are merged.
12
- # Use Tokens#dump for caching purposes.
13
- class Debug < Encoder
14
-
15
- include Streamable
16
- register_for :debug
17
-
18
- FILE_EXTENSION = 'raydebug'
19
-
20
- protected
21
- def text_token text, kind
22
- @out <<
23
- if kind == :space
24
- text
25
- else
26
- text = text.gsub(/[)\\]/, '\\\\\0')
27
- "#{kind}(#{text})"
28
- end
29
- end
30
-
31
- def block_token action, kind
32
- @out << super
33
- end
34
-
35
- def open_token kind
36
- "#{kind}<"
37
- end
38
-
39
- def close_token kind
40
- ">"
41
- end
42
-
43
- end
44
-
45
- end
46
- end
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # = Debug Encoder
5
+ #
6
+ # Fast encoder producing simple debug output.
7
+ #
8
+ # It is readable and diff-able and is used for testing.
9
+ #
10
+ # You cannot fully restore the tokens information from the
11
+ # output, because consecutive :space tokens are merged.
12
+ # Use Tokens#dump for caching purposes.
13
+ class Debug < Encoder
14
+
15
+ include Streamable
16
+ register_for :debug
17
+
18
+ FILE_EXTENSION = 'raydebug'
19
+
20
+ protected
21
+ def text_token text, kind
22
+ @out <<
23
+ if kind == :space
24
+ text
25
+ else
26
+ text = text.gsub(/[)\\]/, '\\\\\0')
27
+ "#{kind}(#{text})"
28
+ end
29
+ end
30
+
31
+ def block_token action, kind
32
+ @out << super
33
+ end
34
+
35
+ def open_token kind
36
+ "#{kind}<"
37
+ end
38
+
39
+ def close_token kind
40
+ ">"
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
@@ -1,20 +1,20 @@
1
- module CodeRay
2
- module Encoders
3
-
4
- load :html
5
-
6
- class Div < HTML
7
-
8
- FILE_EXTENSION = 'div.html'
9
-
10
- register_for :div
11
-
12
- DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge({
13
- :css => :style,
14
- :wrap => :div,
15
- })
16
-
17
- end
18
-
19
- end
20
- end
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ load :html
5
+
6
+ class Div < HTML
7
+
8
+ FILE_EXTENSION = 'div.html'
9
+
10
+ register_for :div
11
+
12
+ DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge({
13
+ :css => :style,
14
+ :wrap => :div,
15
+ })
16
+
17
+ end
18
+
19
+ end
20
+ end
@@ -1,245 +1,249 @@
1
- module CodeRay
2
- module Encoders
3
-
4
- # = HTML Encoder
5
- #
6
- # This is CodeRay's most important highlighter:
7
- # It provides save, fast XHTML generation and CSS support.
8
- #
9
- # == Usage
10
- #
11
- # require 'coderay'
12
- # puts CodeRay.scan('Some /code/', :ruby).html #-> a HTML page
13
- # puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span) #-> <span class="CodeRay"><span class="co">Some</span> /code/</span>
14
- # puts CodeRay.scan('Some /code/', :ruby).span #-> the same
15
- #
16
- # puts CodeRay.scan('Some code', :ruby).html(
17
- # :wrap => nil,
18
- # :line_numbers => :inline,
19
- # :css => :style
20
- # )
21
- # #-> <span class="no">1</span> <span style="color:#036; font-weight:bold;">Some</span> code
22
- #
23
- # == Options
24
- #
25
- # === :tab_width
26
- # Convert \t characters to +n+ spaces (a number.)
27
- # Default: 8
28
- #
29
- # === :css
30
- # How to include the styles; can be :class or :style.
31
- #
32
- # Default: :class
33
- #
34
- # === :wrap
35
- # Wrap in :page, :div, :span or nil.
36
- #
37
- # You can also use Encoders::Div and Encoders::Span.
38
- #
39
- # Default: nil
40
- #
41
- # === :line_numbers
42
- # Include line numbers in :table, :inline, :list or nil (no line numbers)
43
- #
44
- # Default: nil
45
- #
46
- # === :line_number_start
47
- # Where to start with line number counting.
48
- #
49
- # Default: 1
50
- #
51
- # === :bold_every
52
- # Make every +n+-th number appear bold.
53
- #
54
- # Default: 10
55
- #
56
- # === :hint
57
- # Include some information into the output using the title attribute.
58
- # Can be :info (show token type on mouse-over), :info_long (with full path) or :debug (via inspect).
59
- #
60
- # Default: false
61
- class HTML < Encoder
62
-
63
- include Streamable
64
- register_for :html
65
-
66
- FILE_EXTENSION = 'html'
67
-
68
- DEFAULT_OPTIONS = {
69
- :tab_width => 8,
70
-
71
- :level => :xhtml,
72
- :css => :class,
73
-
74
- :style => :cycnus,
75
-
76
- :wrap => nil,
77
-
78
- :line_numbers => nil,
79
- :line_number_start => 1,
80
- :bold_every => 10,
81
-
82
- :hint => false,
83
- }
84
-
85
- helper :classes, :output, :css
86
-
87
- attr_reader :css
88
-
89
- protected
90
-
91
- HTML_ESCAPE = { #:nodoc:
92
- '&' => '&amp;',
93
- '"' => '&quot;',
94
- '>' => '&gt;',
95
- '<' => '&lt;',
96
- }
97
-
98
- # This was to prevent illegal HTML.
99
- # Strange chars should still be avoided in codes.
100
- evil_chars = Array(0x00...0x20) - [?\n, ?\t, ?\s]
101
- evil_chars.each { |i| HTML_ESCAPE[i.chr] = ' ' }
102
- #ansi_chars = Array(0x7f..0xff)
103
- #ansi_chars.each { |i| HTML_ESCAPE[i.chr] = '&#%d;' % i }
104
- # \x9 (\t) and \xA (\n) not included
105
- #HTML_ESCAPE_PATTERN = /[\t&"><\0-\x8\xB-\x1f\x7f-\xff]/
106
- HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1f]/
107
-
108
- TOKEN_KIND_TO_INFO = Hash.new { |h, kind|
109
- h[kind] =
110
- case kind
111
- when :pre_constant
112
- 'Predefined constant'
113
- else
114
- kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
115
- end
116
- }
117
-
118
- # Generate a hint about the given +classes+ in a +hint+ style.
119
- #
120
- # +hint+ may be :info, :info_long or :debug.
121
- def self.token_path_to_hint hint, classes
122
- return '' unless hint
123
- title =
124
- case hint
125
- when :info
126
- TOKEN_KIND_TO_INFO[classes.first]
127
- when :info_long
128
- classes.reverse.map { |kind| TOKEN_KIND_TO_INFO[kind] }.join('/')
129
- when :debug
130
- classes.inspect
131
- end
132
- " title=\"#{title}\""
133
- end
134
-
135
- def setup options
136
- super
137
-
138
- @HTML_ESCAPE = HTML_ESCAPE.dup
139
- @HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
140
-
141
- @opened = [nil]
142
- @css = CSS.new options[:style]
143
-
144
- hint = options[:hint]
145
- if hint and not [:debug, :info, :info_long].include? hint
146
- raise ArgumentError, "Unknown value %p for :hint; expected :info, :debug, false or nil." % hint
147
- end
148
-
149
- case options[:css]
150
-
151
- when :class
152
- @css_style = Hash.new do |h, k|
153
- if k.is_a? Array
154
- type = k.first
155
- else
156
- type = k
157
- end
158
- c = ClassOfKind[type]
159
- if c == :NO_HIGHLIGHT and not hint
160
- h[k] = false
161
- else
162
- title = HTML.token_path_to_hint hint, (k[1..-1] << k.first)
163
- h[k] = '<span%s class="%s">' % [title, c]
164
- end
165
- end
166
-
167
- when :style
168
- @css_style = Hash.new do |h, k|
169
- if k.is_a? Array
170
- styles = k.dup
171
- else
172
- styles = [k]
173
- end
174
- type = styles.first
175
- classes = styles.map { |c| ClassOfKind[c] }
176
- if classes.first == :NO_HIGHLIGHT and not hint
177
- h[k] = false
178
- else
179
- styles.shift if [:delimiter, :modifier, :content, :escape].include? styles.first
180
- title = HTML.token_path_to_hint hint, styles
181
- classes.delete 'il'
182
- style = @css[*classes]
183
- h[k] =
184
- if style
185
- '<span%s style="%s">' % [title, style]
186
- else
187
- false
188
- end
189
- end
190
- end
191
-
192
- else
193
- raise ArgumentError, "Unknown value %p for :css." % options[:css]
194
-
195
- end
196
- end
197
-
198
- def finish options
199
- not_needed = @opened.shift
200
- @out << '</span>' * @opened.size
201
- warn '%d tokens still open' % @opened.size unless @opened.empty?
202
-
203
- @out.extend Output
204
- @out.css = @css
205
- @out.numerize! options[:line_numbers], options
206
- @out.wrap! options[:wrap]
207
-
208
- super
209
- end
210
-
211
- def token text, type
212
- if text.is_a? ::String
213
- if text =~ /#{HTML_ESCAPE_PATTERN}/o
214
- text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
215
- end
216
- @opened[0] = type
217
- if style = @css_style[@opened]
218
- @out << style << text << '</span>'
219
- else
220
- @out << text
221
- end
222
- else
223
- case text
224
- when :open
225
- @opened[0] = type
226
- @out << (@css_style[@opened] || '<span>')
227
- @opened << type
228
- when :close
229
- unless @opened.empty?
230
- raise 'Malformed token stream: Trying to close a token that was never opened.' unless @opened.size > 1
231
- @out << '</span>'
232
- @opened.pop
233
- end
234
- when nil
235
- raise 'Token with nil as text was given: %p' % [[text, type]]
236
- else
237
- raise 'unknown token kind: %p' % text
238
- end
239
- end
240
- end
241
-
242
- end
243
-
244
- end
245
- end
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # = HTML Encoder
5
+ #
6
+ # This is CodeRay's most important highlighter:
7
+ # It provides save, fast XHTML generation and CSS support.
8
+ #
9
+ # == Usage
10
+ #
11
+ # require 'coderay'
12
+ # puts CodeRay.scan('Some /code/', :ruby).html #-> a HTML page
13
+ # puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span) #-> <span class="CodeRay"><span class="co">Some</span> /code/</span>
14
+ # puts CodeRay.scan('Some /code/', :ruby).span #-> the same
15
+ #
16
+ # puts CodeRay.scan('Some code', :ruby).html(
17
+ # :wrap => nil,
18
+ # :line_numbers => :inline,
19
+ # :css => :style
20
+ # )
21
+ # #-> <span class="no">1</span> <span style="color:#036; font-weight:bold;">Some</span> code
22
+ #
23
+ # == Options
24
+ #
25
+ # === :tab_width
26
+ # Convert \t characters to +n+ spaces (a number.)
27
+ # Default: 8
28
+ #
29
+ # === :css
30
+ # How to include the styles; can be :class or :style.
31
+ #
32
+ # Default: :class
33
+ #
34
+ # === :wrap
35
+ # Wrap in :page, :div, :span or nil.
36
+ #
37
+ # You can also use Encoders::Div and Encoders::Span.
38
+ #
39
+ # Default: nil
40
+ #
41
+ # === :line_numbers
42
+ # Include line numbers in :table, :inline, :list or nil (no line numbers)
43
+ #
44
+ # Default: nil
45
+ #
46
+ # === :line_number_start
47
+ # Where to start with line number counting.
48
+ #
49
+ # Default: 1
50
+ #
51
+ # === :bold_every
52
+ # Make every +n+-th number appear bold.
53
+ #
54
+ # Default: 10
55
+ #
56
+ # === :hint
57
+ # Include some information into the output using the title attribute.
58
+ # Can be :info (show token type on mouse-over), :info_long (with full path) or :debug (via inspect).
59
+ #
60
+ # Default: false
61
+ class HTML < Encoder
62
+
63
+ include Streamable
64
+ register_for :html
65
+
66
+ FILE_EXTENSION = 'html'
67
+
68
+ DEFAULT_OPTIONS = {
69
+ :tab_width => 8,
70
+
71
+ :level => :xhtml,
72
+ :css => :class,
73
+
74
+ :style => :cycnus,
75
+
76
+ :wrap => nil,
77
+
78
+ :line_numbers => nil,
79
+ :line_number_start => 1,
80
+ :bold_every => 10,
81
+
82
+ :hint => false,
83
+ }
84
+
85
+ helper :classes, :output, :css
86
+
87
+ attr_reader :css
88
+
89
+ protected
90
+
91
+ HTML_ESCAPE = { #:nodoc:
92
+ '&' => '&amp;',
93
+ '"' => '&quot;',
94
+ '>' => '&gt;',
95
+ '<' => '&lt;',
96
+ }
97
+
98
+ # This was to prevent illegal HTML.
99
+ # Strange chars should still be avoided in codes.
100
+ evil_chars = Array(0x00...0x20) - [?\n, ?\t, ?\s]
101
+ evil_chars.each { |i| HTML_ESCAPE[i.chr] = ' ' }
102
+ #ansi_chars = Array(0x7f..0xff)
103
+ #ansi_chars.each { |i| HTML_ESCAPE[i.chr] = '&#%d;' % i }
104
+ # \x9 (\t) and \xA (\n) not included
105
+ #HTML_ESCAPE_PATTERN = /[\t&"><\0-\x8\xB-\x1f\x7f-\xff]/
106
+ HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1f]/
107
+
108
+ TOKEN_KIND_TO_INFO = Hash.new { |h, kind|
109
+ h[kind] =
110
+ case kind
111
+ when :pre_constant
112
+ 'Predefined constant'
113
+ else
114
+ kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
115
+ end
116
+ }
117
+
118
+ # Generate a hint about the given +classes+ in a +hint+ style.
119
+ #
120
+ # +hint+ may be :info, :info_long or :debug.
121
+ def self.token_path_to_hint hint, classes
122
+ return '' unless hint
123
+ title =
124
+ case hint
125
+ when :info
126
+ TOKEN_KIND_TO_INFO[classes.first]
127
+ when :info_long
128
+ classes.reverse.map { |kind| TOKEN_KIND_TO_INFO[kind] }.join('/')
129
+ when :debug
130
+ classes.inspect
131
+ end
132
+ " title=\"#{title}\""
133
+ end
134
+
135
+ def setup options
136
+ super
137
+
138
+ @HTML_ESCAPE = HTML_ESCAPE.dup
139
+ @HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
140
+
141
+ @opened = [nil]
142
+ @css = CSS.new options[:style]
143
+
144
+ hint = options[:hint]
145
+ if hint and not [:debug, :info, :info_long].include? hint
146
+ raise ArgumentError, "Unknown value %p for :hint; expected :info, :debug, false or nil." % hint
147
+ end
148
+
149
+ case options[:css]
150
+
151
+ when :class
152
+ @css_style = Hash.new do |h, k|
153
+ if k.is_a? Array
154
+ type = k.first
155
+ else
156
+ type = k
157
+ end
158
+ c = ClassOfKind[type]
159
+ if c == :NO_HIGHLIGHT and not hint
160
+ h[k] = false
161
+ else
162
+ title = HTML.token_path_to_hint hint, (k[1..-1] << k.first)
163
+ h[k] = '<span%s class="%s">' % [title, c]
164
+ end
165
+ end
166
+
167
+ when :style
168
+ @css_style = Hash.new do |h, k|
169
+ if k.is_a? Array
170
+ styles = k.dup
171
+ else
172
+ styles = [k]
173
+ end
174
+ type = styles.first
175
+ classes = styles.map { |c| ClassOfKind[c] }
176
+ if classes.first == :NO_HIGHLIGHT and not hint
177
+ h[k] = false
178
+ else
179
+ styles.shift if [:delimiter, :modifier, :content, :escape].include? styles.first
180
+ title = HTML.token_path_to_hint hint, styles
181
+ classes.delete 'il'
182
+ style = @css[*classes]
183
+ h[k] =
184
+ if style
185
+ '<span%s style="%s">' % [title, style]
186
+ else
187
+ false
188
+ end
189
+ end
190
+ end
191
+
192
+ else
193
+ raise ArgumentError, "Unknown value %p for :css." % options[:css]
194
+
195
+ end
196
+ end
197
+
198
+ def finish options
199
+ not_needed = @opened.shift
200
+ @out << '</span>' * @opened.size
201
+ warn '%d tokens still open: %p' % [@opened.size, @opened] unless @opened.empty?
202
+
203
+ @out.extend Output
204
+ @out.css = @css
205
+ @out.numerize! options[:line_numbers], options
206
+ @out.wrap! options[:wrap]
207
+
208
+ super
209
+ end
210
+
211
+ def token text, type
212
+ if text.is_a? ::String
213
+ if text =~ /#{HTML_ESCAPE_PATTERN}/o
214
+ text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
215
+ end
216
+ @opened[0] = type
217
+ if style = @css_style[@opened]
218
+ @out << style << text << '</span>'
219
+ else
220
+ @out << text
221
+ end
222
+ else
223
+ case text
224
+ when :open
225
+ @opened[0] = type
226
+ @out << (@css_style[@opened] || '<span>')
227
+ @opened << type
228
+ when :close
229
+ if @opened.empty?
230
+ # nothing to close
231
+ else
232
+ if @opened.size == 1 or @opened.last != type
233
+ raise 'Malformed token stream: Trying to close a token (%p) that is not open. Open are: %p.' % [type, @opened[1..-1]] if $DEBUG
234
+ end
235
+ @out << '</span>'
236
+ @opened.pop
237
+ end
238
+ when nil
239
+ raise 'Token with nil as text was given: %p' % [[text, type]]
240
+ else
241
+ raise 'unknown token kind: %p' % text
242
+ end
243
+ end
244
+ end
245
+
246
+ end
247
+
248
+ end
249
+ end