coderay 0.7.1.147 → 0.7.2.165

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/bin/coderay +54 -56
  2. data/demo/suite.rb +54 -54
  3. data/lib/coderay.rb +187 -187
  4. data/lib/coderay/duo.rb +29 -29
  5. data/lib/coderay/encoder.rb +173 -173
  6. data/lib/coderay/encoders/_map.rb +8 -8
  7. data/lib/coderay/encoders/count.rb +21 -21
  8. data/lib/coderay/encoders/debug.rb +46 -46
  9. data/lib/coderay/encoders/div.rb +20 -20
  10. data/lib/coderay/encoders/html.rb +249 -245
  11. data/lib/coderay/encoders/html/classes.rb +73 -73
  12. data/lib/coderay/encoders/html/css.rb +65 -65
  13. data/lib/coderay/encoders/html/numerization.rb +122 -122
  14. data/lib/coderay/encoders/html/output.rb +195 -195
  15. data/lib/coderay/encoders/null.rb +26 -26
  16. data/lib/coderay/encoders/page.rb +21 -21
  17. data/lib/coderay/encoders/span.rb +20 -20
  18. data/lib/coderay/encoders/statistic.rb +81 -81
  19. data/lib/coderay/encoders/text.rb +33 -33
  20. data/lib/coderay/encoders/tokens.rb +44 -44
  21. data/lib/coderay/encoders/xml.rb +71 -71
  22. data/lib/coderay/encoders/yaml.rb +22 -22
  23. data/lib/coderay/helpers/filetype.rb +152 -153
  24. data/lib/coderay/helpers/gzip_simple.rb +67 -68
  25. data/lib/coderay/helpers/plugin.rb +297 -297
  26. data/lib/coderay/helpers/word_list.rb +46 -47
  27. data/lib/coderay/scanner.rb +238 -238
  28. data/lib/coderay/scanners/_map.rb +15 -14
  29. data/lib/coderay/scanners/c.rb +163 -155
  30. data/lib/coderay/scanners/delphi.rb +131 -129
  31. data/lib/coderay/scanners/html.rb +174 -167
  32. data/lib/coderay/scanners/nitro_xhtml.rb +130 -0
  33. data/lib/coderay/scanners/plaintext.rb +15 -15
  34. data/lib/coderay/scanners/rhtml.rb +73 -65
  35. data/lib/coderay/scanners/ruby.rb +404 -397
  36. data/lib/coderay/scanners/ruby/patterns.rb +216 -216
  37. data/lib/coderay/scanners/xml.rb +18 -18
  38. data/lib/coderay/style.rb +20 -20
  39. data/lib/coderay/styles/_map.rb +3 -3
  40. data/lib/coderay/styles/cycnus.rb +18 -18
  41. data/lib/coderay/styles/murphy.rb +18 -18
  42. data/lib/coderay/tokens.rb +322 -322
  43. metadata +86 -86
  44. data/lib/coderay/scanners/nitro_html.rb +0 -125
  45. data/lib/coderay/scanners/yaml.rb +0 -85
@@ -1,10 +1,10 @@
1
1
  # = WordList
2
- #
2
+ #
3
3
  # Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy cYcnus de>
4
4
  #
5
5
  # License:: LGPL / ask the author
6
6
  # Version:: 1.0 (2006-Feb-3)
7
- #
7
+ #
8
8
  # A WordList is a Hash with some additional features.
9
9
  # It is intended to be used for keyword recognition.
10
10
  #
@@ -14,11 +14,11 @@
14
14
  # For case insensitive words use CaseIgnoringWordList.
15
15
  #
16
16
  # Example:
17
- #
17
+ #
18
18
  # # define word arrays
19
19
  # RESERVED_WORDS = %w[
20
20
  # asm break case continue default do else
21
- # ...
21
+ # ...
22
22
  # ]
23
23
  #
24
24
  # PREDEFINED_TYPES = %w[
@@ -45,34 +45,33 @@
45
45
  # # use it
46
46
  # kind = IDENT_KIND[match]
47
47
  # ...
48
- #
49
48
  class WordList < Hash
50
49
 
51
- # Create a WordList for the given +words+.
52
- #
53
- # This WordList responds to [] with +true+, if the word is
54
- # in +words+, and with +false+ otherwise.
55
- def self.for words
56
- new.add words
57
- end
50
+ # Create a WordList for the given +words+.
51
+ #
52
+ # This WordList responds to [] with +true+, if the word is
53
+ # in +words+, and with +false+ otherwise.
54
+ def self.for words
55
+ new.add words
56
+ end
58
57
 
59
- # Creates a new WordList with +default+ as default value.
60
- def initialize default = false, &block
61
- super default, &block
62
- end
58
+ # Creates a new WordList with +default+ as default value.
59
+ def initialize default = false, &block
60
+ super default, &block
61
+ end
63
62
 
64
- # Checks if a word is included.
65
- def include? word
66
- has_key? word
67
- end
63
+ # Checks if a word is included.
64
+ def include? word
65
+ has_key? word
66
+ end
68
67
 
69
- # Add words to the list and associate them with +kind+.
70
- def add words, kind = true
71
- words.each do |word|
72
- self[word] = kind
73
- end
74
- self
75
- end
68
+ # Add words to the list and associate them with +kind+.
69
+ def add words, kind = true
70
+ words.each do |word|
71
+ self[word] = kind
72
+ end
73
+ self
74
+ end
76
75
 
77
76
  end
78
77
 
@@ -81,27 +80,27 @@ end
81
80
  # keys are compared case-insensitively.
82
81
  class CaseIgnoringWordList < WordList
83
82
 
84
- # Creates a new WordList with +default+ as default value.
85
- #
86
- # Text case is ignored.
87
- def initialize default = false, &block
88
- block ||= proc do |h, k|
89
- h[k] = h.fetch k.downcase, default
90
- end
91
- super default
92
- end
83
+ # Creates a new WordList with +default+ as default value.
84
+ #
85
+ # Text case is ignored.
86
+ def initialize default = false, &block
87
+ block ||= proc do |h, k|
88
+ h[k] = h.fetch k.downcase, default
89
+ end
90
+ super default
91
+ end
93
92
 
94
- # Checks if a word is included.
95
- def include? word
96
- has_key? word.downcase
97
- end
93
+ # Checks if a word is included.
94
+ def include? word
95
+ has_key? word.downcase
96
+ end
98
97
 
99
- # Add words to the list and associate them with +kind+.
100
- def add words, kind = true
101
- words.each do |word|
102
- self[word.downcase] = kind
103
- end
104
- self
105
- end
98
+ # Add words to the list and associate them with +kind+.
99
+ def add words, kind = true
100
+ words.each do |word|
101
+ self[word.downcase] = kind
102
+ end
103
+ self
104
+ end
106
105
 
107
106
  end
@@ -1,238 +1,238 @@
1
- module CodeRay
2
-
3
- require 'coderay/helpers/plugin'
4
-
5
- # = Scanners
6
- #
7
- # $Id: scanner.rb 143 2006-06-28 23:05:18Z murphy $
8
- #
9
- # This module holds the Scanner class and its subclasses.
10
- # For example, the Ruby scanner is named CodeRay::Scanners::Ruby
11
- # can be found in coderay/scanners/ruby.
12
- #
13
- # Scanner also provides methods and constants for the register
14
- # mechanism and the [] method that returns the Scanner class
15
- # belonging to the given lang.
16
- #
17
- # See PluginHost.
18
- module Scanners
19
- extend PluginHost
20
- plugin_path File.dirname(__FILE__), 'scanners'
21
-
22
- require 'strscan'
23
-
24
- # = Scanner
25
- #
26
- # The base class for all Scanners.
27
- #
28
- # It is a subclass of Ruby's great +StringScanner+, which
29
- # makes it easy to access the scanning methods inside.
30
- #
31
- # It is also +Enumerable+, so you can use it like an Array of
32
- # Tokens:
33
- #
34
- # require 'coderay'
35
- #
36
- # c_scanner = CodeRay::Scanners[:c].new "if (*p == '{') nest++;"
37
- #
38
- # for text, kind in c_scanner
39
- # puts text if kind == :operator
40
- # end
41
- #
42
- # # prints: (*==)++;
43
- #
44
- # OK, this is a very simple example :)
45
- # You can also use +map+, +any?+, +find+ and even +sort_by+,
46
- # if you want.
47
- class Scanner < StringScanner
48
- extend Plugin
49
- plugin_host Scanners
50
-
51
- # Raised if a Scanner fails while scanning
52
- ScanError = Class.new(Exception)
53
-
54
- require 'coderay/helpers/word_list'
55
-
56
- # The default options for all scanner classes.
57
- #
58
- # Define @default_options for subclasses.
59
- DEFAULT_OPTIONS = { :stream => false }
60
-
61
- class << self
62
-
63
- # Returns if the Scanner can be used in streaming mode.
64
- def streamable?
65
- is_a? Streamable
66
- end
67
-
68
- def normify code
69
- code = code.to_s.to_unix
70
- end
71
-
72
- end
73
-
74
- =begin
75
- ## Excluded for speed reasons; protected seems to make methods slow.
76
-
77
- # Save the StringScanner methods from being called.
78
- # This would not be useful for highlighting.
79
- strscan_public_methods =
80
- StringScanner.instance_methods -
81
- StringScanner.ancestors[1].instance_methods
82
- protected(*strscan_public_methods)
83
- =end
84
-
85
- # Create a new Scanner.
86
- #
87
- # * +code+ is the input String and is handled by the superclass
88
- # StringScanner.
89
- # * +options+ is a Hash with Symbols as keys.
90
- # It is merged with the default options of the class (you can
91
- # overwrite default options here.)
92
- # * +block+ is the callback for streamed highlighting.
93
- #
94
- # If you set :stream to +true+ in the options, the Scanner uses a
95
- # TokenStream with the +block+ as callback to handle the tokens.
96
- #
97
- # Else, a Tokens object is used.
98
- def initialize code='', options = {}, &block
99
- @options = self.class::DEFAULT_OPTIONS.merge options
100
- raise "I am only the basic Scanner class. I can't scan "\
101
- "anything. :( Use my subclasses." if self.class == Scanner
102
-
103
- super Scanner.normify(code)
104
-
105
- @tokens = options[:tokens]
106
- if @options[:stream]
107
- warn "warning in CodeRay::Scanner.new: :stream is set, "\
108
- "but no block was given" unless block_given?
109
- raise NotStreamableError, self unless kind_of? Streamable
110
- @tokens ||= TokenStream.new(&block)
111
- else
112
- warn "warning in CodeRay::Scanner.new: Block given, "\
113
- "but :stream is #{@options[:stream]}" if block_given?
114
- @tokens ||= Tokens.new
115
- end
116
-
117
- setup
118
- end
119
-
120
- # More mnemonic accessor name for the input string.
121
- alias code string
122
-
123
- def reset
124
- super
125
- reset_instance
126
- end
127
-
128
- def string= code
129
- code = Scanner.normify(code)
130
- super code
131
- reset_instance
132
- end
133
-
134
- # Scans the code and returns all tokens in a Tokens object.
135
- def tokenize new_string=nil, options = {}
136
- options = @options.merge(options)
137
- self.string = new_string if new_string
138
- @cached_tokens =
139
- if @options[:stream] # :stream must have been set already
140
- reset unless new_string
141
- scan_tokens @tokens, options
142
- @tokens
143
- else
144
- scan_tokens @tokens, options
145
- end
146
- end
147
-
148
- def tokens
149
- @cached_tokens ||= tokenize
150
- end
151
-
152
- # Traverses the tokens.
153
- def each &block
154
- raise ArgumentError,
155
- 'Cannot traverse TokenStream.' if @options[:stream]
156
- tokens.each(&block)
157
- end
158
- include Enumerable
159
-
160
- # The current line position of the scanner.
161
- #
162
- # Beware, this is implemented inefficiently. It should be used
163
- # for debugging only.
164
- def line
165
- string[0..pos].count("\n") + 1
166
- end
167
-
168
- protected
169
-
170
- # Can be implemented by subclasses to do some initialization
171
- # that has to be done once per instance.
172
- #
173
- # Use reset for initialization that has to be done once per
174
- # scan.
175
- def setup
176
- end
177
-
178
- # This is the central method, and commonly the only one a
179
- # subclass implements.
180
- #
181
- # Subclasses must implement this method; it must return +tokens+
182
- # and must only use Tokens#<< for storing scanned tokens!
183
- def scan_tokens tokens, options
184
- raise NotImplementedError,
185
- "#{self.class}#scan_tokens not implemented."
186
- end
187
-
188
- def reset_instance
189
- @tokens.clear unless @options[:keep_tokens]
190
- @cached_tokens = nil
191
- end
192
-
193
- # Scanner error with additional status information
194
- def raise_inspect msg, tokens, ambit = 30
195
- raise ScanError, <<-EOE % [
196
-
197
-
198
- ***ERROR in %s: %s
199
-
200
- tokens:
201
- %s
202
-
203
- current line: %d pos = %d
204
- matched: %p
205
- bol? = %p, eos? = %p
206
-
207
- surrounding code:
208
- %p ~~ %p
209
-
210
-
211
- ***ERROR***
212
-
213
- EOE
214
- File.basename(caller[0]),
215
- msg,
216
- tokens.last(10).map { |t| t.inspect }.join("\n"),
217
- line, pos,
218
- matched, bol?, eos?,
219
- string[pos-ambit,ambit],
220
- string[pos,ambit],
221
- ]
222
- end
223
-
224
- end
225
-
226
- end
227
- end
228
-
229
- class String
230
- # I love this hack. It seems to silence all dos/unix/mac newline problems.
231
- def to_unix
232
- if index ?\r
233
- gsub(/\r\n?/, "\n")
234
- else
235
- self
236
- end
237
- end
238
- end
1
+ module CodeRay
2
+
3
+ require 'coderay/helpers/plugin'
4
+
5
+ # = Scanners
6
+ #
7
+ # $Id: scanner.rb 154 2006-07-11 05:37:50Z murphy $
8
+ #
9
+ # This module holds the Scanner class and its subclasses.
10
+ # For example, the Ruby scanner is named CodeRay::Scanners::Ruby
11
+ # can be found in coderay/scanners/ruby.
12
+ #
13
+ # Scanner also provides methods and constants for the register
14
+ # mechanism and the [] method that returns the Scanner class
15
+ # belonging to the given lang.
16
+ #
17
+ # See PluginHost.
18
+ module Scanners
19
+ extend PluginHost
20
+ plugin_path File.dirname(__FILE__), 'scanners'
21
+
22
+ require 'strscan'
23
+
24
+ # = Scanner
25
+ #
26
+ # The base class for all Scanners.
27
+ #
28
+ # It is a subclass of Ruby's great +StringScanner+, which
29
+ # makes it easy to access the scanning methods inside.
30
+ #
31
+ # It is also +Enumerable+, so you can use it like an Array of
32
+ # Tokens:
33
+ #
34
+ # require 'coderay'
35
+ #
36
+ # c_scanner = CodeRay::Scanners[:c].new "if (*p == '{') nest++;"
37
+ #
38
+ # for text, kind in c_scanner
39
+ # puts text if kind == :operator
40
+ # end
41
+ #
42
+ # # prints: (*==)++;
43
+ #
44
+ # OK, this is a very simple example :)
45
+ # You can also use +map+, +any?+, +find+ and even +sort_by+,
46
+ # if you want.
47
+ class Scanner < StringScanner
48
+ extend Plugin
49
+ plugin_host Scanners
50
+
51
+ # Raised if a Scanner fails while scanning
52
+ ScanError = Class.new(Exception)
53
+
54
+ require 'coderay/helpers/word_list'
55
+
56
+ # The default options for all scanner classes.
57
+ #
58
+ # Define @default_options for subclasses.
59
+ DEFAULT_OPTIONS = { :stream => false }
60
+
61
+ class << self
62
+
63
+ # Returns if the Scanner can be used in streaming mode.
64
+ def streamable?
65
+ is_a? Streamable
66
+ end
67
+
68
+ def normify code
69
+ code = code.to_s.to_unix
70
+ end
71
+
72
+ end
73
+
74
+ =begin
75
+ ## Excluded for speed reasons; protected seems to make methods slow.
76
+
77
+ # Save the StringScanner methods from being called.
78
+ # This would not be useful for highlighting.
79
+ strscan_public_methods =
80
+ StringScanner.instance_methods -
81
+ StringScanner.ancestors[1].instance_methods
82
+ protected(*strscan_public_methods)
83
+ =end
84
+
85
+ # Create a new Scanner.
86
+ #
87
+ # * +code+ is the input String and is handled by the superclass
88
+ # StringScanner.
89
+ # * +options+ is a Hash with Symbols as keys.
90
+ # It is merged with the default options of the class (you can
91
+ # overwrite default options here.)
92
+ # * +block+ is the callback for streamed highlighting.
93
+ #
94
+ # If you set :stream to +true+ in the options, the Scanner uses a
95
+ # TokenStream with the +block+ as callback to handle the tokens.
96
+ #
97
+ # Else, a Tokens object is used.
98
+ def initialize code='', options = {}, &block
99
+ @options = self.class::DEFAULT_OPTIONS.merge options
100
+ raise "I am only the basic Scanner class. I can't scan "\
101
+ "anything. :( Use my subclasses." if self.class == Scanner
102
+
103
+ super Scanner.normify(code)
104
+
105
+ @tokens = options[:tokens]
106
+ if @options[:stream]
107
+ warn "warning in CodeRay::Scanner.new: :stream is set, "\
108
+ "but no block was given" unless block_given?
109
+ raise NotStreamableError, self unless kind_of? Streamable
110
+ @tokens ||= TokenStream.new(&block)
111
+ else
112
+ warn "warning in CodeRay::Scanner.new: Block given, "\
113
+ "but :stream is #{@options[:stream]}" if block_given?
114
+ @tokens ||= Tokens.new
115
+ end
116
+
117
+ setup
118
+ end
119
+
120
+ # More mnemonic accessor name for the input string.
121
+ alias code string
122
+
123
+ def reset
124
+ super
125
+ reset_instance
126
+ end
127
+
128
+ def string= code
129
+ code = Scanner.normify(code)
130
+ super code
131
+ reset_instance
132
+ end
133
+
134
+ # Scans the code and returns all tokens in a Tokens object.
135
+ def tokenize new_string=nil, options = {}
136
+ options = @options.merge(options)
137
+ self.string = new_string if new_string
138
+ @cached_tokens =
139
+ if @options[:stream] # :stream must have been set already
140
+ reset unless new_string
141
+ scan_tokens @tokens, options
142
+ @tokens
143
+ else
144
+ scan_tokens @tokens, options
145
+ end
146
+ end
147
+
148
+ def tokens
149
+ @cached_tokens ||= tokenize
150
+ end
151
+
152
+ # Traverses the tokens.
153
+ def each &block
154
+ raise ArgumentError,
155
+ 'Cannot traverse TokenStream.' if @options[:stream]
156
+ tokens.each(&block)
157
+ end
158
+ include Enumerable
159
+
160
+ # The current line position of the scanner.
161
+ #
162
+ # Beware, this is implemented inefficiently. It should be used
163
+ # for debugging only.
164
+ def line
165
+ string[0..pos].count("\n") + 1
166
+ end
167
+
168
+ protected
169
+
170
+ # Can be implemented by subclasses to do some initialization
171
+ # that has to be done once per instance.
172
+ #
173
+ # Use reset for initialization that has to be done once per
174
+ # scan.
175
+ def setup
176
+ end
177
+
178
+ # This is the central method, and commonly the only one a
179
+ # subclass implements.
180
+ #
181
+ # Subclasses must implement this method; it must return +tokens+
182
+ # and must only use Tokens#<< for storing scanned tokens!
183
+ def scan_tokens tokens, options
184
+ raise NotImplementedError,
185
+ "#{self.class}#scan_tokens not implemented."
186
+ end
187
+
188
+ def reset_instance
189
+ @tokens.clear unless @options[:keep_tokens]
190
+ @cached_tokens = nil
191
+ end
192
+
193
+ # Scanner error with additional status information
194
+ def raise_inspect msg, tokens, state = nil, ambit = 30
195
+ raise ScanError, <<-EOE % [
196
+
197
+
198
+ ***ERROR in %s: %s
199
+
200
+ tokens:
201
+ %s
202
+
203
+ current line: %d pos = %d
204
+ matched: %p state: %p
205
+ bol? = %p, eos? = %p
206
+
207
+ surrounding code:
208
+ %p ~~ %p
209
+
210
+
211
+ ***ERROR***
212
+
213
+ EOE
214
+ File.basename(caller[0]),
215
+ msg,
216
+ tokens.last(10).map { |t| t.inspect }.join("\n"),
217
+ line, pos,
218
+ matched, state, bol?, eos?,
219
+ string[pos-ambit,ambit],
220
+ string[pos,ambit],
221
+ ]
222
+ end
223
+
224
+ end
225
+
226
+ end
227
+ end
228
+
229
+ class String
230
+ # I love this hack. It seems to silence all dos/unix/mac newline problems.
231
+ def to_unix
232
+ if index ?\r
233
+ gsub(/\r\n?/, "\n")
234
+ else
235
+ self
236
+ end
237
+ end
238
+ end