summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2013-03-10 21:12:53 +0100
committerKornelius Kalnbach <murphy@rubychan.de>2013-03-10 21:12:53 +0100
commit3d7f34571a0b2e58ee90498bc54f160bda2bed45 (patch)
tree1c6ac0807a3beae63e3eedca70f5622d71f0c32a
parent0d2d0bef962d27abb0b6d7423bce6010bb71976c (diff)
downloadcoderay-3d7f34571a0b2e58ee90498bc54f160bda2bed45.tar.gz
fix #41 by removing special case
Also, clean up some code.
-rw-r--r--Changes.textile1
-rw-r--r--lib/coderay/encoders/html/numbering.rb39
-rwxr-xr-xtest/functional/examples.rb2
3 files changed, 24 insertions, 18 deletions
diff --git a/Changes.textile b/Changes.textile
index 8143b0e..be05e9d 100644
--- a/Changes.textile
+++ b/Changes.textile
@@ -5,6 +5,7 @@ p=. _This files lists all changes in the CodeRay library since the 0.9.8 release
h2. Changes in 1.1
* Remove double-click toggle handler from HTML table output
+* Display line numbers in HTML @:table@ mode even for single-line code (remove special case) [#41, thanks to Ariejan de Vroom]
h2. Changes in 1.0.9
diff --git a/lib/coderay/encoders/html/numbering.rb b/lib/coderay/encoders/html/numbering.rb
index e717429..332145b 100644
--- a/lib/coderay/encoders/html/numbering.rb
+++ b/lib/coderay/encoders/html/numbering.rb
@@ -1,15 +1,15 @@
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
@@ -56,12 +56,17 @@ module Encoders
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
+ 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/]
- line_count += 1 if not ends_with_newline
+
+ if ends_with_newline
+ line_count = output.count("\n")
+ else
+ line_count = output.count("\n") + 1
+ end
+ else
+ line_count = 1
end
case mode
@@ -74,30 +79,30 @@ module Encoders
line_number += 1
"<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{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)
-
+
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
-
+
output
end
-
+
end
-
+
end
-
+
end
end
diff --git a/test/functional/examples.rb b/test/functional/examples.rb
index b8e0a2a..15f9ca3 100755
--- a/test/functional/examples.rb
+++ b/test/functional/examples.rb
@@ -38,7 +38,7 @@ end
<body>
<table class="CodeRay"><tr>
- <td class="line-numbers"><pre>
+ <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
</pre></td>
<td class="code"><pre>puts <span class="string"><span class="delimiter">&quot;</span><span class="content">Hello, world!</span><span class="delimiter">&quot;</span></span></pre></td>
</tr></table>