diff options
author | murphy <murphy@rubychan.de> | 2005-11-04 00:16:58 +0000 |
---|---|---|
committer | murphy <murphy@rubychan.de> | 2005-11-04 00:16:58 +0000 |
commit | 7371093d78689728c1bf8326987c24f73864edb2 (patch) | |
tree | 5707450828c5f8f816413b0a64e35d56c9fafec2 /lib/coderay/encoders/helpers | |
parent | 4d5b6c59075ea5435a8ac1879453f21247b38604 (diff) | |
download | coderay-7371093d78689728c1bf8326987c24f73864edb2.tar.gz |
HTML highlighting:
html_css.rb: Bugfixes
CSS style scanning went for wrong range
moved some styles into the right place
html_output.rb:
numerization excluded
templates renamed
new LIST template
html_numerization.rb (new):
new :line_numbers style :list (beta)
Benchmarking:
Split Options into lines
:list style for testing
Diffstat (limited to 'lib/coderay/encoders/helpers')
-rw-r--r-- | lib/coderay/encoders/helpers/html_css.rb | 13 | ||||
-rw-r--r-- | lib/coderay/encoders/helpers/html_numerization.rb | 112 | ||||
-rw-r--r-- | lib/coderay/encoders/helpers/html_output.rb | 86 |
3 files changed, 126 insertions, 85 deletions
diff --git a/lib/coderay/encoders/helpers/html_css.rb b/lib/coderay/encoders/helpers/html_css.rb index 6629bbf..80ccab4 100644 --- a/lib/coderay/encoders/helpers/html_css.rb +++ b/lib/coderay/encoders/helpers/html_css.rb @@ -12,7 +12,7 @@ module CodeRay module Encoders cl = @classes[styles.first]
return '' unless cl
style = false
- 1.upto(cl.size + 1) do |offset|
+ 1.upto(styles.size) do |offset|
break if style = cl[styles[offset .. -1]]
end
return style
@@ -46,16 +46,14 @@ module CodeRay module Encoders border: 1px solid silver;
font-family: 'Courier New', 'Terminal', monospace;
color: black;
- width: 100%;
- padding: 2px;
}
.CodeRay pre { margin: 0px; }
div.CodeRay { }
-span.CodeRay { white-space: pre; border: 0; }
+span.CodeRay { white-space: pre; border: 0px; padding: 2px; }
-table.CodeRay { border-collapse: collapse; }
+table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px; }
table.CodeRay td { padding: 2px 4px; vertical-align: top; }
.CodeRay .line_numbers, .CodeRay .no {
@@ -67,8 +65,9 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; } .CodeRay .no { padding: 0px 4px; }
.CodeRay .code { width: 100%; }
-.CodeRay .code {
-}
+ol.CodeRay { font-size: 10pt; }
+ol.CodeRay li { white-space: pre; }
+
.CodeRay .code pre { overflow: auto; }
MAIN
diff --git a/lib/coderay/encoders/helpers/html_numerization.rb b/lib/coderay/encoders/helpers/html_numerization.rb new file mode 100644 index 0000000..f51b5d3 --- /dev/null +++ b/lib/coderay/encoders/helpers/html_numerization.rb @@ -0,0 +1,112 @@ +module CodeRay
+ module Encoders
+
+ class HTML
+
+ module Output
+
+ def numerize *args
+ clone.numerize!(*args)
+ end
+
+ NUMERIZABLE_WRAPPINGS = {
+ :table => [:div, :page],
+ :inline => :all,
+ :list => [:div, :page],
+ nil => :all
+ }
+
+ 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
+
+ allowed_wrappings = NUMERIZABLE_WRAPPINGS[mode]
+ unless allowed_wrappings == :all or allowed_wrappings.include? options[:wrap]
+ raise ArgumentError, "Can't numerize, :wrap must be in %p, but is %p" % [NUMERIZABLE_WRAPPINGS, options[:wrap]]
+ end
+
+ bold_every = options[:bold_every]
+ bolding =
+ if bold_every == :no_bolding or bold_every == 0
+ proc { |line| line.to_s }
+ elsif bold_every.is_a? Integer
+ proc do |line|
+ if line % bold_every == 0
+ "<strong>#{line}</strong>" # every bold_every-th number in bold
+ else
+ line.to_s
+ end
+ end
+ else
+ raise ArgumentError, "Invalid value %p for :bolding; :no_bolding or Integer expected." % bolding
+ end
+
+ line_count = count("\n")
+ line_count += 1 if self[-1] != ?\n
+
+ case mode
+ when :inline
+ max_width = (start + line_count).to_s.size
+ line = start
+ gsub!(/^/) do
+ line_number = bolding.call line
+ line += 1
+ "<span class=\"no\">#{ line_number.rjust(max_width) }</span> "
+ end
+ #wrap! :div
+
+ when :table
+ # This is really ugly.
+ # Because even monospace fonts seem to have different heights when bold,
+ # I make the newline bold, both in the code and the line numbers.
+ # FIXME Still not working perfect for Mr. Internet Exploder
+ # FIXME Firefox struggles with very long codes (> 200 lines)
+ line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n")
+ line_numbers << "\n" # also for Mr. MS Internet Exploder :-/
+ line_numbers.gsub!(/\n/) { "<tt>\n</tt>" }
+
+ line_numbers_table_tpl = TABLE.apply('LINE_NUMBERS', line_numbers)
+ gsub!(/\n/) { "<tt>\n</tt>" }
+ wrap_in! line_numbers_table_tpl
+ @wrapped_in = :div
+
+ when :list
+ 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 = '</span>' * opened_tags.size
+
+ "<li>#{open}#{line}#{close}</li>"
+ end
+ wrap_in! LIST
+ @wrapped_in = :div
+
+ else
+ raise ArgumentError, "Unknown value %p for mode: expected one of %p" %
+ [mode, NUMERIZABLE_WRAPPINGS.keys - [:all]]
+ end
+
+ self
+ end
+
+ end
+
+ end
+
+end
+end
diff --git a/lib/coderay/encoders/helpers/html_output.rb b/lib/coderay/encoders/helpers/html_output.rb index c6012f7..f17965d 100644 --- a/lib/coderay/encoders/helpers/html_output.rb +++ b/lib/coderay/encoders/helpers/html_output.rb @@ -13,6 +13,8 @@ module CodeRay # TODO: more doc.
module Output
+ require 'coderay/encoders/helpers/html_numerization.rb'
+
attr_accessor :wrapped_in
class << self
@@ -104,82 +106,6 @@ module CodeRay clone.wrap!(*args)
end
- NUMERIZABLE_WRAPPINGS = {
- :table => [:div, :page],
- :inline => :all,
- nil => :all
- }
-
- 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
-
- allowed_wrappings = NUMERIZABLE_WRAPPINGS[mode]
- unless allowed_wrappings == :all or allowed_wrappings.include? options[:wrap]
- raise ArgumentError, "Can't numerize, :wrap must be in %p, but is %p" % [NUMERIZABLE_WRAPPINGS, options[:wrap]]
- end
-
- bold_every = options[:bold_every]
- bolding =
- if bold_every == :no_bolding or bold_every == 0
- proc { |line| line.to_s }
- elsif bold_every.is_a? Integer
- proc do |line|
- if line % bold_every == 0
- "<strong>#{line}</strong>" # every bold_every-th number in bold
- else
- line.to_s
- end
- end
- else
- raise ArgumentError, "Invalid value %p for :bolding; :no_bolding or Integer expected." % bolding
- end
-
- line_count = count("\n")
- line_count += 1 if self[-1] != ?\n
-
- case mode
- when :inline
- max_width = (start + line_count).to_s.size
- line = start
- gsub!(/^/) do
- line_number = bolding.call line
- line += 1
- "<span class=\"no\">#{ line_number.rjust(max_width) }</span> "
- end
- #wrap! :div
-
- when :table
- # This is really ugly.
- # Because even monospace fonts seem to have different heights when bold,
- # I make the newline bold, both in the code and the line numbers.
- # FIXME Still not working perfect for Mr. Internet Exploder
- line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n")
- line_numbers << "\n" # also for Mr. MS Internet Exploder :-/
- line_numbers.gsub!(/\n/) { "<tt>\n</tt>" }
-
- line_numbers_tpl = DIV_TABLE.apply('LINE_NUMBERS', line_numbers)
- gsub!(/\n/) { "<tt>\n</tt>" }
- wrap_in! line_numbers_tpl
- @wrapped_in = :div
-
- else
- raise ArgumentError, "Unknown value %p for mode: :inline or :table expected" % mode
- end
-
- self
- end
-
- def numerize *args
- clone.numerize!(*args)
- end
-
def stylesheet in_tag = false
Output.stylesheet in_tag
end
@@ -224,14 +150,18 @@ module CodeRay </div>
DIV
- DIV_TABLE = <<-`DIV_TABLE`
+ TABLE = <<-`TABLE`
<table class="CodeRay"> <tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><%LINE_NUMBERS%></pre></td>
<td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><%CONTENT%></pre></td>
</tr> </table>
- DIV_TABLE
+ TABLE
# title="double click to expand"
+ LIST = <<-`LIST`
+<ol class="CodeRay"><%CONTENT%></ol>
+ LIST
+
PAGE = <<-`PAGE`
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|