coderay 1.0.0.598.pre → 1.0.0.738.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/bin/coderay +1 -1
  2. data/lib/coderay.rb +38 -32
  3. data/lib/coderay/duo.rb +1 -54
  4. data/lib/coderay/encoder.rb +31 -33
  5. data/lib/coderay/encoders/_map.rb +4 -2
  6. data/lib/coderay/encoders/comment_filter.rb +0 -61
  7. data/lib/coderay/encoders/count.rb +2 -23
  8. data/lib/coderay/encoders/debug.rb +11 -60
  9. data/lib/coderay/encoders/filter.rb +0 -46
  10. data/lib/coderay/encoders/html.rb +83 -91
  11. data/lib/coderay/encoders/html/css.rb +1 -6
  12. data/lib/coderay/encoders/html/numbering.rb +18 -21
  13. data/lib/coderay/encoders/html/output.rb +10 -52
  14. data/lib/coderay/encoders/json.rb +19 -39
  15. data/lib/coderay/encoders/lines_of_code.rb +7 -52
  16. data/lib/coderay/encoders/null.rb +6 -13
  17. data/lib/coderay/encoders/statistic.rb +30 -93
  18. data/lib/coderay/encoders/terminal.rb +3 -4
  19. data/lib/coderay/encoders/text.rb +1 -23
  20. data/lib/coderay/encoders/token_kind_filter.rb +0 -58
  21. data/lib/coderay/helpers/file_type.rb +119 -240
  22. data/lib/coderay/helpers/gzip.rb +41 -0
  23. data/lib/coderay/helpers/plugin.rb +237 -307
  24. data/lib/coderay/scanner.rb +112 -88
  25. data/lib/coderay/scanners/_map.rb +3 -3
  26. data/lib/coderay/scanners/c.rb +7 -7
  27. data/lib/coderay/scanners/clojure.rb +204 -0
  28. data/lib/coderay/scanners/css.rb +10 -20
  29. data/lib/coderay/scanners/debug.rb +9 -55
  30. data/lib/coderay/scanners/diff.rb +21 -4
  31. data/lib/coderay/scanners/html.rb +65 -18
  32. data/lib/coderay/scanners/java.rb +3 -2
  33. data/lib/coderay/scanners/java_script.rb +3 -3
  34. data/lib/coderay/scanners/json.rb +7 -6
  35. data/lib/coderay/scanners/php.rb +2 -1
  36. data/lib/coderay/scanners/rhtml.rb +6 -2
  37. data/lib/coderay/scanners/ruby.rb +193 -193
  38. data/lib/coderay/scanners/ruby/patterns.rb +15 -82
  39. data/lib/coderay/scanners/ruby/string_state.rb +71 -0
  40. data/lib/coderay/scanners/sql.rb +1 -1
  41. data/lib/coderay/scanners/yaml.rb +4 -2
  42. data/lib/coderay/styles/_map.rb +2 -2
  43. data/lib/coderay/styles/alpha.rb +48 -38
  44. data/lib/coderay/styles/cycnus.rb +2 -1
  45. data/lib/coderay/token_kinds.rb +88 -86
  46. data/lib/coderay/tokens.rb +88 -112
  47. data/test/functional/basic.rb +184 -5
  48. data/test/functional/examples.rb +4 -4
  49. data/test/functional/for_redcloth.rb +3 -2
  50. data/test/functional/suite.rb +7 -6
  51. metadata +11 -24
  52. data/lib/coderay/helpers/gzip_simple.rb +0 -123
  53. data/test/functional/load_plugin_scanner.rb +0 -11
  54. data/test/functional/vhdl.rb +0 -126
  55. data/test/functional/word_list.rb +0 -79
@@ -20,7 +20,7 @@ module Encoders
20
20
  parse style::TOKEN_COLORS
21
21
  end
22
22
 
23
- def [] *styles
23
+ def get_style styles
24
24
  cl = @classes[styles.first]
25
25
  return '' unless cl
26
26
  style = ''
@@ -63,8 +63,3 @@ module Encoders
63
63
 
64
64
  end
65
65
  end
66
-
67
- if $0 == __FILE__
68
- require 'pp'
69
- pp CodeRay::Encoders::HTML::CSS.new
70
- end
@@ -3,9 +3,9 @@ module Encoders
3
3
 
4
4
  class HTML
5
5
 
6
- module Output # :nodoc:
6
+ module Numbering # :nodoc:
7
7
 
8
- def number! mode = :table, options = {}
8
+ def self.number! output, mode = :table, options = {}
9
9
  return self unless mode
10
10
 
11
11
  options = DEFAULT_OPTIONS.merge options
@@ -26,7 +26,7 @@ module Encoders
26
26
  "<a href=\"##{anchor}\" name=\"#{anchor}\">#{line}</a>"
27
27
  end
28
28
  else
29
- proc { |line| line.to_s }
29
+ proc { |line| line.to_s } # :to_s.to_proc in Ruby 1.8.7+
30
30
  end
31
31
 
32
32
  bold_every = options[:bold_every]
@@ -56,12 +56,20 @@ module Encoders
56
56
  raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
57
57
  end
58
58
 
59
+ line_count = output.count("\n")
60
+ position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n)
61
+ if position_of_last_newline
62
+ after_last_newline = output[position_of_last_newline + 1 .. -1]
63
+ ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/]
64
+ line_count += 1 if not ends_with_newline
65
+ end
66
+
59
67
  case mode
60
68
  when :inline
61
69
  max_width = (start + line_count).to_s.size
62
70
  line_number = start
63
71
  opened_tags = []
64
- gsub!(/^.*$\n?/) do |line|
72
+ output.gsub!(/^.*$\n?/) do |line|
65
73
  line.chomp!
66
74
  open = opened_tags.join
67
75
  line.scan(%r!<(/)?span[^>]*>?!) do |close,|
@@ -80,13 +88,13 @@ module Encoders
80
88
  end
81
89
 
82
90
  when :table
83
- line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n")
91
+ line_numbers = (start ... start + line_count).map(&bolding).join("\n")
84
92
  line_numbers << "\n"
93
+ line_numbers_table_template = Output::TABLE.apply('LINE_NUMBERS', line_numbers)
85
94
 
86
- line_numbers_table_template = TABLE.apply('LINE_NUMBERS', line_numbers)
87
- gsub!(/<\/div>\n/) { '</div>' }
88
- wrap_in! line_numbers_table_template
89
- @wrapped_in = :div
95
+ output.gsub!(/<\/div>\n/, '</div>')
96
+ output.wrap_in! line_numbers_table_template
97
+ output.wrapped_in = :div
90
98
 
91
99
  when :list
92
100
  raise NotImplementedError, 'The :list option is no longer available. Use :table.'
@@ -96,18 +104,7 @@ module Encoders
96
104
  [mode, [:table, :inline]]
97
105
  end
98
106
 
99
- self
100
- end
101
-
102
- def line_count
103
- line_count = count("\n")
104
- position_of_last_newline = rindex(?\n)
105
- if position_of_last_newline
106
- after_last_newline = self[position_of_last_newline + 1 .. -1]
107
- ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/]
108
- line_count += 1 if not ends_with_newline
109
- end
110
- line_count
107
+ output
111
108
  end
112
109
 
113
110
  end
@@ -17,26 +17,13 @@ module Encoders
17
17
 
18
18
  class << self
19
19
 
20
- # This makes Output look like a class.
21
- #
22
- # Example:
23
- #
24
- # a = Output.new '<span class="co">Code</span>'
25
- # a.wrap! :page
26
- def new string, css = CSS.new, element = nil
27
- output = string.clone.extend self
28
- output.wrapped_in = element
29
- output.css = css
30
- output
31
- end
32
-
33
20
  # Raises an exception if an object that doesn't respond to to_str is extended by Output,
34
21
  # to prevent users from misuse. Use Module#remove_method to disable.
35
- def extended o
22
+ def extended o # :nodoc:
36
23
  warn "The Output module is intended to extend instances of String, not #{o.class}." unless o.respond_to? :to_str
37
24
  end
38
25
 
39
- def make_stylesheet css, in_tag = false
26
+ def make_stylesheet css, in_tag = false # :nodoc:
40
27
  sheet = css.stylesheet
41
28
  sheet = <<-CSS if in_tag
42
29
  <style type="text/css">
@@ -46,27 +33,13 @@ module Encoders
46
33
  sheet
47
34
  end
48
35
 
49
- def page_template_for_css css
36
+ def page_template_for_css css # :nodoc:
50
37
  sheet = make_stylesheet css
51
38
  PAGE.apply 'CSS', sheet
52
39
  end
53
40
 
54
- # Define a new wrapper. This is meta programming.
55
- def wrapper *wrappers
56
- wrappers.each do |wrapper|
57
- define_method wrapper do |*args|
58
- wrap wrapper, *args
59
- end
60
- define_method "#{wrapper}!".to_sym do |*args|
61
- wrap! wrapper, *args
62
- end
63
- end
64
- end
65
-
66
41
  end
67
42
 
68
- wrapper :div, :span, :page
69
-
70
43
  def wrapped_in? element
71
44
  wrapped_in == element
72
45
  end
@@ -76,16 +49,13 @@ module Encoders
76
49
  end
77
50
  attr_writer :wrapped_in
78
51
 
79
- def wrap_in template
80
- clone.wrap_in! template
81
- end
82
-
83
52
  def wrap_in! template
84
53
  Template.wrap! self, template, 'CONTENT'
85
54
  self
86
55
  end
87
56
 
88
57
  def apply_title! title
58
+ # FIXME: This may change the output!
89
59
  self.sub!(/(<title>)(<\/title>)/) { $1 + title + $2 }
90
60
  self
91
61
  end
@@ -116,14 +86,12 @@ module Encoders
116
86
  self
117
87
  end
118
88
 
119
- def wrap *args
120
- clone.wrap!(*args)
121
- end
122
-
123
89
  def stylesheet in_tag = false
124
90
  Output.make_stylesheet @css, in_tag
125
91
  end
126
92
 
93
+ #-- don't include the templates in docu
94
+
127
95
  class Template < String # :nodoc:
128
96
 
129
97
  def self.wrap! str, template, target
@@ -145,34 +113,24 @@ module Encoders
145
113
  end
146
114
  end
147
115
 
148
- module Simple # :nodoc:
149
- def ` str #` <-- for stupid editors
150
- Template.new str
151
- end
152
- end
153
116
  end
154
117
 
155
- extend Template::Simple
156
-
157
- #-- don't include the templates in docu
158
-
159
- SPAN = `<span class="CodeRay"><%CONTENT%></span>`
118
+ SPAN = Template.new '<span class="CodeRay"><%CONTENT%></span>'
160
119
 
161
- DIV = <<-`DIV`
120
+ DIV = Template.new <<-DIV
162
121
  <div class="CodeRay">
163
122
  <div class="code"><pre><%CONTENT%></pre></div>
164
123
  </div>
165
124
  DIV
166
125
 
167
- TABLE = <<-`TABLE`
126
+ TABLE = Template.new <<-TABLE
168
127
  <table class="CodeRay"><tr>
169
128
  <td class="line_numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><%LINE_NUMBERS%></pre></td>
170
129
  <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><%CONTENT%></pre></td>
171
130
  </tr></table>
172
131
  TABLE
173
- # title="double click to expand"
174
132
 
175
- PAGE = <<-`PAGE`
133
+ PAGE = Template.new <<-PAGE
176
134
  <!DOCTYPE html>
177
135
  <html>
178
136
  <head>
@@ -1,4 +1,3 @@
1
- ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
2
1
  module CodeRay
3
2
  module Encoders
4
3
 
@@ -18,20 +17,33 @@ module Encoders
18
17
  # ]
19
18
  class JSON < Encoder
20
19
 
20
+ begin
21
+ require 'json'
22
+ rescue LoadError
23
+ begin
24
+ require 'rubygems'
25
+ gem "json#{'-jruby' if defined? JRUBY_VERSION}"
26
+ require 'json'
27
+ rescue LoadError
28
+ $stderr.puts "The JSON encoder needs the JSON library.\n" \
29
+ "Please gem install json."
30
+ raise
31
+ end
32
+ end
33
+
21
34
  register_for :json
22
35
  FILE_EXTENSION = 'json'
23
36
 
24
37
  protected
25
38
  def setup options
26
- begin
27
- require 'json'
28
- rescue LoadError
29
- require 'rubygems'
30
- require 'json'
31
- end
32
39
  @out = []
33
40
  end
34
41
 
42
+ def finish options
43
+ @out.to_json
44
+ end
45
+
46
+ public
35
47
  def text_token text, kind
36
48
  @out << { :type => 'text', :text => text, :kind => kind }
37
49
  end
@@ -52,39 +64,7 @@ module Encoders
52
64
  @out << { :type => 'block', :action => 'end_line', :kind => kind }
53
65
  end
54
66
 
55
- def finish options
56
- @out.to_json
57
- end
58
-
59
67
  end
60
68
 
61
69
  end
62
70
  end
63
-
64
- if $0 == __FILE__
65
- $VERBOSE = true
66
- $: << File.join(File.dirname(__FILE__), '..')
67
- eval DATA.read, nil, $0, __LINE__ + 4
68
- end
69
-
70
- __END__
71
- require 'test/unit'
72
- $:.delete '.'
73
- require 'rubygems' if RUBY_VERSION < '1.9'
74
-
75
- class JSONEncoderTest < Test::Unit::TestCase
76
-
77
- def test_json_output
78
- json = CodeRay.scan('puts "Hello world!"', :ruby).json
79
- assert_equal [
80
- {"type"=>"text", "text"=>"puts", "kind"=>"ident"},
81
- {"type"=>"text", "text"=>" ", "kind"=>"space"},
82
- {"type"=>"block", "action"=>"open", "kind"=>"string"},
83
- {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
84
- {"type"=>"text", "text"=>"Hello world!", "kind"=>"content"},
85
- {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
86
- {"type"=>"block", "action"=>"close", "kind"=>"string"},
87
- ], JSON.load(json)
88
- end
89
-
90
- end
@@ -1,4 +1,3 @@
1
- ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
2
1
  module CodeRay
3
2
  module Encoders
4
3
 
@@ -23,70 +22,26 @@ module Encoders
23
22
 
24
23
  protected
25
24
 
25
+ def setup options
26
+ @out = 0
27
+ end
28
+
26
29
  def compile tokens, options
27
30
  if scanner = tokens.scanner
28
31
  kinds_not_loc = scanner.class::KINDS_NOT_LOC
29
32
  else
30
- warn 'Tokens have no scanner.' if $VERBOSE
33
+ warn "Tokens have no associated scanner, counting all nonempty lines." if $VERBOSE
31
34
  kinds_not_loc = CodeRay::Scanners::Scanner::KINDS_NOT_LOC
32
35
  end
33
36
  code = tokens.token_kind_filter :exclude => kinds_not_loc
34
- @loc = code.text.scan(NON_EMPTY_LINE).size
37
+ @out = code.to_s.scan(NON_EMPTY_LINE).size
35
38
  end
36
39
 
37
40
  def finish options
38
- @loc
41
+ @out
39
42
  end
40
43
 
41
44
  end
42
45
 
43
46
  end
44
47
  end
45
-
46
- if $0 == __FILE__
47
- $VERBOSE = true
48
- $: << File.join(File.dirname(__FILE__), '..')
49
- eval DATA.read, nil, $0, __LINE__ + 4
50
- end
51
-
52
- __END__
53
- require 'test/unit'
54
-
55
- class LinesOfCodeTest < Test::Unit::TestCase
56
-
57
- def test_creation
58
- assert CodeRay::Encoders::LinesOfCode < CodeRay::Encoders::Encoder
59
- filter = nil
60
- assert_nothing_raised do
61
- filter = CodeRay.encoder :loc
62
- end
63
- assert_kind_of CodeRay::Encoders::LinesOfCode, filter
64
- assert_nothing_raised do
65
- filter = CodeRay.encoder :lines_of_code
66
- end
67
- assert_kind_of CodeRay::Encoders::LinesOfCode, filter
68
- end
69
-
70
- def test_lines_of_code
71
- tokens = CodeRay.scan <<-RUBY, :ruby
72
- #!/usr/bin/env ruby
73
-
74
- # a minimal Ruby program
75
- puts "Hello world!"
76
- RUBY
77
- assert_equal 1, CodeRay::Encoders::LinesOfCode.new.encode_tokens(tokens)
78
- assert_equal 1, tokens.lines_of_code
79
- assert_equal 1, tokens.loc
80
- end
81
-
82
- def test_filtering_block_tokens
83
- tokens = CodeRay::Tokens.new
84
- tokens.concat ["Hello\n", :world]
85
- tokens.concat ["Hello\n", :space]
86
- tokens.concat ["Hello\n", :comment]
87
- assert_equal 2, CodeRay::Encoders::LinesOfCode.new.encode_tokens(tokens)
88
- assert_equal 2, tokens.lines_of_code
89
- assert_equal 2, tokens.loc
90
- end
91
-
92
- end
@@ -1,25 +1,18 @@
1
1
  module CodeRay
2
2
  module Encoders
3
-
3
+
4
4
  # = Null Encoder
5
5
  #
6
6
  # Does nothing and returns an empty string.
7
7
  class Null < Encoder
8
-
8
+
9
9
  register_for :null
10
-
11
- # Defined for faster processing.
12
- def to_proc
13
- proc {}
14
- end
15
-
16
- protected
17
-
18
- def token(*)
10
+
11
+ def text_token text, kind
19
12
  # do nothing
20
13
  end
21
-
14
+
22
15
  end
23
-
16
+
24
17
  end
25
18
  end
@@ -1,4 +1,3 @@
1
- ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
2
1
  module CodeRay
3
2
  module Encoders
4
3
 
@@ -24,37 +23,7 @@ module Encoders
24
23
  @tokens = tokens
25
24
  super
26
25
  end
27
-
28
- def text_token text, kind
29
- @real_token_count += 1 unless kind == :space
30
- @type_stats[kind].count += 1
31
- @type_stats[kind].size += text.size
32
- @type_stats['TOTAL'].size += text.size
33
- @type_stats['TOTAL'].count += 1
34
- end
35
-
36
- # TODO Hierarchy handling
37
- def begin_group kind
38
- block_token 'begin_group'
39
- end
40
-
41
- def end_group kind
42
- block_token 'end_group'
43
- end
44
-
45
- def begin_line kind
46
- block_token 'begin_line'
47
- end
48
-
49
- def end_line kind
50
- block_token 'end_line'
51
- end
52
-
53
- def block_token action
54
- @type_stats['TOTAL'].count += 1
55
- @type_stats[action].count += 1
56
- end
57
-
26
+
58
27
  STATS = <<-STATS # :nodoc:
59
28
 
60
29
  Code Statistics
@@ -89,71 +58,39 @@ Token Types (%d):
89
58
  ]
90
59
  end
91
60
 
92
- end
93
-
94
- end
95
- end
96
-
97
- if $0 == __FILE__
98
- $VERBOSE = true
99
- $: << File.join(File.dirname(__FILE__), '..')
100
- eval DATA.read, nil, $0, __LINE__ + 4
101
- end
61
+ public
62
+
63
+ def text_token text, kind
64
+ @real_token_count += 1 unless kind == :space
65
+ @type_stats[kind].count += 1
66
+ @type_stats[kind].size += text.size
67
+ @type_stats['TOTAL'].size += text.size
68
+ @type_stats['TOTAL'].count += 1
69
+ end
102
70
 
103
- __END__
104
- require 'test/unit'
71
+ # TODO Hierarchy handling
72
+ def begin_group kind
73
+ block_token 'begin_group'
74
+ end
105
75
 
106
- class StatisticEncoderTest < Test::Unit::TestCase
107
-
108
- def test_creation
109
- assert CodeRay::Encoders::Statistic < CodeRay::Encoders::Encoder
110
- stats = nil
111
- assert_nothing_raised do
112
- stats = CodeRay.encoder :statistic
76
+ def end_group kind
77
+ block_token 'end_group'
113
78
  end
114
- assert_kind_of CodeRay::Encoders::Encoder, stats
115
- end
116
-
117
- TEST_INPUT = CodeRay::Tokens[
118
- ['10', :integer],
119
- ['(\\)', :operator],
120
- [:begin_group, :string],
121
- ['test', :content],
122
- [:end_group, :string],
123
- [:begin_line, :test],
124
- ["\n", :space],
125
- ["\n \t", :space],
126
- [" \n", :space],
127
- ["[]", :method],
128
- [:end_line, :test],
129
- ].flatten
130
- TEST_OUTPUT = <<-'DEBUG'
131
79
 
132
- Code Statistics
80
+ def begin_line kind
81
+ block_token 'begin_line'
82
+ end
133
83
 
134
- Tokens 11
135
- Non-Whitespace 4
136
- Bytes Total 20
84
+ def end_line kind
85
+ block_token 'end_line'
86
+ end
87
+
88
+ def block_token action
89
+ @type_stats['TOTAL'].count += 1
90
+ @type_stats[action].count += 1
91
+ end
137
92
 
138
- Token Types (5):
139
- type count ratio size (average)
140
- -------------------------------------------------------------
141
- TOTAL 11 100.00 % 1.8
142
- space 3 27.27 % 3.0
143
- begin_group 1 9.09 % 0.0
144
- begin_line 1 9.09 % 0.0
145
- content 1 9.09 % 4.0
146
- end_group 1 9.09 % 0.0
147
- end_line 1 9.09 % 0.0
148
- integer 1 9.09 % 2.0
149
- method 1 9.09 % 2.0
150
- operator 1 9.09 % 3.0
151
-
152
- DEBUG
153
-
154
- def test_filtering_text_tokens
155
- assert_equal TEST_OUTPUT, CodeRay::Encoders::Statistic.new.encode_tokens(TEST_INPUT)
156
- assert_equal TEST_OUTPUT, TEST_INPUT.statistic
157
93
  end
158
-
159
- end
94
+
95
+ end
96
+ end