coderay 1.0.0.800pre → 1.0.0.815pre
Sign up to get free protection for your applications and to get access to all the features.
- data/{README.rdoc → README_INDEX.rdoc} +2 -0
- data/Rakefile +3 -3
- data/bin/coderay +31 -9
- data/lib/coderay.rb +2 -1
- data/lib/coderay/encoder.rb +11 -1
- data/lib/coderay/encoders/_map.rb +0 -1
- data/lib/coderay/encoders/count.rb +9 -3
- data/lib/coderay/encoders/debug.rb +1 -1
- data/lib/coderay/encoders/filter.rb +12 -6
- data/lib/coderay/encoders/html.rb +11 -1
- data/lib/coderay/encoders/html/css.rb +1 -1
- data/lib/coderay/encoders/html/output.rb +0 -1
- data/lib/coderay/encoders/json.rb +20 -7
- data/lib/coderay/encoders/lines_of_code.rb +2 -1
- data/lib/coderay/encoders/statistic.rb +5 -6
- data/lib/coderay/encoders/text.rb +8 -5
- data/lib/coderay/encoders/token_kind_filter.rb +1 -0
- data/lib/coderay/encoders/xml.rb +5 -3
- data/lib/coderay/encoders/yaml.rb +13 -8
- data/lib/coderay/helpers/file_type.rb +4 -4
- data/lib/coderay/helpers/plugin.rb +7 -5
- data/lib/coderay/scanner.rb +30 -18
- data/lib/coderay/scanners/_map.rb +14 -13
- data/lib/coderay/scanners/clojure.rb +1 -1
- data/lib/coderay/scanners/css.rb +36 -27
- data/lib/coderay/scanners/{rhtml.rb → erb.rb} +3 -3
- data/lib/coderay/scanners/groovy.rb +1 -1
- data/lib/coderay/scanners/java_script.rb +1 -1
- data/lib/coderay/scanners/php.rb +2 -2
- data/lib/coderay/scanners/ruby.rb +11 -6
- data/lib/coderay/tokens.rb +1 -3
- data/test/functional/basic.rb +26 -19
- data/test/functional/examples.rb +2 -0
- data/test/functional/for_redcloth.rb +12 -6
- data/test/functional/suite.rb +2 -1
- metadata +26 -9
- data/lib/coderay/scanners/nitro_xhtml.rb +0 -136
- data/lib/coderay/scanners/scheme.rb +0 -136
@@ -111,6 +111,8 @@ Where would we be without all those people?
|
|
111
111
|
less useless
|
112
112
|
* Term::ANSIColor[http://term-ansicolor.rubyforge.org/]
|
113
113
|
* PLEAC[http://pleac.sourceforge.net/] code examples
|
114
|
+
* Github
|
115
|
+
* Travis CI (http://travis-ci.org/rubychan/github)
|
114
116
|
|
115
117
|
=== Free
|
116
118
|
|
data/Rakefile
CHANGED
@@ -14,7 +14,7 @@ if File.directory? 'rake_tasks'
|
|
14
14
|
|
15
15
|
else
|
16
16
|
|
17
|
-
# fallback tasks when rake_tasks folder is not present
|
17
|
+
# fallback tasks when rake_tasks folder is not present (eg. in the distribution package)
|
18
18
|
desc 'Run CodeRay tests (basic)'
|
19
19
|
task :test do
|
20
20
|
ruby './test/functional/suite.rb'
|
@@ -26,9 +26,9 @@ else
|
|
26
26
|
desc 'Generate documentation for CodeRay'
|
27
27
|
Rake::RDocTask.new :doc do |rd|
|
28
28
|
rd.title = 'CodeRay Documentation'
|
29
|
-
rd.main = '
|
29
|
+
rd.main = 'README_INDEX.rdoc'
|
30
30
|
rd.rdoc_files.add Dir['lib']
|
31
|
-
rd.rdoc_files.add
|
31
|
+
rd.rdoc_files.add rd.main
|
32
32
|
rd.rdoc_dir = 'doc'
|
33
33
|
end
|
34
34
|
|
data/bin/coderay
CHANGED
@@ -105,23 +105,38 @@ when 'highlight', nil
|
|
105
105
|
if output_file
|
106
106
|
output_filetype ||= CodeRay::FileType[output_file]
|
107
107
|
else
|
108
|
-
output_filetype ||=
|
108
|
+
output_filetype ||= :term
|
109
109
|
end
|
110
110
|
|
111
|
+
output_filetype = :page if output_filetype.to_s == 'html'
|
112
|
+
|
111
113
|
if input_file
|
112
114
|
input = File.read input_file
|
113
115
|
else
|
114
116
|
input = $stdin.read
|
115
117
|
end
|
116
118
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
119
|
+
begin
|
120
|
+
file =
|
121
|
+
if output_file
|
122
|
+
File.open output_file, 'w'
|
123
|
+
else
|
124
|
+
$stdout.sync = true
|
125
|
+
$stdout
|
126
|
+
end
|
127
|
+
CodeRay.encode(input, input_filetype, output_filetype, :out => file)
|
128
|
+
file.puts
|
129
|
+
rescue CodeRay::PluginHost::PluginNotFound => boom
|
130
|
+
if boom.message[/CodeRay::(\w+)s could not load plugin :?(.*?): /]
|
131
|
+
puts "I don't know the #$1 \"#$2\"."
|
132
|
+
else
|
133
|
+
puts boom.message
|
122
134
|
end
|
123
|
-
|
124
|
-
|
135
|
+
# puts "I don't know this plugin: #{boom.message[/Could not load plugin (.*?): /, 1]}."
|
136
|
+
rescue CodeRay::Scanners::Scanner::ScanError # FIXME: rescue Errno::EPIPE
|
137
|
+
# ignore
|
138
|
+
ensure
|
139
|
+
file.close
|
125
140
|
end
|
126
141
|
end
|
127
142
|
when 'list'
|
@@ -151,6 +166,13 @@ when 'commands'
|
|
151
166
|
when 'help'
|
152
167
|
help
|
153
168
|
else
|
154
|
-
|
169
|
+
$stdout = $stderr
|
155
170
|
help
|
171
|
+
puts
|
172
|
+
if subcommand[/\A\w+\z/]
|
173
|
+
puts "Unknown command: #{subcommand}"
|
174
|
+
else
|
175
|
+
puts "File not found: #{subcommand}"
|
176
|
+
end
|
177
|
+
exit 1
|
156
178
|
end
|
data/lib/coderay.rb
CHANGED
@@ -158,6 +158,7 @@ module CodeRay
|
|
158
158
|
#
|
159
159
|
# See also demo/demo_simple.
|
160
160
|
def scan code, lang, options = {}, &block
|
161
|
+
# FIXME: return a proxy for direct-stream encoding
|
161
162
|
scanner = Scanners[lang].new code, options, &block
|
162
163
|
scanner.tokenize
|
163
164
|
end
|
@@ -187,7 +188,7 @@ module CodeRay
|
|
187
188
|
# encodes it with the Encoder for +format+.
|
188
189
|
# +options+ will be passed to the Encoder.
|
189
190
|
#
|
190
|
-
# See CodeRay::Encoder.encode
|
191
|
+
# See CodeRay::Encoder.encode.
|
191
192
|
def encode code, lang, format, options = {}
|
192
193
|
encoder(format, options).encode code, lang, options
|
193
194
|
end
|
data/lib/coderay/encoder.rb
CHANGED
@@ -153,7 +153,17 @@ module CodeRay
|
|
153
153
|
#
|
154
154
|
# See the HTML Encoder for an example of option caching.
|
155
155
|
def setup options
|
156
|
-
@out =
|
156
|
+
@out = get_output(options)
|
157
|
+
end
|
158
|
+
|
159
|
+
def get_output options
|
160
|
+
options[:out] || ''
|
161
|
+
end
|
162
|
+
|
163
|
+
# Append data.to_s to the output. Returns the argument.
|
164
|
+
def output data
|
165
|
+
@out << data.to_s
|
166
|
+
data
|
157
167
|
end
|
158
168
|
|
159
169
|
# Called with merged options after encoding starts.
|
@@ -11,17 +11,23 @@ module Encoders
|
|
11
11
|
protected
|
12
12
|
|
13
13
|
def setup options
|
14
|
-
|
14
|
+
super
|
15
|
+
|
16
|
+
@count = 0
|
17
|
+
end
|
18
|
+
|
19
|
+
def finish options
|
20
|
+
output @count
|
15
21
|
end
|
16
22
|
|
17
23
|
public
|
18
24
|
|
19
25
|
def text_token text, kind
|
20
|
-
@
|
26
|
+
@count += 1
|
21
27
|
end
|
22
28
|
|
23
29
|
def begin_group kind
|
24
|
-
@
|
30
|
+
@count += 1
|
25
31
|
end
|
26
32
|
alias end_group begin_group
|
27
33
|
alias begin_line begin_group
|
@@ -21,29 +21,35 @@ module Encoders
|
|
21
21
|
|
22
22
|
protected
|
23
23
|
def setup options
|
24
|
-
|
24
|
+
super
|
25
|
+
|
26
|
+
@tokens = options[:tokens] || Tokens.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def finish options
|
30
|
+
output @tokens
|
25
31
|
end
|
26
32
|
|
27
33
|
public
|
28
34
|
|
29
35
|
def text_token text, kind # :nodoc:
|
30
|
-
@
|
36
|
+
@tokens.text_token text, kind
|
31
37
|
end
|
32
38
|
|
33
39
|
def begin_group kind # :nodoc:
|
34
|
-
@
|
40
|
+
@tokens.begin_group kind
|
35
41
|
end
|
36
42
|
|
37
43
|
def begin_line kind # :nodoc:
|
38
|
-
@
|
44
|
+
@tokens.begin_line kind
|
39
45
|
end
|
40
46
|
|
41
47
|
def end_group kind # :nodoc:
|
42
|
-
@
|
48
|
+
@tokens.end_group kind
|
43
49
|
end
|
44
50
|
|
45
51
|
def end_line kind # :nodoc:
|
46
|
-
@
|
52
|
+
@tokens.end_line kind
|
47
53
|
end
|
48
54
|
|
49
55
|
end
|
@@ -164,6 +164,11 @@ module Encoders
|
|
164
164
|
def setup options
|
165
165
|
super
|
166
166
|
|
167
|
+
if options[:wrap] || options[:line_numbers]
|
168
|
+
@real_out = @out
|
169
|
+
@out = ''
|
170
|
+
end
|
171
|
+
|
167
172
|
@HTML_ESCAPE = HTML_ESCAPE.dup
|
168
173
|
@HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
|
169
174
|
|
@@ -214,7 +219,7 @@ module Encoders
|
|
214
219
|
|
215
220
|
def finish options
|
216
221
|
unless @opened.empty?
|
217
|
-
warn '%d tokens still open: %p' % [@opened.size, @opened]
|
222
|
+
warn '%d tokens still open: %p' % [@opened.size, @opened] if $CODERAY_DEBUG
|
218
223
|
@out << '</span>' while @opened.pop
|
219
224
|
@last_opened = nil
|
220
225
|
end
|
@@ -227,6 +232,11 @@ module Encoders
|
|
227
232
|
@out.wrap! options[:wrap]
|
228
233
|
@out.apply_title! options[:title]
|
229
234
|
|
235
|
+
if defined?(@real_out) && @real_out
|
236
|
+
@real_out << @out
|
237
|
+
@out = @real_out
|
238
|
+
end
|
239
|
+
|
230
240
|
super
|
231
241
|
end
|
232
242
|
|
@@ -36,32 +36,45 @@ module Encoders
|
|
36
36
|
|
37
37
|
protected
|
38
38
|
def setup options
|
39
|
-
|
39
|
+
super
|
40
|
+
|
41
|
+
@first = true
|
42
|
+
@out << '['
|
40
43
|
end
|
41
44
|
|
42
45
|
def finish options
|
43
|
-
@out
|
46
|
+
@out << ']'
|
47
|
+
end
|
48
|
+
|
49
|
+
def append data
|
50
|
+
if @first
|
51
|
+
@first = false
|
52
|
+
else
|
53
|
+
@out << ','
|
54
|
+
end
|
55
|
+
|
56
|
+
@out << data.to_json
|
44
57
|
end
|
45
58
|
|
46
59
|
public
|
47
60
|
def text_token text, kind
|
48
|
-
|
61
|
+
append :type => 'text', :text => text, :kind => kind
|
49
62
|
end
|
50
63
|
|
51
64
|
def begin_group kind
|
52
|
-
|
65
|
+
append :type => 'block', :action => 'open', :kind => kind
|
53
66
|
end
|
54
67
|
|
55
68
|
def end_group kind
|
56
|
-
|
69
|
+
append :type => 'block', :action => 'close', :kind => kind
|
57
70
|
end
|
58
71
|
|
59
72
|
def begin_line kind
|
60
|
-
|
73
|
+
append :type => 'block', :action => 'begin_line', :kind => kind
|
61
74
|
end
|
62
75
|
|
63
76
|
def end_line kind
|
64
|
-
|
77
|
+
append :type => 'block', :action => 'end_line', :kind => kind
|
65
78
|
end
|
66
79
|
|
67
80
|
end
|
@@ -15,15 +15,12 @@ module Encoders
|
|
15
15
|
protected
|
16
16
|
|
17
17
|
def setup options
|
18
|
+
super
|
19
|
+
|
18
20
|
@type_stats = Hash.new { |h, k| h[k] = TypeStats.new 0, 0 }
|
19
21
|
@real_token_count = 0
|
20
22
|
end
|
21
23
|
|
22
|
-
def generate tokens, options
|
23
|
-
@tokens = tokens
|
24
|
-
super
|
25
|
-
end
|
26
|
-
|
27
24
|
STATS = <<-STATS # :nodoc:
|
28
25
|
|
29
26
|
Code Statistics
|
@@ -51,11 +48,13 @@ Token Types (%d):
|
|
51
48
|
types_stats = @type_stats.sort_by { |k, v| [-v.count, k.to_s] }.map do |k, v|
|
52
49
|
TOKEN_TYPES_ROW % [k, v.count, 100.0 * v.count / all_count, v.size]
|
53
50
|
end.join
|
54
|
-
STATS % [
|
51
|
+
@out << STATS % [
|
55
52
|
all_count, @real_token_count, all_size,
|
56
53
|
@type_stats.delete_if { |k, v| k.is_a? String }.size,
|
57
54
|
types_stats
|
58
55
|
]
|
56
|
+
|
57
|
+
super
|
59
58
|
end
|
60
59
|
|
61
60
|
public
|
@@ -24,19 +24,22 @@ module Encoders
|
|
24
24
|
|
25
25
|
def text_token text, kind
|
26
26
|
super
|
27
|
-
|
27
|
+
|
28
|
+
if @first
|
29
|
+
@first = false
|
30
|
+
else
|
31
|
+
@out << @sep
|
32
|
+
end if @sep
|
28
33
|
end
|
29
34
|
|
30
35
|
protected
|
31
36
|
def setup options
|
32
37
|
super
|
38
|
+
|
39
|
+
@first = true
|
33
40
|
@sep = options[:separator]
|
34
41
|
end
|
35
42
|
|
36
|
-
def finish options
|
37
|
-
super.chomp @sep
|
38
|
-
end
|
39
|
-
|
40
43
|
end
|
41
44
|
|
42
45
|
end
|
data/lib/coderay/encoders/xml.rb
CHANGED
@@ -10,7 +10,7 @@ module Encoders
|
|
10
10
|
|
11
11
|
FILE_EXTENSION = 'xml'
|
12
12
|
|
13
|
-
|
13
|
+
autoload :REXML, 'rexml/document'
|
14
14
|
|
15
15
|
DEFAULT_OPTIONS = {
|
16
16
|
:tab_width => 8,
|
@@ -20,6 +20,8 @@ module Encoders
|
|
20
20
|
|
21
21
|
protected
|
22
22
|
def setup options
|
23
|
+
super
|
24
|
+
|
23
25
|
@doc = REXML::Document.new
|
24
26
|
@doc << REXML::XMLDecl.new
|
25
27
|
@tab_width = options[:tab_width]
|
@@ -27,9 +29,9 @@ module Encoders
|
|
27
29
|
end
|
28
30
|
|
29
31
|
def finish options
|
30
|
-
@out = ''
|
31
32
|
@doc.write @out, options[:pretty], options[:transitive], true
|
32
|
-
|
33
|
+
|
34
|
+
super
|
33
35
|
end
|
34
36
|
|
35
37
|
public
|
@@ -6,39 +6,44 @@ module Encoders
|
|
6
6
|
# Slow.
|
7
7
|
class YAML < Encoder
|
8
8
|
|
9
|
+
autoload :YAML, 'yaml'
|
10
|
+
|
9
11
|
register_for :yaml
|
10
12
|
|
11
13
|
FILE_EXTENSION = 'yaml'
|
12
14
|
|
13
15
|
protected
|
14
16
|
def setup options
|
15
|
-
|
16
|
-
|
17
|
+
super
|
18
|
+
|
19
|
+
@data = []
|
17
20
|
end
|
18
21
|
|
19
22
|
def finish options
|
20
|
-
@out
|
23
|
+
YAML.dump @data, @out
|
24
|
+
|
25
|
+
super
|
21
26
|
end
|
22
27
|
|
23
28
|
public
|
24
29
|
def text_token text, kind
|
25
|
-
@
|
30
|
+
@data << [text, kind]
|
26
31
|
end
|
27
32
|
|
28
33
|
def begin_group kind
|
29
|
-
@
|
34
|
+
@data << [:begin_group, kind]
|
30
35
|
end
|
31
36
|
|
32
37
|
def end_group kind
|
33
|
-
@
|
38
|
+
@data << [:end_group, kind]
|
34
39
|
end
|
35
40
|
|
36
41
|
def begin_line kind
|
37
|
-
@
|
42
|
+
@data << [:begin_line, kind]
|
38
43
|
end
|
39
44
|
|
40
45
|
def end_line kind
|
41
|
-
@
|
46
|
+
@data << [:end_line, kind]
|
42
47
|
end
|
43
48
|
|
44
49
|
end
|