./lib/coderay/encoders/html/numbering.rb in coderay-1.0.0.598.pre vs ./lib/coderay/encoders/html/numbering.rb in coderay-1.0.0.738.pre

- old
+ new

@@ -1,13 +1,13 @@ module CodeRay module Encoders class HTML - module Output # :nodoc: + module Numbering # :nodoc: - def number! mode = :table, options = {} + def self.number! output, mode = :table, options = {} return self unless mode options = DEFAULT_OPTIONS.merge options start = options[:line_number_start] @@ -24,11 +24,11 @@ line = line.to_s anchor = anchor_prefix + line "<a href=\"##{anchor}\" name=\"#{anchor}\">#{line}</a>" end else - proc { |line| line.to_s } + proc { |line| line.to_s } # :to_s.to_proc in Ruby 1.8.7+ end bold_every = options[:bold_every] highlight_lines = options[:highlight_lines] bolding = @@ -54,16 +54,24 @@ end else raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every end + line_count = output.count("\n") + position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n) + if position_of_last_newline + after_last_newline = output[position_of_last_newline + 1 .. -1] + ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/] + line_count += 1 if not ends_with_newline + end + case mode when :inline max_width = (start + line_count).to_s.size line_number = start opened_tags = [] - gsub!(/^.*$\n?/) do |line| + output.gsub!(/^.*$\n?/) do |line| line.chomp! open = opened_tags.join line.scan(%r!<(/)?span[^>]*>?!) do |close,| if close opened_tags.pop @@ -78,37 +86,26 @@ line_number += 1 "<span class=\"no\">#{indent}#{line_number_text}</span>#{open}#{line}#{close}\n" end when :table - line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n") + line_numbers = (start ... start + line_count).map(&bolding).join("\n") line_numbers << "\n" + line_numbers_table_template = Output::TABLE.apply('LINE_NUMBERS', line_numbers) - line_numbers_table_template = TABLE.apply('LINE_NUMBERS', line_numbers) - gsub!(/<\/div>\n/) { '</div>' } - wrap_in! line_numbers_table_template - @wrapped_in = :div + output.gsub!(/<\/div>\n/, '</div>') + 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 - self - end - - def line_count - line_count = count("\n") - position_of_last_newline = rindex(?\n) - if position_of_last_newline - after_last_newline = self[position_of_last_newline + 1 .. -1] - ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/] - line_count += 1 if not ends_with_newline - end - line_count + output end end end