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.
- data/bin/coderay +54 -56
- data/demo/suite.rb +54 -54
- data/lib/coderay.rb +187 -187
- data/lib/coderay/duo.rb +29 -29
- data/lib/coderay/encoder.rb +173 -173
- data/lib/coderay/encoders/_map.rb +8 -8
- data/lib/coderay/encoders/count.rb +21 -21
- data/lib/coderay/encoders/debug.rb +46 -46
- data/lib/coderay/encoders/div.rb +20 -20
- data/lib/coderay/encoders/html.rb +249 -245
- data/lib/coderay/encoders/html/classes.rb +73 -73
- data/lib/coderay/encoders/html/css.rb +65 -65
- data/lib/coderay/encoders/html/numerization.rb +122 -122
- data/lib/coderay/encoders/html/output.rb +195 -195
- data/lib/coderay/encoders/null.rb +26 -26
- data/lib/coderay/encoders/page.rb +21 -21
- data/lib/coderay/encoders/span.rb +20 -20
- data/lib/coderay/encoders/statistic.rb +81 -81
- data/lib/coderay/encoders/text.rb +33 -33
- data/lib/coderay/encoders/tokens.rb +44 -44
- data/lib/coderay/encoders/xml.rb +71 -71
- data/lib/coderay/encoders/yaml.rb +22 -22
- data/lib/coderay/helpers/filetype.rb +152 -153
- data/lib/coderay/helpers/gzip_simple.rb +67 -68
- data/lib/coderay/helpers/plugin.rb +297 -297
- data/lib/coderay/helpers/word_list.rb +46 -47
- data/lib/coderay/scanner.rb +238 -238
- data/lib/coderay/scanners/_map.rb +15 -14
- data/lib/coderay/scanners/c.rb +163 -155
- data/lib/coderay/scanners/delphi.rb +131 -129
- data/lib/coderay/scanners/html.rb +174 -167
- data/lib/coderay/scanners/nitro_xhtml.rb +130 -0
- data/lib/coderay/scanners/plaintext.rb +15 -15
- data/lib/coderay/scanners/rhtml.rb +73 -65
- data/lib/coderay/scanners/ruby.rb +404 -397
- data/lib/coderay/scanners/ruby/patterns.rb +216 -216
- data/lib/coderay/scanners/xml.rb +18 -18
- data/lib/coderay/style.rb +20 -20
- data/lib/coderay/styles/_map.rb +3 -3
- data/lib/coderay/styles/cycnus.rb +18 -18
- data/lib/coderay/styles/murphy.rb +18 -18
- data/lib/coderay/tokens.rb +322 -322
- metadata +86 -86
- data/lib/coderay/scanners/nitro_html.rb +0 -125
- 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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
63
|
+
# Checks if a word is included.
|
64
|
+
def include? word
|
65
|
+
has_key? word
|
66
|
+
end
|
68
67
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
93
|
+
# Checks if a word is included.
|
94
|
+
def include? word
|
95
|
+
has_key? word.downcase
|
96
|
+
end
|
98
97
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
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
|
data/lib/coderay/scanner.rb
CHANGED
@@ -1,238 +1,238 @@
|
|
1
|
-
module CodeRay
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
def normify code
|
69
|
-
code = code.to_s.to_unix
|
70
|
-
end
|
71
|
-
|
72
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
=end
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
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
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
end
|
228
|
-
|
229
|
-
class String
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
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
|