coderay 1.0.0 → 1.0.0.598.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/FOLDERS +49 -0
  2. data/Rakefile +6 -5
  3. data/bin/coderay +74 -190
  4. data/bin/coderay_stylesheet +4 -0
  5. data/{README_INDEX.rdoc → lib/README} +20 -10
  6. data/lib/coderay.rb +60 -62
  7. data/lib/coderay/duo.rb +55 -2
  8. data/lib/coderay/encoder.rb +39 -52
  9. data/lib/coderay/encoders/_map.rb +7 -11
  10. data/lib/coderay/encoders/comment_filter.rb +61 -0
  11. data/lib/coderay/encoders/count.rb +26 -11
  12. data/lib/coderay/encoders/debug.rb +60 -11
  13. data/lib/coderay/encoders/div.rb +8 -9
  14. data/lib/coderay/encoders/filter.rb +52 -12
  15. data/lib/coderay/encoders/html.rb +113 -106
  16. data/lib/coderay/encoders/html/css.rb +7 -2
  17. data/lib/coderay/encoders/html/numbering.rb +27 -24
  18. data/lib/coderay/encoders/html/output.rb +58 -15
  19. data/lib/coderay/encoders/json.rb +44 -37
  20. data/lib/coderay/encoders/lines_of_code.rb +56 -9
  21. data/lib/coderay/encoders/null.rb +13 -6
  22. data/lib/coderay/encoders/page.rb +8 -8
  23. data/lib/coderay/encoders/span.rb +9 -10
  24. data/lib/coderay/encoders/statistic.rb +114 -51
  25. data/lib/coderay/encoders/terminal.rb +10 -7
  26. data/lib/coderay/encoders/text.rb +36 -17
  27. data/lib/coderay/encoders/token_kind_filter.rb +58 -1
  28. data/lib/coderay/encoders/xml.rb +11 -13
  29. data/lib/coderay/encoders/yaml.rb +14 -16
  30. data/lib/coderay/for_redcloth.rb +1 -1
  31. data/lib/coderay/helpers/file_type.rb +240 -125
  32. data/lib/coderay/helpers/gzip_simple.rb +123 -0
  33. data/lib/coderay/helpers/plugin.rb +307 -241
  34. data/lib/coderay/helpers/word_list.rb +126 -65
  35. data/lib/coderay/scanner.rb +103 -153
  36. data/lib/coderay/scanners/_map.rb +16 -18
  37. data/lib/coderay/scanners/c.rb +13 -13
  38. data/lib/coderay/scanners/cpp.rb +6 -6
  39. data/lib/coderay/scanners/css.rb +48 -47
  40. data/lib/coderay/scanners/debug.rb +55 -9
  41. data/lib/coderay/scanners/delphi.rb +4 -4
  42. data/lib/coderay/scanners/diff.rb +25 -43
  43. data/lib/coderay/scanners/groovy.rb +2 -2
  44. data/lib/coderay/scanners/html.rb +30 -107
  45. data/lib/coderay/scanners/java.rb +5 -6
  46. data/lib/coderay/scanners/java/builtin_types.rb +0 -2
  47. data/lib/coderay/scanners/java_script.rb +6 -6
  48. data/lib/coderay/scanners/json.rb +6 -7
  49. data/lib/coderay/scanners/nitro_xhtml.rb +136 -0
  50. data/lib/coderay/scanners/php.rb +12 -13
  51. data/lib/coderay/scanners/plaintext.rb +26 -0
  52. data/lib/coderay/scanners/python.rb +4 -4
  53. data/lib/coderay/scanners/{erb.rb → rhtml.rb} +11 -19
  54. data/lib/coderay/scanners/ruby.rb +208 -219
  55. data/lib/coderay/scanners/ruby/patterns.rb +85 -18
  56. data/lib/coderay/scanners/scheme.rb +136 -0
  57. data/lib/coderay/scanners/sql.rb +22 -29
  58. data/lib/coderay/scanners/yaml.rb +10 -11
  59. data/lib/coderay/styles/_map.rb +2 -2
  60. data/lib/coderay/styles/alpha.rb +104 -102
  61. data/lib/coderay/styles/cycnus.rb +143 -0
  62. data/lib/coderay/styles/murphy.rb +123 -0
  63. data/lib/coderay/token_kinds.rb +86 -87
  64. data/lib/coderay/tokens.rb +169 -26
  65. data/test/functional/basic.rb +14 -200
  66. data/test/functional/examples.rb +14 -20
  67. data/test/functional/for_redcloth.rb +8 -15
  68. data/test/functional/load_plugin_scanner.rb +11 -0
  69. data/test/functional/suite.rb +6 -9
  70. data/test/functional/vhdl.rb +126 -0
  71. data/test/functional/word_list.rb +79 -0
  72. metadata +129 -107
  73. data/lib/coderay/helpers/gzip.rb +0 -41
  74. data/lib/coderay/scanners/clojure.rb +0 -217
  75. data/lib/coderay/scanners/haml.rb +0 -168
  76. data/lib/coderay/scanners/ruby/string_state.rb +0 -71
  77. data/lib/coderay/scanners/text.rb +0 -26
  78. data/lib/coderay/tokens_proxy.rb +0 -55
  79. data/lib/coderay/version.rb +0 -3
data/lib/coderay.rb CHANGED
@@ -1,21 +1,16 @@
1
- # encoding: utf-8
2
- # Encoding.default_internal = 'UTF-8'
3
-
4
1
  # = CodeRay Library
5
2
  #
6
3
  # CodeRay is a Ruby library for syntax highlighting.
7
4
  #
8
- # I try to make CodeRay easy to use and intuitive, but at the same time fully
9
- # featured, complete, fast and efficient.
5
+ # I try to make CodeRay easy to use and intuitive, but at the same time fully featured, complete,
6
+ # fast and efficient.
10
7
  #
11
8
  # See README.
12
9
  #
13
10
  # It consists mainly of
14
- # * the main engine: CodeRay (Scanners::Scanner, Tokens, Encoders::Encoder)
15
- # * the plugin system: PluginHost, Plugin
11
+ # * the main engine: CodeRay (Scanners::Scanner, Tokens, Encoders::Encoder), PluginHost
16
12
  # * the scanners in CodeRay::Scanners
17
13
  # * the encoders in CodeRay::Encoders
18
- # * the styles in CodeRay::Styles
19
14
  #
20
15
  # Here's a fancy graphic to light up this gray docu:
21
16
  #
@@ -27,8 +22,8 @@
27
22
  #
28
23
  # == Usage
29
24
  #
30
- # Remember you need RubyGems to use CodeRay, unless you have it in your load
31
- # path. Run Ruby with -rubygems option if required.
25
+ # Remember you need RubyGems to use CodeRay, unless you have it in your load path. Run Ruby with
26
+ # -rubygems option if required.
32
27
  #
33
28
  # === Highlight Ruby code in a string as html
34
29
  #
@@ -127,30 +122,22 @@ module CodeRay
127
122
 
128
123
  $CODERAY_DEBUG ||= false
129
124
 
130
- require 'coderay/version'
131
-
132
- # helpers
133
- autoload :FileType, 'coderay/helpers/file_type'
134
-
135
- # Tokens
136
- autoload :Tokens, 'coderay/tokens'
137
- autoload :TokensProxy, 'coderay/tokens_proxy'
138
- autoload :TokenKinds, 'coderay/token_kinds'
139
-
140
- # Plugin system
141
- autoload :PluginHost, 'coderay/helpers/plugin'
142
- autoload :Plugin, 'coderay/helpers/plugin'
143
-
144
- # Plugins
145
- autoload :Scanners, 'coderay/scanner'
146
- autoload :Encoders, 'coderay/encoder'
147
- autoload :Styles, 'coderay/style'
148
-
149
- # Convenience access and reusable Encoder/Scanner pair
150
- autoload :Duo, 'coderay/duo'
151
-
125
+ # Version: Major.Minor.Teeny[.Revision]
126
+ # Major: 0 for pre-stable, 1 for stable
127
+ # Minor: feature milestone
128
+ # Teeny: development state, 0 for pre-release
129
+ # Revision: Subversion Revision number (generated on rake gem:make)
130
+ 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
+
152
139
  class << self
153
-
140
+
154
141
  # Scans the given +code+ (a String) with the Scanner for +lang+.
155
142
  #
156
143
  # This is a simple way to use CodeRay. Example:
@@ -159,15 +146,15 @@ module CodeRay
159
146
  #
160
147
  # See also demo/demo_simple.
161
148
  def scan code, lang, options = {}, &block
162
- # FIXME: return a proxy for direct-stream encoding
163
- TokensProxy.new code, lang, options, block
149
+ scanner = Scanners[lang].new code, options, &block
150
+ scanner.tokenize
164
151
  end
165
-
152
+
166
153
  # Scans +filename+ (a path to a code file) with the Scanner for +lang+.
167
154
  #
168
155
  # If +lang+ is :auto or omitted, the CodeRay::FileType module is used to
169
156
  # determine it. If it cannot find out what type it is, it uses
170
- # CodeRay::Scanners::Text.
157
+ # CodeRay::Scanners::Plaintext.
171
158
  #
172
159
  # Calls CodeRay.scan.
173
160
  #
@@ -175,22 +162,35 @@ module CodeRay
175
162
  # require 'coderay'
176
163
  # page = CodeRay.scan_file('some_c_code.c').html
177
164
  def scan_file filename, lang = :auto, options = {}, &block
178
- lang = FileType.fetch filename, :text, true if lang == :auto
179
- code = File.read filename
180
- scan code, lang, options, &block
165
+ file = IO.read filename
166
+ if lang == :auto
167
+ require 'coderay/helpers/file_type'
168
+ lang = FileType.fetch filename, :plaintext, true
169
+ end
170
+ scan file, lang, options = {}, &block
181
171
  end
182
-
172
+
183
173
  # Encode a string.
184
174
  #
185
175
  # This scans +code+ with the the Scanner for +lang+ and then
186
176
  # encodes it with the Encoder for +format+.
187
177
  # +options+ will be passed to the Encoder.
188
178
  #
189
- # See CodeRay::Encoder.encode.
179
+ # See CodeRay::Encoder.encode
190
180
  def encode code, lang, format, options = {}
191
181
  encoder(format, options).encode code, lang, options
192
182
  end
193
-
183
+
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
194
  # Encode pre-scanned Tokens.
195
195
  # Use this together with CodeRay.scan:
196
196
  #
@@ -203,7 +203,7 @@ module CodeRay
203
203
  def encode_tokens tokens, format, options = {}
204
204
  encoder(format, options).encode_tokens tokens, options
205
205
  end
206
-
206
+
207
207
  # Encodes +filename+ (a path to a code file) with the Scanner for +lang+.
208
208
  #
209
209
  # See CodeRay.scan_file.
@@ -216,17 +216,7 @@ module CodeRay
216
216
  tokens = scan_file filename, :auto, get_scanner_options(options)
217
217
  encode_tokens tokens, format, options
218
218
  end
219
-
220
- # Highlight a string into a HTML <div>.
221
- #
222
- # CSS styles use classes, so you have to include a stylesheet
223
- # in your output.
224
- #
225
- # See encode.
226
- def highlight code, lang, options = { :css => :class }, format = :div
227
- encode code, lang, format, options
228
- end
229
-
219
+
230
220
  # Highlight a file into a HTML <div>.
231
221
  #
232
222
  # CSS styles use classes, so you have to include a stylesheet
@@ -236,7 +226,7 @@ module CodeRay
236
226
  def highlight_file filename, options = { :css => :class }, format = :div
237
227
  encode_file filename, format, options
238
228
  end
239
-
229
+
240
230
  # Finds the Encoder class for +format+ and creates an instance, passing
241
231
  # +options+ to it.
242
232
  #
@@ -254,15 +244,15 @@ module CodeRay
254
244
  def encoder format, options = {}
255
245
  Encoders[format].new options
256
246
  end
257
-
247
+
258
248
  # Finds the Scanner class for +lang+ and creates an instance, passing
259
249
  # +options+ to it.
260
250
  #
261
251
  # See Scanner.new.
262
- def scanner lang, options = {}, &block
263
- Scanners[lang].new '', options, &block
252
+ def scanner lang, options = {}
253
+ Scanners[lang].new '', options
264
254
  end
265
-
255
+
266
256
  # Extract the options for the scanner from the +options+ hash.
267
257
  #
268
258
  # Returns an empty Hash if <tt>:scanner_options</tt> is not set.
@@ -272,7 +262,15 @@ module CodeRay
272
262
  def get_scanner_options options
273
263
  options.fetch :scanner_options, {}
274
264
  end
275
-
265
+
276
266
  end
277
-
267
+
268
+ 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
278
276
  end
data/lib/coderay/duo.rb CHANGED
@@ -1,3 +1,4 @@
1
+ ($:.unshift '..'; require 'coderay') unless defined? CodeRay
1
2
  module CodeRay
2
3
 
3
4
  # = Duo
@@ -21,7 +22,7 @@ module CodeRay
21
22
  # Create a new Duo, holding a lang and a format to highlight code.
22
23
  #
23
24
  # simple:
24
- # CodeRay::Duo[:ruby, :html].highlight 'bla 42'
25
+ # CodeRay::Duo[:ruby, :page].highlight 'bla 42'
25
26
  #
26
27
  # with options:
27
28
  # CodeRay::Duo[:ruby, :html, :hint => :debug].highlight '????::??'
@@ -35,7 +36,7 @@ module CodeRay
35
36
  # The options are forwarded to scanner and encoder
36
37
  # (see CodeRay.get_scanner_options).
37
38
  def initialize lang = nil, format = nil, options = {}
38
- if format.nil? && lang.is_a?(Hash) && lang.size == 1
39
+ if format == nil and lang.is_a? Hash and lang.size == 1
39
40
  @lang = lang.keys.first
40
41
  @format = lang[@lang]
41
42
  else
@@ -79,3 +80,55 @@ module CodeRay
79
80
  end
80
81
 
81
82
  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,10 +8,9 @@ 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
-
12
11
  extend PluginHost
13
12
  plugin_path File.dirname(__FILE__), 'encoders'
14
-
13
+
15
14
  # = Encoder
16
15
  #
17
16
  # The Encoder base class. Together with Scanner and
@@ -27,32 +26,27 @@ module CodeRay
27
26
  class Encoder
28
27
  extend Plugin
29
28
  plugin_host Encoders
30
-
29
+
31
30
  class << self
32
-
31
+
33
32
  # If FILE_EXTENSION isn't defined, this method returns the
34
33
  # downcase class name instead.
35
34
  def const_missing sym
36
35
  if sym == :FILE_EXTENSION
37
- (defined?(@plugin_id) && @plugin_id || name[/\w+$/].downcase).to_s
36
+ plugin_id
38
37
  else
39
38
  super
40
39
  end
41
40
  end
42
-
43
- # The default file extension for output file of this encoder class.
44
- def file_extension
45
- self::FILE_EXTENSION
46
- end
47
-
41
+
48
42
  end
49
-
43
+
50
44
  # Subclasses are to store their default options in this constant.
51
45
  DEFAULT_OPTIONS = { }
52
-
46
+
53
47
  # The options you gave the Encoder at creating.
54
- attr_accessor :options, :scanner
55
-
48
+ attr_accessor :options
49
+
56
50
  # Creates a new Encoder.
57
51
  # +options+ is saved and used for all encode operations, as long
58
52
  # as you don't overwrite it there by passing additional options.
@@ -65,43 +59,55 @@ module CodeRay
65
59
  # added to the options you passed at creation.
66
60
  def initialize options = {}
67
61
  @options = self.class::DEFAULT_OPTIONS.merge options
68
- @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN = false
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
69
65
  end
70
-
66
+
71
67
  # Encode a Tokens object.
72
68
  def encode_tokens tokens, options = {}
73
69
  options = @options.merge options
74
- @scanner = tokens.scanner if tokens.respond_to? :scanner
75
70
  setup options
76
71
  compile tokens, options
77
72
  finish options
78
73
  end
79
-
74
+
80
75
  # Encode the given +code+ using the Scanner for +lang+.
81
76
  def encode code, lang, options = {}
82
77
  options = @options.merge options
83
- @scanner = Scanners[lang].new code, CodeRay.get_scanner_options(options).update(:tokens => self)
84
78
  setup options
85
- @scanner.tokenize
79
+ scanner_options = CodeRay.get_scanner_options options
80
+ scanner_options[:tokens] = self
81
+ CodeRay.scan code, lang, scanner_options
86
82
  finish options
87
83
  end
88
-
84
+
89
85
  # You can use highlight instead of encode, if that seems
90
86
  # more clear to you.
91
87
  alias highlight encode
92
-
93
- # The default file extension for this encoder.
88
+
89
+ # Return the default file extension for outputs of this encoder.
94
90
  def file_extension
95
- self.class.file_extension
91
+ self.class::FILE_EXTENSION
96
92
  end
97
93
 
98
94
  def << token
99
- unless @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN
100
- warn 'Using old Tokens#<< interface.'
101
- @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN = true
102
- end
95
+ warn 'Using old Tokens#<< interface.' unless $ALREADY_WARNED_OLD_INTERFACE
96
+ $ALREADY_WARNED_OLD_INTERFACE = true
103
97
  self.token(*token)
104
98
  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
105
111
 
106
112
  # Called with +content+ and +kind+ of the currently scanned token.
107
113
  # For simple scanners, it's enougth to implement this method.
@@ -121,13 +127,12 @@ module CodeRay
121
127
  when :end_line
122
128
  end_line kind
123
129
  else
124
- raise ArgumentError, 'Unknown token content type: %p, kind = %p' % [content, kind]
130
+ raise 'Unknown token content type: %p, kind = %p' % [content, kind]
125
131
  end
126
132
  end
127
133
 
128
134
  # Called for each text token ([text, kind]), where text is a String.
129
135
  def text_token text, kind
130
- @out << text
131
136
  end
132
137
 
133
138
  # Starts a token group with the given +kind+.
@@ -148,24 +153,6 @@ module CodeRay
148
153
 
149
154
  protected
150
155
 
151
- # Called with merged options before encoding starts.
152
- # Sets @out to an empty string.
153
- #
154
- # See the HTML Encoder for an example of option caching.
155
- def setup options
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
167
- end
168
-
169
156
  # Called with merged options after encoding starts.
170
157
  # The return value is the result of encoding, typically @out.
171
158
  def finish options
@@ -191,11 +178,11 @@ module CodeRay
191
178
  end
192
179
  raise 'odd number list for Tokens' if content
193
180
  end
194
-
181
+
195
182
  alias tokens compile
196
183
  public :tokens
197
184
 
198
185
  end
199
-
186
+
200
187
  end
201
188
  end