@@ -126,22 +126,21 @@ class HTML < Encoder
126
126
127
127
protected
128
128
129
- HTML_ESCAPE = { #:nodoc:
130
- '&' => '&' ,
131
- '"' => '"' ,
132
- '>' => '>' ,
133
- '<' => '<' ,
134
- }
129
+ def self . make_html_escape_hash
130
+ {
131
+ '&' => '&' ,
132
+ '"' => '"' ,
133
+ '>' => '>' ,
134
+ '<' => '<' ,
135
+ # "\t" => will be set to ' ' * options[:tab_width] during setup
136
+ } . tap do |hash |
137
+ # Escape ASCII control codes except \x9 == \t and \xA == \n.
138
+ ( Array ( 0x00 ..0x8 ) + Array ( 0xB ..0x1F ) ) . each { |invalid | hash [ invalid . chr ] = ' ' }
139
+ end
140
+ end
135
141
136
- # This was to prevent illegal HTML.
137
- # Strange chars should still be avoided in codes.
138
- evil_chars = Array ( 0x00 ...0x20 ) - [ ?\n, ?\t, ?\s]
139
- evil_chars . each { |i | HTML_ESCAPE [ i . chr ] = ' ' }
140
- #ansi_chars = Array(0x7f..0xff)
141
- #ansi_chars.each { |i| HTML_ESCAPE[i.chr] = '&#%d;' % i }
142
- # \x9 (\t) and \xA (\n) not included
143
- #HTML_ESCAPE_PATTERN = /[\t&"><\0-\x8\xB-\x1f\x7f-\xff]/
144
- HTML_ESCAPE_PATTERN = /[\t "&><\0 -\x8 \xB -\x1f ]/
142
+ HTML_ESCAPE = make_html_escape_hash
143
+ HTML_ESCAPE_PATTERN = /[\t "&><\0 -\x8 \xB -\x1F ]/
145
144
146
145
TOKEN_KIND_TO_INFO = Hash . new do |h , kind |
147
146
h [ kind ] = kind . to_s . gsub ( /_/ , ' ' ) . gsub ( /\b \w / ) { $&. capitalize }
@@ -255,20 +254,10 @@ def finish options
255
254
public
256
255
257
256
def text_token text , kind
258
- if text =~ /#{ HTML_ESCAPE_PATTERN } /o
259
- text = text . gsub ( /#{ HTML_ESCAPE_PATTERN } /o ) { |m | @HTML_ESCAPE [ m ] }
260
- end
261
-
262
257
style = @span_for_kind [ @last_opened ? [ kind , *@opened ] : kind ]
263
258
264
- if @break_lines && ( i = text . index ( "\n " ) ) && ( c = @opened . size + ( style ? 1 : 0 ) ) > 0
265
- close = '</span>' * c
266
- reopen = ''
267
- @opened . each_with_index do |k , index |
268
- reopen << ( @span_for_kind [ index > 0 ? [ k , *@opened [ 0 ... index ] ] : k ] || '<span>' )
269
- end
270
- text [ i .. -1 ] = text [ i .. -1 ] . gsub ( "\n " , "#{ close } \n #{ reopen } #{ style } " )
271
- end
259
+ text = text . gsub ( /#{ HTML_ESCAPE_PATTERN } /o ) { |m | @HTML_ESCAPE [ m ] } if text =~ /#{ HTML_ESCAPE_PATTERN } /o
260
+ text = break_lines ( text , style ) if @break_lines && ( style || @opened . size > 0 ) && text . index ( "\n " )
272
261
273
262
if style
274
263
@out << style << text << '</span>'
@@ -285,9 +274,7 @@ def begin_group kind
285
274
end
286
275
287
276
def end_group kind
288
- if $CODERAY_DEBUG && ( @opened . empty? || @opened . last != kind )
289
- warn 'Malformed token stream: Trying to close a token group (%p) that is not open. Open are: %p.' % [ kind , @opened [ 1 ..-1 ] ]
290
- end
277
+ check_group_nesting 'token group' , kind if $CODERAY_DEBUG
291
278
if @opened . pop
292
279
@out << '</span>'
293
280
@last_opened = @opened . last if @last_opened
@@ -310,15 +297,28 @@ def begin_line kind
310
297
end
311
298
312
299
def end_line kind
313
- if $CODERAY_DEBUG && ( @opened . empty? || @opened . last != kind )
314
- warn 'Malformed token stream: Trying to close a line (%p) that is not open. Open are: %p.' % [ kind , @opened [ 1 ..-1 ] ]
315
- end
300
+ check_group_nesting 'line' , kind if $CODERAY_DEBUG
316
301
if @opened . pop
317
302
@out << '</span>'
318
303
@last_opened = @opened . last if @last_opened
319
304
end
320
305
end
321
306
307
+ protected
308
+
309
+ def check_group_nesting name , kind
310
+ if @opened . empty? || @opened . last != kind
311
+ warn "Malformed token stream: Trying to close a #{ name } (%p) that is not open. Open are: %p." % [ kind , @opened [ 1 ..-1 ] ]
312
+ end
313
+ end
314
+
315
+ def break_lines text , style
316
+ reopen = ''
317
+ @opened . each_with_index do |k , index |
318
+ reopen << ( @span_for_kind [ index > 0 ? [ k , *@opened [ 0 ...index ] ] : k ] || '<span>' )
319
+ end
320
+ text . gsub ( "\n " , "#{ '</span>' * @opened . size } #{ '</span>' if style } \n #{ reopen } #{ style } " )
321
+ end
322
322
end
323
323
324
324
end
0 commit comments