1
1
module CodeRay
2
2
module Encoders
3
-
3
+
4
4
class HTML
5
-
5
+
6
6
module Numbering # :nodoc:
7
-
7
+
8
8
def self . number! output , mode = :table , options = { }
9
9
return self unless mode
10
-
10
+
11
11
options = DEFAULT_OPTIONS . merge options
12
-
12
+
13
13
start = options [ :line_number_start ]
14
14
unless start . is_a? Integer
15
15
raise ArgumentError , "Invalid value %p for :line_number_start; Integer expected." % start
@@ -56,12 +56,17 @@ def self.number! output, mode = :table, options = {}
56
56
raise ArgumentError , 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
57
57
end
58
58
59
- line_count = output . count ( "\n " )
60
- position_of_last_newline = output . rindex ( RUBY_VERSION >= '1.9' ? /\n / : ?\n)
61
- if position_of_last_newline
59
+ if position_of_last_newline = output . rindex ( RUBY_VERSION >= '1.9' ? /\n / : ?\n)
62
60
after_last_newline = output [ position_of_last_newline + 1 .. -1 ]
63
61
ends_with_newline = after_last_newline [ /\A (?:<\/ span>)*\z / ]
64
- line_count += 1 if not ends_with_newline
62
+
63
+ if ends_with_newline
64
+ line_count = output . count ( "\n " )
65
+ else
66
+ line_count = output . count ( "\n " ) + 1
67
+ end
68
+ else
69
+ line_count = 1
65
70
end
66
71
67
72
case mode
@@ -74,30 +79,30 @@ def self.number! output, mode = :table, options = {}
74
79
line_number += 1
75
80
"<span class=\" line-numbers\" >#{ indent } #{ line_number_text } </span>#{ line } "
76
81
end
77
-
82
+
78
83
when :table
79
84
line_numbers = ( start ... start + line_count ) . map ( &bolding ) . join ( "\n " )
80
85
line_numbers << "\n "
81
86
line_numbers_table_template = Output ::TABLE . apply ( 'LINE_NUMBERS' , line_numbers )
82
-
87
+
83
88
output . gsub! ( /<\/ div>\n / , '</div>' )
84
89
output . wrap_in! line_numbers_table_template
85
90
output . wrapped_in = :div
86
-
91
+
87
92
when :list
88
93
raise NotImplementedError , 'The :list option is no longer available. Use :table.'
89
-
94
+
90
95
else
91
96
raise ArgumentError , 'Unknown value %p for mode: expected one of %p' %
92
97
[ mode , [ :table , :inline ] ]
93
98
end
94
-
99
+
95
100
output
96
101
end
97
-
102
+
98
103
end
99
-
104
+
100
105
end
101
-
106
+
102
107
end
103
108
end
0 commit comments