diff options
Diffstat (limited to 'lib/coderay/encoders')
-rw-r--r-- | lib/coderay/encoders/html.rb | 33 | ||||
-rw-r--r-- | lib/coderay/encoders/html/numbering.rb | 16 |
2 files changed, 30 insertions, 19 deletions
diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index c32dbd1..0d91cdf 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -47,6 +47,12 @@ module Encoders # # Default: 'CodeRay output' # + # === :independent_lines + # Split multilines blocks into line-wide blocks. + # Forced to true if :line_numbers option is set to :inline. + # + # Default: false + # # === :line_numbers # Include line numbers in :table, :inline, or nil (no line numbers) # @@ -99,7 +105,8 @@ module Encoders :style => :alpha, :wrap => nil, :title => 'CodeRay output', - + + :independent_lines => false, :line_numbers => nil, :line_number_anchors => 'n', :line_number_start => 1, @@ -167,7 +174,11 @@ module Encoders @real_out = @out @out = '' end - + + options[:independent_lines] = true if options[:line_numbers] == :inline + + @independent_lines = (options[:independent_lines] == true) + @HTML_ESCAPE = HTML_ESCAPE.dup @HTML_ESCAPE["\t"] = ' ' * options[:tab_width] @@ -245,13 +256,25 @@ module Encoders if text =~ /#{HTML_ESCAPE_PATTERN}/o text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } end - if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] + + style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] + + if @independent_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0 + close = '</span>' * c + reopen = '' + @opened.each_with_index do |k, index| + reopen << (@span_for_kind[index > 0 ? [k, *@opened[0 ... index ]] : k] || '<span>') + end + text[i .. -1] = text[i .. -1].gsub("\n", "#{close}\n#{reopen}#{style}") + end + + if style @out << style << text << '</span>' else @out << text end end - + # token groups, eg. strings def begin_group kind @out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '<span>') @@ -299,4 +322,4 @@ module Encoders end end -end +end
\ No newline at end of file diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb index 15ce11b..904a64f 100644 --- a/lib/coderay/encoders/html/numbering.rb +++ b/lib/coderay/encoders/html/numbering.rb @@ -68,23 +68,11 @@ module Encoders when :inline max_width = (start + line_count).to_s.size line_number = start - nesting = [] output.gsub!(/^.*$\n?/) do |line| - line.chomp! - open = nesting.join - line.scan(%r!<(/)?span[^>]*>?!) do |close,| - if close - nesting.pop - else - nesting << $& - end - end - close = '</span>' * nesting.size - line_number_text = bolding.call line_number indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x) line_number += 1 - "<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{open}#{line}#{close}\n" + "<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{line}" end when :table @@ -112,4 +100,4 @@ module Encoders end end -end +end
\ No newline at end of file |