coderay 0.4.3.48

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/LICENSE +340 -0
  2. data/README +103 -0
  3. data/demo/demo_count.rb +10 -0
  4. data/demo/demo_css.rb +4 -0
  5. data/demo/demo_div.rb +19 -0
  6. data/demo/demo_dump.rb +15 -0
  7. data/demo/demo_encoder.rb +39 -0
  8. data/demo/demo_global_vars.rb +13 -0
  9. data/demo/demo_global_vars2.rb +28 -0
  10. data/demo/demo_html.rb +394 -0
  11. data/demo/demo_html2.rb +11 -0
  12. data/demo/demo_load_encoder.rb +17 -0
  13. data/demo/demo_more.rb +204 -0
  14. data/demo/demo_scanner.rb +36 -0
  15. data/demo/demo_server.rb +92 -0
  16. data/demo/demo_simple.rb +10 -0
  17. data/demo/demo_stream.rb +25 -0
  18. data/demo/demo_stream2.rb +8 -0
  19. data/demo/demo_tokens.rb +3 -0
  20. data/lib/coderay.rb +284 -0
  21. data/lib/coderay/encoder.rb +151 -0
  22. data/lib/coderay/encoders/count.rb +21 -0
  23. data/lib/coderay/encoders/div.rb +16 -0
  24. data/lib/coderay/encoders/helpers/html_css.rb +155 -0
  25. data/lib/coderay/encoders/helpers/html_helper.rb +68 -0
  26. data/lib/coderay/encoders/helpers/html_output.rb +237 -0
  27. data/lib/coderay/encoders/html.rb +169 -0
  28. data/lib/coderay/encoders/null.rb +20 -0
  29. data/lib/coderay/encoders/span.rb +16 -0
  30. data/lib/coderay/encoders/statistic.rb +74 -0
  31. data/lib/coderay/encoders/text.rb +33 -0
  32. data/lib/coderay/encoders/tokens.rb +44 -0
  33. data/lib/coderay/encoders/yaml.rb +19 -0
  34. data/lib/coderay/helpers/filetype.rb +145 -0
  35. data/lib/coderay/helpers/gzip_simple.rb +123 -0
  36. data/lib/coderay/helpers/plugin.rb +286 -0
  37. data/lib/coderay/helpers/scanner_helper.rb +63 -0
  38. data/lib/coderay/scanner.rb +197 -0
  39. data/lib/coderay/scanners/c.rb +147 -0
  40. data/lib/coderay/scanners/delphi.rb +123 -0
  41. data/lib/coderay/scanners/helpers/ruby_helper.rb +212 -0
  42. data/lib/coderay/scanners/plaintext.rb +13 -0
  43. data/lib/coderay/scanners/ruby.rb +337 -0
  44. data/lib/coderay/tokens.rb +324 -0
  45. metadata +89 -0
@@ -0,0 +1,324 @@
1
+ module CodeRay
2
+
3
+ # = Tokens
4
+ #
5
+ # The Tokens class represents a list of tokens returnd from
6
+ # a Scanner.
7
+ #
8
+ # A token is not a special object, just a two-element Array
9
+ # consisting of
10
+ # * the _token_ _kind_ (a Symbol representing the type of the token)
11
+ # * the _token_ _text_ (the original source of the token in a String)
12
+ #
13
+ # A token looks like this:
14
+ #
15
+ # [:comment, '# It looks like this']
16
+ # [:float, '3.1415926']
17
+ # [:error, '���']
18
+ #
19
+ # Some scanners also yield some kind of sub-tokens, represented by special
20
+ # token texts, namely :open and :close .
21
+ #
22
+ # The Ruby scanner, for example, splits "a string" into:
23
+ #
24
+ # [
25
+ # [:open, :string],
26
+ # [:delimiter, '"'],
27
+ # [:content, 'a string'],
28
+ # [:delimiter, '"'],
29
+ # [:close, :string]
30
+ # ]
31
+ #
32
+ # Tokens is also the interface between Scanners and Encoders:
33
+ # The input is split and saved into a Tokens object. The Encoder
34
+ # then builds the output from this object.
35
+ #
36
+ # Thus, the syntax below becomes clear:
37
+ #
38
+ # CodeRay.scan('price = 2.59', :ruby).html
39
+ # # the Tokens object is here -------^
40
+ #
41
+ # See how small it is? ;)
42
+ #
43
+ # Tokens gives you the power to handle pre-scanned code very easily:
44
+ # You can convert it to a webpage, a YAML file, or dump it into a gzip'ed string
45
+ # that you put in your DB.
46
+ #
47
+ # Tokens' subclass TokenStream allows streaming to save memory.
48
+ class Tokens < Array
49
+
50
+ class << self
51
+
52
+ # Convert the token to a string.
53
+ #
54
+ # This format is used by Encoders.Tokens.
55
+ # It can be reverted using read_token.
56
+ def write_token text, type
57
+ if text.is_a? String
58
+ "#{type}\t#{escape(text)}\n"
59
+ else
60
+ ":#{text}\t#{type}\t\n"
61
+ end
62
+ end
63
+
64
+ # Read a token from the string.
65
+ #
66
+ # Inversion of write_token.
67
+ #
68
+ # TODO Test this!
69
+ def read_token token
70
+ type, text = token.split("\t", 2)
71
+ if type[0] == ?:
72
+ [text.to_sym, type[1..-1].to_sym]
73
+ else
74
+ [type.to_sym, unescape(text)]
75
+ end
76
+ end
77
+
78
+ # Escapes a string for use in write_token.
79
+ def escape text
80
+ text.gsub(/[\n\\]/, '\\\\\&')
81
+ end
82
+
83
+ # Unescapes a string created by escape.
84
+ def unescape text
85
+ text.gsub(/\\[\n\\]/) { |m| m[1,1] }
86
+ end
87
+
88
+ end
89
+
90
+ # Whether the object is a TokenStream.
91
+ #
92
+ # Returns false.
93
+ def stream?
94
+ false
95
+ end
96
+
97
+ alias :orig_each :each
98
+ # Iterates over all tokens.
99
+ #
100
+ # If a filter is given, only tokens of that kind are yielded.
101
+ def each kind_filter = nil, &block
102
+ unless kind_filter
103
+ orig_each(&block)
104
+ else
105
+ orig_each do |text, kind|
106
+ next unless kind == kind_filter
107
+ yield text, kind
108
+ end
109
+ end
110
+ end
111
+
112
+ # Iterates over all text tokens.
113
+ # Range tokens like [:open, :string] are left out.
114
+ #
115
+ # Example:
116
+ # tokens.each_text_token { |text, kind| text.replace html_escape(text) }
117
+ def each_text_token
118
+ orig_each do |text, kind|
119
+ next unless text.respond_to? :to_str
120
+ yield text, kind
121
+ end
122
+ end
123
+
124
+ # Encode the tokens using encoder.
125
+ #
126
+ # encoder can be
127
+ # * a symbol like :html oder :statistic
128
+ # * an Encoder class
129
+ # * an Encoder object
130
+ #
131
+ # options are passed to the encoder.
132
+ def encode encoder, options = {}
133
+ unless encoder.is_a? Encoders::Encoder
134
+ unless encoder.is_a? Class
135
+ encoder_class = Encoders[encoder]
136
+ end
137
+ encoder = encoder_class.new options
138
+ end
139
+ encoder.encode_tokens self, options
140
+ end
141
+
142
+
143
+ # Turn into a string using Encoders::Text.
144
+ #
145
+ # +options+ are passed to the encoder if given.
146
+ def to_s options = {}
147
+ encode :text, options
148
+ end
149
+
150
+
151
+ # Redirects unknown methods to encoder calls.
152
+ #
153
+ # For example, if you call +tokens.html+, the HTML encoder
154
+ # is used to highlight the tokens.
155
+ def method_missing meth, options = {}
156
+ Encoders[meth].new(options).encode_tokens self
157
+ end
158
+
159
+ # Returns the tokens compressed by joining consecutive
160
+ # tokens of the same kind.
161
+ #
162
+ # This can not be undone, but should yield the same output
163
+ # in most Encoders. It basically makes the output smaller.
164
+ #
165
+ # Combined with dump, it saves space for the cost
166
+ # calculating time.
167
+ #
168
+ # If the scanner is written carefully, this is not required -
169
+ # for example, consecutive //-comment lines can already be
170
+ # joined in one token by the Scanner.
171
+ def optimize
172
+ print ' Tokens#optimize: before: %d - ' % size if $DEBUG
173
+ last_kind = last_text = nil
174
+ new = self.class.new
175
+ each do |text, kind|
176
+ if text.is_a? String
177
+ if kind == last_kind
178
+ last_text << text
179
+ else
180
+ new << [last_text, last_kind] if last_kind
181
+ last_text = text
182
+ last_kind = kind
183
+ end
184
+ else
185
+ new << [last_text, last_kind] if last_kind
186
+ last_kind = last_text = nil
187
+ new << [text, kind]
188
+ end
189
+ end
190
+ new << [last_text, last_kind] if last_kind
191
+ print 'after: %d (%d saved = %2.0f%%)' %
192
+ [new.size, size - new.size, 1.0 - (new.size.to_f / size)] if $DEBUG
193
+ new
194
+ end
195
+
196
+ # Compact the object itself; see optimize.
197
+ def optimize!
198
+ replace optimize
199
+ end
200
+
201
+ # Dumps the object into a String that can be saved
202
+ # in files or databases.
203
+ #
204
+ # The dump is created with Marshal.dump;
205
+ # In addition, it is gzipped using GZip.gzip.
206
+ #
207
+ # The returned String object includes Undumping
208
+ # so it has an #undump method. See Tokens.load.
209
+ #
210
+ # You can configure the level of compression,
211
+ # but the default value 7 should be what you want
212
+ # in most cases as it is a good comprimise between
213
+ # speed and compression rate.
214
+ #
215
+ # See GZip module.
216
+ def dump gzip_level = 7
217
+ require 'coderay/helpers/gzip_simple'
218
+ dump = Marshal.dump self
219
+ dump = dump.gzip gzip_level
220
+ dump.extend Undumping
221
+ end
222
+
223
+ # The total size of the tokens;
224
+ # Should be equal to the input size before
225
+ # scanning.
226
+ def text_size
227
+ map { |t, k| t }.join.size
228
+ end
229
+
230
+ # Include this module to give an object an #undump
231
+ # method.
232
+ #
233
+ # The string returned by Tokens.dump includes Undumping.
234
+ module Undumping
235
+ # Calls Tokens.load with itself.
236
+ def undump
237
+ Tokens.load self
238
+ end
239
+ end
240
+
241
+ # Undump the object using Marshal.load, then
242
+ # unzip it using GZip.gunzip.
243
+ #
244
+ # The result is commonly a Tokens object, but
245
+ # this is not guaranteed.
246
+ def Tokens.load dump
247
+ require 'coderay/helpers/gzip_simple'
248
+ dump = dump.gunzip
249
+ @dump = Marshal.load dump
250
+ end
251
+
252
+ end
253
+
254
+
255
+ # = TokenStream
256
+ #
257
+ # The TokenStream class is a fake Array without elements.
258
+ #
259
+ # It redirects the method << to a block given at creation.
260
+ #
261
+ # This allows scanners and Encoders to use streaming (no
262
+ # tokens are saved, the input is highlighted the same time it
263
+ # is scanned) with the same code.
264
+ #
265
+ # See CodeRay.encode_stream and CodeRay.scan_stream
266
+ class TokenStream < Tokens
267
+
268
+ # Whether the object is a TokenStream.
269
+ #
270
+ # Returns true.
271
+ def stream?
272
+ true
273
+ end
274
+
275
+ # The Array is empty, but size counts the tokens given by <<.
276
+ attr_reader :size
277
+
278
+ # Creates a new TokenStream that calls +block+ whenever
279
+ # its << method is called.
280
+ #
281
+ # Example:
282
+ #
283
+ # require 'coderay'
284
+ #
285
+ # token_stream = CodeRay::TokenStream.new do |kind, text|
286
+ # puts 'kind: %s, text size: %d.' % [kind, text.size]
287
+ # end
288
+ #
289
+ # token_stream << [:regexp, '/\d+/']
290
+ # #-> kind: rexpexp, text size: 5.
291
+ #
292
+ def initialize &block
293
+ raise ArgumentError, 'Block expected for streaming.' unless block
294
+ @callback = block
295
+ @size = 0
296
+ end
297
+
298
+ # Calls +block+ with +token+ and increments size.
299
+ #
300
+ # Returns self.
301
+ def << token
302
+ @callback.call token
303
+ @size += 1
304
+ self
305
+ end
306
+
307
+ # This method is not implemented due to speed reasons. Use Tokens.
308
+ def text_size
309
+ raise NotImplementedError, 'This method is not implemented due to speed reasons.'
310
+ end
311
+
312
+ # A TokenStream cannot be dumped. Use Tokens.
313
+ def dump
314
+ raise NotImplementedError, 'A TokenStream cannot be dumped.'
315
+ end
316
+
317
+ # A TokenStream cannot be optimized. Use Tokens.
318
+ def optimize
319
+ raise NotImplementedError, 'A TokenStream cannot be optimized.'
320
+ end
321
+
322
+ end
323
+
324
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: coderay
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.4.3.48
7
+ date: 2005-10-01
8
+ summary: CodeRay is a fast syntax highlighter engine for many languages.
9
+ require_paths:
10
+ - lib
11
+ email: murphy@cYcnus.de
12
+ homepage: http://rd.cycnus.de/coderay
13
+ rubyforge_project: coderay
14
+ description: "CodeRay is a Ruby library for syntax highlighting. I try to make CodeRay easy to
15
+ use and intuitive, but at the same time fully featured, complete, fast and
16
+ efficient. Usage is simple: require 'coderay' code = 'some %q(weird (Ruby)
17
+ can't shock) me!' puts CodeRay.scan(code, :ruby).html"
18
+ autorequire: coderay
19
+ default_executable:
20
+ bindir: bin
21
+ has_rdoc: true
22
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
23
+ requirements:
24
+ -
25
+ - ">"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.0.0
28
+ version:
29
+ platform: ruby
30
+ authors:
31
+ - murphy
32
+ files:
33
+ - "./lib/coderay.rb"
34
+ - "./lib/coderay/encoder.rb"
35
+ - "./lib/coderay/scanner.rb"
36
+ - "./lib/coderay/tokens.rb"
37
+ - "./lib/coderay/encoders/yaml.rb"
38
+ - "./lib/coderay/encoders/div.rb"
39
+ - "./lib/coderay/encoders/tokens.rb"
40
+ - "./lib/coderay/encoders/statistic.rb"
41
+ - "./lib/coderay/encoders/null.rb"
42
+ - "./lib/coderay/encoders/span.rb"
43
+ - "./lib/coderay/encoders/html.rb"
44
+ - "./lib/coderay/encoders/text.rb"
45
+ - "./lib/coderay/encoders/count.rb"
46
+ - "./lib/coderay/encoders/helpers/html_helper.rb"
47
+ - "./lib/coderay/encoders/helpers/html_output.rb"
48
+ - "./lib/coderay/encoders/helpers/html_css.rb"
49
+ - "./lib/coderay/helpers/plugin.rb"
50
+ - "./lib/coderay/helpers/filetype.rb"
51
+ - "./lib/coderay/helpers/gzip_simple.rb"
52
+ - "./lib/coderay/helpers/scanner_helper.rb"
53
+ - "./lib/coderay/scanners/c.rb"
54
+ - "./lib/coderay/scanners/delphi.rb"
55
+ - "./lib/coderay/scanners/plaintext.rb"
56
+ - "./lib/coderay/scanners/ruby.rb"
57
+ - "./lib/coderay/scanners/helpers/ruby_helper.rb"
58
+ - "./demo/demo_global_vars2.rb"
59
+ - "./demo/demo_encoder.rb"
60
+ - "./demo/demo_div.rb"
61
+ - "./demo/demo_count.rb"
62
+ - "./demo/demo_simple.rb"
63
+ - "./demo/demo_tokens.rb"
64
+ - "./demo/demo_css.rb"
65
+ - "./demo/demo_html.rb"
66
+ - "./demo/demo_dump.rb"
67
+ - "./demo/demo_stream2.rb"
68
+ - "./demo/demo_load_encoder.rb"
69
+ - "./demo/demo_html2.rb"
70
+ - "./demo/demo_scanner.rb"
71
+ - "./demo/demo_stream.rb"
72
+ - "./demo/demo_global_vars.rb"
73
+ - "./demo/demo_more.rb"
74
+ - "./demo/demo_server.rb"
75
+ - "./README"
76
+ - "./LICENSE"
77
+ test_files: []
78
+ rdoc_options:
79
+ - "-SNw2"
80
+ - "-mREADME"
81
+ - "-a"
82
+ - "-t CodeRay Documentation"
83
+ extra_rdoc_files:
84
+ - "./README"
85
+ executables: []
86
+ extensions: []
87
+ requirements:
88
+ - strscan
89
+ dependencies: []