coderay 1.1.0.rc2 → 1.1.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c0077f84e2d594d0f25b74fa857af9ef648296f
4
- data.tar.gz: 8a18fd96a16808dcad2244f2ed61f32d60fb4f54
3
+ metadata.gz: 8bff592dbd8aa41d4ddeb5670a64cd73885d82c2
4
+ data.tar.gz: 662f10398eb79840e188251c077a62985a68ee2e
5
5
  SHA512:
6
- metadata.gz: a62e8ec122a108c5585d8539d3a6885c1adb51c7b1042231c0b27a5d3d44f998329285aacb72d2e575e8e11e09a653741f5eceaece1bb14340d45f07e1f3d72e
7
- data.tar.gz: 50ee917b13eaec6627578d14d6b324c887a3b89600c6f313afb8004a578d7dbcb0465812500c89894c6a9e0140904d7c6a427d64e6a6235fb2a4bf08791732db
6
+ metadata.gz: 937b65024bdd37d6ebd39af906f12d3f4e8a43b58631ad72c48c310ef4802828cda9c4c74a9a644799bf59c56c7bafeae86d559e39833bf61f50e4a59893a861
7
+ data.tar.gz: da3ae5dd02dfa9df53d1d22f99ec133c1823df1c657afa34c77f34fb152cbe426fe4869464200f4ce4fd7da66c3aff6b37611da8d2ef38ea94756421b14ad3c7
@@ -18,7 +18,8 @@ module Encoders
18
18
  register_for :debug_lint
19
19
 
20
20
  def text_token text, kind
21
- raise Lint::EmptyToken, 'empty token' if text.empty?
21
+ raise Lint::EmptyToken, 'empty token for %p' % [kind] if text.empty?
22
+ raise Lint::UnknownTokenKind, 'unknown token kind %p (text was %p)' % [kind, text] unless TokenKinds.has_key? kind
22
23
  super
23
24
  end
24
25
 
@@ -285,7 +285,7 @@ module Encoders
285
285
 
286
286
  def make_span_for_kinds method, hint
287
287
  Hash.new do |h, kinds|
288
- h[kinds.is_a?(Symbol) ? kinds : kinds.dup] = begin
288
+ begin
289
289
  css_class = css_class_for_kinds(kinds)
290
290
  title = HTML.token_path_to_hint hint, kinds if hint
291
291
 
@@ -297,6 +297,9 @@ module Encoders
297
297
  "<span#{title}#{" class=\"#{css_class}\"" if css_class}>"
298
298
  end
299
299
  end
300
+ end.tap do |span|
301
+ h.clear if h.size >= 100
302
+ h[kinds] = span
300
303
  end
301
304
  end
302
305
  end
@@ -309,8 +312,8 @@ module Encoders
309
312
 
310
313
  def break_lines text, style
311
314
  reopen = ''
312
- @opened.each_with_index do |k, index|
313
- reopen << (@span_for_kinds[index > 0 ? [k, *@opened[0...index]] : k] || '<span>')
315
+ @opened.each_with_index do |kind, index|
316
+ reopen << (@span_for_kinds[index > 0 ? [kind, *@opened[0...index]] : kind] || '<span>')
314
317
  end
315
318
  text.gsub("\n", "#{'</span>' * @opened.size}#{'</span>' if style}\n#{reopen}#{style}")
316
319
  end
@@ -17,10 +17,12 @@ module Encoders
17
17
 
18
18
  InvalidTokenStream = Class.new StandardError
19
19
  EmptyToken = Class.new InvalidTokenStream
20
+ UnknownTokenKind = Class.new InvalidTokenStream
20
21
  IncorrectTokenGroupNesting = Class.new InvalidTokenStream
21
22
 
22
23
  def text_token text, kind
23
- raise EmptyToken, 'empty token' if text.empty?
24
+ raise EmptyToken, 'empty token for %p' % [kind] if text.empty?
25
+ raise UnknownTokenKind, 'unknown token kind %p (text was %p)' % [kind, text] unless TokenKinds.has_key? kind
24
26
  end
25
27
 
26
28
  def begin_group kind
@@ -30,7 +30,7 @@ module CodeRay
30
30
  # * a file could not be found
31
31
  # * the requested Plugin is not registered
32
32
  PluginNotFound = Class.new LoadError
33
- HostNotFound = Class.new LoadError
33
+ HostNotFound = Class.new LoadError
34
34
 
35
35
  PLUGIN_HOSTS = []
36
36
  PLUGIN_HOSTS_BY_ID = {} # dummy hash
@@ -49,8 +49,8 @@ module CodeRay
49
49
  def [] id, *args, &blk
50
50
  plugin = validate_id(id)
51
51
  begin
52
- plugin = plugin_hash.[] plugin, *args, &blk
53
- end while plugin.is_a? Symbol
52
+ plugin = plugin_hash.[](plugin, *args, &blk)
53
+ end while plugin.is_a? String
54
54
  plugin
55
55
  end
56
56
 
@@ -95,7 +95,7 @@ module CodeRay
95
95
  def map hash
96
96
  for from, to in hash
97
97
  from = validate_id from
98
- to = validate_id to
98
+ to = validate_id to
99
99
  plugin_hash[from] = to unless plugin_hash.has_key? from
100
100
  end
101
101
  end
@@ -197,22 +197,22 @@ module CodeRay
197
197
  File.join plugin_path, "#{plugin_id}.rb"
198
198
  end
199
199
 
200
- # Converts +id+ to a Symbol if it is a String,
201
- # or returns +id+ if it already is a Symbol.
200
+ # Converts +id+ to a valid plugin ID String, or returns +nil+.
202
201
  #
203
202
  # Raises +ArgumentError+ for all other objects, or if the
204
203
  # given String includes non-alphanumeric characters (\W).
205
204
  def validate_id id
206
- if id.is_a? Symbol or id.nil?
207
- id
208
- elsif id.is_a? String
205
+ case id
206
+ when Symbol
207
+ id.to_s
208
+ when String
209
209
  if id[/\w+/] == id
210
- id.downcase.to_sym
210
+ id.downcase
211
211
  else
212
212
  raise ArgumentError, "Invalid id given: #{id}"
213
213
  end
214
214
  else
215
- raise ArgumentError, "String or Symbol expected, but #{id.class} given."
215
+ raise ArgumentError, "Symbol or String expected, but #{id.class} given."
216
216
  end
217
217
  end
218
218
 
@@ -1,9 +1,11 @@
1
+ require 'set'
2
+
1
3
  module CodeRay
2
4
  module Scanners
3
5
 
4
6
  # = Debug Scanner
5
7
  #
6
- # Interprets the output of the Encoders::Debug encoder.
8
+ # Interprets the output of the Encoders::Debug encoder (basically the inverse function).
7
9
  class Debug < Scanner
8
10
 
9
11
  register_for :debug
@@ -11,6 +13,11 @@ module Scanners
11
13
 
12
14
  protected
13
15
 
16
+ def setup
17
+ super
18
+ @known_token_kinds = TokenKinds.keys.map(&:to_s).to_set
19
+ end
20
+
14
21
  def scan_tokens encoder, options
15
22
 
16
23
  opened_tokens = []
@@ -21,16 +28,19 @@ module Scanners
21
28
  encoder.text_token match, :space
22
29
 
23
30
  elsif match = scan(/ (\w+) \( ( [^\)\\]* ( \\. [^\)\\]* )* ) \)? /x)
24
- kind = self[1].to_sym
25
- match = self[2].gsub(/\\(.)/m, '\1')
26
- unless TokenKinds.has_key? kind
27
- kind = :error
28
- match = matched
31
+ if @known_token_kinds.include? self[1]
32
+ encoder.text_token self[2].gsub(/\\(.)/m, '\1'), self[1].to_sym
33
+ else
34
+ encoder.text_token matched, :unknown
29
35
  end
30
- encoder.text_token match, kind
31
36
 
32
37
  elsif match = scan(/ (\w+) ([<\[]) /x)
33
- kind = self[1].to_sym
38
+ if @known_token_kinds.include? self[1]
39
+ kind = self[1].to_sym
40
+ else
41
+ kind = :unknown
42
+ end
43
+
34
44
  opened_tokens << kind
35
45
  case self[2]
36
46
  when '<'
@@ -1,9 +1,11 @@
1
+ require 'set'
2
+
1
3
  module CodeRay
2
4
  module Scanners
3
5
 
4
- # = Debug Scanner
6
+ # = Raydebug Scanner
5
7
  #
6
- # Parses the output of the Encoders::Debug encoder.
8
+ # Highlights the output of the Encoders::Debug encoder.
7
9
  class Raydebug < Scanner
8
10
 
9
11
  register_for :raydebug
@@ -12,6 +14,11 @@ module Scanners
12
14
 
13
15
  protected
14
16
 
17
+ def setup
18
+ super
19
+ @known_token_kinds = TokenKinds.keys.map(&:to_s).to_set
20
+ end
21
+
15
22
  def scan_tokens encoder, options
16
23
 
17
24
  opened_tokens = []
@@ -26,20 +33,22 @@ module Scanners
26
33
  encoder.text_token kind, :class
27
34
  encoder.text_token '(', :operator
28
35
  match = self[2]
29
- encoder.text_token match, kind.to_sym unless match.empty?
36
+ unless match.empty?
37
+ if @known_token_kinds.include? kind
38
+ encoder.text_token match, kind.to_sym
39
+ else
40
+ encoder.text_token match, :plain
41
+ end
42
+ end
30
43
  encoder.text_token match, :operator if match = scan(/\)/)
31
44
 
32
45
  elsif match = scan(/ (\w+) ([<\[]) /x)
33
- kind = self[1]
34
- case self[2]
35
- when '<'
36
- encoder.text_token kind, :class
37
- when '['
38
- encoder.text_token kind, :class
46
+ encoder.text_token self[1], :class
47
+ if @known_token_kinds.include? self[1]
48
+ kind = self[1].to_sym
39
49
  else
40
- raise 'CodeRay bug: This case should not be reached.'
50
+ kind = :unknown
41
51
  end
42
- kind = kind.to_sym
43
52
  opened_tokens << kind
44
53
  encoder.begin_group kind
45
54
  encoder.text_token self[2], :operator
@@ -157,13 +157,16 @@ module Scanners
157
157
  yield
158
158
  ])
159
159
 
160
- FANCY_STRING_START = / % ( [QqrsWwx] | (?![a-zA-Z0-9]) ) ([^a-zA-Z0-9]) /x
160
+ FANCY_STRING_START = / % ( [iIqQrswWx] | (?![a-zA-Z0-9]) ) ([^a-zA-Z0-9]) /x
161
161
  FANCY_STRING_KIND = Hash.new(:string).merge({
162
+ 'i' => :symbol,
163
+ 'I' => :symbol,
162
164
  'r' => :regexp,
163
165
  's' => :symbol,
164
166
  'x' => :shell,
165
167
  })
166
168
  FANCY_STRING_INTERPRETED = Hash.new(true).merge({
169
+ 'i' => false,
167
170
  'q' => false,
168
171
  's' => false,
169
172
  'w' => false,
@@ -16,7 +16,6 @@ module Scanners
16
16
 
17
17
  STRING_PATTERN = Hash.new do |h, k|
18
18
  delim, interpreted = *k
19
- # delim = delim.dup # workaround for old Ruby
20
19
  delim_pattern = Regexp.escape(delim)
21
20
  if closing_paren = CLOSING_PAREN[delim]
22
21
  delim_pattern << Regexp.escape(closing_paren)
@@ -29,12 +28,13 @@ module Scanners
29
28
  # '| [|?*+(){}\[\].^$]'
30
29
  # end
31
30
 
32
- h[k] =
33
- if interpreted && delim != '#'
34
- / (?= [#{delim_pattern}] | \# [{$@] ) /mx
35
- else
36
- / (?= [#{delim_pattern}] ) /mx
37
- end
31
+ if interpreted && delim != '#'
32
+ / (?= [#{delim_pattern}] | \# [{$@] ) /mx
33
+ else
34
+ / (?= [#{delim_pattern}] ) /mx
35
+ end.tap do |pattern|
36
+ h[k] = pattern if (delim.respond_to?(:ord) ? delim.ord : delim[0]) < 256
37
+ end
38
38
  end
39
39
 
40
40
  def initialize kind, interpreted, delim, heredoc = false
@@ -125,7 +125,7 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
125
125
  .string .modifier { color: #E40 }
126
126
  .symbol { color:#A60 }
127
127
  .symbol .content { color:#A60 }
128
- .symbol .delimiter { color:#630 }
128
+ .symbol .delimiter { color:#740 }
129
129
  .tag { color:#070; font-weight:bold }
130
130
  .type { color:#339; font-weight:bold }
131
131
  .value { color: #088 }
@@ -1,10 +1,7 @@
1
1
  module CodeRay
2
2
 
3
3
  # A Hash of all known token kinds and their associated CSS classes.
4
- TokenKinds = Hash.new do |h, k|
5
- warn 'Undefined Token kind: %p' % [k] if $CODERAY_DEBUG
6
- false
7
- end
4
+ TokenKinds = Hash.new(false)
8
5
 
9
6
  # speedup
10
7
  TokenKinds.compare_by_identity if TokenKinds.respond_to? :compare_by_identity
@@ -83,5 +80,6 @@ module CodeRay
83
80
  :plain => false # almost all scanners
84
81
  )
85
82
 
86
- TokenKinds[:method] = TokenKinds[:function]
83
+ TokenKinds[:method] = TokenKinds[:function]
84
+ TokenKinds[:unknown] = TokenKinds[:plain]
87
85
  end
@@ -164,9 +164,7 @@ more code # and another comment, in-line.
164
164
  end
165
165
  end
166
166
  assert_equal 'reserved', CodeRay::TokenKinds[:reserved]
167
- assert_warning 'Undefined Token kind: :shibboleet' do
168
- assert_equal false, CodeRay::TokenKinds[:shibboleet]
169
- end
167
+ assert_equal false, CodeRay::TokenKinds[:shibboleet]
170
168
  end
171
169
 
172
170
  class Milk < CodeRay::Encoders::Encoder
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coderay
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0.rc2
4
+ version: 1.1.0.rc3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kornelius Kalnbach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-14 00:00:00.000000000 Z
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fast and easy syntax highlighting for selected languages, written in
14
14
  Ruby. Comes with RedCloth integration and LOC counter.