coderay 0.7.1.147 → 0.7.2.165
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.
- data/bin/coderay +54 -56
- data/demo/suite.rb +54 -54
- data/lib/coderay.rb +187 -187
- data/lib/coderay/duo.rb +29 -29
- data/lib/coderay/encoder.rb +173 -173
- data/lib/coderay/encoders/_map.rb +8 -8
- data/lib/coderay/encoders/count.rb +21 -21
- data/lib/coderay/encoders/debug.rb +46 -46
- data/lib/coderay/encoders/div.rb +20 -20
- data/lib/coderay/encoders/html.rb +249 -245
- data/lib/coderay/encoders/html/classes.rb +73 -73
- data/lib/coderay/encoders/html/css.rb +65 -65
- data/lib/coderay/encoders/html/numerization.rb +122 -122
- data/lib/coderay/encoders/html/output.rb +195 -195
- data/lib/coderay/encoders/null.rb +26 -26
- data/lib/coderay/encoders/page.rb +21 -21
- data/lib/coderay/encoders/span.rb +20 -20
- data/lib/coderay/encoders/statistic.rb +81 -81
- data/lib/coderay/encoders/text.rb +33 -33
- data/lib/coderay/encoders/tokens.rb +44 -44
- data/lib/coderay/encoders/xml.rb +71 -71
- data/lib/coderay/encoders/yaml.rb +22 -22
- data/lib/coderay/helpers/filetype.rb +152 -153
- data/lib/coderay/helpers/gzip_simple.rb +67 -68
- data/lib/coderay/helpers/plugin.rb +297 -297
- data/lib/coderay/helpers/word_list.rb +46 -47
- data/lib/coderay/scanner.rb +238 -238
- data/lib/coderay/scanners/_map.rb +15 -14
- data/lib/coderay/scanners/c.rb +163 -155
- data/lib/coderay/scanners/delphi.rb +131 -129
- data/lib/coderay/scanners/html.rb +174 -167
- data/lib/coderay/scanners/nitro_xhtml.rb +130 -0
- data/lib/coderay/scanners/plaintext.rb +15 -15
- data/lib/coderay/scanners/rhtml.rb +73 -65
- data/lib/coderay/scanners/ruby.rb +404 -397
- data/lib/coderay/scanners/ruby/patterns.rb +216 -216
- data/lib/coderay/scanners/xml.rb +18 -18
- data/lib/coderay/style.rb +20 -20
- data/lib/coderay/styles/_map.rb +3 -3
- data/lib/coderay/styles/cycnus.rb +18 -18
- data/lib/coderay/styles/murphy.rb +18 -18
- data/lib/coderay/tokens.rb +322 -322
- metadata +86 -86
- data/lib/coderay/scanners/nitro_html.rb +0 -125
- data/lib/coderay/scanners/yaml.rb +0 -85
data/bin/coderay
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# CodeRay Executable
|
3
|
-
#
|
3
|
+
#
|
4
4
|
# Version: 0.1
|
5
5
|
# Author: murphy
|
6
6
|
|
7
7
|
def err msg
|
8
|
-
|
8
|
+
$stderr.puts msg
|
9
9
|
end
|
10
10
|
|
11
11
|
begin
|
12
|
-
|
12
|
+
require 'coderay'
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
if ARGV.empty?
|
15
|
+
puts <<-USAGE
|
16
16
|
CodeRay #{CodeRay::Version} (http://rd.cYcnus.de/coderay)
|
17
17
|
Usage:
|
18
18
|
coderay -<lang> [-<format>] < file > output
|
@@ -20,60 +20,58 @@ Usage:
|
|
20
20
|
Example:
|
21
21
|
coderay -ruby -statistic < foo.rb
|
22
22
|
coderay codegen.c # generates codegen.c.html
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
first, second = ARGV
|
23
|
+
USAGE
|
24
|
+
end
|
27
25
|
|
28
|
-
|
29
|
-
if first[/-(\w+)/] == first
|
30
|
-
lang = $1.to_sym
|
31
|
-
input = $stdin.read
|
32
|
-
tokens = CodeRay.scan input, lang
|
33
|
-
elsif first == '-'
|
34
|
-
lang = $1.to_sym
|
35
|
-
input = $stdin.read
|
36
|
-
tokens = CodeRay.scan input, lang
|
37
|
-
else
|
38
|
-
file = first
|
39
|
-
tokens = CodeRay.scan_file file
|
40
|
-
output_filename, output_ext = file, /#{Regexp.escape(File.extname(file))}$/
|
41
|
-
end
|
42
|
-
else
|
43
|
-
puts 'No lang/file given.'
|
44
|
-
exit 1
|
45
|
-
end
|
26
|
+
first, second = ARGV
|
46
27
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
28
|
+
if first
|
29
|
+
if first[/-(\w+)/] == first
|
30
|
+
lang = $1.to_sym
|
31
|
+
input = $stdin.read
|
32
|
+
tokens = CodeRay.scan input, lang
|
33
|
+
elsif first == '-'
|
34
|
+
lang = $1.to_sym
|
35
|
+
input = $stdin.read
|
36
|
+
tokens = CodeRay.scan input, lang
|
37
|
+
else
|
38
|
+
file = first
|
39
|
+
tokens = CodeRay.scan_file file
|
40
|
+
output_filename, output_ext = file, /#{Regexp.escape(File.extname(file))}$/
|
41
|
+
end
|
42
|
+
else
|
43
|
+
puts 'No lang/file given.'
|
44
|
+
exit 1
|
45
|
+
end
|
57
46
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
47
|
+
if second
|
48
|
+
if second[/-(\w+)/] == second
|
49
|
+
format = $1.to_sym
|
50
|
+
else
|
51
|
+
raise 'Invalid format (must be -xxx).'
|
52
|
+
end
|
53
|
+
else
|
54
|
+
$stderr.puts 'No format given; setting to default (HTML Page)'
|
55
|
+
format = :page
|
56
|
+
end
|
57
|
+
|
58
|
+
output = tokens.encode format
|
59
|
+
out = $stdout
|
60
|
+
if output_filename
|
61
|
+
output_filename += '.' + CodeRay::Encoders[format]::FILE_EXTENSION
|
62
|
+
if File.exist? output_filename
|
63
|
+
err 'File %s already exists.' % output_filename
|
64
|
+
exit
|
65
|
+
else
|
66
|
+
out = File.open output_filename, 'w'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
out.print output
|
72
70
|
|
73
71
|
rescue => boom
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
72
|
+
err "Error: #{boom.message}\n"
|
73
|
+
err boom.backtrace
|
74
|
+
err '-' * 50
|
75
|
+
err ARGV.options
|
76
|
+
exit 1
|
79
77
|
end
|
data/demo/suite.rb
CHANGED
@@ -7,46 +7,46 @@ require 'test/unit'
|
|
7
7
|
include Test::Unit
|
8
8
|
|
9
9
|
class CodeRaySuite < TestCase
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
10
|
+
|
11
|
+
def self.dir &block
|
12
|
+
@dir ||= File.dirname(__FILE__)
|
13
|
+
if block
|
14
|
+
Dir.chdir @dir, &block
|
15
|
+
end
|
16
|
+
@dir
|
17
|
+
end
|
18
|
+
|
19
|
+
def dir &block
|
20
|
+
self.class.dir(&block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_ALL
|
24
|
+
dir do
|
25
|
+
for input in Dir["demo_*.rb"] - %w(demo_server.rb demo_stream.rb)
|
26
|
+
puts "[ testing #{input}... ]"
|
27
|
+
name = File.basename(input, ".rb")
|
28
|
+
output = name + '.out'
|
29
|
+
code = File.open(input, 'rb') { |f| break f.read }
|
30
|
+
|
31
|
+
result = `ruby -wI../lib #{input}`
|
32
|
+
|
33
|
+
if File.exist? output
|
34
|
+
expected = File.read output
|
35
|
+
ok = expected == result
|
36
|
+
computed = output.sub('.out', '.computed')
|
37
|
+
unless ok
|
38
|
+
File.open(computed, 'w') { |f| f.write result }
|
39
|
+
print `gvimdiff #{output} #{computed}` if $DEBUG
|
40
|
+
end
|
41
|
+
assert(ok, "Output error: #{computed} != #{output}") unless $DEBUG
|
42
|
+
else
|
43
|
+
File.open(output, 'w') do |f| f.write result end
|
44
|
+
puts "New test: #{output}"
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
50
|
|
51
51
|
end
|
52
52
|
|
@@ -55,28 +55,28 @@ $suite = TestSuite.new 'CodeRay Demos Test'
|
|
55
55
|
$suite << CodeRaySuite.suite
|
56
56
|
|
57
57
|
def load_suite name
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
begin
|
59
|
+
require name + '/suite.rb'
|
60
|
+
rescue LoadError
|
61
|
+
$stderr.puts <<-ERR
|
62
62
|
|
63
63
|
!! Folder #{File.split(__FILE__).first + '/' + name} not found
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
64
|
+
|
65
|
+
ERR
|
66
|
+
false
|
67
|
+
end
|
68
68
|
end
|
69
69
|
|
70
70
|
if subsuite = ARGV.find { |a| break $1 if a[/^([^-].*)/] }
|
71
|
-
|
71
|
+
load_suite(subsuite) or exit
|
72
72
|
else
|
73
|
-
|
73
|
+
Dir[mydir + '/*/'].each { |suite| load_suite suite }
|
74
74
|
end
|
75
75
|
|
76
76
|
if ARGV.include? '-f'
|
77
|
-
|
78
|
-
|
77
|
+
require 'test/unit/ui/fox/testrunner'
|
78
|
+
UI::Fox::TestRunner.run $suite
|
79
79
|
else
|
80
|
-
|
81
|
-
|
80
|
+
require 'test/unit/ui/console/testrunner'
|
81
|
+
UI::Console::TestRunner.run $suite
|
82
82
|
end
|
data/lib/coderay.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# = CodeRay Library
|
2
2
|
#
|
3
|
-
# $Id: coderay.rb
|
3
|
+
# $Id: coderay.rb 162 2006-07-11 06:03:43Z murphy $
|
4
4
|
#
|
5
5
|
# CodeRay is a Ruby library for syntax highlighting.
|
6
6
|
#
|
@@ -67,24 +67,24 @@
|
|
67
67
|
# * All methods take an optional hash as last parameter, +options+, that is send to
|
68
68
|
# the Encoder / Scanner.
|
69
69
|
# * Input and language are always sorted in this order: +code+, +lang+.
|
70
|
-
#
|
70
|
+
# (This is in alphabetical order, if you need a mnemonic ;)
|
71
71
|
#
|
72
72
|
# You should be able to highlight everything you want just using these methods;
|
73
73
|
# so there is no need to dive into CodeRay's deep class hierarchy.
|
74
74
|
#
|
75
75
|
# The examples in the demo directory demonstrate common cases using this interface.
|
76
|
-
#
|
76
|
+
#
|
77
77
|
# = Basic Access Ways
|
78
78
|
#
|
79
79
|
# Read this to get a general view what CodeRay provides.
|
80
80
|
#
|
81
81
|
# == Scanning
|
82
|
-
#
|
83
|
-
#
|
84
|
-
#
|
82
|
+
#
|
83
|
+
# Scanning means analysing an input string, splitting it up into Tokens.
|
84
|
+
# Each Token knows about what type it is: string, comment, class name, etc.
|
85
85
|
#
|
86
|
-
#
|
87
|
-
#
|
86
|
+
# Each +lang+ (language) has its own Scanner; for example, <tt>:ruby</tt> code is
|
87
|
+
# handled by CodeRay::Scanners::Ruby.
|
88
88
|
#
|
89
89
|
# CodeRay.scan:: Scan a string in a given language into Tokens.
|
90
90
|
# This is the most common method to use.
|
@@ -108,212 +108,212 @@
|
|
108
108
|
#
|
109
109
|
# Streaming saves RAM by running Scanner and Encoder in some sort of
|
110
110
|
# pipe mode; see TokenStream.
|
111
|
-
#
|
111
|
+
#
|
112
112
|
# CodeRay.scan_stream:: Scan in stream mode.
|
113
|
-
#
|
114
|
-
#
|
113
|
+
#
|
114
|
+
# == All-in-One Encoding
|
115
115
|
#
|
116
116
|
# CodeRay.encode:: Highlight a string with a given input and output format.
|
117
117
|
#
|
118
118
|
# == Instanciating
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#
|
122
|
-
#
|
119
|
+
#
|
120
|
+
# You can use an Encoder instance to highlight multiple inputs. This way, the setup
|
121
|
+
# for this Encoder must only be done once.
|
122
|
+
#
|
123
123
|
# CodeRay.encoder:: Create an Encoder instance with format and options.
|
124
124
|
#
|
125
125
|
# There is no CodeRay.scanner method because Scanners are bound to an input string
|
126
126
|
# on creation; you can't re-use them with another string.
|
127
127
|
#
|
128
|
-
#
|
128
|
+
# The scanning methods provide more flexibility; we recommend to use these.
|
129
129
|
module CodeRay
|
130
|
-
|
131
|
-
# Version: Major.Minor.Teeny[.Revision]
|
132
|
-
# Major: 0 for pre-release
|
133
|
-
# Minor: odd for beta, even for stable
|
134
|
-
# Teeny: development state
|
135
|
-
# Revision: Subversion Revision number (generated on rake)
|
136
|
-
Version = '0.7.1'
|
137
|
-
|
138
|
-
require 'coderay/tokens'
|
139
|
-
require 'coderay/scanner'
|
140
|
-
require 'coderay/encoder'
|
141
|
-
require 'coderay/duo'
|
142
|
-
require 'coderay/style'
|
143
130
|
|
131
|
+
# Version: Major.Minor.Teeny[.Revision]
|
132
|
+
# Major: 0 for pre-release
|
133
|
+
# Minor: odd for beta, even for stable
|
134
|
+
# Teeny: development state
|
135
|
+
# Revision: Subversion Revision number (generated on rake)
|
136
|
+
Version = '0.7.2'
|
137
|
+
|
138
|
+
require 'coderay/tokens'
|
139
|
+
require 'coderay/scanner'
|
140
|
+
require 'coderay/encoder'
|
141
|
+
require 'coderay/duo'
|
142
|
+
require 'coderay/style'
|
143
|
+
|
144
|
+
|
145
|
+
class << self
|
146
|
+
|
147
|
+
# Scans the given +code+ (a String) with the Scanner for +lang+.
|
148
|
+
#
|
149
|
+
# This is a simple way to use CodeRay. Example:
|
150
|
+
# require 'coderay'
|
151
|
+
# page = CodeRay.scan("puts 'Hello, world!'", :ruby).html
|
152
|
+
#
|
153
|
+
# See also demo/demo_simple.
|
154
|
+
def scan code, lang, options = {}, &block
|
155
|
+
scanner = Scanners[lang].new code, options, &block
|
156
|
+
scanner.tokenize
|
157
|
+
end
|
158
|
+
|
159
|
+
# Scans +filename+ (a path to a code file) with the Scanner for +lang+.
|
160
|
+
#
|
161
|
+
# If +lang+ is :auto or omitted, the CodeRay::FileType module is used to
|
162
|
+
# determine it. If it cannot find out what type it is, it uses
|
163
|
+
# CodeRay::Scanners::Plaintext.
|
164
|
+
#
|
165
|
+
# Calls CodeRay.scan.
|
166
|
+
#
|
167
|
+
# Example:
|
168
|
+
# require 'coderay'
|
169
|
+
# page = CodeRay.scan_file('some_c_code.c').html
|
170
|
+
def scan_file filename, lang = :auto, options = {}, &block
|
171
|
+
file = IO.read filename
|
172
|
+
if lang == :auto
|
173
|
+
require 'coderay/helpers/filetype'
|
174
|
+
lang = FileType.fetch filename, :plaintext, true
|
175
|
+
end
|
176
|
+
scan file, lang, options = {}, &block
|
177
|
+
end
|
178
|
+
|
179
|
+
# Scan the +code+ (a string) with the scanner for +lang+.
|
180
|
+
#
|
181
|
+
# Calls scan.
|
182
|
+
#
|
183
|
+
# See CodeRay.scan.
|
184
|
+
def scan_stream code, lang, options = {}, &block
|
185
|
+
options[:stream] = true
|
186
|
+
scan code, lang, options, &block
|
187
|
+
end
|
188
|
+
|
189
|
+
# Encode a string in Streaming mode.
|
190
|
+
#
|
191
|
+
# This starts scanning +code+ with the the Scanner for +lang+
|
192
|
+
# while encodes the output with the Encoder for +format+.
|
193
|
+
# +options+ will be passed to the Encoder.
|
194
|
+
#
|
195
|
+
# See CodeRay::Encoder.encode_stream
|
196
|
+
def encode_stream code, lang, format, options = {}
|
197
|
+
encoder(format, options).encode_stream code, lang, options
|
198
|
+
end
|
144
199
|
|
145
|
-
|
200
|
+
# Encode a string.
|
201
|
+
#
|
202
|
+
# This scans +code+ with the the Scanner for +lang+ and then
|
203
|
+
# encodes it with the Encoder for +format+.
|
204
|
+
# +options+ will be passed to the Encoder.
|
205
|
+
#
|
206
|
+
# See CodeRay::Encoder.encode
|
207
|
+
def encode code, lang, format, options = {}
|
208
|
+
encoder(format, options).encode code, lang, options
|
209
|
+
end
|
146
210
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
scanner.tokenize
|
157
|
-
end
|
211
|
+
# Highlight a string into a HTML <div>.
|
212
|
+
#
|
213
|
+
# CSS styles use classes, so you have to include a stylesheet
|
214
|
+
# in your output.
|
215
|
+
#
|
216
|
+
# See encode.
|
217
|
+
def highlight code, lang, options = { :css => :class }, format = :div
|
218
|
+
encode code, lang, format, options
|
219
|
+
end
|
158
220
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
file = IO.read filename
|
172
|
-
if lang == :auto
|
173
|
-
require 'coderay/helpers/filetype'
|
174
|
-
lang = FileType.fetch filename, :plaintext, true
|
175
|
-
end
|
176
|
-
scan file, lang, options = {}, &block
|
177
|
-
end
|
221
|
+
# Encode pre-scanned Tokens.
|
222
|
+
# Use this together with CodeRay.scan:
|
223
|
+
#
|
224
|
+
# require 'coderay'
|
225
|
+
#
|
226
|
+
# # Highlight a short Ruby code example in a HTML span
|
227
|
+
# tokens = CodeRay.scan '1 + 2', :ruby
|
228
|
+
# puts CodeRay.encode_tokens(tokens, :span)
|
229
|
+
#
|
230
|
+
def encode_tokens tokens, format, options = {}
|
231
|
+
encoder(format, options).encode_tokens tokens, options
|
232
|
+
end
|
178
233
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
234
|
+
# Encodes +filename+ (a path to a code file) with the Scanner for +lang+.
|
235
|
+
#
|
236
|
+
# See CodeRay.scan_file.
|
237
|
+
# Notice that the second argument is the output +format+, not the input language.
|
238
|
+
#
|
239
|
+
# Example:
|
240
|
+
# require 'coderay'
|
241
|
+
# page = CodeRay.encode_file 'some_c_code.c', :html
|
242
|
+
def encode_file filename, format, options = {}
|
243
|
+
tokens = scan_file filename, :auto, get_scanner_options(options)
|
244
|
+
encode_tokens tokens, format, options
|
245
|
+
end
|
188
246
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
end
|
247
|
+
# Highlight a file into a HTML <div>.
|
248
|
+
#
|
249
|
+
# CSS styles use classes, so you have to include a stylesheet
|
250
|
+
# in your output.
|
251
|
+
#
|
252
|
+
# See encode.
|
253
|
+
def highlight_file filename, options = { :css => :class }, format = :div
|
254
|
+
encode_file filename, format, options
|
255
|
+
end
|
199
256
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
def highlight code, lang, options = { :css => :class }, format = :div
|
218
|
-
encode code, lang, format, options
|
219
|
-
end
|
220
|
-
|
221
|
-
# Encode pre-scanned Tokens.
|
222
|
-
# Use this together with CodeRay.scan:
|
223
|
-
#
|
224
|
-
# require 'coderay'
|
225
|
-
#
|
226
|
-
# # Highlight a short Ruby code example in a HTML span
|
227
|
-
# tokens = CodeRay.scan '1 + 2', :ruby
|
228
|
-
# puts CodeRay.encode_tokens(tokens, :span)
|
229
|
-
#
|
230
|
-
def encode_tokens tokens, format, options = {}
|
231
|
-
encoder(format, options).encode_tokens tokens, options
|
232
|
-
end
|
257
|
+
# Finds the Encoder class for +format+ and creates an instance, passing
|
258
|
+
# +options+ to it.
|
259
|
+
#
|
260
|
+
# Example:
|
261
|
+
# require 'coderay'
|
262
|
+
#
|
263
|
+
# stats = CodeRay.encoder(:statistic)
|
264
|
+
# stats.encode("puts 17 + 4\n", :ruby)
|
265
|
+
#
|
266
|
+
# puts '%d out of %d tokens have the kind :integer.' % [
|
267
|
+
# stats.type_stats[:integer].count,
|
268
|
+
# stats.real_token_count
|
269
|
+
# ]
|
270
|
+
# #-> 2 out of 4 tokens have the kind :integer.
|
271
|
+
def encoder format, options = {}
|
272
|
+
Encoders[format].new options
|
273
|
+
end
|
233
274
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
# page = CodeRay.encode_file 'some_c_code.c', :html
|
242
|
-
def encode_file filename, format, options = {}
|
243
|
-
tokens = scan_file filename, :auto, get_scanner_options(options)
|
244
|
-
encode_tokens tokens, format, options
|
245
|
-
end
|
275
|
+
# Finds the Scanner class for +lang+ and creates an instance, passing
|
276
|
+
# +options+ to it.
|
277
|
+
#
|
278
|
+
# See Scanner.new.
|
279
|
+
def scanner lang, options = {}
|
280
|
+
Scanners[lang].new '', options
|
281
|
+
end
|
246
282
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
# Finds the Encoder class for +format+ and creates an instance, passing
|
258
|
-
# +options+ to it.
|
259
|
-
#
|
260
|
-
# Example:
|
261
|
-
# require 'coderay'
|
262
|
-
#
|
263
|
-
# stats = CodeRay.encoder(:statistic)
|
264
|
-
# stats.encode("puts 17 + 4\n", :ruby)
|
265
|
-
#
|
266
|
-
# puts '%d out of %d tokens have the kind :integer.' % [
|
267
|
-
# stats.type_stats[:integer].count,
|
268
|
-
# stats.real_token_count
|
269
|
-
# ]
|
270
|
-
# #-> 2 out of 4 tokens have the kind :integer.
|
271
|
-
def encoder format, options = {}
|
272
|
-
Encoders[format].new options
|
273
|
-
end
|
283
|
+
# Extract the options for the scanner from the +options+ hash.
|
284
|
+
#
|
285
|
+
# Returns an empty Hash if <tt>:scanner_options</tt> is not set.
|
286
|
+
#
|
287
|
+
# This is used if a method like CodeRay.encode has to provide options
|
288
|
+
# for Encoder _and_ scanner.
|
289
|
+
def get_scanner_options options
|
290
|
+
options.fetch :scanner_options, {}
|
291
|
+
end
|
274
292
|
|
275
|
-
|
276
|
-
# +options+ to it.
|
277
|
-
#
|
278
|
-
# See Scanner.new.
|
279
|
-
def scanner lang, options = {}
|
280
|
-
Scanners[lang].new '', options
|
281
|
-
end
|
293
|
+
end
|
282
294
|
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
def get_scanner_options options
|
290
|
-
options.fetch :scanner_options, {}
|
291
|
-
end
|
295
|
+
# This Exception is raised when you try to stream with something that is not
|
296
|
+
# capable of streaming.
|
297
|
+
class NotStreamableError < Exception
|
298
|
+
def initialize obj
|
299
|
+
@obj = obj
|
300
|
+
end
|
292
301
|
|
293
|
-
|
302
|
+
def to_s
|
303
|
+
'%s is not Streamable!' % @obj.class
|
304
|
+
end
|
305
|
+
end
|
294
306
|
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
@obj = obj
|
300
|
-
end
|
307
|
+
# A dummy module that is included by subclasses of CodeRay::Scanner an CodeRay::Encoder
|
308
|
+
# to show that they are able to handle streams.
|
309
|
+
module Streamable
|
310
|
+
end
|
301
311
|
|
302
|
-
def to_s
|
303
|
-
'%s is not Streamable!' % @obj.class
|
304
|
-
end
|
305
|
-
end
|
306
|
-
|
307
|
-
# A dummy module that is included by subclasses of CodeRay::Scanner an CodeRay::Encoder
|
308
|
-
# to show that they are able to handle streams.
|
309
|
-
module Streamable
|
310
|
-
end
|
311
|
-
|
312
312
|
end
|
313
313
|
|
314
314
|
# Run a test script.
|
315
315
|
if $0 == __FILE__
|
316
|
-
|
317
|
-
|
318
|
-
|
316
|
+
$stderr.print 'Press key to print demo.'; gets
|
317
|
+
code = File.read($0)[/module CodeRay.*/m]
|
318
|
+
print CodeRay.scan(code, :ruby).html
|
319
319
|
end
|