summaryrefslogtreecommitdiff
path: root/lib/coderay
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2010-04-02 01:53:38 +0000
committermurphy <murphy@rubychan.de>2010-04-02 01:53:38 +0000
commitb9377ad2633d5f7de92b339f1ad28b11942adfe3 (patch)
tree759095552fbebb6b01c2fcd9c8398d97237a5e81 /lib/coderay
parent1de8e57c915561e0c5ce7e98e8760c7e7ad0a38e (diff)
downloadcoderay-b9377ad2633d5f7de92b339f1ad28b11942adfe3.tar.gz
New HTML Encoder option :line_number_anchors (closes #208).
Also removed the :list wrapping style, and made a lot of cleanup in the styles. See Changes.textile.
Diffstat (limited to 'lib/coderay')
-rw-r--r--lib/coderay/encoders/html.rb11
-rw-r--r--lib/coderay/encoders/html/numerization.rb85
-rw-r--r--lib/coderay/encoders/html/output.rb12
-rw-r--r--lib/coderay/styles/alpha.rb24
-rw-r--r--lib/coderay/styles/cycnus.rb23
-rw-r--r--lib/coderay/styles/murphy.rb8
6 files changed, 71 insertions, 92 deletions
diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index 56857dc..dcdffa1 100644
--- a/lib/coderay/encoders/html.rb
+++ b/lib/coderay/encoders/html.rb
@@ -49,10 +49,18 @@ module Encoders
# Default: 'CodeRay output'
#
# === :line_numbers
- # Include line numbers in :table, :inline, :list or nil (no line numbers)
+ # Include line numbers in :table, :inline, or nil (no line numbers)
#
# Default: nil
#
+ # === :line_number_anchors
+ # Adds anchors and links to the line numbers. Can be false (off), true (on),
+ # or a prefix string that will be prepended to the anchor name.
+ #
+ # The prefix must consist only of letters, digits, and underscores.
+ #
+ # Default: true, default prefix name: "line"
+ #
# === :line_number_start
# Where to start with line number counting.
#
@@ -96,6 +104,7 @@ module Encoders
:title => 'CodeRay output',
:line_numbers => nil,
+ :line_number_anchors => 'n',
:line_number_start => 1,
:bold_every => 10,
:highlight_lines => nil,
diff --git a/lib/coderay/encoders/html/numerization.rb b/lib/coderay/encoders/html/numerization.rb
index 5bfcc2d..1590ad0 100644
--- a/lib/coderay/encoders/html/numerization.rb
+++ b/lib/coderay/encoders/html/numerization.rb
@@ -9,13 +9,6 @@ module Encoders
clone.numerize!(*args)
end
-=begin NUMERIZABLE_WRAPPINGS = {
- :table => [:div, :page, nil],
- :inline => :all,
- :list => [:div, :page, nil]
- }
- NUMERIZABLE_WRAPPINGS.default = :all
-=end
def numerize! mode = :table, options = {}
return self unless mode
@@ -25,69 +18,55 @@ module Encoders
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
-
+
+ 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
+ "<a href=\"##{anchor}\" name=\"#{anchor}\">#{line}</a>"
+ 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
- proc { |line| line.to_s }
+ anchoring
elsif highlight_lines.is_a? Enumerable
highlight_lines = highlight_lines.to_set
proc do |line|
if highlight_lines.include? line
- "<strong class=\"highlighted\">#{line}</strong>" # highlighted line numbers in bold
+ "<strong class=\"highlighted\">#{anchoring[line]}</strong>" # highlighted line numbers in bold
else
- line.to_s
+ 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
- "<strong>#{line}</strong>" # every bold_every-th number in bold
+ "<strong>#{anchoring[line]}</strong>" # every bold_every-th number in bold
else
- line.to_s
+ 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
- gsub!(/^/) do
- line_number_text = bolding.call line_number
- indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x)
- res = "<span class=\"no\">#{indent}#{line_number_text}</span> "
- line_number += 1
- res
- end
-
- 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_table_tpl = TABLE.apply('LINE_NUMBERS', line_numbers)
- gsub!(/<\/div>\n/) { '</div>' }
- 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
@@ -97,16 +76,28 @@ module Encoders
end
end
close = '</span>' * opened_tags.size
-
- "<li>#{open}#{line}#{close}</li>\n"
+
+ line_number_text = bolding.call line_number
+ indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x)
+ line_number += 1
+ "<span class=\"no\">#{indent}#{line_number_text}</span>#{open}#{line}#{close}\n"
end
- chomp!("\n")
- wrap_in! LIST
+
+ 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/) { '</div>' }
+ 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, :list, :inline]]
+ [mode, [:table, :inline]]
end
self
diff --git a/lib/coderay/encoders/html/output.rb b/lib/coderay/encoders/html/output.rb
index 83c4ec6..533235d 100644
--- a/lib/coderay/encoders/html/output.rb
+++ b/lib/coderay/encoders/html/output.rb
@@ -168,18 +168,12 @@ module Encoders
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="line_numbers" title="double click to toggle" ondblclick="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>
TABLE
# title="double click to expand"
- LIST = <<-`LIST`
-<ol class="CodeRay">
-<%CONTENT%>
-</ol>
- LIST
-
PAGE = <<-`PAGE`
<!DOCTYPE html>
<html>
@@ -187,6 +181,10 @@ module Encoders
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
+.CodeRay .line_numbers a, .CodeRay .no a {
+ text-decoration: inherit;
+ color: inherit;
+}
<%CSS%>
</style>
</head>
diff --git a/lib/coderay/styles/alpha.rb b/lib/coderay/styles/alpha.rb
index 655a4de..411ac0c 100644
--- a/lib/coderay/styles/alpha.rb
+++ b/lib/coderay/styles/alpha.rb
@@ -18,29 +18,23 @@ module Styles
font-family: 'Courier New', 'Terminal', monospace;
color: #{normal_color};
}
-.CodeRay pre { margin: 0px }
+.CodeRay pre { margin: 0px; }
-div.CodeRay { }
+span.CodeRay { white-space: pre; border: 0px; padding: 2px; }
-span.CodeRay { white-space: pre; border: 0px; padding: 2px }
-
-table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px }
-table.CodeRay td { padding: 2px 4px; vertical-align: top }
+table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px; }
+table.CodeRay td { padding: 2px 4px; vertical-align: top; }
.CodeRay .line_numbers, .CodeRay .no {
background-color: #{numbers_background};
color: gray;
text-align: right;
}
-.CodeRay .line_numbers tt { font-weight: bold }
-.CodeRay .line_numbers .highlighted { color: red }
-.CodeRay .no { padding: 0px 4px }
-.CodeRay .code { width: 100% }
-
-ol.CodeRay { font-size: 10pt }
-ol.CodeRay li { white-space: pre }
-
-.CodeRay .code pre { overflow: auto }
+.CodeRay .line_numbers a:target, .CodeRay .no a:target { color: blue; }
+.CodeRay .line_numbers .highlighted, .CodeRay .no .highlighted { color: red; }
+.CodeRay .no { padding: 0px 4px; }
+.CodeRay .code { width: 100%; }
+.CodeRay .code pre { overflow: auto; }
MAIN
TOKEN_COLORS = <<-'TOKENS'
diff --git a/lib/coderay/styles/cycnus.rb b/lib/coderay/styles/cycnus.rb
index a7f715f..a7eba80 100644
--- a/lib/coderay/styles/cycnus.rb
+++ b/lib/coderay/styles/cycnus.rb
@@ -18,29 +18,22 @@ module Styles
font-family: 'Courier New', 'Terminal', monospace;
color: #{normal_color};
}
-.CodeRay pre { margin: 0px }
+.CodeRay pre { margin: 0px; }
-div.CodeRay { }
+span.CodeRay { white-space: pre; border: 0px; padding: 2px; }
-span.CodeRay { white-space: pre; border: 0px; padding: 2px }
-
-table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px }
-table.CodeRay td { padding: 2px 4px; vertical-align: top }
+table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px; }
+table.CodeRay td { padding: 2px 4px; vertical-align: top; }
.CodeRay .line_numbers, .CodeRay .no {
background-color: #{numbers_background};
color: gray;
text-align: right;
}
-.CodeRay .line_numbers tt { font-weight: bold }
-.CodeRay .line_numbers .highlighted { color: red }
-.CodeRay .no { padding: 0px 4px }
-.CodeRay .code { width: 100% }
-
-ol.CodeRay { font-size: 10pt }
-ol.CodeRay li { white-space: pre }
-
-.CodeRay .code pre { overflow: auto }
+.CodeRay .line_numbers .highlighted, .CodeRay .no .highlighted { color: red; }
+.CodeRay .no { padding: 0px 4px; }
+.CodeRay .code { width: 100%; }
+.CodeRay .code pre { overflow: auto; }
MAIN
TOKEN_COLORS = <<-'TOKENS'
diff --git a/lib/coderay/styles/murphy.rb b/lib/coderay/styles/murphy.rb
index 033d949..0610dd9 100644
--- a/lib/coderay/styles/murphy.rb
+++ b/lib/coderay/styles/murphy.rb
@@ -20,8 +20,6 @@ module Styles
}
.CodeRay pre { margin: 0px; }
-div.CodeRay { }
-
span.CodeRay { white-space: pre; border: 0px; padding: 2px; }
table.CodeRay { border-collapse: collapse; width: 100%; padding: 2px; }
@@ -32,13 +30,9 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
color: gray;
text-align: right;
}
-.CodeRay .line_numbers tt { font-weight: bold; }
+.CodeRay .line_numbers .highlighted, .CodeRay .no .highlighted { color: red; }
.CodeRay .no { padding: 0px 4px; }
.CodeRay .code { width: 100%; }
-
-ol.CodeRay { font-size: 10pt; }
-ol.CodeRay li { white-space: pre; }
-
.CodeRay .code pre { overflow: auto; }
MAIN