@@ -186,7 +186,7 @@ def setup options
186
186
@last_opened = nil
187
187
@css = CSS . new options [ :style ]
188
188
189
- @span_for_kind = make_span_for_kind ( options [ :css ] , options [ :hint ] , @css )
189
+ @span_for_kinds = make_span_for_kinds ( options [ :css ] , options [ :hint ] )
190
190
191
191
@set_last_opened = options [ :hint ] || options [ :css ] == :style
192
192
end
@@ -217,7 +217,7 @@ def finish options
217
217
public
218
218
219
219
def text_token text , kind
220
- style = @span_for_kind [ @last_opened ? [ kind , *@opened ] : kind ]
220
+ style = @span_for_kinds [ @last_opened ? [ kind , *@opened ] : kind ]
221
221
222
222
text = text . gsub ( /#{ HTML_ESCAPE_PATTERN } /o ) { |m | @HTML_ESCAPE [ m ] } if text =~ /#{ HTML_ESCAPE_PATTERN } /o
223
223
text = break_lines ( text , style ) if @break_lines && ( style || @opened . size > 0 ) && text . index ( "\n " )
@@ -231,22 +231,19 @@ def text_token text, kind
231
231
232
232
# token groups, eg. strings
233
233
def begin_group kind
234
- @out << ( @span_for_kind [ @last_opened ? [ kind , *@opened ] : kind ] || '<span>' )
234
+ @out << ( @span_for_kinds [ @last_opened ? [ kind , *@opened ] : kind ] || '<span>' )
235
235
@opened << kind
236
236
@last_opened = kind if @set_last_opened
237
237
end
238
238
239
239
def end_group kind
240
240
check_group_nesting 'token group' , kind if $CODERAY_DEBUG
241
- if @opened . pop
242
- @out << '</span>'
243
- @last_opened = @opened . last if @last_opened
244
- end
241
+ close_span
245
242
end
246
243
247
244
# whole lines to be highlighted, eg. a deleted line in a diff
248
245
def begin_line kind
249
- if style = @span_for_kind [ @last_opened ? [ kind , *@opened ] : kind ]
246
+ if style = @span_for_kinds [ @last_opened ? [ kind , *@opened ] : kind ]
250
247
if style [ 'class="' ]
251
248
@out << style . sub ( 'class="' , 'class="line ' )
252
249
else
@@ -261,10 +258,7 @@ def begin_line kind
261
258
262
259
def end_line kind
263
260
check_group_nesting 'line' , kind if $CODERAY_DEBUG
264
- if @opened . pop
265
- @out << '</span>'
266
- @last_opened = @opened . last if @last_opened
267
- end
261
+ close_span
268
262
end
269
263
270
264
protected
@@ -281,18 +275,29 @@ def check_options! options
281
275
options [ :break_lines ] = true if options [ :line_numbers ] == :inline
282
276
end
283
277
284
- def make_span_for_kind method , hint , css
278
+ def css_class_for_kinds kinds
279
+ TokenKinds [ kinds . is_a? ( Symbol ) ? kinds : kinds . first ]
280
+ end
281
+
282
+ def style_for_kinds kinds
283
+ css_classes = kinds . is_a? ( Array ) ? kinds . map { |c | TokenKinds [ c ] } : [ TokenKinds [ kinds ] ]
284
+ @css . get_style_for_css_classes css_classes
285
+ end
286
+
287
+ def make_span_for_kinds method , hint
285
288
Hash . new do |h , kinds |
286
289
h [ kinds . is_a? ( Symbol ) ? kinds : kinds . dup ] = begin
287
- css_class = TokenKinds [ kinds . is_a? ( Symbol ) ? kinds : kinds . first ]
290
+ css_class = css_class_for_kinds ( kinds )
288
291
title = HTML . token_path_to_hint hint , kinds if hint
289
292
290
- if method == :style
291
- style = css . get_style ( kinds . is_a? ( Array ) ? kinds . map { |c | TokenKinds [ c ] } : [ TokenKinds [ kinds ] ] )
292
- "<span#{ title } #{ " style=\" #{ style } \" " if style } >"
293
- else
294
- "<span#{ title } #{ " class=\" #{ css_class } \" " if css_class } >"
295
- end if css_class || title
293
+ if css_class || title
294
+ if method == :style
295
+ style = style_for_kinds ( kinds )
296
+ "<span#{ title } #{ " style=\" #{ style } \" " if style } >"
297
+ else
298
+ "<span#{ title } #{ " class=\" #{ css_class } \" " if css_class } >"
299
+ end
300
+ end
296
301
end
297
302
end
298
303
end
@@ -306,10 +311,17 @@ def check_group_nesting name, kind
306
311
def break_lines text , style
307
312
reopen = ''
308
313
@opened . each_with_index do |k , index |
309
- reopen << ( @span_for_kind [ index > 0 ? [ k , *@opened [ 0 ...index ] ] : k ] || '<span>' )
314
+ reopen << ( @span_for_kinds [ index > 0 ? [ k , *@opened [ 0 ...index ] ] : k ] || '<span>' )
310
315
end
311
316
text . gsub ( "\n " , "#{ '</span>' * @opened . size } #{ '</span>' if style } \n #{ reopen } #{ style } " )
312
317
end
318
+
319
+ def close_span
320
+ if @opened . pop
321
+ @out << '</span>'
322
+ @last_opened = @opened . last if @last_opened
323
+ end
324
+ end
313
325
end
314
326
315
327
end
0 commit comments