raldred-coderay 0.9.0 → 0.9.339

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 (58) hide show
  1. data/lib/README +128 -0
  2. data/lib/coderay.rb +319 -0
  3. data/lib/coderay/duo.rb +85 -0
  4. data/lib/coderay/encoder.rb +187 -0
  5. data/lib/coderay/encoders/_map.rb +9 -0
  6. data/lib/coderay/encoders/count.rb +21 -0
  7. data/lib/coderay/encoders/debug.rb +49 -0
  8. data/lib/coderay/encoders/div.rb +20 -0
  9. data/lib/coderay/encoders/html.rb +306 -0
  10. data/lib/coderay/encoders/html/css.rb +70 -0
  11. data/lib/coderay/encoders/html/numerization.rb +133 -0
  12. data/lib/coderay/encoders/html/output.rb +206 -0
  13. data/lib/coderay/encoders/json.rb +19 -0
  14. data/lib/coderay/encoders/null.rb +26 -0
  15. data/lib/coderay/encoders/page.rb +21 -0
  16. data/lib/coderay/encoders/span.rb +20 -0
  17. data/lib/coderay/encoders/statistic.rb +77 -0
  18. data/lib/coderay/encoders/term.rb +114 -0
  19. data/lib/coderay/encoders/text.rb +32 -0
  20. data/lib/coderay/encoders/tokens.rb +44 -0
  21. data/lib/coderay/encoders/xml.rb +71 -0
  22. data/lib/coderay/encoders/yaml.rb +22 -0
  23. data/lib/coderay/for_redcloth.rb +73 -0
  24. data/lib/coderay/helpers/file_type.rb +226 -0
  25. data/lib/coderay/helpers/gzip_simple.rb +123 -0
  26. data/lib/coderay/helpers/plugin.rb +339 -0
  27. data/lib/coderay/helpers/word_list.rb +124 -0
  28. data/lib/coderay/scanner.rb +271 -0
  29. data/lib/coderay/scanners/_map.rb +21 -0
  30. data/lib/coderay/scanners/c.rb +166 -0
  31. data/lib/coderay/scanners/css.rb +202 -0
  32. data/lib/coderay/scanners/debug.rb +61 -0
  33. data/lib/coderay/scanners/delphi.rb +150 -0
  34. data/lib/coderay/scanners/diff.rb +104 -0
  35. data/lib/coderay/scanners/groovy.rb +271 -0
  36. data/lib/coderay/scanners/html.rb +175 -0
  37. data/lib/coderay/scanners/java.rb +173 -0
  38. data/lib/coderay/scanners/java/builtin_types.rb +419 -0
  39. data/lib/coderay/scanners/java_script.rb +195 -0
  40. data/lib/coderay/scanners/json.rb +107 -0
  41. data/lib/coderay/scanners/nitro_xhtml.rb +132 -0
  42. data/lib/coderay/scanners/php.rb +404 -0
  43. data/lib/coderay/scanners/plaintext.rb +18 -0
  44. data/lib/coderay/scanners/python.rb +232 -0
  45. data/lib/coderay/scanners/rhtml.rb +71 -0
  46. data/lib/coderay/scanners/ruby.rb +386 -0
  47. data/lib/coderay/scanners/ruby/patterns.rb +232 -0
  48. data/lib/coderay/scanners/scheme.rb +142 -0
  49. data/lib/coderay/scanners/sql.rb +162 -0
  50. data/lib/coderay/scanners/xml.rb +17 -0
  51. data/lib/coderay/scanners/yaml.rb +142 -0
  52. data/lib/coderay/style.rb +20 -0
  53. data/lib/coderay/styles/_map.rb +7 -0
  54. data/lib/coderay/styles/cycnus.rb +151 -0
  55. data/lib/coderay/styles/murphy.rb +132 -0
  56. data/lib/coderay/token_classes.rb +86 -0
  57. data/lib/coderay/tokens.rb +387 -0
  58. metadata +59 -1
data/lib/README ADDED
@@ -0,0 +1,128 @@
1
+ = CodeRay
2
+
3
+ [- Tired of blue'n'gray? Try the original version of this documentation on
4
+ coderay.rubychan.de[http://coderay.rubychan.de/doc/] (use Ctrl+Click to open it in its own frame.) -]
5
+
6
+ == About
7
+ CodeRay is a Ruby library for syntax highlighting.
8
+
9
+ Syntax highlighting means: You put your code in, and you get it back colored;
10
+ Keywords, strings, floats, comments - all in different colors.
11
+ And with line numbers.
12
+
13
+ *Syntax* *Highlighting*...
14
+ * makes code easier to read and maintain
15
+ * lets you detect syntax errors faster
16
+ * helps you to understand the syntax of a language
17
+ * looks nice
18
+ * is what everybody should have on their website
19
+ * solves all your problems and makes the girls run after you
20
+
21
+ Version: 0.8.1
22
+ Author:: murphy (Kornelius Kalnbach)
23
+ Contact:: murphy rubychan de
24
+ Website:: coderay.rubychan.de[http://coderay.rubychan.de]
25
+ License:: GNU LGPL; see LICENSE file in the main directory.
26
+
27
+ == Installation
28
+
29
+ You need RubyGems[http://rubyforge.org/frs/?group_id=126].
30
+
31
+ % gem install coderay
32
+
33
+
34
+ === Dependencies
35
+
36
+ CodeRay needs Ruby 1.8.6 or later. It also runs with Ruby 1.9.1+ and JRuby 1.1+.
37
+
38
+
39
+ == Example Usage
40
+ (Forgive me, but this is not highlighted.)
41
+
42
+ require 'coderay'
43
+
44
+ tokens = CodeRay.scan "puts 'Hello, world!'", :ruby
45
+ page = tokens.html :line_numbers => :inline, :wrap => :page
46
+ puts page
47
+
48
+
49
+ == Documentation
50
+
51
+ See CodeRay.
52
+
53
+ Please report errors in this documentation to <murphy rubychan de>.
54
+
55
+
56
+ == Credits
57
+
58
+ === Special Thanks to
59
+
60
+ * licenser (Heinz N. Gies) for ending my QBasic career, inventing the Coder
61
+ project and the input/output plugin system.
62
+ CodeRay would not exist without him.
63
+ * bovi (Daniel Bovensiepen) for helping me out on various occasions.
64
+
65
+ === Thanks to
66
+
67
+ * Caleb Clausen for writing RubyLexer (see
68
+ http://rubyforge.org/projects/rubylexer) and lots of very interesting mail
69
+ traffic
70
+ * birkenfeld (Georg Brandl) and mitsuhiku (Arnim Ronacher) for PyKleur, now pygments.
71
+ You guys rock!
72
+ * Jamis Buck for writing Syntax (see http://rubyforge.org/projects/syntax)
73
+ I got some useful ideas from it.
74
+ * Doug Kearns and everyone else who worked on ruby.vim - it not only helped me
75
+ coding CodeRay, but also gave me a wonderful target to reach for the Ruby
76
+ scanner.
77
+ * everyone who uses CodeBB on http://www.rubyforen.de and http://www.python-forum.de
78
+ * iGEL, magichisoka, manveru, WoNáDo and everyone I forgot from rubyforen.de
79
+ * Dethix from ruby-mine.de
80
+ * zickzackw
81
+ * Dookie (who is no longer with us...) and Leonidas from http://www.python-forum.de
82
+ * Andreas Schwarz for finding out that CaseIgnoringWordList was not case
83
+ ignoring! Such things really make you write tests.
84
+ * closure for the first version of the Scheme scanner.
85
+ * Stefan Walk for the first version of the JavaScript scanner.
86
+ * Josh Goebel for another version of the JavaScript scanner and a Diff scanner.
87
+ * Jonathan Younger for pointing out the licence confusion caused by wrong LICENSE file.
88
+ * Jeremy Hinegardner for finding the shebang-on-empty-file bug in FileType.
89
+ * Charles Oliver Nutter and Yehuda Katz for helping me benchmark CodeRay on JRuby.
90
+ * Andreas Neuhaus for pointing out a markup bug in coderay/for_redcloth.
91
+ * The folks at redmine.org - thank you for using and fixing CodeRay!
92
+ * matz and all Ruby gods and gurus
93
+ * The inventors of: the computer, the internet, the true color display, HTML &
94
+ CSS, VIM, RUBY, pizza, microwaves, guitars, scouting, programming, anime,
95
+ manga, coke and green ice tea.
96
+
97
+ Where would we be without all those people?
98
+
99
+ === Created using
100
+
101
+ * Ruby[http://ruby-lang.org/]
102
+ * Chihiro (my Sony VAIO laptop); Henrietta (my old MacBook);
103
+ Triella, born Rico (my new MacBook); as well as
104
+ Seras and Hikari (my PCs)
105
+ * RDE[http://homepage2.nifty.com/sakazuki/rde_e.html],
106
+ VIM[http://vim.org] and TextMate[http://macromates.com]
107
+ * Subversion[http://subversion.tigris.org/]
108
+ * Redmine[http://redmine.org/]
109
+ * Firefox[http://www.mozilla.org/products/firefox/],
110
+ Firebug[http://getfirebug.com/], and
111
+ Thunderbird[http://www.mozilla.org/products/thunderbird/]
112
+ * RubyGems[http://docs.rubygems.org/] and Rake[http://rake.rubyforge.org/]
113
+ * TortoiseSVN[http://tortoisesvn.tigris.org/] using Apache via
114
+ XAMPP[http://www.apachefriends.org/en/xampp.html]
115
+ * RDoc (though I'm quite unsatisfied with it)
116
+ * Microsoft Windows (yes, I confess!) and MacOS X
117
+ * GNUWin32, MinGW and some other tools to make the shell under windows a bit
118
+ less useless
119
+ * Term::ANSIColor[http://term-ansicolor.rubyforge.org/]
120
+ * PLEAC[http://pleac.sourceforge.net/] code examples
121
+
122
+ === Free
123
+
124
+ * As you can see, CodeRay was created under heavy use of *free* software.
125
+ * So CodeRay is also *free*.
126
+ * If you use CodeRay to create software, think about making this software
127
+ *free*, too.
128
+ * Thanks :)
data/lib/coderay.rb ADDED
@@ -0,0 +1,319 @@
1
+ # = CodeRay Library
2
+ #
3
+ # CodeRay is a Ruby library for syntax highlighting.
4
+ #
5
+ # I try to make CodeRay easy to use and intuitive, but at the same time fully featured, complete,
6
+ # fast and efficient.
7
+ #
8
+ # See README.
9
+ #
10
+ # It consists mainly of
11
+ # * the main engine: CodeRay (Scanners::Scanner, Tokens/TokenStream, Encoders::Encoder), PluginHost
12
+ # * the scanners in CodeRay::Scanners
13
+ # * the encoders in CodeRay::Encoders
14
+ #
15
+ # Here's a fancy graphic to light up this gray docu:
16
+ #
17
+ # http://rd.cYcnus.de/coderay/scheme.png
18
+ #
19
+ # == Documentation
20
+ #
21
+ # See CodeRay, Encoders, Scanners, Tokens.
22
+ #
23
+ # == Usage
24
+ #
25
+ # Remember you need RubyGems to use CodeRay, unless you have it in your load path. Run Ruby with
26
+ # -rubygems option if required.
27
+ #
28
+ # === Highlight Ruby code in a string as html
29
+ #
30
+ # require 'coderay'
31
+ # print CodeRay.scan('puts "Hello, world!"', :ruby).html
32
+ #
33
+ # # prints something like this:
34
+ # puts <span class="s">&quot;Hello, world!&quot;</span>
35
+ #
36
+ #
37
+ # === Highlight C code from a file in a html div
38
+ #
39
+ # require 'coderay'
40
+ # print CodeRay.scan(File.read('ruby.h'), :c).div
41
+ # print CodeRay.scan_file('ruby.h').html.div
42
+ #
43
+ # You can include this div in your page. The used CSS styles can be printed with
44
+ #
45
+ # % coderay_stylesheet
46
+ #
47
+ # === Highlight without typing too much
48
+ #
49
+ # If you are one of the hasty (or lazy, or extremely curious) people, just run this file:
50
+ #
51
+ # % ruby -rubygems /path/to/coderay/coderay.rb > example.html
52
+ #
53
+ # and look at the file it created in your browser.
54
+ #
55
+ # = CodeRay Module
56
+ #
57
+ # The CodeRay module provides convenience methods for the engine.
58
+ #
59
+ # * The +lang+ and +format+ arguments select Scanner and Encoder to use. These are
60
+ # simply lower-case symbols, like <tt>:python</tt> or <tt>:html</tt>.
61
+ # * All methods take an optional hash as last parameter, +options+, that is send to
62
+ # the Encoder / Scanner.
63
+ # * Input and language are always sorted in this order: +code+, +lang+.
64
+ # (This is in alphabetical order, if you need a mnemonic ;)
65
+ #
66
+ # You should be able to highlight everything you want just using these methods;
67
+ # so there is no need to dive into CodeRay's deep class hierarchy.
68
+ #
69
+ # The examples in the demo directory demonstrate common cases using this interface.
70
+ #
71
+ # = Basic Access Ways
72
+ #
73
+ # Read this to get a general view what CodeRay provides.
74
+ #
75
+ # == Scanning
76
+ #
77
+ # Scanning means analysing an input string, splitting it up into Tokens.
78
+ # Each Token knows about what type it is: string, comment, class name, etc.
79
+ #
80
+ # Each +lang+ (language) has its own Scanner; for example, <tt>:ruby</tt> code is
81
+ # handled by CodeRay::Scanners::Ruby.
82
+ #
83
+ # CodeRay.scan:: Scan a string in a given language into Tokens.
84
+ # This is the most common method to use.
85
+ # CodeRay.scan_file:: Scan a file and guess the language using FileType.
86
+ #
87
+ # The Tokens object you get from these methods can encode itself; see Tokens.
88
+ #
89
+ # == Encoding
90
+ #
91
+ # Encoding means compiling Tokens into an output. This can be colored HTML or
92
+ # LaTeX, a textual statistic or just the number of non-whitespace tokens.
93
+ #
94
+ # Each Encoder provides output in a specific +format+, so you select Encoders via
95
+ # formats like <tt>:html</tt> or <tt>:statistic</tt>.
96
+ #
97
+ # CodeRay.encode:: Scan and encode a string in a given language.
98
+ # CodeRay.encode_tokens:: Encode the given tokens.
99
+ # CodeRay.encode_file:: Scan a file, guess the language using FileType and encode it.
100
+ #
101
+ # == Streaming
102
+ #
103
+ # Streaming saves RAM by running Scanner and Encoder in some sort of
104
+ # pipe mode; see TokenStream.
105
+ #
106
+ # CodeRay.scan_stream:: Scan in stream mode.
107
+ #
108
+ # == All-in-One Encoding
109
+ #
110
+ # CodeRay.encode:: Highlight a string with a given input and output format.
111
+ #
112
+ # == Instanciating
113
+ #
114
+ # You can use an Encoder instance to highlight multiple inputs. This way, the setup
115
+ # for this Encoder must only be done once.
116
+ #
117
+ # CodeRay.encoder:: Create an Encoder instance with format and options.
118
+ # CodeRay.scanner:: Create an Scanner instance for lang, with '' as default code.
119
+ #
120
+ # To make use of CodeRay.scanner, use CodeRay::Scanner::code=.
121
+ #
122
+ # The scanning methods provide more flexibility; we recommend to use these.
123
+ #
124
+ # == Reusing Scanners and Encoders
125
+ #
126
+ # If you want to re-use scanners and encoders (because that is faster), see
127
+ # CodeRay::Duo for the most convenient (and recommended) interface.
128
+ module CodeRay
129
+
130
+ # Version: Major.Minor.Teeny[.Revision]
131
+ # Major: 0 for pre-stable, 1 for stable
132
+ # Minor: feature milestone
133
+ # Teeny: development state, 0 for pre-release
134
+ # Revision: Subversion Revision number (generated on rake gem:make)
135
+ VERSION = '0.9.0'
136
+
137
+ require 'coderay/tokens'
138
+ require 'coderay/scanner'
139
+ require 'coderay/encoder'
140
+ require 'coderay/duo'
141
+ require 'coderay/style'
142
+
143
+
144
+ class << self
145
+
146
+ # Scans the given +code+ (a String) with the Scanner for +lang+.
147
+ #
148
+ # This is a simple way to use CodeRay. Example:
149
+ # require 'coderay'
150
+ # page = CodeRay.scan("puts 'Hello, world!'", :ruby).html
151
+ #
152
+ # See also demo/demo_simple.
153
+ def scan code, lang, options = {}, &block
154
+ scanner = Scanners[lang].new code, options, &block
155
+ scanner.tokenize
156
+ end
157
+
158
+ # Scans +filename+ (a path to a code file) with the Scanner for +lang+.
159
+ #
160
+ # If +lang+ is :auto or omitted, the CodeRay::FileType module is used to
161
+ # determine it. If it cannot find out what type it is, it uses
162
+ # CodeRay::Scanners::Plaintext.
163
+ #
164
+ # Calls CodeRay.scan.
165
+ #
166
+ # Example:
167
+ # require 'coderay'
168
+ # page = CodeRay.scan_file('some_c_code.c').html
169
+ def scan_file filename, lang = :auto, options = {}, &block
170
+ file = IO.read filename
171
+ if lang == :auto
172
+ require 'coderay/helpers/file_type'
173
+ lang = FileType.fetch filename, :plaintext, true
174
+ end
175
+ scan file, lang, options = {}, &block
176
+ end
177
+
178
+ # Scan the +code+ (a string) with the scanner for +lang+.
179
+ #
180
+ # Calls scan.
181
+ #
182
+ # See CodeRay.scan.
183
+ def scan_stream code, lang, options = {}, &block
184
+ options[:stream] = true
185
+ scan code, lang, options, &block
186
+ end
187
+
188
+ # Encode a string in Streaming mode.
189
+ #
190
+ # This starts scanning +code+ with the the Scanner for +lang+
191
+ # while encodes the output with the Encoder for +format+.
192
+ # +options+ will be passed to the Encoder.
193
+ #
194
+ # See CodeRay::Encoder.encode_stream
195
+ def encode_stream code, lang, format, options = {}
196
+ encoder(format, options).encode_stream code, lang, options
197
+ end
198
+
199
+ # Encode a string.
200
+ #
201
+ # This scans +code+ with the the Scanner for +lang+ and then
202
+ # encodes it with the Encoder for +format+.
203
+ # +options+ will be passed to the Encoder.
204
+ #
205
+ # See CodeRay::Encoder.encode
206
+ def encode code, lang, format, options = {}
207
+ encoder(format, options).encode code, lang, options
208
+ end
209
+
210
+ # Highlight a string into a HTML <div>.
211
+ #
212
+ # CSS styles use classes, so you have to include a stylesheet
213
+ # in your output.
214
+ #
215
+ # See encode.
216
+ def highlight code, lang, options = { :css => :class }, format = :div
217
+ encode code, lang, format, options
218
+ end
219
+
220
+ # Encode pre-scanned Tokens.
221
+ # Use this together with CodeRay.scan:
222
+ #
223
+ # require 'coderay'
224
+ #
225
+ # # Highlight a short Ruby code example in a HTML span
226
+ # tokens = CodeRay.scan '1 + 2', :ruby
227
+ # puts CodeRay.encode_tokens(tokens, :span)
228
+ #
229
+ def encode_tokens tokens, format, options = {}
230
+ encoder(format, options).encode_tokens tokens, options
231
+ end
232
+
233
+ # Encodes +filename+ (a path to a code file) with the Scanner for +lang+.
234
+ #
235
+ # See CodeRay.scan_file.
236
+ # Notice that the second argument is the output +format+, not the input language.
237
+ #
238
+ # Example:
239
+ # require 'coderay'
240
+ # page = CodeRay.encode_file 'some_c_code.c', :html
241
+ def encode_file filename, format, options = {}
242
+ tokens = scan_file filename, :auto, get_scanner_options(options)
243
+ encode_tokens tokens, format, options
244
+ end
245
+
246
+ # Highlight a file into a HTML <div>.
247
+ #
248
+ # CSS styles use classes, so you have to include a stylesheet
249
+ # in your output.
250
+ #
251
+ # See encode.
252
+ def highlight_file filename, options = { :css => :class }, format = :div
253
+ encode_file filename, format, options
254
+ end
255
+
256
+ # Finds the Encoder class for +format+ and creates an instance, passing
257
+ # +options+ to it.
258
+ #
259
+ # Example:
260
+ # require 'coderay'
261
+ #
262
+ # stats = CodeRay.encoder(:statistic)
263
+ # stats.encode("puts 17 + 4\n", :ruby)
264
+ #
265
+ # puts '%d out of %d tokens have the kind :integer.' % [
266
+ # stats.type_stats[:integer].count,
267
+ # stats.real_token_count
268
+ # ]
269
+ # #-> 2 out of 4 tokens have the kind :integer.
270
+ def encoder format, options = {}
271
+ Encoders[format].new options
272
+ end
273
+
274
+ # Finds the Scanner class for +lang+ and creates an instance, passing
275
+ # +options+ to it.
276
+ #
277
+ # See Scanner.new.
278
+ def scanner lang, options = {}
279
+ Scanners[lang].new '', options
280
+ end
281
+
282
+ # Extract the options for the scanner from the +options+ hash.
283
+ #
284
+ # Returns an empty Hash if <tt>:scanner_options</tt> is not set.
285
+ #
286
+ # This is used if a method like CodeRay.encode has to provide options
287
+ # for Encoder _and_ scanner.
288
+ def get_scanner_options options
289
+ options.fetch :scanner_options, {}
290
+ end
291
+
292
+ end
293
+
294
+ # This Exception is raised when you try to stream with something that is not
295
+ # capable of streaming.
296
+ class NotStreamableError < Exception
297
+ def initialize obj
298
+ @obj = obj
299
+ end
300
+
301
+ def to_s
302
+ '%s is not Streamable!' % @obj.class
303
+ end
304
+ end
305
+
306
+ # A dummy module that is included by subclasses of CodeRay::Scanner an CodeRay::Encoder
307
+ # to show that they are able to handle streams.
308
+ module Streamable
309
+ end
310
+
311
+ end
312
+
313
+ # Run a test script.
314
+ if $0 == __FILE__
315
+ $stderr.print 'Press key to print demo.'; gets
316
+ # Just use this file as an example of Ruby code.
317
+ code = File.read(__FILE__)[/module CodeRay.*/m]
318
+ print CodeRay.scan(code, :ruby).html
319
+ end
@@ -0,0 +1,85 @@
1
+ module CodeRay
2
+
3
+ # = Duo
4
+ #
5
+ # A Duo is a convenient way to use CodeRay. You just create a Duo,
6
+ # giving it a lang (language of the input code) and a format (desired
7
+ # output format), and call Duo#highlight with the code.
8
+ #
9
+ # Duo makes it easy to re-use both scanner and encoder for a repetitive
10
+ # task. It also provides a very easy interface syntax:
11
+ #
12
+ # require 'coderay'
13
+ # CodeRay::Duo[:python, :div].highlight 'import this'
14
+ #
15
+ # Until you want to do uncommon things with CodeRay, I recommend to use
16
+ # this method, since it takes care of everything.
17
+ class Duo
18
+
19
+ attr_accessor :lang, :format, :options
20
+
21
+ # Create a new Duo, holding a lang and a format to highlight code.
22
+ #
23
+ # simple:
24
+ # CodeRay::Duo[:ruby, :page].highlight 'bla 42'
25
+ #
26
+ # streaming:
27
+ # CodeRay::Duo[:ruby, :page].highlight 'bar 23', :stream => true
28
+ #
29
+ # with options:
30
+ # CodeRay::Duo[:ruby, :html, :hint => :debug].highlight '????::??'
31
+ #
32
+ # alternative syntax without options:
33
+ # CodeRay::Duo[:ruby => :statistic].encode 'class << self; end'
34
+ #
35
+ # alternative syntax with options:
36
+ # CodeRay::Duo[{ :ruby => :statistic }, :do => :something].encode 'abc'
37
+ #
38
+ # The options are forwarded to scanner and encoder
39
+ # (see CodeRay.get_scanner_options).
40
+ def initialize lang = nil, format = nil, options = {}
41
+ if format == nil and lang.is_a? Hash and lang.size == 1
42
+ @lang = lang.keys.first
43
+ @format = lang[@lang]
44
+ else
45
+ @lang = lang
46
+ @format = format
47
+ end
48
+ @options = options
49
+ end
50
+
51
+ class << self
52
+ # To allow calls like Duo[:ruby, :html].highlight.
53
+ alias [] new
54
+ end
55
+
56
+ # The scanner of the duo. Only created once.
57
+ def scanner
58
+ @scanner ||= CodeRay.scanner @lang, CodeRay.get_scanner_options(@options)
59
+ end
60
+
61
+ # The encoder of the duo. Only created once.
62
+ def encoder
63
+ @encoder ||= CodeRay.encoder @format, @options
64
+ end
65
+
66
+ # Tokenize and highlight the code using +scanner+ and +encoder+.
67
+ #
68
+ # If the :stream option is set, the Duo will go into streaming mode,
69
+ # saving memory for the cost of time.
70
+ def encode code, options = { :stream => false }
71
+ stream = options.delete :stream
72
+ options = @options.merge options
73
+ if stream
74
+ encoder.encode_stream(code, @lang, options)
75
+ else
76
+ scanner.code = code
77
+ encoder.encode_tokens(scanner.tokenize, options)
78
+ end
79
+ end
80
+ alias highlight encode
81
+
82
+ end
83
+
84
+ end
85
+