From 6cec727fb2c4af0beb44a02b78e3af9e3ba43fc2 Mon Sep 17 00:00:00 2001 From: Tim Robertson Date: Tue, 20 May 2014 22:45:53 -0700 Subject: [PATCH 1/4] add a wrap_lines option which wraps code lines in divs --- lib/coderay/encoders/html/numbering.rb | 49 ++++++++++++++++---------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb index a1b9c04a..52438b87 100644 --- a/lib/coderay/encoders/html/numbering.rb +++ b/lib/coderay/encoders/html/numbering.rb @@ -1,20 +1,20 @@ module CodeRay module Encoders - + class HTML - + module Numbering # :nodoc: - + def self.number! output, mode = :table, options = {} return self unless mode - + options = DEFAULT_OPTIONS.merge options - + start = options[:line_number_start] unless start.is_a? Integer raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start end - + anchor_prefix = options[:line_number_anchors] anchor_prefix = 'line' if anchor_prefix == true anchor_prefix = anchor_prefix.to_s[/[\w-]+/] if anchor_prefix @@ -28,7 +28,7 @@ def self.number! output, mode = :table, options = {} else :to_s.to_proc end - + bold_every = options[:bold_every] highlight_lines = options[:highlight_lines] bolding = @@ -55,11 +55,11 @@ def self.number! output, mode = :table, options = {} else raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every end - + if position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n) after_last_newline = output[position_of_last_newline + 1 .. -1] ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/] - + if ends_with_newline line_count = output.count("\n") else @@ -68,7 +68,7 @@ def self.number! output, mode = :table, options = {} else line_count = 1 end - + case mode when :inline max_width = (start + line_count).to_s.size @@ -77,32 +77,43 @@ def self.number! output, mode = :table, options = {} line_number_text = bolding.call line_number indent = ' ' * (max_width - line_number.to_s.size) line_number += 1 - "#{indent}#{line_number_text}#{line}" + numbered_line = "#{indent}#{line_number_text}#{line}" + if options.wrap_lines + numbered_line = "
${numbered_line}
" + end + numbered_line end - when :table line_numbers = (start ... start + line_count).map(&bolding).join("\n") line_numbers << "\n" line_numbers_table_template = Output::TABLE.apply('LINE_NUMBERS', line_numbers) - + + if options.wrap_lines + line_number = start + output.gsub!(/^.*$\n?/) do |line| + "
#{line}
" + line_number += 1 + end + end + output.gsub!(/<\/div>\n/, '') output.wrap_in! line_numbers_table_template output.wrapped_in = :div - + when :list raise NotImplementedError, 'The :list option is no longer available. Use :table.' - + else raise ArgumentError, 'Unknown value %p for mode: expected one of %p' % [mode, [:table, :inline]] end - + output end - + end - + end - + end end From 2b618e94577aa1fceaa924486e8963ded32b7510 Mon Sep 17 00:00:00 2001 From: Tim Robertson Date: Tue, 20 May 2014 22:55:13 -0700 Subject: [PATCH 2/4] fix options fuckup --- lib/coderay/encoders/html/numbering.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb index 52438b87..d5559a36 100644 --- a/lib/coderay/encoders/html/numbering.rb +++ b/lib/coderay/encoders/html/numbering.rb @@ -88,7 +88,7 @@ def self.number! output, mode = :table, options = {} line_numbers << "\n" line_numbers_table_template = Output::TABLE.apply('LINE_NUMBERS', line_numbers) - if options.wrap_lines + if options[:wrap_lines] line_number = start output.gsub!(/^.*$\n?/) do |line| "
#{line}
" From 045bd8c09db31fd5b01a2e280fcb76f2b0f7ca73 Mon Sep 17 00:00:00 2001 From: Tim Robertson Date: Tue, 20 May 2014 22:57:08 -0700 Subject: [PATCH 3/4] bugfix --- lib/coderay/encoders/html/numbering.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb index d5559a36..cc4cdcbc 100644 --- a/lib/coderay/encoders/html/numbering.rb +++ b/lib/coderay/encoders/html/numbering.rb @@ -91,8 +91,9 @@ def self.number! output, mode = :table, options = {} if options[:wrap_lines] line_number = start output.gsub!(/^.*$\n?/) do |line| - "
#{line}
" + wrapped_line = "
#{line}
" line_number += 1 + wrapped_line end end From 1149efd6d90d5177c7b74eb03ba4594d89bb16ab Mon Sep 17 00:00:00 2001 From: Tim Robertson Date: Wed, 21 May 2014 19:04:33 -0700 Subject: [PATCH 4/4] move line number to data attribute --- lib/coderay/encoders/html/numbering.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb index cc4cdcbc..589ca186 100644 --- a/lib/coderay/encoders/html/numbering.rb +++ b/lib/coderay/encoders/html/numbering.rb @@ -79,7 +79,7 @@ def self.number! output, mode = :table, options = {} line_number += 1 numbered_line = "#{indent}#{line_number_text}#{line}" if options.wrap_lines - numbered_line = "
${numbered_line}
" + numbered_line = "
${numbered_line}
" end numbered_line end @@ -91,7 +91,7 @@ def self.number! output, mode = :table, options = {} if options[:wrap_lines] line_number = start output.gsub!(/^.*$\n?/) do |line| - wrapped_line = "
#{line}
" + wrapped_line = "
#{line}
" line_number += 1 wrapped_line end