From 7863986fad84f56ba537ce3714d614c33391fd85 Mon Sep 17 00:00:00 2001 From: murphy Date: Thu, 23 Mar 2006 00:05:58 +0000 Subject: HTML Encoder: inline numerization corrected. New demo: demo/demo_html_list.rb. Some demos adjusted. C Scanner: error messages fixed. Rakefile: test_demos now in debug mode. test/suite.rb is using reusable Scanners now. --- Rakefile | 2 +- bench/caching.rb | 6 +- demo/demo_highlight.out | 4 +- demo/demo_html.out | 4 +- demo/demo_html2.out | 26 +++--- demo/demo_html_list.out | 128 ++++++++++++++++++++++++++++++ demo/demo_html_list.rb | 12 +++ demo/demo_more.out | 2 +- lib/coderay/duo.rb | 5 ++ lib/coderay/encoders/html/numerization.rb | 4 +- lib/coderay/scanner.rb | 18 +++-- lib/coderay/scanners/c.rb | 6 +- test/suite.rb | 5 +- 13 files changed, 186 insertions(+), 36 deletions(-) create mode 100644 demo/demo_html_list.out create mode 100644 demo/demo_html_list.rb 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; } - +
-
1
 2
 3
@@ -137,7 +137,7 @@ ol.CodeRay li { white-space: pre; }
 </html>
 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; } - +
-
1
 2
 3
@@ -881,7 +881,7 @@ ol.CodeRay li { white-space: pre; }
 
 end
 
+ 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; } - +
-
1
 2
 3
@@ -133,19 +133,19 @@ ol.CodeRay li { white-space: pre; }
 
 puts out.page
 
+
-
 8  require 'coderay'
- 9  
-10  # scan this file
-11  tokens = CodeRay.scan(File.read($0) * 1, :ruby)
-12  
-13  # output it with two styles of line numbers
-14  out = tokens.div(:line_numbers => :table)
-15  out << '<hr />'
-16  out << tokens.div(:line_numbers => :inline, :line_number_start => 8)
-17  
-18  puts out.page
+	
 8 require 'coderay'
+ 9 
+10 # scan this file
+11 tokens = CodeRay.scan(File.read($0) * 1, :ruby)
+12 
+13 # output it with two styles of line numbers
+14 out = tokens.div(:line_numbers => :table)
+15 out << '<hr />'
+16 out << tokens.div(:line_numbers => :inline, :line_number_start => 8)
+17 
+18 puts out.page
 
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 @@ + + + + + CodeRay HTML Encoder Example + + + + +
+
-1 $: << '..'
+ 0 require 'coderay'
+ 1 
+ 2 tokens = CodeRay.scan File.read(__FILE__), :ruby
+ 3 html = tokens.html(:tab_width => 2, :line_numbers => :inline, :line_number_start => -1)
+ 4 
+ 5 puts html.page
+ 6 
+ 7 commment = <<_
+ 8 This code must be > 10 lines
+ 9 because I want to test the correct adjustment of the line numbers.
+10 _
+
+
+ + + 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 = "#{indent}#{line_number} " line += 1 - "#{ line_number.rjust(max_width) } " + 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 -- cgit v1.2.1