coderay 0.9.8 → 1.0.0

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 (87) hide show
  1. data/{lib/README → README_INDEX.rdoc} +10 -21
  2. data/Rakefile +6 -6
  3. data/bin/coderay +193 -64
  4. data/lib/coderay.rb +61 -105
  5. data/lib/coderay/duo.rb +17 -21
  6. data/lib/coderay/encoder.rb +100 -112
  7. data/lib/coderay/encoders/_map.rb +12 -7
  8. data/lib/coderay/encoders/comment_filter.rb +12 -30
  9. data/lib/coderay/encoders/count.rb +29 -11
  10. data/lib/coderay/encoders/debug.rb +32 -20
  11. data/lib/coderay/encoders/div.rb +13 -9
  12. data/lib/coderay/encoders/filter.rb +34 -51
  13. data/lib/coderay/encoders/html.rb +155 -161
  14. data/lib/coderay/encoders/html/css.rb +4 -9
  15. data/lib/coderay/encoders/html/numbering.rb +115 -0
  16. data/lib/coderay/encoders/html/output.rb +22 -70
  17. data/lib/coderay/encoders/json.rb +59 -45
  18. data/lib/coderay/encoders/lines_of_code.rb +12 -57
  19. data/lib/coderay/encoders/null.rb +6 -14
  20. data/lib/coderay/encoders/page.rb +13 -9
  21. data/lib/coderay/encoders/span.rb +13 -9
  22. data/lib/coderay/encoders/statistic.rb +58 -39
  23. data/lib/coderay/encoders/terminal.rb +179 -0
  24. data/lib/coderay/encoders/text.rb +31 -17
  25. data/lib/coderay/encoders/token_kind_filter.rb +111 -0
  26. data/lib/coderay/encoders/xml.rb +19 -18
  27. data/lib/coderay/encoders/yaml.rb +37 -9
  28. data/lib/coderay/for_redcloth.rb +4 -4
  29. data/lib/coderay/helpers/file_type.rb +127 -246
  30. data/lib/coderay/helpers/gzip.rb +41 -0
  31. data/lib/coderay/helpers/plugin.rb +241 -306
  32. data/lib/coderay/helpers/word_list.rb +65 -126
  33. data/lib/coderay/scanner.rb +173 -156
  34. data/lib/coderay/scanners/_map.rb +18 -17
  35. data/lib/coderay/scanners/c.rb +63 -77
  36. data/lib/coderay/scanners/clojure.rb +217 -0
  37. data/lib/coderay/scanners/cpp.rb +71 -84
  38. data/lib/coderay/scanners/css.rb +103 -120
  39. data/lib/coderay/scanners/debug.rb +47 -44
  40. data/lib/coderay/scanners/delphi.rb +70 -76
  41. data/lib/coderay/scanners/diff.rb +141 -50
  42. data/lib/coderay/scanners/erb.rb +81 -0
  43. data/lib/coderay/scanners/groovy.rb +104 -113
  44. data/lib/coderay/scanners/haml.rb +168 -0
  45. data/lib/coderay/scanners/html.rb +181 -110
  46. data/lib/coderay/scanners/java.rb +73 -75
  47. data/lib/coderay/scanners/java/builtin_types.rb +2 -0
  48. data/lib/coderay/scanners/java_script.rb +90 -101
  49. data/lib/coderay/scanners/json.rb +40 -53
  50. data/lib/coderay/scanners/php.rb +123 -147
  51. data/lib/coderay/scanners/python.rb +93 -91
  52. data/lib/coderay/scanners/raydebug.rb +66 -0
  53. data/lib/coderay/scanners/ruby.rb +343 -326
  54. data/lib/coderay/scanners/ruby/patterns.rb +40 -106
  55. data/lib/coderay/scanners/ruby/string_state.rb +71 -0
  56. data/lib/coderay/scanners/sql.rb +80 -66
  57. data/lib/coderay/scanners/text.rb +26 -0
  58. data/lib/coderay/scanners/xml.rb +1 -1
  59. data/lib/coderay/scanners/yaml.rb +74 -73
  60. data/lib/coderay/style.rb +10 -7
  61. data/lib/coderay/styles/_map.rb +3 -3
  62. data/lib/coderay/styles/alpha.rb +143 -0
  63. data/lib/coderay/token_kinds.rb +90 -0
  64. data/lib/coderay/tokens.rb +102 -277
  65. data/lib/coderay/tokens_proxy.rb +55 -0
  66. data/lib/coderay/version.rb +3 -0
  67. data/test/functional/basic.rb +200 -18
  68. data/test/functional/examples.rb +130 -0
  69. data/test/functional/for_redcloth.rb +15 -8
  70. data/test/functional/suite.rb +9 -6
  71. metadata +103 -123
  72. data/FOLDERS +0 -53
  73. data/bin/coderay_stylesheet +0 -4
  74. data/lib/coderay/encoders/html/numerization.rb +0 -133
  75. data/lib/coderay/encoders/term.rb +0 -158
  76. data/lib/coderay/encoders/token_class_filter.rb +0 -84
  77. data/lib/coderay/helpers/gzip_simple.rb +0 -123
  78. data/lib/coderay/scanners/nitro_xhtml.rb +0 -136
  79. data/lib/coderay/scanners/plaintext.rb +0 -20
  80. data/lib/coderay/scanners/rhtml.rb +0 -78
  81. data/lib/coderay/scanners/scheme.rb +0 -145
  82. data/lib/coderay/styles/cycnus.rb +0 -152
  83. data/lib/coderay/styles/murphy.rb +0 -134
  84. data/lib/coderay/token_classes.rb +0 -86
  85. data/test/functional/load_plugin_scanner.rb +0 -11
  86. data/test/functional/vhdl.rb +0 -126
  87. data/test/functional/word_list.rb +0 -79
@@ -1,10 +1,9 @@
1
- ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
2
1
  module CodeRay
3
2
  module Encoders
4
3
 
5
4
  # Counts the LoC (Lines of Code). Returns an Integer >= 0.
6
5
  #
7
- # Alias: :loc
6
+ # Alias: +loc+
8
7
  #
9
8
  # Everything that is not comment, markup, doctype/shebang, or an empty line,
10
9
  # is considered to be code.
@@ -15,76 +14,32 @@ module Encoders
15
14
  #
16
15
  # A Scanner class should define the token kinds that are not code in the
17
16
  # KINDS_NOT_LOC constant, which defaults to [:comment, :doctype].
18
- class LinesOfCode < Encoder
17
+ class LinesOfCode < TokenKindFilter
19
18
 
20
19
  register_for :lines_of_code
21
20
 
22
21
  NON_EMPTY_LINE = /^\s*\S.*$/
23
22
 
24
- def compile tokens, options
25
- if scanner = tokens.scanner
23
+ protected
24
+
25
+ def setup options
26
+ if scanner
26
27
  kinds_not_loc = scanner.class::KINDS_NOT_LOC
27
28
  else
28
- warn ArgumentError, 'Tokens have no scanner.' if $VERBOSE
29
+ warn "Tokens have no associated scanner, counting all nonempty lines." if $VERBOSE
29
30
  kinds_not_loc = CodeRay::Scanners::Scanner::KINDS_NOT_LOC
30
31
  end
31
- code = tokens.token_class_filter :exclude => kinds_not_loc
32
- @loc = code.text.scan(NON_EMPTY_LINE).size
32
+
33
+ options[:exclude] = kinds_not_loc
34
+
35
+ super options
33
36
  end
34
37
 
35
38
  def finish options
36
- @loc
39
+ output @tokens.text.scan(NON_EMPTY_LINE).size
37
40
  end
38
41
 
39
42
  end
40
43
 
41
44
  end
42
45
  end
43
-
44
- if $0 == __FILE__
45
- $VERBOSE = true
46
- $: << File.join(File.dirname(__FILE__), '..')
47
- eval DATA.read, nil, $0, __LINE__ + 4
48
- end
49
-
50
- __END__
51
- require 'test/unit'
52
-
53
- class LinesOfCodeTest < Test::Unit::TestCase
54
-
55
- def test_creation
56
- assert CodeRay::Encoders::LinesOfCode < CodeRay::Encoders::Encoder
57
- filter = nil
58
- assert_nothing_raised do
59
- filter = CodeRay.encoder :loc
60
- end
61
- assert_kind_of CodeRay::Encoders::LinesOfCode, filter
62
- assert_nothing_raised do
63
- filter = CodeRay.encoder :lines_of_code
64
- end
65
- assert_kind_of CodeRay::Encoders::LinesOfCode, filter
66
- end
67
-
68
- def test_lines_of_code
69
- tokens = CodeRay.scan <<-RUBY, :ruby
70
- #!/usr/bin/env ruby
71
-
72
- # a minimal Ruby program
73
- puts "Hello world!"
74
- RUBY
75
- assert_equal 1, CodeRay::Encoders::LinesOfCode.new.encode_tokens(tokens)
76
- assert_equal 1, tokens.lines_of_code
77
- assert_equal 1, tokens.loc
78
- end
79
-
80
- def test_filtering_block_tokens
81
- tokens = CodeRay::Tokens.new
82
- tokens << ["Hello\n", :world]
83
- tokens << ["Hello\n", :space]
84
- tokens << ["Hello\n", :comment]
85
- assert_equal 2, CodeRay::Encoders::LinesOfCode.new.encode_tokens(tokens)
86
- assert_equal 2, tokens.lines_of_code
87
- assert_equal 2, tokens.loc
88
- end
89
-
90
- end
@@ -1,26 +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
-
9
- include Streamable
8
+
10
9
  register_for :null
11
-
12
- # Defined for faster processing
13
- def to_proc
14
- proc {}
15
- end
16
-
17
- protected
18
-
19
- def token(*)
10
+
11
+ def text_token text, kind
20
12
  # do nothing
21
13
  end
22
-
14
+
23
15
  end
24
-
16
+
25
17
  end
26
18
  end
@@ -1,20 +1,24 @@
1
1
  module CodeRay
2
2
  module Encoders
3
-
3
+
4
4
  load :html
5
-
5
+
6
+ # Wraps the output into a HTML page, using CSS classes and
7
+ # line numbers in the table format by default.
8
+ #
9
+ # See Encoders::HTML for available options.
6
10
  class Page < HTML
7
-
11
+
8
12
  FILE_EXTENSION = 'html'
9
-
13
+
10
14
  register_for :page
11
-
15
+
12
16
  DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge \
13
- :css => :class,
14
- :wrap => :page,
17
+ :css => :class,
18
+ :wrap => :page,
15
19
  :line_numbers => :table
16
-
20
+
17
21
  end
18
-
22
+
19
23
  end
20
24
  end
@@ -1,19 +1,23 @@
1
1
  module CodeRay
2
2
  module Encoders
3
-
3
+
4
4
  load :html
5
-
5
+
6
+ # Wraps HTML output into a SPAN element, using inline styles by default.
7
+ #
8
+ # See Encoders::HTML for available options.
6
9
  class Span < HTML
7
-
10
+
8
11
  FILE_EXTENSION = 'span.html'
9
-
12
+
10
13
  register_for :span
11
-
14
+
12
15
  DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge \
13
- :css => :style,
14
- :wrap => :span
15
-
16
+ :css => :style,
17
+ :wrap => :span,
18
+ :line_numbers => false
19
+
16
20
  end
17
-
21
+
18
22
  end
19
23
  end
@@ -1,43 +1,27 @@
1
1
  module CodeRay
2
2
  module Encoders
3
-
3
+
4
4
  # Makes a statistic for the given tokens.
5
+ #
6
+ # Alias: +stats+
5
7
  class Statistic < Encoder
6
-
7
- include Streamable
8
- register_for :stats, :statistic
9
-
10
- attr_reader :type_stats, :real_token_count
11
-
8
+
9
+ register_for :statistic
10
+
11
+ attr_reader :type_stats, :real_token_count # :nodoc:
12
+
13
+ TypeStats = Struct.new :count, :size # :nodoc:
14
+
12
15
  protected
13
-
14
- TypeStats = Struct.new :count, :size
15
-
16
+
16
17
  def setup options
18
+ super
19
+
17
20
  @type_stats = Hash.new { |h, k| h[k] = TypeStats.new 0, 0 }
18
21
  @real_token_count = 0
19
22
  end
20
-
21
- def generate tokens, options
22
- @tokens = tokens
23
- super
24
- end
25
-
26
- def text_token text, kind
27
- @real_token_count += 1 unless kind == :space
28
- @type_stats[kind].count += 1
29
- @type_stats[kind].size += text.size
30
- @type_stats['TOTAL'].size += text.size
31
- @type_stats['TOTAL'].count += 1
32
- end
33
-
34
- # TODO Hierarchy handling
35
- def block_token action, kind
36
- @type_stats['TOTAL'].count += 1
37
- @type_stats['open/close'].count += 1
38
- end
39
-
40
- STATS = <<-STATS
23
+
24
+ STATS = <<-STATS # :nodoc:
41
25
 
42
26
  Code Statistics
43
27
 
@@ -49,12 +33,12 @@ Token Types (%d):
49
33
  type count ratio size (average)
50
34
  -------------------------------------------------------------
51
35
  %s
52
- STATS
53
- # space 12007 33.81 % 1.7
54
- TOKEN_TYPES_ROW = <<-TKR
36
+ STATS
37
+
38
+ TOKEN_TYPES_ROW = <<-TKR # :nodoc:
55
39
  %-20s %8d %6.2f %% %5.1f
56
- TKR
57
-
40
+ TKR
41
+
58
42
  def finish options
59
43
  all = @type_stats['TOTAL']
60
44
  all_count, all_size = all.count, all.size
@@ -64,14 +48,49 @@ Token Types (%d):
64
48
  types_stats = @type_stats.sort_by { |k, v| [-v.count, k.to_s] }.map do |k, v|
65
49
  TOKEN_TYPES_ROW % [k, v.count, 100.0 * v.count / all_count, v.size]
66
50
  end.join
67
- STATS % [
51
+ @out << STATS % [
68
52
  all_count, @real_token_count, all_size,
69
53
  @type_stats.delete_if { |k, v| k.is_a? String }.size,
70
54
  types_stats
71
55
  ]
56
+
57
+ super
72
58
  end
73
-
59
+
60
+ public
61
+
62
+ def text_token text, kind
63
+ @real_token_count += 1 unless kind == :space
64
+ @type_stats[kind].count += 1
65
+ @type_stats[kind].size += text.size
66
+ @type_stats['TOTAL'].size += text.size
67
+ @type_stats['TOTAL'].count += 1
68
+ end
69
+
70
+ # TODO Hierarchy handling
71
+ def begin_group kind
72
+ block_token ':begin_group', kind
73
+ end
74
+
75
+ def end_group kind
76
+ block_token ':end_group', kind
77
+ end
78
+
79
+ def begin_line kind
80
+ block_token ':begin_line', kind
81
+ end
82
+
83
+ def end_line kind
84
+ block_token ':end_line', kind
85
+ end
86
+
87
+ def block_token action, kind
88
+ @type_stats['TOTAL'].count += 1
89
+ @type_stats[action].count += 1
90
+ @type_stats[kind].count += 1
91
+ end
92
+
74
93
  end
75
-
94
+
76
95
  end
77
96
  end
@@ -0,0 +1,179 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # Outputs code highlighted for a color terminal.
5
+ #
6
+ # Note: This encoder is in beta. It currently doesn't use the Styles.
7
+ #
8
+ # Alias: +term+
9
+ #
10
+ # == Authors & License
11
+ #
12
+ # By Rob Aldred (http://robaldred.co.uk)
13
+ #
14
+ # Based on idea by Nathan Weizenbaum (http://nex-3.com)
15
+ #
16
+ # MIT License (http://www.opensource.org/licenses/mit-license.php)
17
+ class Terminal < Encoder
18
+
19
+ register_for :terminal
20
+
21
+ TOKEN_COLORS = {
22
+ :annotation => '35',
23
+ :attribute_name => '33',
24
+ :attribute_value => '31',
25
+ :binary => '1;35',
26
+ :char => {
27
+ :self => '36', :delimiter => '34'
28
+ },
29
+ :class => '1;35',
30
+ :class_variable => '36',
31
+ :color => '32',
32
+ :comment => '37',
33
+ :complex => '34',
34
+ :constant => ['34', '4'],
35
+ :decoration => '35',
36
+ :definition => '1;32',
37
+ :directive => ['32', '4'],
38
+ :doc => '46',
39
+ :doctype => '1;30',
40
+ :doc_string => ['31', '4'],
41
+ :entity => '33',
42
+ :error => ['1;33', '41'],
43
+ :exception => '1;31',
44
+ :float => '1;35',
45
+ :function => '1;34',
46
+ :global_variable => '42',
47
+ :hex => '1;36',
48
+ :include => '33',
49
+ :integer => '1;34',
50
+ :key => '35',
51
+ :label => '1;15',
52
+ :local_variable => '33',
53
+ :octal => '1;35',
54
+ :operator_name => '1;29',
55
+ :predefined_constant => '1;36',
56
+ :predefined_type => '1;30',
57
+ :predefined => ['4', '1;34'],
58
+ :preprocessor => '36',
59
+ :pseudo_class => '34',
60
+ :regexp => {
61
+ :self => '31',
62
+ :content => '31',
63
+ :delimiter => '1;29',
64
+ :modifier => '35',
65
+ :function => '1;29'
66
+ },
67
+ :reserved => '1;31',
68
+ :shell => {
69
+ :self => '42',
70
+ :content => '1;29',
71
+ :delimiter => '37',
72
+ },
73
+ :string => {
74
+ :self => '32',
75
+ :modifier => '1;32',
76
+ :escape => '1;36',
77
+ :delimiter => '1;32',
78
+ },
79
+ :symbol => '1;32',
80
+ :tag => '34',
81
+ :type => '1;34',
82
+ :value => '36',
83
+ :variable => '34',
84
+
85
+ :insert => '42',
86
+ :delete => '41',
87
+ :change => '44',
88
+ :head => '45'
89
+ }
90
+ TOKEN_COLORS[:keyword] = TOKEN_COLORS[:reserved]
91
+ TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
92
+ TOKEN_COLORS[:imaginary] = TOKEN_COLORS[:complex]
93
+ TOKEN_COLORS[:begin_group] = TOKEN_COLORS[:end_group] =
94
+ TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
95
+
96
+ protected
97
+
98
+ def setup(options)
99
+ super
100
+ @opened = []
101
+ @subcolors = nil
102
+ end
103
+
104
+ public
105
+
106
+ def text_token text, kind
107
+ if color = (@subcolors || TOKEN_COLORS)[kind]
108
+ if Hash === color
109
+ if color[:self]
110
+ color = color[:self]
111
+ else
112
+ @out << text
113
+ return
114
+ end
115
+ end
116
+
117
+ @out << ansi_colorize(color)
118
+ @out << text.gsub("\n", ansi_clear + "\n" + ansi_colorize(color))
119
+ @out << ansi_clear
120
+ @out << ansi_colorize(@subcolors[:self]) if @subcolors && @subcolors[:self]
121
+ else
122
+ @out << text
123
+ end
124
+ end
125
+
126
+ def begin_group kind
127
+ @opened << kind
128
+ @out << open_token(kind)
129
+ end
130
+ alias begin_line begin_group
131
+
132
+ def end_group kind
133
+ if @opened.empty?
134
+ # nothing to close
135
+ else
136
+ @opened.pop
137
+ @out << ansi_clear
138
+ @out << open_token(@opened.last)
139
+ end
140
+ end
141
+
142
+ def end_line kind
143
+ if @opened.empty?
144
+ # nothing to close
145
+ else
146
+ @opened.pop
147
+ # whole lines to be highlighted,
148
+ # eg. added/modified/deleted lines in a diff
149
+ @out << "\t" * 100 + ansi_clear
150
+ @out << open_token(@opened.last)
151
+ end
152
+ end
153
+
154
+ private
155
+
156
+ def open_token kind
157
+ if color = TOKEN_COLORS[kind]
158
+ if Hash === color
159
+ @subcolors = color
160
+ ansi_colorize(color[:self]) if color[:self]
161
+ else
162
+ @subcolors = {}
163
+ ansi_colorize(color)
164
+ end
165
+ else
166
+ @subcolors = nil
167
+ ''
168
+ end
169
+ end
170
+
171
+ def ansi_colorize(color)
172
+ Array(color).map { |c| "\e[#{c}m" }.join
173
+ end
174
+ def ansi_clear
175
+ ansi_colorize(0)
176
+ end
177
+ end
178
+ end
179
+ end