coderay 1.0.0.738.pre → 1.0.0.778.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/{lib/README → README.rdoc} +0 -0
  2. data/Rakefile +5 -5
  3. data/lib/coderay.rb +2 -2
  4. data/lib/coderay/encoder.rb +4 -4
  5. data/lib/coderay/encoders/_map.rb +3 -1
  6. data/lib/coderay/encoders/html.rb +9 -17
  7. data/lib/coderay/encoders/html/numbering.rb +5 -5
  8. data/lib/coderay/encoders/html/output.rb +3 -3
  9. data/lib/coderay/encoders/lines_of_code.rb +6 -9
  10. data/lib/coderay/encoders/statistic.rb +26 -25
  11. data/lib/coderay/encoders/terminal.rb +2 -2
  12. data/lib/coderay/encoders/text.rb +9 -9
  13. data/lib/coderay/encoders/xml.rb +8 -8
  14. data/lib/coderay/encoders/yaml.rb +5 -6
  15. data/lib/coderay/for_redcloth.rb +1 -1
  16. data/lib/coderay/helpers/file_type.rb +44 -42
  17. data/lib/coderay/helpers/plugin.rb +9 -17
  18. data/lib/coderay/helpers/word_list.rb +65 -126
  19. data/lib/coderay/scanner.rb +13 -5
  20. data/lib/coderay/scanners/_map.rb +3 -2
  21. data/lib/coderay/scanners/c.rb +5 -5
  22. data/lib/coderay/scanners/clojure.rb +27 -14
  23. data/lib/coderay/scanners/cpp.rb +5 -5
  24. data/lib/coderay/scanners/css.rb +1 -1
  25. data/lib/coderay/scanners/html.rb +60 -29
  26. data/lib/coderay/scanners/java.rb +2 -2
  27. data/lib/coderay/scanners/java_script.rb +1 -1
  28. data/lib/coderay/scanners/nitro_xhtml.rb +1 -1
  29. data/lib/coderay/scanners/php.rb +4 -4
  30. data/lib/coderay/scanners/python.rb +2 -2
  31. data/lib/coderay/scanners/rhtml.rb +1 -1
  32. data/lib/coderay/scanners/ruby.rb +4 -11
  33. data/lib/coderay/scanners/ruby/patterns.rb +1 -1
  34. data/lib/coderay/scanners/scheme.rb +2 -2
  35. data/lib/coderay/scanners/sql.rb +26 -19
  36. data/lib/coderay/scanners/text.rb +26 -0
  37. data/lib/coderay/styles/alpha.rb +7 -6
  38. data/lib/coderay/token_kinds.rb +5 -5
  39. data/test/functional/basic.rb +1 -1
  40. data/test/functional/examples.rb +5 -3
  41. metadata +85 -85
  42. data/lib/coderay/scanners/plaintext.rb +0 -26
File without changes
data/Rakefile CHANGED
@@ -1,9 +1,8 @@
1
1
  $:.unshift File.dirname(__FILE__) unless $:.include? '.'
2
- require 'rake/rdoctask'
3
2
 
4
3
  ROOT = '.'
5
4
  LIB_ROOT = File.join ROOT, 'lib'
6
- EXTRA_RDOC_FILES = %w(lib/README FOLDERS)
5
+ EXTRA_RDOC_FILES = %w(README.rdoc FOLDERS)
7
6
 
8
7
  task :default => :test
9
8
 
@@ -23,13 +22,14 @@ else
23
22
  ruby './test/functional/for_redcloth.rb'
24
23
  end
25
24
 
25
+ gem 'rdoc' if defined? gem
26
+ require 'rdoc/task'
26
27
  desc 'Generate documentation for CodeRay'
27
28
  Rake::RDocTask.new :doc do |rd|
28
29
  rd.title = 'CodeRay Documentation'
29
- rd.main = 'lib/README'
30
+ rd.main = 'README.rdoc'
30
31
  rd.rdoc_files.add Dir['lib']
31
- rd.rdoc_files.add 'lib/README'
32
- rd.rdoc_files.add 'FOLDERS'
32
+ rd.rdoc_files.add 'README.rdoc'
33
33
  rd.rdoc_dir = 'doc'
34
34
  end
35
35
 
data/lib/coderay.rb CHANGED
@@ -168,7 +168,7 @@ module CodeRay
168
168
  #
169
169
  # If +lang+ is :auto or omitted, the CodeRay::FileType module is used to
170
170
  # determine it. If it cannot find out what type it is, it uses
171
- # CodeRay::Scanners::Plaintext.
171
+ # CodeRay::Scanners::Text.
172
172
  #
173
173
  # Calls CodeRay.scan.
174
174
  #
@@ -179,7 +179,7 @@ module CodeRay
179
179
  file = IO.read filename
180
180
  if lang == :auto
181
181
  require 'coderay/helpers/file_type'
182
- lang = FileType.fetch filename, :plaintext, true
182
+ lang = FileType.fetch filename, :text, true
183
183
  end
184
184
  scan file, lang, options = {}, &block
185
185
  end
@@ -46,7 +46,7 @@ module CodeRay
46
46
  DEFAULT_OPTIONS = { }
47
47
 
48
48
  # The options you gave the Encoder at creating.
49
- attr_accessor :options
49
+ attr_accessor :options, :scanner
50
50
 
51
51
  # Creates a new Encoder.
52
52
  # +options+ is saved and used for all encode operations, as long
@@ -66,6 +66,7 @@ module CodeRay
66
66
  # Encode a Tokens object.
67
67
  def encode_tokens tokens, options = {}
68
68
  options = @options.merge options
69
+ @scanner = tokens.scanner if tokens.respond_to? :scanner
69
70
  setup options
70
71
  compile tokens, options
71
72
  finish options
@@ -74,10 +75,9 @@ module CodeRay
74
75
  # Encode the given +code+ using the Scanner for +lang+.
75
76
  def encode code, lang, options = {}
76
77
  options = @options.merge options
78
+ @scanner = Scanners[lang].new code, CodeRay.get_scanner_options(options).update(:tokens => self)
77
79
  setup options
78
- scanner_options = CodeRay.get_scanner_options options
79
- scanner_options[:tokens] = self
80
- CodeRay.scan code, lang, scanner_options
80
+ @scanner.tokenize
81
81
  finish options
82
82
  end
83
83
 
@@ -2,9 +2,11 @@ module CodeRay
2
2
  module Encoders
3
3
 
4
4
  map \
5
- :loc => :lines_of_code,
5
+ :loc => :lines_of_code,
6
6
  :term => :terminal,
7
+ :tty => :terminal,
7
8
  :plain => :text,
9
+ :plaintext => :text,
8
10
  :remove_comments => :comment_filter,
9
11
  :stats => :statistic,
10
12
  :tex => :latex
@@ -136,13 +136,7 @@ module Encoders
136
136
  HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1f]/
137
137
 
138
138
  TOKEN_KIND_TO_INFO = Hash.new do |h, kind|
139
- h[kind] =
140
- case kind
141
- when :pre_constant # FIXME: rename to :predefined_constant
142
- 'Predefined constant'
143
- else
144
- kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
145
- end
139
+ h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
146
140
  end
147
141
 
148
142
  TRANSPARENT_TOKEN_KINDS = Set[
@@ -153,18 +147,14 @@ module Encoders
153
147
  #
154
148
  # +hint+ may be :info, :info_long or :debug.
155
149
  def self.token_path_to_hint hint, kinds
156
- # FIXME: TRANSPARENT_TOKEN_KINDS?
157
- # if TRANSPARENT_TOKEN_KINDS.include? kinds.first
158
- # kinds = kinds[1..-1]
159
- # else
160
- # kinds = kinds[1..-1] + kinds.first
161
- # end
150
+ kinds = Array kinds
162
151
  title =
163
152
  case hint
164
153
  when :info
165
- TOKEN_KIND_TO_INFO[Array(kinds).first]
154
+ kinds = kinds[1..-1] if TRANSPARENT_TOKEN_KINDS.include? kinds.first
155
+ TOKEN_KIND_TO_INFO[kinds.first]
166
156
  when :info_long
167
- kinds.map { |kind| TOKEN_KIND_TO_INFO[kind] }.join('/')
157
+ kinds.reverse.map { |kind| TOKEN_KIND_TO_INFO[kind] }.join('/')
168
158
  when :debug
169
159
  kinds.inspect
170
160
  end
@@ -184,7 +174,7 @@ module Encoders
184
174
  hint = options[:hint]
185
175
  if hint && ![:debug, :info, :info_long].include?(hint)
186
176
  raise ArgumentError, "Unknown value %p for :hint; \
187
- expected :info, :debug, false, or nil." % hint
177
+ expected :info, :info_long, :debug, false, or nil." % hint
188
178
  end
189
179
 
190
180
  css_classes = TokenKinds
@@ -218,6 +208,8 @@ module Encoders
218
208
  else
219
209
  raise ArgumentError, "Unknown value %p for :css." % options[:css]
220
210
  end
211
+
212
+ @set_last_opened = options[:hint] || options[:css] == :style
221
213
  end
222
214
 
223
215
  def finish options
@@ -255,7 +247,7 @@ module Encoders
255
247
  def begin_group kind
256
248
  @out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '<span>')
257
249
  @opened << kind
258
- @last_opened = kind if @options[:css] == :style
250
+ @last_opened = kind if @set_last_opened
259
251
  end
260
252
 
261
253
  def end_group kind
@@ -68,18 +68,18 @@ module Encoders
68
68
  when :inline
69
69
  max_width = (start + line_count).to_s.size
70
70
  line_number = start
71
- opened_tags = []
71
+ nesting = []
72
72
  output.gsub!(/^.*$\n?/) do |line|
73
73
  line.chomp!
74
- open = opened_tags.join
74
+ open = nesting.join
75
75
  line.scan(%r!<(/)?span[^>]*>?!) do |close,|
76
76
  if close
77
- opened_tags.pop
77
+ nesting.pop
78
78
  else
79
- opened_tags << $&
79
+ nesting << $&
80
80
  end
81
81
  end
82
- close = '</span>' * opened_tags.size
82
+ close = '</span>' * nesting.size
83
83
 
84
84
  line_number_text = bolding.call line_number
85
85
  indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x)
@@ -3,14 +3,14 @@ module Encoders
3
3
 
4
4
  class HTML
5
5
 
6
- # This module is included in the output String from thew HTML Encoder.
6
+ # This module is included in the output String of the HTML Encoder.
7
7
  #
8
8
  # It provides methods like wrap, div, page etc.
9
9
  #
10
10
  # Remember to use #clone instead of #dup to keep the modules the object was
11
11
  # extended with.
12
12
  #
13
- # TODO: more doc.
13
+ # TODO: Rewrite this without monkey patching.
14
14
  module Output
15
15
 
16
16
  attr_accessor :css
@@ -25,7 +25,7 @@ module Encoders
25
25
 
26
26
  def make_stylesheet css, in_tag = false # :nodoc:
27
27
  sheet = css.stylesheet
28
- sheet = <<-CSS if in_tag
28
+ sheet = <<-'CSS' if in_tag
29
29
  <style type="text/css">
30
30
  #{sheet}
31
31
  </style>
@@ -14,7 +14,7 @@ module Encoders
14
14
  #
15
15
  # A Scanner class should define the token kinds that are not code in the
16
16
  # KINDS_NOT_LOC constant, which defaults to [:comment, :doctype].
17
- class LinesOfCode < Encoder
17
+ class LinesOfCode < TokenKindFilter
18
18
 
19
19
  register_for :lines_of_code
20
20
 
@@ -23,22 +23,19 @@ module Encoders
23
23
  protected
24
24
 
25
25
  def setup options
26
- @out = 0
27
- end
28
-
29
- def compile tokens, options
30
- if scanner = tokens.scanner
26
+ if scanner
31
27
  kinds_not_loc = scanner.class::KINDS_NOT_LOC
32
28
  else
33
29
  warn "Tokens have no associated scanner, counting all nonempty lines." if $VERBOSE
34
30
  kinds_not_loc = CodeRay::Scanners::Scanner::KINDS_NOT_LOC
35
31
  end
36
- code = tokens.token_kind_filter :exclude => kinds_not_loc
37
- @out = code.to_s.scan(NON_EMPTY_LINE).size
32
+
33
+ options[:exclude] = kinds_not_loc
34
+ super options
38
35
  end
39
36
 
40
37
  def finish options
41
- @out
38
+ @out.to_s.scan(NON_EMPTY_LINE).size
42
39
  end
43
40
 
44
41
  end
@@ -1,29 +1,29 @@
1
1
  module CodeRay
2
2
  module Encoders
3
-
3
+
4
4
  # Makes a statistic for the given tokens.
5
5
  #
6
6
  # Alias: +stats+
7
7
  class Statistic < Encoder
8
-
9
- register_for :stats, :statistic
10
-
8
+
9
+ register_for :statistic
10
+
11
11
  attr_reader :type_stats, :real_token_count # :nodoc:
12
-
12
+
13
13
  TypeStats = Struct.new :count, :size # :nodoc:
14
-
14
+
15
15
  protected
16
-
16
+
17
17
  def setup options
18
18
  @type_stats = Hash.new { |h, k| h[k] = TypeStats.new 0, 0 }
19
19
  @real_token_count = 0
20
20
  end
21
-
21
+
22
22
  def generate tokens, options
23
23
  @tokens = tokens
24
24
  super
25
25
  end
26
-
26
+
27
27
  STATS = <<-STATS # :nodoc:
28
28
 
29
29
  Code Statistics
@@ -36,12 +36,12 @@ Token Types (%d):
36
36
  type count ratio size (average)
37
37
  -------------------------------------------------------------
38
38
  %s
39
- STATS
40
- # space 12007 33.81 % 1.7
39
+ STATS
40
+
41
41
  TOKEN_TYPES_ROW = <<-TKR # :nodoc:
42
42
  %-20s %8d %6.2f %% %5.1f
43
- TKR
44
-
43
+ TKR
44
+
45
45
  def finish options
46
46
  all = @type_stats['TOTAL']
47
47
  all_count, all_size = all.count, all.size
@@ -57,7 +57,7 @@ Token Types (%d):
57
57
  types_stats
58
58
  ]
59
59
  end
60
-
60
+
61
61
  public
62
62
 
63
63
  def text_token text, kind
@@ -67,30 +67,31 @@ Token Types (%d):
67
67
  @type_stats['TOTAL'].size += text.size
68
68
  @type_stats['TOTAL'].count += 1
69
69
  end
70
-
70
+
71
71
  # TODO Hierarchy handling
72
72
  def begin_group kind
73
- block_token 'begin_group'
73
+ block_token ':begin_group', kind
74
74
  end
75
-
75
+
76
76
  def end_group kind
77
- block_token 'end_group'
77
+ block_token ':end_group', kind
78
78
  end
79
-
79
+
80
80
  def begin_line kind
81
- block_token 'begin_line'
81
+ block_token ':begin_line', kind
82
82
  end
83
-
83
+
84
84
  def end_line kind
85
- block_token 'end_line'
85
+ block_token ':end_line', kind
86
86
  end
87
87
 
88
- def block_token action
88
+ def block_token action, kind
89
89
  @type_stats['TOTAL'].count += 1
90
90
  @type_stats[action].count += 1
91
+ @type_stats[kind].count += 1
91
92
  end
92
-
93
+
93
94
  end
94
-
95
+
95
96
  end
96
97
  end
@@ -53,8 +53,8 @@ module CodeRay
53
53
  :local_variable => '33',
54
54
  :oct => '1;35',
55
55
  :operator_name => '1;29',
56
- :pre_constant => '1;36',
57
- :pre_type => '1;30',
56
+ :predefined_constant => '1;36',
57
+ :predefined_type => '1;30',
58
58
  :predefined => ['4', '1;34'],
59
59
  :preprocessor => '36',
60
60
  :pseudo_class => '34',
@@ -4,7 +4,7 @@ module Encoders
4
4
  # Concats the tokens into a single string, resulting in the original
5
5
  # code string if no tokens were removed.
6
6
  #
7
- # Alias: +plain+
7
+ # Alias: +plain+, +plaintext+
8
8
  #
9
9
  # == Options
10
10
  #
@@ -13,31 +13,31 @@ module Encoders
13
13
  #
14
14
  # Default: empty String
15
15
  class Text < Encoder
16
-
16
+
17
17
  register_for :text
18
-
18
+
19
19
  FILE_EXTENSION = 'txt'
20
-
20
+
21
21
  DEFAULT_OPTIONS = {
22
22
  :separator => nil
23
23
  }
24
-
24
+
25
25
  def text_token text, kind
26
26
  super
27
27
  @out << @sep if @sep
28
28
  end
29
-
29
+
30
30
  protected
31
31
  def setup options
32
32
  super
33
33
  @sep = options[:separator]
34
34
  end
35
-
35
+
36
36
  def finish options
37
37
  super.chomp @sep
38
38
  end
39
-
39
+
40
40
  end
41
-
41
+
42
42
  end
43
43
  end
@@ -1,38 +1,38 @@
1
1
  module CodeRay
2
2
  module Encoders
3
-
3
+
4
4
  # = XML Encoder
5
5
  #
6
6
  # Uses REXML. Very slow.
7
7
  class XML < Encoder
8
-
8
+
9
9
  register_for :xml
10
-
10
+
11
11
  FILE_EXTENSION = 'xml'
12
-
12
+
13
13
  require 'rexml/document'
14
-
14
+
15
15
  DEFAULT_OPTIONS = {
16
16
  :tab_width => 8,
17
17
  :pretty => -1,
18
18
  :transitive => false,
19
19
  }
20
-
20
+
21
21
  protected
22
-
23
22
  def setup options
24
23
  @doc = REXML::Document.new
25
24
  @doc << REXML::XMLDecl.new
26
25
  @tab_width = options[:tab_width]
27
26
  @root = @node = @doc.add_element('coderay-tokens')
28
27
  end
29
-
28
+
30
29
  def finish options
31
30
  @out = ''
32
31
  @doc.write @out, options[:pretty], options[:transitive], true
33
32
  @out
34
33
  end
35
34
 
35
+ public
36
36
  def text_token text, kind
37
37
  if kind == :space
38
38
  token = @node