coderay 1.0.0.738.pre → 1.0.0.778.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/{lib/README → README.rdoc} +0 -0
- data/Rakefile +5 -5
- data/lib/coderay.rb +2 -2
- data/lib/coderay/encoder.rb +4 -4
- data/lib/coderay/encoders/_map.rb +3 -1
- data/lib/coderay/encoders/html.rb +9 -17
- data/lib/coderay/encoders/html/numbering.rb +5 -5
- data/lib/coderay/encoders/html/output.rb +3 -3
- data/lib/coderay/encoders/lines_of_code.rb +6 -9
- data/lib/coderay/encoders/statistic.rb +26 -25
- data/lib/coderay/encoders/terminal.rb +2 -2
- data/lib/coderay/encoders/text.rb +9 -9
- data/lib/coderay/encoders/xml.rb +8 -8
- data/lib/coderay/encoders/yaml.rb +5 -6
- data/lib/coderay/for_redcloth.rb +1 -1
- data/lib/coderay/helpers/file_type.rb +44 -42
- data/lib/coderay/helpers/plugin.rb +9 -17
- data/lib/coderay/helpers/word_list.rb +65 -126
- data/lib/coderay/scanner.rb +13 -5
- data/lib/coderay/scanners/_map.rb +3 -2
- data/lib/coderay/scanners/c.rb +5 -5
- data/lib/coderay/scanners/clojure.rb +27 -14
- data/lib/coderay/scanners/cpp.rb +5 -5
- data/lib/coderay/scanners/css.rb +1 -1
- data/lib/coderay/scanners/html.rb +60 -29
- data/lib/coderay/scanners/java.rb +2 -2
- data/lib/coderay/scanners/java_script.rb +1 -1
- data/lib/coderay/scanners/nitro_xhtml.rb +1 -1
- data/lib/coderay/scanners/php.rb +4 -4
- data/lib/coderay/scanners/python.rb +2 -2
- data/lib/coderay/scanners/rhtml.rb +1 -1
- data/lib/coderay/scanners/ruby.rb +4 -11
- data/lib/coderay/scanners/ruby/patterns.rb +1 -1
- data/lib/coderay/scanners/scheme.rb +2 -2
- data/lib/coderay/scanners/sql.rb +26 -19
- data/lib/coderay/scanners/text.rb +26 -0
- data/lib/coderay/styles/alpha.rb +7 -6
- data/lib/coderay/token_kinds.rb +5 -5
- data/test/functional/basic.rb +1 -1
- data/test/functional/examples.rb +5 -3
- metadata +85 -85
- data/lib/coderay/scanners/plaintext.rb +0 -26
@@ -1,15 +1,15 @@
|
|
1
1
|
module CodeRay
|
2
2
|
module Encoders
|
3
|
-
|
3
|
+
|
4
4
|
# = YAML Encoder
|
5
5
|
#
|
6
6
|
# Slow.
|
7
7
|
class YAML < Encoder
|
8
|
-
|
8
|
+
|
9
9
|
register_for :yaml
|
10
|
-
|
10
|
+
|
11
11
|
FILE_EXTENSION = 'yaml'
|
12
|
-
|
12
|
+
|
13
13
|
protected
|
14
14
|
def setup options
|
15
15
|
require 'yaml'
|
@@ -21,7 +21,6 @@ module Encoders
|
|
21
21
|
end
|
22
22
|
|
23
23
|
public
|
24
|
-
|
25
24
|
def text_token text, kind
|
26
25
|
@out << [text, kind]
|
27
26
|
end
|
@@ -43,6 +42,6 @@ module Encoders
|
|
43
42
|
end
|
44
43
|
|
45
44
|
end
|
46
|
-
|
45
|
+
|
47
46
|
end
|
48
47
|
end
|
data/lib/coderay/for_redcloth.rb
CHANGED
@@ -45,7 +45,7 @@ module CodeRay
|
|
45
45
|
if !opts[:lang] && RedCloth::VERSION.to_s >= '4.2.0'
|
46
46
|
# simulating pre-4.2 behavior
|
47
47
|
if opts[:text].sub!(/\A\[(\w+)\]/, '')
|
48
|
-
if CodeRay::Scanners[$1].plugin_id == :
|
48
|
+
if CodeRay::Scanners[$1].plugin_id == :text
|
49
49
|
opts[:text] = $& + opts[:text]
|
50
50
|
else
|
51
51
|
opts[:lang] = $1
|
@@ -7,13 +7,13 @@ module CodeRay
|
|
7
7
|
# == Usage
|
8
8
|
#
|
9
9
|
# # determine the type of the given
|
10
|
-
# lang = FileType[
|
10
|
+
# lang = FileType[file_name]
|
11
11
|
#
|
12
|
-
# # return :
|
13
|
-
# lang = FileType.fetch
|
12
|
+
# # return :text if the file type is unknown
|
13
|
+
# lang = FileType.fetch file_name, :text
|
14
14
|
#
|
15
15
|
# # try the shebang line, too
|
16
|
-
# lang = FileType.fetch
|
16
|
+
# lang = FileType.fetch file_name, :text, true
|
17
17
|
module FileType
|
18
18
|
|
19
19
|
UnknownFileType = Class.new Exception
|
@@ -77,46 +77,48 @@ module CodeRay
|
|
77
77
|
end
|
78
78
|
|
79
79
|
TypeFromExt = {
|
80
|
-
'c'
|
81
|
-
'
|
82
|
-
'
|
83
|
-
'
|
84
|
-
'
|
85
|
-
'
|
86
|
-
'
|
87
|
-
'
|
88
|
-
'
|
89
|
-
'
|
90
|
-
'
|
80
|
+
'c' => :c,
|
81
|
+
'cfc' => :xml,
|
82
|
+
'cfm' => :xml,
|
83
|
+
'clj' => :clojure,
|
84
|
+
'css' => :css,
|
85
|
+
'diff' => :diff,
|
86
|
+
'dpr' => :delphi,
|
87
|
+
'gemspec' => :ruby,
|
88
|
+
'groovy' => :groovy,
|
89
|
+
'gvy' => :groovy,
|
90
|
+
'h' => :c,
|
91
|
+
'htm' => :html,
|
92
|
+
'html' => :html,
|
91
93
|
'html.erb' => :rhtml,
|
92
|
-
'java'
|
93
|
-
'js'
|
94
|
-
'json'
|
95
|
-
'mab'
|
96
|
-
'pas'
|
97
|
-
'patch'
|
98
|
-
'php'
|
99
|
-
'php3'
|
100
|
-
'php4'
|
101
|
-
'php5'
|
102
|
-
'py'
|
103
|
-
'py3'
|
104
|
-
'pyw'
|
105
|
-
'rake'
|
94
|
+
'java' => :java,
|
95
|
+
'js' => :java_script,
|
96
|
+
'json' => :json,
|
97
|
+
'mab' => :ruby,
|
98
|
+
'pas' => :delphi,
|
99
|
+
'patch' => :diff,
|
100
|
+
'php' => :php,
|
101
|
+
'php3' => :php,
|
102
|
+
'php4' => :php,
|
103
|
+
'php5' => :php,
|
104
|
+
'py' => :python,
|
105
|
+
'py3' => :python,
|
106
|
+
'pyw' => :python,
|
107
|
+
'rake' => :ruby,
|
106
108
|
'raydebug' => :raydebug,
|
107
|
-
'rb'
|
108
|
-
'rbw'
|
109
|
-
'rhtml'
|
110
|
-
'rjs'
|
111
|
-
'rpdf'
|
112
|
-
'rxml'
|
113
|
-
'sch'
|
114
|
-
'sql'
|
115
|
-
'ss'
|
116
|
-
'xhtml'
|
117
|
-
'xml'
|
118
|
-
'yaml'
|
119
|
-
'yml'
|
109
|
+
'rb' => :ruby,
|
110
|
+
'rbw' => :ruby,
|
111
|
+
'rhtml' => :rhtml,
|
112
|
+
'rjs' => :ruby,
|
113
|
+
'rpdf' => :ruby,
|
114
|
+
'rxml' => :ruby,
|
115
|
+
'sch' => :scheme,
|
116
|
+
'sql' => :sql,
|
117
|
+
'ss' => :scheme,
|
118
|
+
'xhtml' => :xhtml,
|
119
|
+
'xml' => :xml,
|
120
|
+
'yaml' => :yaml,
|
121
|
+
'yml' => :yaml,
|
120
122
|
}
|
121
123
|
for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
|
122
124
|
TypeFromExt[cpp_alias] = :cpp
|
@@ -120,14 +120,12 @@ module CodeRay
|
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
123
|
-
# Every plugin must register itself for
|
124
|
-
#
|
123
|
+
# Every plugin must register itself for +id+ by calling register_for,
|
124
|
+
# which calls this method.
|
125
125
|
#
|
126
126
|
# See Plugin#register_for.
|
127
|
-
def register plugin,
|
128
|
-
|
129
|
-
plugin_hash[validate_id(id)] = plugin
|
130
|
-
end
|
127
|
+
def register plugin, id
|
128
|
+
plugin_hash[validate_id(id)] = plugin
|
131
129
|
end
|
132
130
|
|
133
131
|
# A Hash of plugion_id => Plugin pairs.
|
@@ -154,13 +152,6 @@ module CodeRay
|
|
154
152
|
plugin_hash.values.grep(Class)
|
155
153
|
end
|
156
154
|
|
157
|
-
# Returns an array of all plugin titles.
|
158
|
-
#
|
159
|
-
# Note: This loads all plugins using load_all.
|
160
|
-
def all_titles
|
161
|
-
all_plugins.map { |plugin| plugin.title }
|
162
|
-
end
|
163
|
-
|
164
155
|
protected
|
165
156
|
|
166
157
|
# Return a plugin hash that automatically loads plugins.
|
@@ -239,7 +230,8 @@ module CodeRay
|
|
239
230
|
# See CodeRay::PluginHost for examples.
|
240
231
|
module Plugin
|
241
232
|
|
242
|
-
# Register this class for the given
|
233
|
+
# Register this class for the given +id+.
|
234
|
+
#
|
243
235
|
# Example:
|
244
236
|
# class MyPlugin < PluginHost::BaseClass
|
245
237
|
# register_for :my_id
|
@@ -247,9 +239,9 @@ module CodeRay
|
|
247
239
|
# end
|
248
240
|
#
|
249
241
|
# See PluginHost.register.
|
250
|
-
def register_for
|
251
|
-
@plugin_id =
|
252
|
-
plugin_host.register self,
|
242
|
+
def register_for id
|
243
|
+
@plugin_id = id
|
244
|
+
plugin_host.register self, id
|
253
245
|
end
|
254
246
|
|
255
247
|
# Returns the title of the plugin, or sets it to the
|
@@ -1,138 +1,77 @@
|
|
1
1
|
module CodeRay
|
2
|
-
|
3
|
-
# = WordList
|
4
|
-
#
|
5
|
-
# <b>A Hash subclass designed for mapping word lists to token types.</b>
|
6
|
-
#
|
7
|
-
# Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de>
|
8
|
-
#
|
9
|
-
# License:: LGPL / ask the author
|
10
|
-
# Version:: 1.1 (2006-Oct-19)
|
11
|
-
#
|
12
|
-
# A WordList is a Hash with some additional features.
|
13
|
-
# It is intended to be used for keyword recognition.
|
14
|
-
#
|
15
|
-
# WordList is highly optimized to be used in Scanners,
|
16
|
-
# typically to decide whether a given ident is a special token.
|
17
|
-
#
|
18
|
-
# For case insensitive words use CaseIgnoringWordList.
|
19
|
-
#
|
20
|
-
# Example:
|
21
|
-
#
|
22
|
-
# # define word arrays
|
23
|
-
# RESERVED_WORDS = %w[
|
24
|
-
# asm break case continue default do else
|
25
|
-
# ...
|
26
|
-
# ]
|
27
|
-
#
|
28
|
-
# PREDEFINED_TYPES = %w[
|
29
|
-
# int long short char void
|
30
|
-
# ...
|
31
|
-
# ]
|
32
|
-
#
|
33
|
-
# PREDEFINED_CONSTANTS = %w[
|
34
|
-
# EOF NULL ...
|
35
|
-
# ]
|
36
|
-
#
|
37
|
-
# # make a WordList
|
38
|
-
# IDENT_KIND = WordList.new(:ident).
|
39
|
-
# add(RESERVED_WORDS, :reserved).
|
40
|
-
# add(PREDEFINED_TYPES, :pre_type).
|
41
|
-
# add(PREDEFINED_CONSTANTS, :pre_constant)
|
42
|
-
#
|
43
|
-
# ...
|
44
|
-
#
|
45
|
-
# def scan_tokens tokens, options
|
46
|
-
# ...
|
47
|
-
#
|
48
|
-
# elsif scan(/[A-Za-z_][A-Za-z_0-9]*/)
|
49
|
-
# # use it
|
50
|
-
# kind = IDENT_KIND[match]
|
51
|
-
# ...
|
52
|
-
class WordList < Hash
|
53
|
-
|
54
|
-
# Creates a new WordList with +default+ as default value.
|
55
|
-
#
|
56
|
-
# You can activate +caching+ to store the results for every [] request.
|
2
|
+
|
3
|
+
# = WordList
|
57
4
|
#
|
58
|
-
#
|
59
|
-
# as you expect. Therefore, it is recommended to use the [] method only.
|
60
|
-
def initialize default = false, caching = false, &block
|
61
|
-
if block
|
62
|
-
raise ArgumentError, 'Can\'t combine block with caching.' if caching
|
63
|
-
super(&block)
|
64
|
-
else
|
65
|
-
if caching
|
66
|
-
super() do |h, k|
|
67
|
-
h[k] = h.fetch k, default
|
68
|
-
end
|
69
|
-
else
|
70
|
-
super default
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
# Add words to the list and associate them with +kind+.
|
5
|
+
# <b>A Hash subclass designed for mapping word lists to token types.</b>
|
76
6
|
#
|
77
|
-
#
|
78
|
-
|
79
|
-
|
80
|
-
|
7
|
+
# Copyright (c) 2006-2011 by murphy (Kornelius Kalnbach) <murphy rubychan de>
|
8
|
+
#
|
9
|
+
# License:: LGPL / ask the author
|
10
|
+
# Version:: 2.0 (2011-05-08)
|
11
|
+
#
|
12
|
+
# A WordList is a Hash with some additional features.
|
13
|
+
# It is intended to be used for keyword recognition.
|
14
|
+
#
|
15
|
+
# WordList is optimized to be used in Scanners,
|
16
|
+
# typically to decide whether a given ident is a special token.
|
17
|
+
#
|
18
|
+
# For case insensitive words use CaseIgnoringWordList.
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
#
|
22
|
+
# # define word arrays
|
23
|
+
# RESERVED_WORDS = %w[
|
24
|
+
# asm break case continue default do else
|
25
|
+
# ]
|
26
|
+
#
|
27
|
+
# PREDEFINED_TYPES = %w[
|
28
|
+
# int long short char void
|
29
|
+
# ]
|
30
|
+
#
|
31
|
+
# # make a WordList
|
32
|
+
# IDENT_KIND = WordList.new(:ident).
|
33
|
+
# add(RESERVED_WORDS, :reserved).
|
34
|
+
# add(PREDEFINED_TYPES, :predefined_type)
|
35
|
+
#
|
36
|
+
# ...
|
37
|
+
#
|
38
|
+
# def scan_tokens tokens, options
|
39
|
+
# ...
|
40
|
+
#
|
41
|
+
# elsif scan(/[A-Za-z_][A-Za-z_0-9]*/)
|
42
|
+
# # use it
|
43
|
+
# kind = IDENT_KIND[match]
|
44
|
+
# ...
|
45
|
+
class WordList < Hash
|
46
|
+
|
47
|
+
# Create a new WordList with +default+ as default value.
|
48
|
+
def initialize default = false
|
49
|
+
super default
|
81
50
|
end
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
# keys are compared case-insensitively.
|
90
|
-
#
|
91
|
-
# Ignoring the text case is realized by sending the +downcase+ message to
|
92
|
-
# all keys.
|
93
|
-
#
|
94
|
-
# Caching usually makes a CaseIgnoringWordList faster, but it has to be
|
95
|
-
# activated explicitely.
|
96
|
-
class CaseIgnoringWordList < WordList
|
97
|
-
|
98
|
-
# Creates a new case-insensitive WordList with +default+ as default value.
|
99
|
-
#
|
100
|
-
# You can activate caching to store the results for every [] request.
|
101
|
-
# This speeds up subsequent lookups for the same word, but also
|
102
|
-
# uses memory.
|
103
|
-
def initialize default = false, caching = false
|
104
|
-
if caching
|
105
|
-
super(default, false) do |h, k|
|
106
|
-
h[k] = h.fetch k.downcase, default
|
107
|
-
end
|
108
|
-
else
|
109
|
-
super(default, false)
|
110
|
-
extend Uncached
|
51
|
+
|
52
|
+
# Add words to the list and associate them with +value+.
|
53
|
+
#
|
54
|
+
# Returns +self+, so you can concat add calls.
|
55
|
+
def add words, value = true
|
56
|
+
words.each { |word| self[word] = value }
|
57
|
+
self
|
111
58
|
end
|
59
|
+
|
112
60
|
end
|
113
61
|
|
114
|
-
|
62
|
+
|
63
|
+
# A CaseIgnoringWordList is like a WordList, only that
|
64
|
+
# keys are compared case-insensitively (normalizing keys using +downcase+).
|
65
|
+
class CaseIgnoringWordList < WordList
|
66
|
+
|
115
67
|
def [] key
|
116
|
-
super
|
68
|
+
super key.downcase
|
117
69
|
end
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
def add words, kind = true
|
122
|
-
words.each do |word|
|
123
|
-
self[word.downcase] = kind
|
70
|
+
|
71
|
+
def []= key, value
|
72
|
+
super key.downcase, value
|
124
73
|
end
|
125
|
-
|
74
|
+
|
126
75
|
end
|
127
|
-
|
128
|
-
end
|
129
|
-
|
76
|
+
|
130
77
|
end
|
131
|
-
|
132
|
-
__END__
|
133
|
-
# check memory consumption
|
134
|
-
END {
|
135
|
-
ObjectSpace.each_object(CodeRay::CaseIgnoringWordList) do |wl|
|
136
|
-
p wl.inject(0) { |memo, key, value| memo + key.size + 24 }
|
137
|
-
end
|
138
|
-
}
|
data/lib/coderay/scanner.rb
CHANGED
@@ -52,7 +52,7 @@ module CodeRay
|
|
52
52
|
plugin_host Scanners
|
53
53
|
|
54
54
|
# Raised if a Scanner fails while scanning
|
55
|
-
ScanError = Class.new
|
55
|
+
ScanError = Class.new StandardError
|
56
56
|
|
57
57
|
# The default options for all scanner classes.
|
58
58
|
#
|
@@ -67,7 +67,7 @@ module CodeRay
|
|
67
67
|
# scanner's internal encoding, with invalid and undefined charachters
|
68
68
|
# replaced by placeholders. Always returns a new object.
|
69
69
|
def normalize code
|
70
|
-
original = code
|
70
|
+
# original = code
|
71
71
|
code = code.to_s unless code.is_a? ::String
|
72
72
|
if code.respond_to? :encoding
|
73
73
|
code = encode_with_encoding code, self.encoding
|
@@ -96,7 +96,7 @@ module CodeRay
|
|
96
96
|
def encode_with_encoding code, target_encoding
|
97
97
|
if code.encoding == target_encoding
|
98
98
|
if code.valid_encoding?
|
99
|
-
return to_unix
|
99
|
+
return to_unix(code)
|
100
100
|
else
|
101
101
|
source_encoding = guess_encoding code
|
102
102
|
end
|
@@ -282,8 +282,8 @@ surrounding code:
|
|
282
282
|
EOE
|
283
283
|
File.basename(caller[0]),
|
284
284
|
msg,
|
285
|
-
tokens.size,
|
286
|
-
tokens.last(10).map { |t| t.inspect }.join("\n"),
|
285
|
+
tokens.respond_to?(:size) ? tokens.size : 0,
|
286
|
+
tokens.respond_to?(:last) ? tokens.last(10).map { |t| t.inspect }.join("\n") : '',
|
287
287
|
line, column, pos,
|
288
288
|
matched, state, bol?, eos?,
|
289
289
|
string[pos - ambit, ambit],
|
@@ -291,6 +291,14 @@ surrounding code:
|
|
291
291
|
]
|
292
292
|
end
|
293
293
|
|
294
|
+
# Shorthand for scan_until(/\z/).
|
295
|
+
# This method also avoids a JRuby 1.9 mode bug.
|
296
|
+
def scan_rest
|
297
|
+
rest = self.rest
|
298
|
+
terminate
|
299
|
+
rest
|
300
|
+
end
|
301
|
+
|
294
302
|
end
|
295
303
|
|
296
304
|
end
|