annotate vendor/gems/coderay-0.9.7/lib/coderay/encoders/html/numerization.rb @ 855:7294e8db2515 bug_162

Close obsolete branch bug_162
author Chris Cannam
date Thu, 14 Jul 2011 11:59:19 +0100
parents 0579821a129a
children
rev   line source
Chris@210 1 module CodeRay
Chris@210 2 module Encoders
Chris@210 3
Chris@210 4 class HTML
Chris@210 5
Chris@210 6 module Output
Chris@210 7
Chris@210 8 def numerize *args
Chris@210 9 clone.numerize!(*args)
Chris@210 10 end
Chris@210 11
Chris@210 12 =begin NUMERIZABLE_WRAPPINGS = {
Chris@210 13 :table => [:div, :page, nil],
Chris@210 14 :inline => :all,
Chris@210 15 :list => [:div, :page, nil]
Chris@210 16 }
Chris@210 17 NUMERIZABLE_WRAPPINGS.default = :all
Chris@210 18 =end
Chris@210 19 def numerize! mode = :table, options = {}
Chris@210 20 return self unless mode
Chris@210 21
Chris@210 22 options = DEFAULT_OPTIONS.merge options
Chris@210 23
Chris@210 24 start = options[:line_number_start]
Chris@210 25 unless start.is_a? Integer
Chris@210 26 raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
Chris@210 27 end
Chris@210 28
Chris@210 29 #allowed_wrappings = NUMERIZABLE_WRAPPINGS[mode]
Chris@210 30 #unless allowed_wrappings == :all or allowed_wrappings.include? options[:wrap]
Chris@210 31 # raise ArgumentError, "Can't numerize, :wrap must be in %p, but is %p" % [NUMERIZABLE_WRAPPINGS, options[:wrap]]
Chris@210 32 #end
Chris@210 33
Chris@210 34 bold_every = options[:bold_every]
Chris@210 35 highlight_lines = options[:highlight_lines]
Chris@210 36 bolding =
Chris@210 37 if bold_every == false && highlight_lines == nil
Chris@210 38 proc { |line| line.to_s }
Chris@210 39 elsif highlight_lines.is_a? Enumerable
Chris@210 40 highlight_lines = highlight_lines.to_set
Chris@210 41 proc do |line|
Chris@210 42 if highlight_lines.include? line
Chris@210 43 "<strong class=\"highlighted\">#{line}</strong>" # highlighted line numbers in bold
Chris@210 44 else
Chris@210 45 line.to_s
Chris@210 46 end
Chris@210 47 end
Chris@210 48 elsif bold_every.is_a? Integer
Chris@210 49 raise ArgumentError, ":bolding can't be 0." if bold_every == 0
Chris@210 50 proc do |line|
Chris@210 51 if line % bold_every == 0
Chris@210 52 "<strong>#{line}</strong>" # every bold_every-th number in bold
Chris@210 53 else
Chris@210 54 line.to_s
Chris@210 55 end
Chris@210 56 end
Chris@210 57 else
Chris@210 58 raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
Chris@210 59 end
Chris@210 60
Chris@210 61 case mode
Chris@210 62 when :inline
Chris@210 63 max_width = (start + line_count).to_s.size
Chris@210 64 line_number = start
Chris@210 65 gsub!(/^/) do
Chris@210 66 line_number_text = bolding.call line_number
Chris@210 67 indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x)
Chris@210 68 res = "<span class=\"no\">#{indent}#{line_number_text}</span> "
Chris@210 69 line_number += 1
Chris@210 70 res
Chris@210 71 end
Chris@210 72
Chris@210 73 when :table
Chris@210 74 # This is really ugly.
Chris@210 75 # Because even monospace fonts seem to have different heights when bold,
Chris@210 76 # I make the newline bold, both in the code and the line numbers.
Chris@210 77 # FIXME Still not working perfect for Mr. Internet Exploder
Chris@210 78 line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n")
Chris@210 79 line_numbers << "\n" # also for Mr. MS Internet Exploder :-/
Chris@210 80 line_numbers.gsub!(/\n/) { "<tt>\n</tt>" }
Chris@210 81
Chris@210 82 line_numbers_table_tpl = TABLE.apply('LINE_NUMBERS', line_numbers)
Chris@210 83 gsub!("</div>\n", '</div>')
Chris@210 84 gsub!("\n", "<tt>\n</tt>")
Chris@210 85 wrap_in! line_numbers_table_tpl
Chris@210 86 @wrapped_in = :div
Chris@210 87
Chris@210 88 when :list
Chris@210 89 opened_tags = []
Chris@210 90 gsub!(/^.*$\n?/) do |line|
Chris@210 91 line.chomp!
Chris@210 92
Chris@210 93 open = opened_tags.join
Chris@210 94 line.scan(%r!<(/)?span[^>]*>?!) do |close,|
Chris@210 95 if close
Chris@210 96 opened_tags.pop
Chris@210 97 else
Chris@210 98 opened_tags << $&
Chris@210 99 end
Chris@210 100 end
Chris@210 101 close = '</span>' * opened_tags.size
Chris@210 102
Chris@210 103 "<li>#{open}#{line}#{close}</li>\n"
Chris@210 104 end
Chris@210 105 chomp!("\n")
Chris@210 106 wrap_in! LIST
Chris@210 107 @wrapped_in = :div
Chris@210 108
Chris@210 109 else
Chris@210 110 raise ArgumentError, 'Unknown value %p for mode: expected one of %p' %
Chris@210 111 [mode, [:table, :list, :inline]]
Chris@210 112 end
Chris@210 113
Chris@210 114 self
Chris@210 115 end
Chris@210 116
Chris@210 117 def line_count
Chris@210 118 line_count = count("\n")
Chris@210 119 position_of_last_newline = rindex(?\n)
Chris@210 120 if position_of_last_newline
Chris@210 121 after_last_newline = self[position_of_last_newline + 1 .. -1]
Chris@210 122 ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/]
Chris@210 123 line_count += 1 if not ends_with_newline
Chris@210 124 end
Chris@210 125 line_count
Chris@210 126 end
Chris@210 127
Chris@210 128 end
Chris@210 129
Chris@210 130 end
Chris@210 131
Chris@210 132 end
Chris@210 133 end