From 0e8008ad88b4f56e35d71c3029d02ffb4e21120f Mon Sep 17 00:00:00 2001 From: Etienne Massip Date: Thu, 26 Jan 2012 21:14:59 +0100 Subject: Extracted code making HTML lines independent from numbering code to a specific option in encoder. --- lib/coderay/encoders/html.rb | 27 +++++++++++++++++++++++---- lib/coderay/encoders/html/numbering.rb | 16 ++-------------- 2 files changed, 25 insertions(+), 18 deletions(-) (limited to 'lib/coderay/encoders') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index c32dbd1..14d1307 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,21 @@ module Encoders if text =~ /#{HTML_ESCAPE_PATTERN}/o text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } end + if @independent_lines && @opened.any? && text.end_with?("\n") + text.chomp! + close_eol_reopen = "#{'' * @opened.size}\n" + @opened.each_with_index do |k, index| + close_eol_reopen << (@span_for_kind[index > 0 ? [k, *@opened[0 ... index ]] : k] || '') + end + end if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] @out << style << text << '' else @out << text end + @out << close_eol_reopen if close_eol_reopen end - + # token groups, eg. strings def begin_group kind @out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '') @@ -299,4 +318,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 = '' * nesting.size - line_number_text = bolding.call line_number indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x) line_number += 1 - "#{indent}#{line_number_text}#{open}#{line}#{close}\n" + "#{indent}#{line_number_text}#{line}" end when :table @@ -112,4 +100,4 @@ module Encoders end end -end +end \ No newline at end of file -- cgit v1.2.1 From a2e9acc4dabd2ec2d33ac17d83f92ef370354eb7 Mon Sep 17 00:00:00 2001 From: Etienne Massip Date: Thu, 29 Mar 2012 20:46:07 +0200 Subject: Fixed handling of eols inside token content. --- lib/coderay/encoders/html.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'lib/coderay/encoders') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 14d1307..0d91cdf 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -256,19 +256,23 @@ module Encoders if text =~ /#{HTML_ESCAPE_PATTERN}/o text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } end - if @independent_lines && @opened.any? && text.end_with?("\n") - text.chomp! - close_eol_reopen = "#{'' * @opened.size}\n" + + style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] + + if @independent_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0 + close = '' * c + reopen = '' @opened.each_with_index do |k, index| - close_eol_reopen << (@span_for_kind[index > 0 ? [k, *@opened[0 ... index ]] : k] || '') + reopen << (@span_for_kind[index > 0 ? [k, *@opened[0 ... index ]] : k] || '') end + text[i .. -1] = text[i .. -1].gsub("\n", "#{close}\n#{reopen}#{style}") end - if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] + + if style @out << style << text << '' else @out << text end - @out << close_eol_reopen if close_eol_reopen end # token groups, eg. strings -- cgit v1.2.1 From df4e2bc7f7d8238f56e2d823aea707bfd860ad8f Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Sun, 1 Apr 2012 00:28:15 +0200 Subject: here come the white-space nazis --- lib/coderay/encoders/html.rb | 23 ++++++++++++----------- lib/coderay/encoders/html/numbering.rb | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) (limited to 'lib/coderay/encoders') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 0d91cdf..2ec0f37 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -48,7 +48,7 @@ module Encoders # Default: 'CodeRay output' # # === :independent_lines - # Split multilines blocks into line-wide blocks. + # Split multiline blocks at line breaks. # Forced to true if :line_numbers option is set to :inline. # # Default: false @@ -105,8 +105,9 @@ module Encoders :style => :alpha, :wrap => nil, :title => 'CodeRay output', - - :independent_lines => false, + + :independent_lines => false, + :line_numbers => nil, :line_number_anchors => 'n', :line_number_start => 1, @@ -174,11 +175,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] @@ -256,9 +257,9 @@ module Encoders if text =~ /#{HTML_ESCAPE_PATTERN}/o text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } end - + style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] - + if @independent_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0 close = '' * c reopen = '' @@ -267,14 +268,14 @@ module Encoders end text[i .. -1] = text[i .. -1].gsub("\n", "#{close}\n#{reopen}#{style}") end - + if style @out << style << text << '' else @out << text end end - + # token groups, eg. strings def begin_group kind @out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '') @@ -322,4 +323,4 @@ module Encoders end end -end \ No newline at end of file +end diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb index 904a64f..8bc6259 100644 --- a/lib/coderay/encoders/html/numbering.rb +++ b/lib/coderay/encoders/html/numbering.rb @@ -100,4 +100,4 @@ module Encoders end end -end \ No newline at end of file +end -- cgit v1.2.1 From 5c4124559127f8ce991e31e4ea8a40516fe0757a Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Mon, 2 Apr 2012 00:15:20 +0200 Subject: rename :independent_lines option to :break_lines --- lib/coderay/encoders/html.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'lib/coderay/encoders') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 2ec0f37..635a4d8 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -47,7 +47,8 @@ module Encoders # # Default: 'CodeRay output' # - # === :independent_lines + # === :break_lines + # # Split multiline blocks at line breaks. # Forced to true if :line_numbers option is set to :inline. # @@ -106,7 +107,7 @@ module Encoders :wrap => nil, :title => 'CodeRay output', - :independent_lines => false, + :break_lines => false, :line_numbers => nil, :line_number_anchors => 'n', @@ -176,9 +177,9 @@ module Encoders @out = '' end - options[:independent_lines] = true if options[:line_numbers] == :inline + options[:break_lines] = true if options[:line_numbers] == :inline - @independent_lines = (options[:independent_lines] == true) + @break_lines = (options[:break_lines] == true) @HTML_ESCAPE = HTML_ESCAPE.dup @HTML_ESCAPE["\t"] = ' ' * options[:tab_width] @@ -260,7 +261,7 @@ module Encoders style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] - if @independent_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0 + if @break_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0 close = '' * c reopen = '' @opened.each_with_index do |k, index| -- cgit v1.2.1