@@ -47,6 +47,13 @@ module Encoders
4747 #
4848 # Default: 'CodeRay output'
4949 #
50+ # === :break_lines
51+ #
52+ # Split multiline blocks at line breaks.
53+ # Forced to true if :line_numbers option is set to :inline.
54+ #
55+ # Default: false
56+ #
5057 # === :line_numbers
5158 # Include line numbers in :table, :inline, or nil (no line numbers)
5259 #
@@ -100,6 +107,8 @@ class HTML < Encoder
100107 :wrap => nil ,
101108 :title => 'CodeRay output' ,
102109
110+ :break_lines => false ,
111+
103112 :line_numbers => nil ,
104113 :line_number_anchors => 'n' ,
105114 :line_number_start => 1 ,
@@ -168,6 +177,10 @@ def setup options
168177 @out = ''
169178 end
170179
180+ options [ :break_lines ] = true if options [ :line_numbers ] == :inline
181+
182+ @break_lines = ( options [ :break_lines ] == true )
183+
171184 @HTML_ESCAPE = HTML_ESCAPE . dup
172185 @HTML_ESCAPE [ "\t " ] = ' ' * options [ :tab_width ]
173186
@@ -245,7 +258,19 @@ def text_token text, kind
245258 if text =~ /#{ HTML_ESCAPE_PATTERN } /o
246259 text = text . gsub ( /#{ HTML_ESCAPE_PATTERN } /o ) { |m | @HTML_ESCAPE [ m ] }
247260 end
248- if style = @span_for_kind [ @last_opened ? [ kind , *@opened ] : kind ]
261+
262+ style = @span_for_kind [ @last_opened ? [ kind , *@opened ] : kind ]
263+
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
272+
273+ if style
249274 @out << style << text << '</span>'
250275 else
251276 @out << text
0 commit comments