@@ -47,6 +47,12 @@ module Encoders
47
47
#
48
48
# Default: 'CodeRay output'
49
49
#
50
+ # === :independent_lines
51
+ # Split multilines blocks into line-wide blocks.
52
+ # Forced to true if :line_numbers option is set to :inline.
53
+ #
54
+ # Default: false
55
+ #
50
56
# === :line_numbers
51
57
# Include line numbers in :table, :inline, or nil (no line numbers)
52
58
#
@@ -99,7 +105,8 @@ class HTML < Encoder
99
105
:style => :alpha ,
100
106
:wrap => nil ,
101
107
:title => 'CodeRay output' ,
102
-
108
+
109
+ :independent_lines => false ,
103
110
:line_numbers => nil ,
104
111
:line_number_anchors => 'n' ,
105
112
:line_number_start => 1 ,
@@ -167,7 +174,11 @@ def setup options
167
174
@real_out = @out
168
175
@out = ''
169
176
end
170
-
177
+
178
+ options [ :independent_lines ] = true if options [ :line_numbers ] == :inline
179
+
180
+ @independent_lines = ( options [ :independent_lines ] == true )
181
+
171
182
@HTML_ESCAPE = HTML_ESCAPE . dup
172
183
@HTML_ESCAPE [ "\t " ] = ' ' * options [ :tab_width ]
173
184
@@ -245,13 +256,21 @@ def text_token text, kind
245
256
if text =~ /#{ HTML_ESCAPE_PATTERN } /o
246
257
text = text . gsub ( /#{ HTML_ESCAPE_PATTERN } /o ) { |m | @HTML_ESCAPE [ m ] }
247
258
end
259
+ if @independent_lines && @opened . any? && text . end_with? ( "\n " )
260
+ text . chomp!
261
+ close_eol_reopen = "#{ '</span>' * @opened . size } \n "
262
+ @opened . each_with_index do |k , index |
263
+ close_eol_reopen << ( @span_for_kind [ index > 0 ? [ k , *@opened [ 0 ... index ] ] : k ] || '<span>' )
264
+ end
265
+ end
248
266
if style = @span_for_kind [ @last_opened ? [ kind , *@opened ] : kind ]
249
267
@out << style << text << '</span>'
250
268
else
251
269
@out << text
252
270
end
271
+ @out << close_eol_reopen if close_eol_reopen
253
272
end
254
-
273
+
255
274
# token groups, eg. strings
256
275
def begin_group kind
257
276
@out << ( @span_for_kind [ @last_opened ? [ kind , *@opened ] : kind ] || '<span>' )
@@ -299,4 +318,4 @@ def end_line kind
299
318
end
300
319
301
320
end
302
- end
321
+ end
0 commit comments