coderay 0.9.8 → 1.0.0

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 (87) hide show
  1. data/{lib/README → README_INDEX.rdoc} +10 -21
  2. data/Rakefile +6 -6
  3. data/bin/coderay +193 -64
  4. data/lib/coderay.rb +61 -105
  5. data/lib/coderay/duo.rb +17 -21
  6. data/lib/coderay/encoder.rb +100 -112
  7. data/lib/coderay/encoders/_map.rb +12 -7
  8. data/lib/coderay/encoders/comment_filter.rb +12 -30
  9. data/lib/coderay/encoders/count.rb +29 -11
  10. data/lib/coderay/encoders/debug.rb +32 -20
  11. data/lib/coderay/encoders/div.rb +13 -9
  12. data/lib/coderay/encoders/filter.rb +34 -51
  13. data/lib/coderay/encoders/html.rb +155 -161
  14. data/lib/coderay/encoders/html/css.rb +4 -9
  15. data/lib/coderay/encoders/html/numbering.rb +115 -0
  16. data/lib/coderay/encoders/html/output.rb +22 -70
  17. data/lib/coderay/encoders/json.rb +59 -45
  18. data/lib/coderay/encoders/lines_of_code.rb +12 -57
  19. data/lib/coderay/encoders/null.rb +6 -14
  20. data/lib/coderay/encoders/page.rb +13 -9
  21. data/lib/coderay/encoders/span.rb +13 -9
  22. data/lib/coderay/encoders/statistic.rb +58 -39
  23. data/lib/coderay/encoders/terminal.rb +179 -0
  24. data/lib/coderay/encoders/text.rb +31 -17
  25. data/lib/coderay/encoders/token_kind_filter.rb +111 -0
  26. data/lib/coderay/encoders/xml.rb +19 -18
  27. data/lib/coderay/encoders/yaml.rb +37 -9
  28. data/lib/coderay/for_redcloth.rb +4 -4
  29. data/lib/coderay/helpers/file_type.rb +127 -246
  30. data/lib/coderay/helpers/gzip.rb +41 -0
  31. data/lib/coderay/helpers/plugin.rb +241 -306
  32. data/lib/coderay/helpers/word_list.rb +65 -126
  33. data/lib/coderay/scanner.rb +173 -156
  34. data/lib/coderay/scanners/_map.rb +18 -17
  35. data/lib/coderay/scanners/c.rb +63 -77
  36. data/lib/coderay/scanners/clojure.rb +217 -0
  37. data/lib/coderay/scanners/cpp.rb +71 -84
  38. data/lib/coderay/scanners/css.rb +103 -120
  39. data/lib/coderay/scanners/debug.rb +47 -44
  40. data/lib/coderay/scanners/delphi.rb +70 -76
  41. data/lib/coderay/scanners/diff.rb +141 -50
  42. data/lib/coderay/scanners/erb.rb +81 -0
  43. data/lib/coderay/scanners/groovy.rb +104 -113
  44. data/lib/coderay/scanners/haml.rb +168 -0
  45. data/lib/coderay/scanners/html.rb +181 -110
  46. data/lib/coderay/scanners/java.rb +73 -75
  47. data/lib/coderay/scanners/java/builtin_types.rb +2 -0
  48. data/lib/coderay/scanners/java_script.rb +90 -101
  49. data/lib/coderay/scanners/json.rb +40 -53
  50. data/lib/coderay/scanners/php.rb +123 -147
  51. data/lib/coderay/scanners/python.rb +93 -91
  52. data/lib/coderay/scanners/raydebug.rb +66 -0
  53. data/lib/coderay/scanners/ruby.rb +343 -326
  54. data/lib/coderay/scanners/ruby/patterns.rb +40 -106
  55. data/lib/coderay/scanners/ruby/string_state.rb +71 -0
  56. data/lib/coderay/scanners/sql.rb +80 -66
  57. data/lib/coderay/scanners/text.rb +26 -0
  58. data/lib/coderay/scanners/xml.rb +1 -1
  59. data/lib/coderay/scanners/yaml.rb +74 -73
  60. data/lib/coderay/style.rb +10 -7
  61. data/lib/coderay/styles/_map.rb +3 -3
  62. data/lib/coderay/styles/alpha.rb +143 -0
  63. data/lib/coderay/token_kinds.rb +90 -0
  64. data/lib/coderay/tokens.rb +102 -277
  65. data/lib/coderay/tokens_proxy.rb +55 -0
  66. data/lib/coderay/version.rb +3 -0
  67. data/test/functional/basic.rb +200 -18
  68. data/test/functional/examples.rb +130 -0
  69. data/test/functional/for_redcloth.rb +15 -8
  70. data/test/functional/suite.rb +9 -6
  71. metadata +103 -123
  72. data/FOLDERS +0 -53
  73. data/bin/coderay_stylesheet +0 -4
  74. data/lib/coderay/encoders/html/numerization.rb +0 -133
  75. data/lib/coderay/encoders/term.rb +0 -158
  76. data/lib/coderay/encoders/token_class_filter.rb +0 -84
  77. data/lib/coderay/helpers/gzip_simple.rb +0 -123
  78. data/lib/coderay/scanners/nitro_xhtml.rb +0 -136
  79. data/lib/coderay/scanners/plaintext.rb +0 -20
  80. data/lib/coderay/scanners/rhtml.rb +0 -78
  81. data/lib/coderay/scanners/scheme.rb +0 -145
  82. data/lib/coderay/styles/cycnus.rb +0 -152
  83. data/lib/coderay/styles/murphy.rb +0 -134
  84. data/lib/coderay/token_classes.rb +0 -86
  85. data/test/functional/load_plugin_scanner.rb +0 -11
  86. data/test/functional/vhdl.rb +0 -126
  87. data/test/functional/word_list.rb +0 -79
@@ -0,0 +1,55 @@
1
+ module CodeRay
2
+
3
+ # The result of a scan operation is a TokensProxy, but should act like Tokens.
4
+ #
5
+ # This proxy makes it possible to use the classic CodeRay.scan.encode API
6
+ # while still providing the benefits of direct streaming.
7
+ class TokensProxy
8
+
9
+ attr_accessor :input, :lang, :options, :block
10
+
11
+ # Create a new TokensProxy with the arguments of CodeRay.scan.
12
+ def initialize input, lang, options = {}, block = nil
13
+ @input = input
14
+ @lang = lang
15
+ @options = options
16
+ @block = block
17
+ end
18
+
19
+ # Call CodeRay.encode if +encoder+ is a Symbol;
20
+ # otherwise, convert the receiver to tokens and call encoder.encode_tokens.
21
+ def encode encoder, options = {}
22
+ if encoder.respond_to? :to_sym
23
+ CodeRay.encode(input, lang, encoder, options)
24
+ else
25
+ encoder.encode_tokens tokens, options
26
+ end
27
+ end
28
+
29
+ # Tries to call encode;
30
+ # delegates to tokens otherwise.
31
+ def method_missing method, *args, &blk
32
+ encode method.to_sym, *args
33
+ rescue PluginHost::PluginNotFound
34
+ tokens.send(method, *args, &blk)
35
+ end
36
+
37
+ # The (cached) result of the tokenized input; a Tokens instance.
38
+ def tokens
39
+ @tokens ||= scanner.tokenize(input)
40
+ end
41
+
42
+ # A (cached) scanner instance to use for the scan task.
43
+ def scanner
44
+ @scanner ||= CodeRay.scanner(lang, options, &block)
45
+ end
46
+
47
+ # Overwrite Struct#each.
48
+ def each *args, &blk
49
+ tokens.each(*args, &blk)
50
+ self
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,3 @@
1
+ module CodeRay
2
+ VERSION = '1.0.0'
3
+ end
@@ -1,11 +1,15 @@
1
+ # encoding: utf-8
1
2
  require 'test/unit'
3
+ require File.expand_path('../../lib/assert_warning', __FILE__)
4
+
5
+ $:.unshift File.expand_path('../../../lib', __FILE__)
2
6
  require 'coderay'
3
7
 
4
8
  class BasicTest < Test::Unit::TestCase
5
9
 
6
10
  def test_version
7
11
  assert_nothing_raised do
8
- assert_match(/\A\d\.\d\.\d\z/, CodeRay::VERSION)
12
+ assert_match(/\A\d\.\d\.\d?\z/, CodeRay::VERSION)
9
13
  end
10
14
  end
11
15
 
@@ -14,36 +18,60 @@ class BasicTest < Test::Unit::TestCase
14
18
  RUBY_TEST_TOKENS = [
15
19
  ['puts', :ident],
16
20
  [' ', :space],
17
- [:open, :string],
21
+ [:begin_group, :string],
18
22
  ['"', :delimiter],
19
23
  ['Hello, World!', :content],
20
24
  ['"', :delimiter],
21
- [:close, :string]
22
- ]
25
+ [:end_group, :string]
26
+ ].flatten
23
27
  def test_simple_scan
24
28
  assert_nothing_raised do
25
- assert_equal RUBY_TEST_TOKENS, CodeRay.scan(RUBY_TEST_CODE, :ruby).to_ary
29
+ assert_equal RUBY_TEST_TOKENS, CodeRay.scan(RUBY_TEST_CODE, :ruby).tokens
26
30
  end
27
31
  end
28
32
 
29
- RUBY_TEST_HTML = 'puts <span class="s"><span class="dl">&quot;</span>' +
30
- '<span class="k">Hello, World!</span><span class="dl">&quot;</span></span>'
33
+ RUBY_TEST_HTML = 'puts <span class="string"><span class="delimiter">&quot;</span>' +
34
+ '<span class="content">Hello, World!</span><span class="delimiter">&quot;</span></span>'
31
35
  def test_simple_highlight
32
36
  assert_nothing_raised do
33
37
  assert_equal RUBY_TEST_HTML, CodeRay.scan(RUBY_TEST_CODE, :ruby).html
34
38
  end
35
39
  end
36
40
 
41
+ def test_scan_file
42
+ CodeRay.scan_file __FILE__
43
+ end
44
+
45
+ def test_encode
46
+ assert_equal 1, CodeRay.encode('test', :python, :count)
47
+ end
48
+
49
+ def test_encode_tokens
50
+ assert_equal 1, CodeRay.encode_tokens(CodeRay::Tokens['test', :string], :count)
51
+ end
52
+
53
+ def test_encode_file
54
+ assert_equal File.read(__FILE__), CodeRay.encode_file(__FILE__, :text)
55
+ end
56
+
57
+ def test_highlight
58
+ assert_match '<pre>test</pre>', CodeRay.highlight('test', :python)
59
+ end
60
+
61
+ def test_highlight_file
62
+ assert_match "require <span class=\"string\"><span class=\"delimiter\">'</span><span class=\"content\">test/unit</span><span class=\"delimiter\">'</span></span>\n", CodeRay.highlight_file(__FILE__)
63
+ end
64
+
37
65
  def test_duo
38
66
  assert_equal(RUBY_TEST_CODE,
39
- CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE))
67
+ CodeRay::Duo[:plain, :text].highlight(RUBY_TEST_CODE))
40
68
  assert_equal(RUBY_TEST_CODE,
41
- CodeRay::Duo[:plain => :plain].highlight(RUBY_TEST_CODE))
69
+ CodeRay::Duo[:plain => :text].highlight(RUBY_TEST_CODE))
42
70
  end
43
71
 
44
72
  def test_duo_stream
45
73
  assert_equal(RUBY_TEST_CODE,
46
- CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE, :stream => true))
74
+ CodeRay::Duo[:plain, :text].highlight(RUBY_TEST_CODE, :stream => true))
47
75
  end
48
76
 
49
77
  def test_comment_filter
@@ -98,25 +126,179 @@ more code # and another comment, in-line.
98
126
  assert_equal 0, CodeRay.scan(rHTML, :html).lines_of_code
99
127
  assert_equal 0, CodeRay.scan(rHTML, :php).lines_of_code
100
128
  assert_equal 0, CodeRay.scan(rHTML, :yaml).lines_of_code
101
- assert_equal 4, CodeRay.scan(rHTML, :rhtml).lines_of_code
129
+ assert_equal 4, CodeRay.scan(rHTML, :erb).lines_of_code
102
130
  end
103
131
 
104
- def test_rubygems_not_loaded
105
- assert_equal nil, defined? Gem
106
- end if ENV['check_rubygems'] && RUBY_VERSION < '1.9'
107
-
108
132
  def test_list_of_encoders
109
133
  assert_kind_of(Array, CodeRay::Encoders.list)
110
- assert CodeRay::Encoders.list.include?('count')
134
+ assert CodeRay::Encoders.list.include?(:count)
111
135
  end
112
136
 
113
137
  def test_list_of_scanners
114
138
  assert_kind_of(Array, CodeRay::Scanners.list)
115
- assert CodeRay::Scanners.list.include?('plaintext')
139
+ assert CodeRay::Scanners.list.include?(:text)
140
+ end
141
+
142
+ def test_token_kinds
143
+ assert_kind_of Hash, CodeRay::TokenKinds
144
+ for kind, css_class in CodeRay::TokenKinds
145
+ assert_kind_of Symbol, kind
146
+ if css_class != false
147
+ assert_kind_of String, css_class, "TokenKinds[%p] == %p" % [kind, css_class]
148
+ end
149
+ end
150
+ assert_equal 'reserved', CodeRay::TokenKinds[:reserved]
151
+ assert_warning 'Undefined Token kind: :shibboleet' do
152
+ assert_equal false, CodeRay::TokenKinds[:shibboleet]
153
+ end
154
+ end
155
+
156
+ class Milk < CodeRay::Encoders::Encoder
157
+ FILE_EXTENSION = 'cocoa'
158
+ end
159
+
160
+ class HoneyBee < CodeRay::Encoders::Encoder
161
+ end
162
+
163
+ def test_encoder_file_extension
164
+ assert_nothing_raised do
165
+ assert_equal 'html', CodeRay::Encoders::Page::FILE_EXTENSION
166
+ assert_equal 'cocoa', Milk::FILE_EXTENSION
167
+ assert_equal 'cocoa', Milk.new.file_extension
168
+ assert_equal 'honeybee', HoneyBee::FILE_EXTENSION
169
+ assert_equal 'honeybee', HoneyBee.new.file_extension
170
+ end
171
+ assert_raise NameError do
172
+ HoneyBee::MISSING_CONSTANT
173
+ end
174
+ end
175
+
176
+ def test_encoder_tokens
177
+ encoder = CodeRay::Encoders::Encoder.new
178
+ encoder.send :setup, {}
179
+ assert_raise(ArgumentError) { encoder.token :strange, '' }
180
+ encoder.token 'test', :debug
181
+ end
182
+
183
+ def test_encoder_deprecated_interface
184
+ encoder = CodeRay::Encoders::Encoder.new
185
+ encoder.send :setup, {}
186
+ assert_warning 'Using old Tokens#<< interface.' do
187
+ encoder << ['test', :content]
188
+ end
189
+ assert_raise ArgumentError do
190
+ encoder << [:strange, :input]
191
+ end
192
+ assert_raise ArgumentError do
193
+ encoder.encode_tokens [['test', :token]]
194
+ end
195
+ end
196
+
197
+ def encoder_token_interface_deprecation_warning_given
198
+ CodeRay::Encoders::Encoder.send :class_variable_get, :@@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN
199
+ end
200
+
201
+ def test_scanner_file_extension
202
+ assert_equal 'rb', CodeRay::Scanners::Ruby.file_extension
203
+ assert_equal 'rb', CodeRay::Scanners::Ruby.new.file_extension
204
+ assert_equal 'java', CodeRay::Scanners::Java.file_extension
205
+ assert_equal 'java', CodeRay::Scanners::Java.new.file_extension
206
+ end
207
+
208
+ def test_scanner_lang
209
+ assert_equal :ruby, CodeRay::Scanners::Ruby.lang
210
+ assert_equal :ruby, CodeRay::Scanners::Ruby.new.lang
211
+ assert_equal :java, CodeRay::Scanners::Java.lang
212
+ assert_equal :java, CodeRay::Scanners::Java.new.lang
213
+ end
214
+
215
+ def test_scanner_tokenize
216
+ assert_equal ['foo', :plain], CodeRay::Scanners::Plain.new.tokenize('foo')
217
+ assert_equal [['foo', :plain], ['bar', :plain]], CodeRay::Scanners::Plain.new.tokenize(['foo', 'bar'])
218
+ CodeRay::Scanners::Plain.new.tokenize 42
219
+ end
220
+
221
+ def test_scanner_tokens
222
+ scanner = CodeRay::Scanners::Plain.new
223
+ scanner.tokenize('foo')
224
+ assert_equal ['foo', :plain], scanner.tokens
225
+ scanner.string = ''
226
+ assert_equal ['', :plain], scanner.tokens
227
+ end
228
+
229
+ def test_scanner_line_and_column
230
+ scanner = CodeRay::Scanners::Plain.new "foo\nbär+quux"
231
+ assert_equal 0, scanner.pos
232
+ assert_equal 1, scanner.line
233
+ assert_equal 1, scanner.column
234
+ scanner.scan(/foo/)
235
+ assert_equal 3, scanner.pos
236
+ assert_equal 1, scanner.line
237
+ assert_equal 4, scanner.column
238
+ scanner.scan(/\n/)
239
+ assert_equal 4, scanner.pos
240
+ assert_equal 2, scanner.line
241
+ assert_equal 1, scanner.column
242
+ scanner.scan(/b/)
243
+ assert_equal 5, scanner.pos
244
+ assert_equal 2, scanner.line
245
+ assert_equal 2, scanner.column
246
+ scanner.scan(/a/)
247
+ assert_equal 5, scanner.pos
248
+ assert_equal 2, scanner.line
249
+ assert_equal 2, scanner.column
250
+ scanner.scan(/ä/)
251
+ assert_equal 7, scanner.pos
252
+ assert_equal 2, scanner.line
253
+ assert_equal 4, scanner.column
254
+ scanner.scan(/r/)
255
+ assert_equal 8, scanner.pos
256
+ assert_equal 2, scanner.line
257
+ assert_equal 5, scanner.column
258
+ end
259
+
260
+ def test_scanner_use_subclasses
261
+ assert_raise NotImplementedError do
262
+ CodeRay::Scanners::Scanner.new
263
+ end
264
+ end
265
+
266
+ class InvalidScanner < CodeRay::Scanners::Scanner
267
+ end
268
+
269
+ def test_scanner_scan_tokens
270
+ assert_raise NotImplementedError do
271
+ InvalidScanner.new.tokenize ''
272
+ end
273
+ end
274
+
275
+ class RaisingScanner < CodeRay::Scanners::Scanner
276
+ def scan_tokens encoder, options
277
+ raise_inspect 'message', [], :initial
278
+ end
279
+ end
280
+
281
+ def test_scanner_raise_inspect
282
+ assert_raise CodeRay::Scanners::Scanner::ScanError do
283
+ RaisingScanner.new.tokenize ''
284
+ end
116
285
  end
117
286
 
118
287
  def test_scan_a_frozen_string
119
- CodeRay.scan RUBY_VERSION, :ruby
288
+ assert_nothing_raised do
289
+ CodeRay.scan RUBY_VERSION, :ruby
290
+ CodeRay.scan RUBY_VERSION, :plain
291
+ end
292
+ end
293
+
294
+ def test_scan_a_non_string
295
+ assert_nothing_raised do
296
+ CodeRay.scan 42, :ruby
297
+ CodeRay.scan nil, :ruby
298
+ CodeRay.scan self, :ruby
299
+ CodeRay.encode ENV.to_hash, :ruby, :page
300
+ CodeRay.highlight CodeRay, :plain
301
+ end
120
302
  end
121
303
 
122
304
  end
@@ -0,0 +1,130 @@
1
+ require 'test/unit'
2
+
3
+ $:.unshift File.expand_path('../../../lib', __FILE__)
4
+ require 'coderay'
5
+
6
+ class ExamplesTest < Test::Unit::TestCase
7
+
8
+ def test_examples
9
+ # output as HTML div (using inline CSS styles)
10
+ div = CodeRay.scan('puts "Hello, world!"', :ruby).div
11
+ assert_equal <<-DIV, div
12
+ <div class="CodeRay">
13
+ <div class="code"><pre>puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">Hello, world!</span><span style="color:#710">&quot;</span></span></pre></div>
14
+ </div>
15
+ DIV
16
+
17
+ # ...with line numbers
18
+ div = CodeRay.scan(<<-CODE.chomp, :ruby).div(:line_numbers => :table)
19
+ 5.times do
20
+ puts 'Hello, world!'
21
+ end
22
+ CODE
23
+ assert_equal <<-DIV, div
24
+ <table class="CodeRay"><tr>
25
+ <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
26
+ <a href="#n2" name="n2">2</a>
27
+ <a href="#n3" name="n3">3</a>
28
+ </pre></td>
29
+ <td class="code"><pre><span style="color:#00D">5</span>.times <span style="color:#080;font-weight:bold">do</span>
30
+ puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello, world!</span><span style="color:#710">'</span></span>
31
+ <span style="color:#080;font-weight:bold">end</span></pre></td>
32
+ </tr></table>
33
+ DIV
34
+
35
+ # output as standalone HTML page (using CSS classes)
36
+ page = CodeRay.scan('puts "Hello, world!"', :ruby).page
37
+ assert page[<<-PAGE]
38
+ <body style="background-color: white;">
39
+
40
+ <table class="CodeRay"><tr>
41
+ <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>
42
+ </pre></td>
43
+ <td class="code"><pre>puts <span class="string"><span class="delimiter">&quot;</span><span class="content">Hello, world!</span><span class="delimiter">&quot;</span></span></pre></td>
44
+ </tr></table>
45
+
46
+ </body>
47
+ PAGE
48
+
49
+ # keep scanned tokens for later use
50
+ tokens = CodeRay.scan('{ "just": "an", "example": 42 }', :json)
51
+ assert_kind_of CodeRay::TokensProxy, tokens
52
+
53
+ assert_equal ["{", :operator, " ", :space, :begin_group, :key,
54
+ "\"", :delimiter, "just", :content, "\"", :delimiter,
55
+ :end_group, :key, ":", :operator, " ", :space,
56
+ :begin_group, :string, "\"", :delimiter, "an", :content,
57
+ "\"", :delimiter, :end_group, :string, ",", :operator,
58
+ " ", :space, :begin_group, :key, "\"", :delimiter,
59
+ "example", :content, "\"", :delimiter, :end_group, :key,
60
+ ":", :operator, " ", :space, "42", :integer,
61
+ " ", :space, "}", :operator], tokens.tokens
62
+
63
+ # produce a token statistic
64
+ assert_equal <<-STATISTIC, tokens.statistic
65
+
66
+ Code Statistics
67
+
68
+ Tokens 26
69
+ Non-Whitespace 15
70
+ Bytes Total 31
71
+
72
+ Token Types (7):
73
+ type count ratio size (average)
74
+ -------------------------------------------------------------
75
+ TOTAL 26 100.00 % 1.2
76
+ delimiter 6 23.08 % 1.0
77
+ operator 5 19.23 % 1.0
78
+ space 5 19.23 % 1.0
79
+ key 4 15.38 % 0.0
80
+ :begin_group 3 11.54 % 0.0
81
+ :end_group 3 11.54 % 0.0
82
+ content 3 11.54 % 4.3
83
+ string 2 7.69 % 0.0
84
+ integer 1 3.85 % 2.0
85
+
86
+ STATISTIC
87
+
88
+ # count the tokens
89
+ assert_equal 26, tokens.count
90
+
91
+ # produce a HTML div, but with CSS classes
92
+ div = tokens.div(:css => :class)
93
+ assert_equal <<-DIV, div
94
+ <div class="CodeRay">
95
+ <div class="code"><pre>{ <span class="key"><span class="delimiter">&quot;</span><span class="content">just</span><span class="delimiter">&quot;</span></span>: <span class="string"><span class="delimiter">&quot;</span><span class="content">an</span><span class="delimiter">&quot;</span></span>, <span class="key"><span class="delimiter">&quot;</span><span class="content">example</span><span class="delimiter">&quot;</span></span>: <span class="integer">42</span> }</pre></div>
96
+ </div>
97
+ DIV
98
+
99
+ # highlight a file (HTML div); guess the file type base on the extension
100
+ require 'coderay/helpers/file_type'
101
+ assert_equal :ruby, CodeRay::FileType[__FILE__]
102
+
103
+ # get a new scanner for Python
104
+ python_scanner = CodeRay.scanner :python
105
+ assert_kind_of CodeRay::Scanners::Python, python_scanner
106
+
107
+ # get a new encoder for terminal
108
+ terminal_encoder = CodeRay.encoder :term
109
+ assert_kind_of CodeRay::Encoders::Terminal, terminal_encoder
110
+
111
+ # scanning into tokens
112
+ tokens = python_scanner.tokenize 'import this; # The Zen of Python'
113
+ assert_equal ["import", :keyword, " ", :space, "this", :include,
114
+ ";", :operator, " ", :space, "# The Zen of Python", :comment], tokens
115
+
116
+ # format the tokens
117
+ term = terminal_encoder.encode_tokens(tokens)
118
+ assert_equal "\e[1;31mimport\e[0m \e[33mthis\e[0m; \e[37m# The Zen of Python\e[0m", term
119
+
120
+ # re-using scanner and encoder
121
+ ruby_highlighter = CodeRay::Duo[:ruby, :div]
122
+ div = ruby_highlighter.encode('puts "Hello, world!"')
123
+ assert_equal <<-DIV, div
124
+ <div class="CodeRay">
125
+ <div class="code"><pre>puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">Hello, world!</span><span style="color:#710">&quot;</span></span></pre></div>
126
+ </div>
127
+ DIV
128
+ end
129
+
130
+ end