coderay 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/README CHANGED
@@ -18,7 +18,7 @@ And with line numbers.
18
18
  * is what everybody should have on their website
19
19
  * solves all your problems and makes the girls run after you
20
20
 
21
- Version: 0.9.4
21
+ Version: 0.9.5
22
22
  Author:: murphy (Kornelius Kalnbach)
23
23
  Contact:: murphy rubychan de
24
24
  Website:: coderay.rubychan.de[http://coderay.rubychan.de]
data/lib/coderay.rb CHANGED
@@ -134,7 +134,7 @@ module CodeRay
134
134
  # Minor: feature milestone
135
135
  # Teeny: development state, 0 for pre-release
136
136
  # Revision: Subversion Revision number (generated on rake gem:make)
137
- VERSION = '0.9.4'
137
+ VERSION = '0.9.5'
138
138
 
139
139
  require 'coderay/tokens'
140
140
  require 'coderay/token_classes'
@@ -276,9 +276,13 @@ module Encoders
276
276
  when :begin_line
277
277
  @opened[0] = type
278
278
  if style = @css_style[@opened]
279
- @out << style.sub('<span', '<div')
279
+ if style['class="']
280
+ @out << style.sub('class="', 'class="line ')
281
+ else
282
+ @out << style.sub('>', ' class="line">')
283
+ end
280
284
  else
281
- @out << '<div>'
285
+ @out << '<span class="line">'
282
286
  end
283
287
  @opened << type
284
288
  when :end_line
@@ -289,7 +293,7 @@ module Encoders
289
293
  raise 'Malformed token stream: Trying to close a line (%p) \
290
294
  that is not open. Open are: %p.' % [type, @opened[1..-1]]
291
295
  end
292
- @out << '</div>'
296
+ @out << '</span>'
293
297
  @opened.pop
294
298
  end
295
299
 
@@ -80,8 +80,8 @@ module Encoders
80
80
  line_numbers.gsub!(/\n/) { "<tt>\n</tt>" }
81
81
 
82
82
  line_numbers_table_tpl = TABLE.apply('LINE_NUMBERS', line_numbers)
83
- gsub!(/<\/div>\n/) { '</div>' }
84
- gsub!(/\n/) { "<tt>\n</tt>" }
83
+ gsub!("</div>\n", '</div>')
84
+ gsub!("\n", "<tt>\n</tt>")
85
85
  wrap_in! line_numbers_table_tpl
86
86
  @wrapped_in = :div
87
87
 
@@ -68,15 +68,15 @@ module CodeRay
68
68
 
69
69
  def normify code
70
70
  code = code.to_s
71
- if code.respond_to? :force_encoding
72
- debug, $DEBUG = $DEBUG, false
73
- begin
74
- code.force_encoding 'utf-8'
75
- code[/\z/] # raises an ArgumentError when code contains a non-UTF-8 char
76
- rescue ArgumentError
77
- code.force_encoding 'binary'
78
- ensure
79
- $DEBUG = debug
71
+ if code.respond_to?(:encoding) && (code.encoding.name != 'UTF-8' || !code.valid_encoding?)
72
+ original_encoding = code.encoding
73
+ code.force_encoding 'Windows-1252'
74
+ unless code.valid_encoding?
75
+ code.force_encoding original_encoding
76
+ if code.encoding.name == 'UTF-8'
77
+ code.encode! 'UTF-16BE', :invalid => :replace, :undef => :replace, :replace => '?'
78
+ end
79
+ code.encode! 'UTF-8', :invalid => :replace, :undef => :replace, :replace => '?'
80
80
  end
81
81
  end
82
82
  code.to_unix
@@ -147,7 +147,12 @@ module CodeRay
147
147
 
148
148
  def string= code
149
149
  code = Scanner.normify(code)
150
- super code
150
+ if defined?(RUBY_DESCRIPTION) && RUBY_DESCRIPTION['rubinius 1.0.1']
151
+ reset_state
152
+ @string = code
153
+ else
154
+ super code
155
+ end
151
156
  reset_instance
152
157
  end
153
158
 
@@ -14,12 +14,11 @@ module Scanners
14
14
  ]
15
15
 
16
16
  module RE
17
- NonASCII = /[\x80-\xFF]/
18
17
  Hex = /[0-9a-fA-F]/
19
18
  Unicode = /\\#{Hex}{1,6}(?:\r\n|\s)?/ # differs from standard because it allows uppercase hex too
20
19
  Escape = /#{Unicode}|\\[^\r\n\f0-9a-fA-F]/
21
- NMChar = /[-_a-zA-Z0-9]|#{NonASCII}|#{Escape}/
22
- NMStart = /[_a-zA-Z]|#{NonASCII}|#{Escape}/
20
+ NMChar = /[-_a-zA-Z0-9]|#{Escape}/
21
+ NMStart = /[_a-zA-Z]|#{Escape}/
23
22
  NL = /\r\n|\r|\n|\f/
24
23
  String1 = /"(?:[^\n\r\f\\"]|\\#{NL}|#{Escape})*"?/ # FIXME: buggy regexp
25
24
  String2 = /'(?:[^\n\r\f\\']|\\#{NL}|#{Escape})*'?/ # FIXME: buggy regexp
@@ -72,7 +72,7 @@ module Scanners
72
72
  elsif scan(/ .*/)
73
73
  kind = :comment
74
74
  elsif scan(/.+/)
75
- tokens << [:begin_line, line_kind = :head]
75
+ tokens << [:begin_line, line_kind = :comment]
76
76
  kind = :plain
77
77
  else
78
78
  raise_inspect 'else case rached'
@@ -225,6 +225,12 @@ module Scanners
225
225
  end
226
226
 
227
227
  def scan_tokens tokens, options
228
+ if string.respond_to?(:encoding)
229
+ unless string.encoding == Encoding::ASCII_8BIT
230
+ self.string = string.encode Encoding::ASCII_8BIT,
231
+ :invalid => :replace, :undef => :replace, :replace => '?'
232
+ end
233
+ end
228
234
 
229
235
  if check(RE::PHP_START) || # starts with <?
230
236
  (match?(/\s*<\S/) && exist?(RE::PHP_START)) || # starts with tag and contains <?
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module CodeRay
2
3
  module Scanners
3
4
 
@@ -28,6 +29,16 @@ module Scanners
28
29
 
29
30
  private
30
31
  def scan_tokens tokens, options
32
+ if string.respond_to?(:encoding)
33
+ unless string.encoding == Encoding::UTF_8
34
+ self.string = string.encode Encoding::UTF_8,
35
+ :invalid => :replace, :undef => :replace, :replace => '?'
36
+ end
37
+ unicode = false
38
+ else
39
+ unicode = exist?(/[^\x00-\x7f]/)
40
+ end
41
+
31
42
  last_token_dot = false
32
43
  value_expected = true
33
44
  heredocs = nil
@@ -35,7 +46,7 @@ module Scanners
35
46
  state = :initial
36
47
  depth = nil
37
48
  inline_block_stack = []
38
- unicode = string.respond_to?(:encoding) && string.encoding.name == 'UTF-8'
49
+
39
50
 
40
51
  patterns = Patterns # avoid constant lookup
41
52
 
@@ -171,8 +182,13 @@ module Scanners
171
182
  kind = if match[/^[A-Z]/] and not match?(/\(/) then :constant else :ident end
172
183
  else
173
184
  kind = patterns::IDENT_KIND[match]
174
- if kind == :ident and match[/^[A-Z]/] and not match[/[!?]$/] and not match?(/\(/)
175
- kind = :constant
185
+ if kind == :ident
186
+ if match[/^[A-Z]/] and not match[/[!?]$/] and not match?(/\(/)
187
+ kind = :constant
188
+ elsif scan(/:(?= )/)
189
+ match << ':'
190
+ kind = :symbol
191
+ end
176
192
  elsif kind == :reserved
177
193
  state = patterns::DEF_NEW_STATE[match]
178
194
  value_expected = :set if patterns::KEYWORDS_EXPECTING_VALUE[match]
@@ -182,7 +198,8 @@ module Scanners
182
198
 
183
199
  elsif last_token_dot and match = scan(/#{patterns::METHOD_NAME_OPERATOR}|\(/o)
184
200
  kind = :ident
185
- value_expected = :set if check(/#{patterns::VALUE_FOLLOWS}/o)
201
+ value_expected = :set if check(unicode ? /#{patterns::VALUE_FOLLOWS}/uo :
202
+ /#{patterns::VALUE_FOLLOWS}/o)
186
203
 
187
204
  # OPERATORS #
188
205
  elsif not last_token_dot and match = scan(/ \.\.\.? | (?:\.|::)() | [,\(\)\[\]\{\}] | ==?=? /x)
@@ -212,7 +229,8 @@ module Scanners
212
229
  kind = :delimiter
213
230
  state = patterns::StringState.new :string, match == '"', match # important for streaming
214
231
 
215
- elsif match = scan(/#{patterns::INSTANCE_VARIABLE}/o)
232
+ elsif match = scan(unicode ? /#{patterns::INSTANCE_VARIABLE}/uo :
233
+ /#{patterns::INSTANCE_VARIABLE}/o)
216
234
  kind = :instance_variable
217
235
 
218
236
  elsif value_expected and match = scan(/\//)
@@ -225,7 +243,8 @@ module Scanners
225
243
  elsif match = value_expected ? scan(/[-+]?#{patterns::NUMERIC}/o) : scan(/#{patterns::NUMERIC}/o)
226
244
  kind = self[1] ? :float : :integer
227
245
 
228
- elsif match = scan(/#{patterns::SYMBOL}/o)
246
+ elsif match = scan(unicode ? /#{patterns::SYMBOL}/uo :
247
+ /#{patterns::SYMBOL}/o)
229
248
  case delim = match[1]
230
249
  when ?', ?"
231
250
  tokens << [:open, :symbol]
@@ -237,11 +256,12 @@ module Scanners
237
256
  kind = :symbol
238
257
  end
239
258
 
240
- elsif match = scan(/ [-+!~^]=? | [*|&]{1,2}=? | >>? /x)
259
+ elsif match = scan(/ -[>=]? | [+!~^]=? | [*|&]{1,2}=? | >>? /x)
241
260
  value_expected = :set
242
261
  kind = :operator
243
262
 
244
- elsif value_expected and match = scan(/#{patterns::HEREDOC_OPEN}/o)
263
+ elsif value_expected and match = scan(unicode ? /#{patterns::HEREDOC_OPEN}/uo :
264
+ /#{patterns::HEREDOC_OPEN}/o)
245
265
  indented = self[1] == '-'
246
266
  quote = self[3]
247
267
  delim = self[quote ? 4 : 2]
@@ -261,7 +281,8 @@ module Scanners
261
281
  state = patterns::StringState.new kind, interpreted, self[2]
262
282
  kind = :delimiter
263
283
 
264
- elsif value_expected and match = scan(/#{patterns::CHARACTER}/o)
284
+ elsif value_expected and match = scan(unicode ? /#{patterns::CHARACTER}/uo :
285
+ /#{patterns::CHARACTER}/o)
265
286
  kind = :integer
266
287
 
267
288
  elsif match = scan(/ [\/%]=? | <(?:<|=>?)? | [?:;] /x)
@@ -277,14 +298,16 @@ module Scanners
277
298
  state = patterns::StringState.new :shell, true, match
278
299
  end
279
300
 
280
- elsif match = scan(/#{patterns::GLOBAL_VARIABLE}/o)
301
+ elsif match = scan(unicode ? /#{patterns::GLOBAL_VARIABLE}/uo :
302
+ /#{patterns::GLOBAL_VARIABLE}/o)
281
303
  kind = :global_variable
282
304
 
283
- elsif match = scan(/#{patterns::CLASS_VARIABLE}/o)
305
+ elsif match = scan(unicode ? /#{patterns::CLASS_VARIABLE}/uo :
306
+ /#{patterns::CLASS_VARIABLE}/o)
284
307
  kind = :class_variable
285
308
 
286
309
  else
287
- if !unicode
310
+ if !unicode && !string.respond_to?(:encoding)
288
311
  # check for unicode
289
312
  debug, $DEBUG = $DEBUG, false
290
313
  begin
@@ -300,7 +323,7 @@ module Scanners
300
323
  next if unicode
301
324
  end
302
325
  kind = :error
303
- match = getch
326
+ match = scan(unicode ? /./mu : /./m)
304
327
 
305
328
  end
306
329
 
@@ -322,7 +345,8 @@ module Scanners
322
345
  kind = :operator
323
346
  else
324
347
  state = :initial
325
- if match = scan(/ (?:#{patterns::IDENT}::)* #{patterns::IDENT} /ox)
348
+ if match = scan(unicode ? /(?:#{patterns::IDENT}::)*#{patterns::IDENT}/uo :
349
+ /(?:#{patterns::IDENT}::)*#{patterns::IDENT}/o)
326
350
  kind = :class
327
351
  else
328
352
  next
@@ -331,9 +355,11 @@ module Scanners
331
355
 
332
356
  elsif state == :undef_expected
333
357
  state = :undef_comma_expected
334
- if match = scan(/#{patterns::METHOD_NAME_EX}/o)
358
+ if match = scan(unicode ? /#{patterns::METHOD_NAME_EX}/uo :
359
+ /#{patterns::METHOD_NAME_EX}/o)
335
360
  kind = :method
336
- elsif match = scan(/#{patterns::SYMBOL}/o)
361
+ elsif match = scan(unicode ? /#{patterns::SYMBOL}/uo :
362
+ /#{patterns::SYMBOL}/o)
337
363
  case delim = match[1]
338
364
  when ?', ?"
339
365
  tokens << [:open, :symbol]
@@ -32,7 +32,18 @@ module Scanners
32
32
  add(RESERVED_WORDS, :reserved).
33
33
  add(PREDEFINED_CONSTANTS, :pre_constant)
34
34
 
35
- IDENT = 'ä'[/[[:alpha:]]/] == 'ä' ? /[[:alpha:]_][[:alnum:]_]*/ : /[^\W\d]\w*/
35
+ if /\w/u === ''
36
+ # MRI 1.8.6, 1.8.7
37
+ IDENT = /[^\W\d]\w*/
38
+ else
39
+ if //.respond_to? :encoding
40
+ # MRI 1.9.1, 1.9.2
41
+ IDENT = Regexp.new '[\p{L}\p{M}\p{Pc}\p{Sm}&&[^\x00-\x40\x5b-\x5e\x60\x7b-\x7f]][\p{L}\p{M}\p{N}\p{Pc}\p{Sm}&&[^\x00-\x2f\x3a-\x40\x5b-\x5e\x60\x7b-\x7f]]*'
42
+ else
43
+ # JRuby, Rubinius
44
+ IDENT = /[^\x00-\x40\x5b-\x5e\x60\x7b-\x7f][^\x00-\x2f\x3a-\x40\x5b-\x5e\x60\x7b-\x7f]*/
45
+ end
46
+ end
36
47
 
37
48
  METHOD_NAME = / #{IDENT} [?!]? /ox
38
49
  METHOD_NAME_OPERATOR = /
@@ -109,10 +120,12 @@ module Scanners
109
120
 
110
121
  # NOTE: This is not completely correct, but
111
122
  # nobody needs heredoc delimiters ending with \n.
123
+ # Also, delimiters starting with numbers are allowed.
124
+ # but they are more often than not a false positive.
112
125
  HEREDOC_OPEN = /
113
126
  << (-)? # $1 = float
114
127
  (?:
115
- ( [A-Za-z_0-9]+ ) # $2 = delim
128
+ ( #{IDENT} ) # $2 = delim
116
129
  |
117
130
  ( ["'`\/] ) # $3 = quote, type
118
131
  ( [^\n]*? ) \3 # $4 = delim
@@ -56,7 +56,7 @@ module CodeRay module Scanners
56
56
  if scan(/ \s+ | \\\n /x)
57
57
  kind = :space
58
58
 
59
- elsif scan(/^(?:--\s?|#).*/)
59
+ elsif scan(/(?:--\s?|#).*/)
60
60
  kind = :comment
61
61
 
62
62
  elsif scan(%r! /\* (?: .*? \*/ | .* ) !mx)
@@ -33,6 +33,7 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top }
33
33
  }
34
34
  .CodeRay .line_numbers tt { font-weight: bold }
35
35
  .CodeRay .line_numbers .highlighted { color: red }
36
+ .CodeRay .line { display: block; float: left; width: 100%; }
36
37
  .CodeRay .no { padding: 0px 4px }
37
38
  .CodeRay .code { width: 100% }
38
39
 
@@ -32,6 +32,8 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
32
32
  text-align: right;
33
33
  }
34
34
  .CodeRay .line_numbers tt { font-weight: bold; }
35
+ .CodeRay .line_numbers .highlighted { color: red }
36
+ .CodeRay .line { display: block; float: left; width: 100%; }
35
37
  .CodeRay .no { padding: 0px 4px; }
36
38
  .CodeRay .code { width: 100%; }
37
39
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coderay
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
4
+ hash: 49
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 4
10
- version: 0.9.4
9
+ - 5
10
+ version: 0.9.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - murphy
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-01 00:00:00 +02:00
18
+ date: 2010-09-28 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21