summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Rakefile2
-rw-r--r--bench/caching.rb6
-rw-r--r--demo/demo_highlight.out4
-rw-r--r--demo/demo_html.out4
-rw-r--r--demo/demo_html2.out26
-rw-r--r--demo/demo_html_list.out128
-rw-r--r--demo/demo_html_list.rb12
-rw-r--r--demo/demo_more.out2
-rw-r--r--lib/coderay/duo.rb5
-rw-r--r--lib/coderay/encoders/html/numerization.rb4
-rw-r--r--lib/coderay/scanner.rb18
-rw-r--r--lib/coderay/scanners/c.rb6
-rw-r--r--test/suite.rb5
13 files changed, 186 insertions, 36 deletions
diff --git a/Rakefile b/Rakefile
index 48609b1..2de87a4 100644
--- a/Rakefile
+++ b/Rakefile
@@ -52,7 +52,7 @@ end
desc 'Test CodeRay Demos'
task :test_demos do
- system 'ruby -w ./demo/suite.rb'
+ system 'ruby -wd ./demo/suite.rb'
end
desc 'Test CodeRay'
diff --git a/bench/caching.rb b/bench/caching.rb
index 7ea4e6a..b6c8b66 100644
--- a/bench/caching.rb
+++ b/bench/caching.rb
@@ -4,7 +4,7 @@ require 'benchmark'
N = 1000
$code = 'snoo Snoo ' * 10
Benchmark.bm 15 do |bm|
- bm.report 'loading' do
+ bm.report 'Loading parts' do
CodeRay::Scanners.load :ruby
CodeRay::Encoders.load :div
end
@@ -16,7 +16,7 @@ Benchmark.bm 15 do |bm|
CodeRay::Scanners::Ruby.new($code).tokenize
)
end end
- bm.report 'Semi-cached' do
+ bm.report 'Encoder cached' do
encoder = CodeRay::Encoders::Div.new
N.times do
encoder.encode $code, :ruby
@@ -30,7 +30,7 @@ Benchmark.bm 15 do |bm|
encoder.encode_tokens scanner.tokens
end
end
- bm.report 'Duo cached' do
+ bm.report 'CodeRay::Duo' do
duo = CodeRay::Duo[:ruby, :div]
N.times do
duo.encode $code
diff --git a/demo/demo_highlight.out b/demo/demo_highlight.out
index 621ccc7..f7a686e 100644
--- a/demo/demo_highlight.out
+++ b/demo/demo_highlight.out
@@ -106,7 +106,7 @@ ol.CodeRay li { white-space: pre; }
</style>
<body>
-<table class="CodeRay"> <tr>
+<table class="CodeRay"><tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt>
</tt>2<tt>
</tt>3<tt>
@@ -137,7 +137,7 @@ ol.CodeRay li { white-space: pre; }
</tt>&lt;/html&gt;</span><span style="color:#710;"><tt>
</tt>HTML</span></span><tt>
</tt></pre></td>
-</tr> </table>
+</tr></table>
</body>
</html>
diff --git a/demo/demo_html.out b/demo/demo_html.out
index ac1d8d1..c0efc3c 100644
--- a/demo/demo_html.out
+++ b/demo/demo_html.out
@@ -108,7 +108,7 @@ ol.CodeRay li { white-space: pre; }
</head>
<body style="background-color: white;">
-<table class="CodeRay"> <tr>
+<table class="CodeRay"><tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt>
</tt>2<tt>
</tt>3<tt>
@@ -881,7 +881,7 @@ ol.CodeRay li { white-space: pre; }
</tt><tt>
</tt><span class="r">end</span><tt>
</tt></pre></td>
-</tr> </table>
+</tr></table>
</body>
</html>
diff --git a/demo/demo_html2.out b/demo/demo_html2.out
index 01e4bdd..7bcf11f 100644
--- a/demo/demo_html2.out
+++ b/demo/demo_html2.out
@@ -108,7 +108,7 @@ ol.CodeRay li { white-space: pre; }
</head>
<body style="background-color: white;">
-<table class="CodeRay"> <tr>
+<table class="CodeRay"><tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt>
</tt>2<tt>
</tt>3<tt>
@@ -133,19 +133,19 @@ ol.CodeRay li { white-space: pre; }
</tt><tt>
</tt>puts out.page<tt>
</tt></pre></td>
-</tr> </table>
+</tr></table>
<hr /><div class="CodeRay">
- <div class="code"><pre><span class="no"> 8</span> require <span style="background-color:#fff0f0;"><span style="color:#710;">'</span><span style="color:#D20;">coderay</span><span style="color:#710;">'</span></span>
-<span class="no"> 9</span>
-<span class="no"><strong>10</strong></span> <span style="color:#888;"># scan this file</span>
-<span class="no">11</span> tokens = <span style="color:#036; font-weight:bold;">CodeRay</span>.scan(<span style="color:#036; font-weight:bold;">File</span>.read(<span style="color:#d70; font-weight:bold;">$0</span>) * <span style="color:#00D; font-weight:bold;">1</span>, <span style="color:#A60;">:ruby</span>)
-<span class="no">12</span>
-<span class="no">13</span> <span style="color:#888;"># output it with two styles of line numbers</span>
-<span class="no">14</span> out = tokens.div(<span style="color:#A60;">:line_numbers</span> =&gt; <span style="color:#A60;">:table</span>)
-<span class="no">15</span> out &lt;&lt; <span style="background-color:#fff0f0;"><span style="color:#710;">'</span><span style="color:#D20;">&lt;hr /&gt;</span><span style="color:#710;">'</span></span>
-<span class="no">16</span> out &lt;&lt; tokens.div(<span style="color:#A60;">:line_numbers</span> =&gt; <span style="color:#A60;">:inline</span>, <span style="color:#A60;">:line_number_start</span> =&gt; <span style="color:#00D; font-weight:bold;">8</span>)
-<span class="no">17</span>
-<span class="no">18</span> puts out.page
+ <div class="code"><pre><span class="no"> 8</span> require <span style="background-color:#fff0f0;"><span style="color:#710;">'</span><span style="color:#D20;">coderay</span><span style="color:#710;">'</span></span>
+<span class="no"> 9</span>
+<span class="no"><strong>10</strong></span> <span style="color:#888;"># scan this file</span>
+<span class="no">11</span> tokens = <span style="color:#036; font-weight:bold;">CodeRay</span>.scan(<span style="color:#036; font-weight:bold;">File</span>.read(<span style="color:#d70; font-weight:bold;">$0</span>) * <span style="color:#00D; font-weight:bold;">1</span>, <span style="color:#A60;">:ruby</span>)
+<span class="no">12</span>
+<span class="no">13</span> <span style="color:#888;"># output it with two styles of line numbers</span>
+<span class="no">14</span> out = tokens.div(<span style="color:#A60;">:line_numbers</span> =&gt; <span style="color:#A60;">:table</span>)
+<span class="no">15</span> out &lt;&lt; <span style="background-color:#fff0f0;"><span style="color:#710;">'</span><span style="color:#D20;">&lt;hr /&gt;</span><span style="color:#710;">'</span></span>
+<span class="no">16</span> out &lt;&lt; tokens.div(<span style="color:#A60;">:line_numbers</span> =&gt; <span style="color:#A60;">:inline</span>, <span style="color:#A60;">:line_number_start</span> =&gt; <span style="color:#00D; font-weight:bold;">8</span>)
+<span class="no">17</span>
+<span class="no">18</span> puts out.page
</pre></div>
</div>
diff --git a/demo/demo_html_list.out b/demo/demo_html_list.out
new file mode 100644
index 0000000..e9c99b2
--- /dev/null
+++ b/demo/demo_html_list.out
@@ -0,0 +1,128 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="de">
+<head>
+ <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
+ <title>CodeRay HTML Encoder Example</title>
+ <style type="text/css">
+.CodeRay {
+ background-color: #f8f8f8;
+ border: 1px solid silver;
+ font-family: 'Courier New', 'Terminal', monospace;
+ color: #100;
+}
+.CodeRay pre { margin: 0px; }
+
+div.CodeRay { }
+
+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; }
+
+.CodeRay .line_numbers, .CodeRay .no {
+ background-color: #def;
+ color: gray;
+ text-align: right;
+}
+.CodeRay .line_numbers tt { font-weight: bold; }
+.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 .af { color:#00C; }
+.CodeRay .an { color:#007; }
+.CodeRay .av { color:#700; }
+.CodeRay .aw { color:#C00; }
+.CodeRay .bi { color:#509; font-weight:bold; }
+.CodeRay .c { color:#888; }
+
+.CodeRay .ch { color:#04D; }
+.CodeRay .ch .k { color:#04D; }
+.CodeRay .ch .dl { color:#039; }
+
+.CodeRay .cl { color:#B06; font-weight:bold; }
+.CodeRay .co { color:#036; font-weight:bold; }
+.CodeRay .cr { color:#0A0; }
+.CodeRay .cv { color:#369; }
+.CodeRay .df { color:#099; font-weight:bold; }
+.CodeRay .di { color:#088; font-weight:bold; }
+.CodeRay .dl { color:black; }
+.CodeRay .do { color:#970; }
+.CodeRay .ds { color:#D42; font-weight:bold; }
+.CodeRay .e { color:#666; font-weight:bold; }
+.CodeRay .er { color:#F00; background-color:#FAA; }
+.CodeRay .ex { color:#F00; font-weight:bold; }
+.CodeRay .fl { color:#60E; font-weight:bold; }
+.CodeRay .fu { color:#06B; font-weight:bold; }
+.CodeRay .gv { color:#d70; font-weight:bold; }
+.CodeRay .hx { color:#058; font-weight:bold; }
+.CodeRay .i { color:#00D; font-weight:bold; }
+.CodeRay .ic { color:#B44; font-weight:bold; }
+.CodeRay .il { }
+.CodeRay .in { color:#B2B; font-weight:bold; }
+.CodeRay .iv { color:#33B; }
+.CodeRay .la { color:#970; font-weight:bold; }
+.CodeRay .lv { color:#963; }
+.CodeRay .oc { color:#40E; font-weight:bold; }
+.CodeRay .on { color:#000; font-weight:bold; }
+.CodeRay .op { }
+.CodeRay .pc { color:#038; font-weight:bold; }
+.CodeRay .pd { color:#369; font-weight:bold; }
+.CodeRay .pp { color:#579; }
+.CodeRay .pt { color:#339; font-weight:bold; }
+.CodeRay .r { color:#080; font-weight:bold; }
+
+.CodeRay .rx { background-color:#fff0ff; }
+.CodeRay .rx .k { color:#808; }
+.CodeRay .rx .dl { color:#404; }
+.CodeRay .rx .mod { color:#C2C; }
+.CodeRay .rx .fu { color:#404; font-weight: bold; }
+
+.CodeRay .s { background-color:#fff0f0; }
+.CodeRay .s .s { background-color:#ffe0e0; }
+.CodeRay .s .s .s { background-color:#ffd0d0; }
+.CodeRay .s .k { color:#D20; }
+.CodeRay .s .dl { color:#710; }
+
+.CodeRay .sh { background-color:#f0fff0; }
+.CodeRay .sh .k { color:#2B2; }
+.CodeRay .sh .dl { color:#161; }
+
+.CodeRay .sy { color:#A60; }
+.CodeRay .sy .k { color:#A60; }
+.CodeRay .sy .dl { color:#630; }
+
+.CodeRay .ta { color:#070; }
+.CodeRay .tf { color:#070; font-weight:bold; }
+.CodeRay .ts { color:#D70; font-weight:bold; }
+.CodeRay .ty { color:#339; font-weight:bold; }
+.CodeRay .v { color:#036; }
+.CodeRay .xt { color:#444; }
+
+ </style>
+</head>
+<body style="background-color: white;">
+
+<div class="CodeRay">
+ <div class="code"><pre><span class="no">-1</span> <span class="gv">$:</span> &lt;&lt; <span class="s"><span class="dl">'</span><span class="k">..</span><span class="dl">'</span></span>
+<span class="no"> <strong>0</strong></span> require <span class="s"><span class="dl">'</span><span class="k">coderay</span><span class="dl">'</span></span>
+<span class="no"> 1</span>
+<span class="no"> 2</span> tokens = <span class="co">CodeRay</span>.scan <span class="co">File</span>.read(<span class="pc">__FILE__</span>), <span class="sy">:ruby</span>
+<span class="no"> 3</span> html = tokens.html(<span class="sy">:tab_width</span> =&gt; <span class="i">2</span>, <span class="sy">:line_numbers</span> =&gt; <span class="sy">:inline</span>, <span class="sy">:line_number_start</span> =&gt; <span class="i">-1</span>)
+<span class="no"> 4</span>
+<span class="no"> 5</span> puts html.page
+<span class="no"> 6</span>
+<span class="no"> 7</span> commment = <span class="s"><span class="dl">&lt;&lt;_</span></span><span class="s"><span class="k">
+<span class="no"> 8</span> This code must be &gt; 10 lines
+<span class="no"> 9</span> because I want to test the correct adjustment of the line numbers.</span><span class="dl">
+<span class="no"><strong>10</strong></span> _</span></span>
+</pre></div>
+</div>
+
+</body>
+</html>
diff --git a/demo/demo_html_list.rb b/demo/demo_html_list.rb
new file mode 100644
index 0000000..97724f2
--- /dev/null
+++ b/demo/demo_html_list.rb
@@ -0,0 +1,12 @@
+$: << '..'
+require 'coderay'
+
+tokens = CodeRay.scan File.read(__FILE__), :ruby
+html = tokens.html(:tab_width => 2, :line_numbers => :inline, :line_number_start => -1)
+
+puts html.page
+
+commment = <<_
+This code must be > 10 lines
+because I want to test the correct adjustment of the line numbers.
+_
diff --git a/demo/demo_more.out b/demo/demo_more.out
index 10f4ec6..f945854 100644
--- a/demo/demo_more.out
+++ b/demo/demo_more.out
@@ -1,2 +1,2 @@
-Input: 4983B, Output: 22388B
+Input: 4983B, Output: 22382B
Take a look with your browser.
diff --git a/lib/coderay/duo.rb b/lib/coderay/duo.rb
index 8187277..1957c95 100644
--- a/lib/coderay/duo.rb
+++ b/lib/coderay/duo.rb
@@ -1,5 +1,10 @@
module CodeRay
+ # = Duo
+ #
+ # $Id: scanner.rb 123 2006-03-21 14:46:34Z murphy $
+ #
+ # TODO: Doc.
class Duo
attr_accessor :scanner, :encoder
diff --git a/lib/coderay/encoders/html/numerization.rb b/lib/coderay/encoders/html/numerization.rb
index c5d96f3..19c760e 100644
--- a/lib/coderay/encoders/html/numerization.rb
+++ b/lib/coderay/encoders/html/numerization.rb
@@ -57,8 +57,10 @@ module Encoders
line = start
gsub!(/^/) do
line_number = bolding.call line
+ indent = ' ' * (max_width - line.to_s.size)
+ res = "<span class=\"no\">#{indent}#{line_number}</span> "
line += 1
- "<span class=\"no\">#{ line_number.rjust(max_width) }</span> "
+ res
end
when :table
diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb
index 5ff07a0..16998f2 100644
--- a/lib/coderay/scanner.rb
+++ b/lib/coderay/scanner.rb
@@ -91,14 +91,15 @@ module CodeRay
# TokenStream with the +block+ as callback to handle the tokens.
#
# Else, a Tokens object is used.
- def initialize code, options = {}, &block
+ def initialize code='', options = {}, &block
@options = self.class::DEFAULT_OPTIONS.merge options
raise "I am only the basic Scanner class. I can't scan "\
"anything. :( Use my subclasses." if self.class == Scanner
# I love this hack. It seems to silence
# all dos/unix/mac newline problems.
- super code.gsub(/\r\n?/, "\n")
+ code = code.gsub(/\r\n?/, "\n") if code.index ?\r
+ super code
if @options[:stream]
warn "warning in CodeRay::Scanner.new: :stream is set, "\
@@ -117,12 +118,13 @@ module CodeRay
def reset
super
- reset_tokens
+ reset_instance
end
- def string= str
- super
- reset_tokens
+ def string= code
+ code = code.gsub(/\r\n?/, "\n") if code.index ?\r
+ super code
+ reset_instance
end
# Scans the code and returns all tokens in a Tokens object.
@@ -168,10 +170,10 @@ module CodeRay
"#{self.class}#scan_tokens not implemented."
end
- def reset_tokens
+ def reset_instance
@tokens.clear
@cached_tokens = nil
- end
+ end
# Scanner error with additional status information
def raise_inspect msg, tokens, ambit = 30
diff --git a/lib/coderay/scanners/c.rb b/lib/coderay/scanners/c.rb
index 8fae829..5764254 100644
--- a/lib/coderay/scanners/c.rb
+++ b/lib/coderay/scanners/c.rb
@@ -110,7 +110,7 @@ module CodeRay module Scanners
kind = :error
state = :initial
else
- raise "else case \" reached; %p not handled." % peek(1), tokens
+ raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
end
elsif state == :include_expected
@@ -128,12 +128,12 @@ module CodeRay module Scanners
end
else
- raise 'else-case reached', tokens
+ raise_inspect 'else-case reached', tokens
end
match ||= matched
- raise [match, kind], tokens if kind == :error
+ raise_inspect [match, kind], tokens if kind == :error
tokens << [match, kind]
diff --git a/test/suite.rb b/test/suite.rb
index ef3e2fb..fb50532 100644
--- a/test/suite.rb
+++ b/test/suite.rb
@@ -36,7 +36,7 @@ class CodeRaySuite < TestCase
puts
puts " >> Running #{self.class.name} <<"
puts
- CodeRay::Scanners.load lang
+ scanner = CodeRay::Scanners[lang].new
tokenizer = CodeRay::Encoders[:debug].new
highlighter = CodeRay::Encoders[:html].new(
:tab_width => 2,
@@ -52,7 +52,8 @@ class CodeRaySuite < TestCase
output = name + '.out.' + tokenizer.file_extension
code = File.open(input, 'rb') { |f| break f.read }
- tokens = CodeRay.scan code, lang
+ scanner.string = code
+ tokens = scanner.tokens
result = tokenizer.encode_tokens tokens
highlighted = highlighter.encode_tokens tokens