coderay 1.0.0.598.pre → 1.0.0.738.pre

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 (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
data/bin/coderay CHANGED
@@ -73,7 +73,7 @@ Examples:
73
73
  end
74
74
 
75
75
  if tokens == :scan
76
- output = CodeRay::Duo[lang => format].highlight input
76
+ output = CodeRay.encoder(format).encode(input, lang)
77
77
  else
78
78
  output = tokens.encode format
79
79
  end
data/lib/coderay.rb CHANGED
@@ -1,16 +1,21 @@
1
+ # encoding: utf-8
2
+ # Encoding.default_internal = 'UTF-8'
3
+
1
4
  # = CodeRay Library
2
5
  #
3
6
  # CodeRay is a Ruby library for syntax highlighting.
4
7
  #
5
- # I try to make CodeRay easy to use and intuitive, but at the same time fully featured, complete,
6
- # fast and efficient.
8
+ # I try to make CodeRay easy to use and intuitive, but at the same time fully
9
+ # featured, complete, fast and efficient.
7
10
  #
8
11
  # See README.
9
12
  #
10
13
  # It consists mainly of
11
- # * the main engine: CodeRay (Scanners::Scanner, Tokens, Encoders::Encoder), PluginHost
14
+ # * the main engine: CodeRay (Scanners::Scanner, Tokens, Encoders::Encoder)
15
+ # * the plugin system: PluginHost, Plugin
12
16
  # * the scanners in CodeRay::Scanners
13
17
  # * the encoders in CodeRay::Encoders
18
+ # * the styles in CodeRay::Styles
14
19
  #
15
20
  # Here's a fancy graphic to light up this gray docu:
16
21
  #
@@ -22,8 +27,8 @@
22
27
  #
23
28
  # == Usage
24
29
  #
25
- # Remember you need RubyGems to use CodeRay, unless you have it in your load path. Run Ruby with
26
- # -rubygems option if required.
30
+ # Remember you need RubyGems to use CodeRay, unless you have it in your load
31
+ # path. Run Ruby with -rubygems option if required.
27
32
  #
28
33
  # === Highlight Ruby code in a string as html
29
34
  #
@@ -128,16 +133,25 @@ module CodeRay
128
133
  # Teeny: development state, 0 for pre-release
129
134
  # Revision: Subversion Revision number (generated on rake gem:make)
130
135
  VERSION = '1.0.0'
131
-
132
- require 'coderay/tokens'
133
- require 'coderay/token_kinds'
134
- require 'coderay/scanner'
135
- require 'coderay/encoder'
136
- require 'coderay/duo'
137
- require 'coderay/style'
138
-
136
+
137
+ # Tokens
138
+ autoload :Tokens, 'coderay/tokens'
139
+ autoload :TokenKinds, 'coderay/token_kinds'
140
+
141
+ # Plugin system
142
+ autoload :PluginHost, 'coderay/helpers/plugin'
143
+ autoload :Plugin, 'coderay/helpers/plugin'
144
+
145
+ # Plugins
146
+ autoload :Scanners, 'coderay/scanner'
147
+ autoload :Encoders, 'coderay/encoder'
148
+ autoload :Styles, 'coderay/style'
149
+
150
+ # Convenience access and reusable Encoder/Scanner pair
151
+ autoload :Duo, 'coderay/duo'
152
+
139
153
  class << self
140
-
154
+
141
155
  # Scans the given +code+ (a String) with the Scanner for +lang+.
142
156
  #
143
157
  # This is a simple way to use CodeRay. Example:
@@ -181,16 +195,6 @@ module CodeRay
181
195
  encoder(format, options).encode code, lang, options
182
196
  end
183
197
 
184
- # Highlight a string into a HTML <div>.
185
- #
186
- # CSS styles use classes, so you have to include a stylesheet
187
- # in your output.
188
- #
189
- # See encode.
190
- def highlight code, lang, options = { :css => :class }, format = :div
191
- encode code, lang, format, options
192
- end
193
-
194
198
  # Encode pre-scanned Tokens.
195
199
  # Use this together with CodeRay.scan:
196
200
  #
@@ -217,6 +221,16 @@ module CodeRay
217
221
  encode_tokens tokens, format, options
218
222
  end
219
223
 
224
+ # Highlight a string into a HTML <div>.
225
+ #
226
+ # CSS styles use classes, so you have to include a stylesheet
227
+ # in your output.
228
+ #
229
+ # See encode.
230
+ def highlight code, lang, options = { :css => :class }, format = :div
231
+ encode code, lang, format, options
232
+ end
233
+
220
234
  # Highlight a file into a HTML <div>.
221
235
  #
222
236
  # CSS styles use classes, so you have to include a stylesheet
@@ -266,11 +280,3 @@ module CodeRay
266
280
  end
267
281
 
268
282
  end
269
-
270
- # Run a test script.
271
- if $0 == __FILE__
272
- $stderr.print 'Press key to print demo.'; gets
273
- # Just use this file as an example of Ruby code.
274
- code = File.read(__FILE__)[/module CodeRay.*/m]
275
- print CodeRay.scan(code, :ruby).html
276
- end
data/lib/coderay/duo.rb CHANGED
@@ -1,4 +1,3 @@
1
- ($:.unshift '..'; require 'coderay') unless defined? CodeRay
2
1
  module CodeRay
3
2
 
4
3
  # = Duo
@@ -36,7 +35,7 @@ module CodeRay
36
35
  # The options are forwarded to scanner and encoder
37
36
  # (see CodeRay.get_scanner_options).
38
37
  def initialize lang = nil, format = nil, options = {}
39
- if format == nil and lang.is_a? Hash and lang.size == 1
38
+ if format.nil? && lang.is_a?(Hash) && lang.size == 1
40
39
  @lang = lang.keys.first
41
40
  @format = lang[@lang]
42
41
  else
@@ -80,55 +79,3 @@ module CodeRay
80
79
  end
81
80
 
82
81
  end
83
-
84
- if $0 == __FILE__
85
- $VERBOSE = true
86
- $: << File.join(File.dirname(__FILE__), '..')
87
- eval DATA.read, nil, $0, __LINE__ + 4
88
- end
89
-
90
- __END__
91
- require 'test/unit'
92
-
93
- class DuoTest < Test::Unit::TestCase
94
-
95
- def test_two_arguments
96
- duo = CodeRay::Duo[:ruby, :html]
97
- assert_kind_of CodeRay::Scanners[:ruby], duo.scanner
98
- assert_kind_of CodeRay::Encoders[:html], duo.encoder
99
- end
100
-
101
- def test_two_hash
102
- duo = CodeRay::Duo[:ruby => :html]
103
- assert_kind_of CodeRay::Scanners[:ruby], duo.scanner
104
- assert_kind_of CodeRay::Encoders[:html], duo.encoder
105
- end
106
-
107
- def test_call
108
- duo = CodeRay::Duo[:python => :yaml]
109
- assert_equal <<-'YAML', duo.call('def test: "pass"')
110
- ---
111
- - - def
112
- - :keyword
113
- - - " "
114
- - :space
115
- - - test
116
- - :method
117
- - - ":"
118
- - :operator
119
- - - " "
120
- - :space
121
- - - :begin_group
122
- - :string
123
- - - "\""
124
- - :delimiter
125
- - - pass
126
- - :content
127
- - - "\""
128
- - :delimiter
129
- - - :end_group
130
- - :string
131
- YAML
132
- end
133
-
134
- end
@@ -1,5 +1,5 @@
1
1
  module CodeRay
2
-
2
+
3
3
  # This module holds the Encoder class and its subclasses.
4
4
  # For example, the HTML encoder is named CodeRay::Encoders::HTML
5
5
  # can be found in coderay/encoders/html.
@@ -8,9 +8,10 @@ module CodeRay
8
8
  # mechanism and the [] method that returns the Encoder class
9
9
  # belonging to the given format.
10
10
  module Encoders
11
+
11
12
  extend PluginHost
12
13
  plugin_path File.dirname(__FILE__), 'encoders'
13
-
14
+
14
15
  # = Encoder
15
16
  #
16
17
  # The Encoder base class. Together with Scanner and
@@ -26,27 +27,27 @@ module CodeRay
26
27
  class Encoder
27
28
  extend Plugin
28
29
  plugin_host Encoders
29
-
30
+
30
31
  class << self
31
-
32
+
32
33
  # If FILE_EXTENSION isn't defined, this method returns the
33
34
  # downcase class name instead.
34
35
  def const_missing sym
35
36
  if sym == :FILE_EXTENSION
36
- plugin_id
37
+ plugin_id.to_s
37
38
  else
38
39
  super
39
40
  end
40
41
  end
41
-
42
+
42
43
  end
43
-
44
+
44
45
  # Subclasses are to store their default options in this constant.
45
46
  DEFAULT_OPTIONS = { }
46
-
47
+
47
48
  # The options you gave the Encoder at creating.
48
49
  attr_accessor :options
49
-
50
+
50
51
  # Creates a new Encoder.
51
52
  # +options+ is saved and used for all encode operations, as long
52
53
  # as you don't overwrite it there by passing additional options.
@@ -59,11 +60,9 @@ module CodeRay
59
60
  # added to the options you passed at creation.
60
61
  def initialize options = {}
61
62
  @options = self.class::DEFAULT_OPTIONS.merge options
62
- raise "I am only the basic Encoder class. I can't encode "\
63
- "anything. :( Use my subclasses." if self.class == Encoder
64
- $ALREADY_WARNED_OLD_INTERFACE = false
63
+ @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN = false
65
64
  end
66
-
65
+
67
66
  # Encode a Tokens object.
68
67
  def encode_tokens tokens, options = {}
69
68
  options = @options.merge options
@@ -71,7 +70,7 @@ module CodeRay
71
70
  compile tokens, options
72
71
  finish options
73
72
  end
74
-
73
+
75
74
  # Encode the given +code+ using the Scanner for +lang+.
76
75
  def encode code, lang, options = {}
77
76
  options = @options.merge options
@@ -81,33 +80,23 @@ module CodeRay
81
80
  CodeRay.scan code, lang, scanner_options
82
81
  finish options
83
82
  end
84
-
83
+
85
84
  # You can use highlight instead of encode, if that seems
86
85
  # more clear to you.
87
86
  alias highlight encode
88
-
87
+
89
88
  # Return the default file extension for outputs of this encoder.
90
89
  def file_extension
91
90
  self.class::FILE_EXTENSION
92
91
  end
93
92
 
94
93
  def << token
95
- warn 'Using old Tokens#<< interface.' unless $ALREADY_WARNED_OLD_INTERFACE
96
- $ALREADY_WARNED_OLD_INTERFACE = true
94
+ unless @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN
95
+ warn 'Using old Tokens#<< interface.'
96
+ @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN = true
97
+ end
97
98
  self.token(*token)
98
99
  end
99
-
100
- protected
101
-
102
- # Called with merged options before encoding starts.
103
- # Sets @out to an empty string.
104
- #
105
- # See the HTML Encoder for an example of option caching.
106
- def setup options
107
- @out = ''
108
- end
109
-
110
- public
111
100
 
112
101
  # Called with +content+ and +kind+ of the currently scanned token.
113
102
  # For simple scanners, it's enougth to implement this method.
@@ -127,12 +116,13 @@ module CodeRay
127
116
  when :end_line
128
117
  end_line kind
129
118
  else
130
- raise 'Unknown token content type: %p, kind = %p' % [content, kind]
119
+ raise ArgumentError, 'Unknown token content type: %p, kind = %p' % [content, kind]
131
120
  end
132
121
  end
133
122
 
134
123
  # Called for each text token ([text, kind]), where text is a String.
135
124
  def text_token text, kind
125
+ @out << text
136
126
  end
137
127
 
138
128
  # Starts a token group with the given +kind+.
@@ -153,6 +143,14 @@ module CodeRay
153
143
 
154
144
  protected
155
145
 
146
+ # Called with merged options before encoding starts.
147
+ # Sets @out to an empty string.
148
+ #
149
+ # See the HTML Encoder for an example of option caching.
150
+ def setup options
151
+ @out = ''
152
+ end
153
+
156
154
  # Called with merged options after encoding starts.
157
155
  # The return value is the result of encoding, typically @out.
158
156
  def finish options
@@ -178,11 +176,11 @@ module CodeRay
178
176
  end
179
177
  raise 'odd number list for Tokens' if content
180
178
  end
181
-
179
+
182
180
  alias tokens compile
183
181
  public :tokens
184
182
 
185
183
  end
186
-
184
+
187
185
  end
188
186
  end
@@ -1,6 +1,6 @@
1
1
  module CodeRay
2
2
  module Encoders
3
-
3
+
4
4
  map \
5
5
  :loc => :lines_of_code,
6
6
  :term => :terminal,
@@ -8,6 +8,8 @@ module Encoders
8
8
  :remove_comments => :comment_filter,
9
9
  :stats => :statistic,
10
10
  :tex => :latex
11
-
11
+
12
+ # No default because Tokens#nonsense would not raise NoMethodError.
13
+
12
14
  end
13
15
  end
@@ -1,4 +1,3 @@
1
- ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
2
1
  module CodeRay
3
2
  module Encoders
4
3
 
@@ -24,63 +23,3 @@ module Encoders
24
23
 
25
24
  end
26
25
  end
27
-
28
- if $0 == __FILE__
29
- $VERBOSE = true
30
- $: << File.join(File.dirname(__FILE__), '..')
31
- eval DATA.read, nil, $0, __LINE__ + 4
32
- end
33
-
34
- __END__
35
- require 'test/unit'
36
-
37
- class CommentFilterTest < Test::Unit::TestCase
38
-
39
- def test_filtering_comments
40
- tokens = CodeRay.scan <<-RUBY, :ruby
41
- #!/usr/bin/env ruby
42
- # a minimal Ruby program
43
- puts "Hello world!"
44
- RUBY
45
- assert_equal <<-RUBY_FILTERED, tokens.comment_filter.text
46
- #!/usr/bin/env ruby
47
-
48
- puts "Hello world!"
49
- RUBY_FILTERED
50
- end
51
-
52
- def test_filtering_docstrings
53
- tokens = CodeRay.scan <<-PYTHON, :python
54
- '''
55
- Assuming this is file mymodule.py then this string, being the
56
- first statement in the file will become the mymodule modules
57
- docstring when the file is imported
58
- '''
59
-
60
- class Myclass():
61
- """
62
- The class's docstring
63
- """
64
-
65
- def mymethod(self):
66
- '''The method's docstring'''
67
-
68
- def myfunction():
69
- """The function's docstring"""
70
- PYTHON
71
- assert_equal <<-PYTHON_FILTERED.chomp, tokens.comment_filter.text
72
-
73
-
74
- class Myclass():
75
-
76
-
77
- def mymethod(self):
78
-
79
-
80
- def myfunction():
81
-
82
-
83
- PYTHON_FILTERED
84
- end
85
-
86
- end
@@ -1,4 +1,3 @@
1
- ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
2
1
  module CodeRay
3
2
  module Encoders
4
3
 
@@ -15,6 +14,8 @@ module Encoders
15
14
  @out = 0
16
15
  end
17
16
 
17
+ public
18
+
18
19
  def text_token text, kind
19
20
  @out += 1
20
21
  end
@@ -30,25 +31,3 @@ module Encoders
30
31
 
31
32
  end
32
33
  end
33
-
34
- if $0 == __FILE__
35
- $VERBOSE = true
36
- $: << File.join(File.dirname(__FILE__), '..')
37
- eval DATA.read, nil, $0, __LINE__ + 4
38
- end
39
-
40
- __END__
41
- require 'test/unit'
42
-
43
- class CountTest < Test::Unit::TestCase
44
-
45
- def test_count
46
- tokens = CodeRay.scan <<-RUBY.strip, :ruby
47
- #!/usr/bin/env ruby
48
- # a minimal Ruby program
49
- puts "Hello world!"
50
- RUBY
51
- assert_equal 9, tokens.encode_with(:count)
52
- end
53
-
54
- end