coderay 1.0.0.800pre → 1.0.0.815pre

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 (38) hide show
  1. data/{README.rdoc → README_INDEX.rdoc} +2 -0
  2. data/Rakefile +3 -3
  3. data/bin/coderay +31 -9
  4. data/lib/coderay.rb +2 -1
  5. data/lib/coderay/encoder.rb +11 -1
  6. data/lib/coderay/encoders/_map.rb +0 -1
  7. data/lib/coderay/encoders/count.rb +9 -3
  8. data/lib/coderay/encoders/debug.rb +1 -1
  9. data/lib/coderay/encoders/filter.rb +12 -6
  10. data/lib/coderay/encoders/html.rb +11 -1
  11. data/lib/coderay/encoders/html/css.rb +1 -1
  12. data/lib/coderay/encoders/html/output.rb +0 -1
  13. data/lib/coderay/encoders/json.rb +20 -7
  14. data/lib/coderay/encoders/lines_of_code.rb +2 -1
  15. data/lib/coderay/encoders/statistic.rb +5 -6
  16. data/lib/coderay/encoders/text.rb +8 -5
  17. data/lib/coderay/encoders/token_kind_filter.rb +1 -0
  18. data/lib/coderay/encoders/xml.rb +5 -3
  19. data/lib/coderay/encoders/yaml.rb +13 -8
  20. data/lib/coderay/helpers/file_type.rb +4 -4
  21. data/lib/coderay/helpers/plugin.rb +7 -5
  22. data/lib/coderay/scanner.rb +30 -18
  23. data/lib/coderay/scanners/_map.rb +14 -13
  24. data/lib/coderay/scanners/clojure.rb +1 -1
  25. data/lib/coderay/scanners/css.rb +36 -27
  26. data/lib/coderay/scanners/{rhtml.rb → erb.rb} +3 -3
  27. data/lib/coderay/scanners/groovy.rb +1 -1
  28. data/lib/coderay/scanners/java_script.rb +1 -1
  29. data/lib/coderay/scanners/php.rb +2 -2
  30. data/lib/coderay/scanners/ruby.rb +11 -6
  31. data/lib/coderay/tokens.rb +1 -3
  32. data/test/functional/basic.rb +26 -19
  33. data/test/functional/examples.rb +2 -0
  34. data/test/functional/for_redcloth.rb +12 -6
  35. data/test/functional/suite.rb +2 -1
  36. metadata +26 -9
  37. data/lib/coderay/scanners/nitro_xhtml.rb +0 -136
  38. 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 = 'README.rdoc'
29
+ rd.main = 'README_INDEX.rdoc'
30
30
  rd.rdoc_files.add Dir['lib']
31
- rd.rdoc_files.add 'README.rdoc'
31
+ rd.rdoc_files.add rd.main
32
32
  rd.rdoc_dir = 'doc'
33
33
  end
34
34
 
@@ -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 ||= tty? ? :term : :page
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
- output = CodeRay.scan(input, input_filetype).encode(output_filetype)
118
-
119
- if output_file
120
- File.open output_file, 'w' do |file|
121
- file.puts output
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
- else
124
- puts output
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
- puts "Unknown command: #{subcommand}"
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
@@ -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
@@ -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.
@@ -3,7 +3,6 @@ module Encoders
3
3
 
4
4
  map \
5
5
  :loc => :lines_of_code,
6
- :html => :page,
7
6
  :plain => :text,
8
7
  :plaintext => :text,
9
8
  :remove_comments => :comment_filter,
@@ -11,17 +11,23 @@ module Encoders
11
11
  protected
12
12
 
13
13
  def setup options
14
- @out = 0
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
- @out += 1
26
+ @count += 1
21
27
  end
22
28
 
23
29
  def begin_group kind
24
- @out += 1
30
+ @count += 1
25
31
  end
26
32
  alias end_group begin_group
27
33
  alias begin_line begin_group
@@ -27,7 +27,7 @@ module Encoders
27
27
  if kind == :space
28
28
  @out << text
29
29
  else
30
- # FIXME: Escape (
30
+ # TODO: Escape (
31
31
  text = text.gsub(/[)\\]/, '\\\\\0') # escape ) and \
32
32
  @out << kind.to_s << '(' << text << ')'
33
33
  end
@@ -21,29 +21,35 @@ module Encoders
21
21
 
22
22
  protected
23
23
  def setup options
24
- @out = options[:tokens] || Tokens.new
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
- @out.text_token text, kind
36
+ @tokens.text_token text, kind
31
37
  end
32
38
 
33
39
  def begin_group kind # :nodoc:
34
- @out.begin_group kind
40
+ @tokens.begin_group kind
35
41
  end
36
42
 
37
43
  def begin_line kind # :nodoc:
38
- @out.begin_line kind
44
+ @tokens.begin_line kind
39
45
  end
40
46
 
41
47
  def end_group kind # :nodoc:
42
- @out.end_group kind
48
+ @tokens.end_group kind
43
49
  end
44
50
 
45
51
  def end_line kind # :nodoc:
46
- @out.end_line kind
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
 
@@ -44,7 +44,7 @@ module Encoders
44
44
  ( [^\}]+ )? # $2 = style
45
45
  \s* \} \s*
46
46
  |
47
- ( . ) # $3 = error
47
+ ( [^\n]+ ) # $3 = error
48
48
  /mx
49
49
  def parse stylesheet
50
50
  stylesheet.scan CSS_CLASS_PATTERN do |selectors, style, error|
@@ -55,7 +55,6 @@ module Encoders
55
55
  end
56
56
 
57
57
  def apply_title! title
58
- # FIXME: This may change the output!
59
58
  self.sub!(/(<title>)(<\/title>)/) { $1 + title + $2 }
60
59
  self
61
60
  end
@@ -36,32 +36,45 @@ module Encoders
36
36
 
37
37
  protected
38
38
  def setup options
39
- @out = []
39
+ super
40
+
41
+ @first = true
42
+ @out << '['
40
43
  end
41
44
 
42
45
  def finish options
43
- @out.to_json
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
- @out << { :type => 'text', :text => text, :kind => kind }
61
+ append :type => 'text', :text => text, :kind => kind
49
62
  end
50
63
 
51
64
  def begin_group kind
52
- @out << { :type => 'block', :action => 'open', :kind => kind }
65
+ append :type => 'block', :action => 'open', :kind => kind
53
66
  end
54
67
 
55
68
  def end_group kind
56
- @out << { :type => 'block', :action => 'close', :kind => kind }
69
+ append :type => 'block', :action => 'close', :kind => kind
57
70
  end
58
71
 
59
72
  def begin_line kind
60
- @out << { :type => 'block', :action => 'begin_line', :kind => kind }
73
+ append :type => 'block', :action => 'begin_line', :kind => kind
61
74
  end
62
75
 
63
76
  def end_line kind
64
- @out << { :type => 'block', :action => 'end_line', :kind => kind }
77
+ append :type => 'block', :action => 'end_line', :kind => kind
65
78
  end
66
79
 
67
80
  end
@@ -31,11 +31,12 @@ module Encoders
31
31
  end
32
32
 
33
33
  options[:exclude] = kinds_not_loc
34
+
34
35
  super options
35
36
  end
36
37
 
37
38
  def finish options
38
- @out.to_s.scan(NON_EMPTY_LINE).size
39
+ output @tokens.text.scan(NON_EMPTY_LINE).size
39
40
  end
40
41
 
41
42
  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
- @out << @sep if @sep
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
@@ -34,6 +34,7 @@ module Encoders
34
34
  protected
35
35
  def setup options
36
36
  super
37
+
37
38
  @group_excluded = false
38
39
  @exclude = options[:exclude]
39
40
  @exclude = Array(@exclude) unless @exclude == :all
@@ -10,7 +10,7 @@ module Encoders
10
10
 
11
11
  FILE_EXTENSION = 'xml'
12
12
 
13
- require 'rexml/document'
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
- @out
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
- require 'yaml'
16
- @out = []
17
+ super
18
+
19
+ @data = []
17
20
  end
18
21
 
19
22
  def finish options
20
- @out.to_a.to_yaml
23
+ YAML.dump @data, @out
24
+
25
+ super
21
26
  end
22
27
 
23
28
  public
24
29
  def text_token text, kind
25
- @out << [text, kind]
30
+ @data << [text, kind]
26
31
  end
27
32
 
28
33
  def begin_group kind
29
- @out << [:begin_group, kind]
34
+ @data << [:begin_group, kind]
30
35
  end
31
36
 
32
37
  def end_group kind
33
- @out << [:end_group, kind]
38
+ @data << [:end_group, kind]
34
39
  end
35
40
 
36
41
  def begin_line kind
37
- @out << [:begin_line, kind]
42
+ @data << [:begin_line, kind]
38
43
  end
39
44
 
40
45
  def end_line kind
41
- @out << [:end_line, kind]
46
+ @data << [:end_line, kind]
42
47
  end
43
48
 
44
49
  end