From 7284b3674bada6a009bdfa23bd822a6ba6fc4896 Mon Sep 17 00:00:00 2001 From: murphy Date: Tue, 1 Jun 2010 18:10:30 +0000 Subject: Cleanups in Encoders::HTML::Output. --- lib/coderay/encoders/html.rb | 4 +- lib/coderay/encoders/html/numbering.rb | 118 +++++++++++++++++++++++++++++ lib/coderay/encoders/html/numerization.rb | 122 ------------------------------ lib/coderay/encoders/html/output.rb | 2 - 4 files changed, 120 insertions(+), 126 deletions(-) create mode 100644 lib/coderay/encoders/html/numbering.rb delete mode 100644 lib/coderay/encoders/html/numerization.rb (limited to 'lib/coderay/encoders') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index f14c1cc..eed4dfe 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -111,7 +111,7 @@ module Encoders :hint => false, } - helper :output, :css + helper :output, :numbering, :css attr_reader :css @@ -233,7 +233,7 @@ module Encoders @out.extend Output @out.css = @css - @out.numerize! options[:line_numbers], options + @out.number! options[:line_numbers], options @out.wrap! options[:wrap] @out.apply_title! options[:title] diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb new file mode 100644 index 0000000..3a7edce --- /dev/null +++ b/lib/coderay/encoders/html/numbering.rb @@ -0,0 +1,118 @@ +module CodeRay +module Encoders + + class HTML + + module Output # :nodoc: + + def number! 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 + anchoring = + if anchor_prefix + proc do |line| + line = line.to_s + anchor = anchor_prefix + line + "#{line}" + end + else + proc { |line| line.to_s } + end + + bold_every = options[:bold_every] + highlight_lines = options[:highlight_lines] + bolding = + if bold_every == false && highlight_lines == nil + anchoring + elsif highlight_lines.is_a? Enumerable + highlight_lines = highlight_lines.to_set + proc do |line| + if highlight_lines.include? line + "#{anchoring[line]}" # highlighted line numbers in bold + else + anchoring[line] + end + end + elsif bold_every.is_a? Integer + raise ArgumentError, ":bolding can't be 0." if bold_every == 0 + proc do |line| + if line % bold_every == 0 + "#{anchoring[line]}" # every bold_every-th number in bold + else + anchoring[line] + end + end + else + raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every + end + + case mode + when :inline + max_width = (start + line_count).to_s.size + line_number = start + opened_tags = [] + gsub!(/^.*$\n?/) do |line| + line.chomp! + open = opened_tags.join + line.scan(%r!<(/)?span[^>]*>?!) do |close,| + if close + opened_tags.pop + else + opened_tags << $& + end + end + close = '' * opened_tags.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" + end + + when :table + line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n") + line_numbers << "\n" + + line_numbers_table_template = TABLE.apply('LINE_NUMBERS', line_numbers) + gsub!(/<\/div>\n/) { '' } + wrap_in! line_numbers_table_template + @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 + end + + end + + end + +end +end diff --git a/lib/coderay/encoders/html/numerization.rb b/lib/coderay/encoders/html/numerization.rb deleted file mode 100644 index 1590ad0..0000000 --- a/lib/coderay/encoders/html/numerization.rb +++ /dev/null @@ -1,122 +0,0 @@ -module CodeRay -module Encoders - - class HTML - - module Output - - def numerize *args - clone.numerize!(*args) - end - - def numerize! 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 - anchoring = - if anchor_prefix - proc do |line| - line = line.to_s - anchor = anchor_prefix + line - "#{line}" - end - else - proc { |line| line.to_s } - end - - bold_every = options[:bold_every] - highlight_lines = options[:highlight_lines] - bolding = - if bold_every == false && highlight_lines == nil - anchoring - elsif highlight_lines.is_a? Enumerable - highlight_lines = highlight_lines.to_set - proc do |line| - if highlight_lines.include? line - "#{anchoring[line]}" # highlighted line numbers in bold - else - anchoring[line] - end - end - elsif bold_every.is_a? Integer - raise ArgumentError, ":bolding can't be 0." if bold_every == 0 - proc do |line| - if line % bold_every == 0 - "#{anchoring[line]}" # every bold_every-th number in bold - else - anchoring[line] - end - end - else - raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every - end - - case mode - when :inline - max_width = (start + line_count).to_s.size - line_number = start - opened_tags = [] - gsub!(/^.*$\n?/) do |line| - line.chomp! - open = opened_tags.join - line.scan(%r!<(/)?span[^>]*>?!) do |close,| - if close - opened_tags.pop - else - opened_tags << $& - end - end - close = '' * opened_tags.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" - end - - when :table - line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n") - line_numbers << "\n" - - line_numbers_table_template = TABLE.apply('LINE_NUMBERS', line_numbers) - gsub!(/<\/div>\n/) { '' } - wrap_in! line_numbers_table_template - @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 - end - - end - - end - -end -end diff --git a/lib/coderay/encoders/html/output.rb b/lib/coderay/encoders/html/output.rb index 533235d..dec1d7c 100644 --- a/lib/coderay/encoders/html/output.rb +++ b/lib/coderay/encoders/html/output.rb @@ -13,8 +13,6 @@ module Encoders # TODO: more doc. module Output - require 'coderay/encoders/html/numerization.rb' - attr_accessor :css class << self -- cgit v1.2.1