coderay 0.4.5.73 → 0.5.0.100

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -18,13 +18,13 @@ And with line numbers.
18
18
  * is what everybody should have on their website
19
19
  * solves all your problems and makes the girls run after you
20
20
 
21
- Version: 0.4.4 (2005.october.1)
21
+ Version: 0.5.0 (2005.november.5)
22
22
  Author:: murphy
23
23
  Idea:: licenser
24
24
  Website:: rd.cYcnus.de/coderay[http://rd.cYcnus.de/coderay]
25
25
  Copyright:: (c) 2005 by cYcnus
26
26
  License:: GNU LGPL; see LICENSE file in the main directory.
27
- Subversion:: $Id: README 48 2005-10-01 06:12:12Z murphy $
27
+ Subversion:: $Id: README 94 2005-11-10 21:52:03Z murphy $
28
28
 
29
29
  -----
30
30
 
@@ -101,3 +101,4 @@ Where would we be without all those people?
101
101
  * As you can see, CodeRay was created under heavy use of *free* software.
102
102
  * So CodeRay is also *free*.
103
103
  * If you use CodeRay to create software, think about making this software *free*, too.
104
+ * Thanks :)
@@ -1,4 +1,4 @@
1
1
  require 'coderay'
2
2
 
3
3
  # print the default stylesheet for HTML codes
4
- puts CodeRay::Encoders[:html]::CSS::DEFAULT_STYLESHEET
4
+ puts CodeRay::Encoders[:html]::CSS.new.stylesheet
@@ -15,3 +15,11 @@ tokens_encoder = require_plugin 'CodeRay::Encoders/tokens'
15
15
  print 'Require is also possible: '
16
16
  p tokens_encoder
17
17
  puts 'See?'
18
+
19
+ puts 'Now load some mapped encoders: stats and plain.'
20
+ require_plugin 'CodeRay::Encoders/stats'
21
+ require_plugin 'CodeRay::Encoders/plain'
22
+
23
+ puts 'Require all Encoders:'
24
+ CodeRay::Encoders.load_all
25
+ p CodeRay::Encoders.plugin_hash.sort_by { |k,v| k.to_s }
@@ -0,0 +1,25 @@
1
+ require 'coderay'
2
+
3
+ begin
4
+ CodeRay::Scanners::Ruby
5
+ rescue
6
+ puts 'CodeRay::Encoders::Ruby is not defined; you must load it first.'
7
+ end
8
+
9
+ ruby_scanner = CodeRay::Scanners[:ruby]
10
+ print 'Now it is loaded: '
11
+ p ruby_scanner
12
+ puts 'See?'
13
+
14
+ c_scanner = require_plugin 'CodeRay::Scanners/c'
15
+ print 'Require is also possible: '
16
+ p c_scanner
17
+ puts 'See?'
18
+
19
+ puts 'Now load some mapped scanners: cpp and plain.'
20
+ require_plugin 'CodeRay::Scanners/cpp'
21
+ require_plugin 'CodeRay::Scanners/plain'
22
+
23
+ puts 'Require all Scanners:'
24
+ CodeRay::Scanners.load_all
25
+ p CodeRay::Scanners.plugin_hash.sort_by { |k,v| k.to_s }
@@ -17,7 +17,7 @@ time = Benchmark.realtime do
17
17
  body = %w[C Ruby Genereated\ by].zip([c, ruby, me]).map do |title, code|
18
18
  "<h1>#{title}</h1>\n#{code}"
19
19
  end.join
20
- body = hl.class::Output.new(body, :div).page!
20
+ body = hl.class::Output.new(body, hl.css, :div).page!
21
21
 
22
22
  # CodeRay also provides a simple page generator
23
23
  $output = body #hl.class.wrap_in_page body
@@ -27,7 +27,8 @@ File.open('test.html', 'w') do |f|
27
27
  f.write $output
28
28
  end
29
29
  puts 'Input: %dB, Output: %dB' % [$input.size, $output.size]
30
- puts 'Created "test.html" in %0.3f seconds (%d KB/s). Take a look with your browser.' % [time, $input.size / 1024.0 / time]
30
+ #puts 'Created "test.html" in %0.3f seconds (%d KB/s).' % [time, $input.size / 1024.0 / time]
31
+ puts 'Take a look with your browser.'
31
32
 
32
33
  __END__
33
34
  /**********************************************************************
@@ -0,0 +1,82 @@
1
+ mydir = File.dirname(__FILE__)
2
+ $:.unshift mydir + '/../lib/'
3
+
4
+ $VERBOSE = true
5
+
6
+ require 'test/unit'
7
+ include Test::Unit
8
+
9
+ class CodeRaySuite < TestCase
10
+
11
+ def self.dir &block
12
+ @dir ||= File.dirname(__FILE__)
13
+ if block
14
+ Dir.chdir @dir, &block
15
+ end
16
+ @dir
17
+ end
18
+
19
+ def dir &block
20
+ self.class.dir(&block)
21
+ end
22
+
23
+ def test_ALL
24
+ dir do
25
+ for input in Dir["demo_*.rb"] - ['demo_server.rb', 'demo_stream.rb']
26
+ puts "[ testing #{input}... ]"
27
+ name = File.basename(input, ".rb")
28
+ output = name + '.out'
29
+ code = File.open(input, 'rb') { |f| break f.read }
30
+
31
+ result = `ruby -wI../lib #{input}`
32
+
33
+ if File.exist? output
34
+ expected = File.read output
35
+ ok = expected == result
36
+ computed = output.sub('.out', '.computed')
37
+ unless ok
38
+ File.open(computed, 'w') { |f| f.write result }
39
+ print `gvimdiff #{output} #{computed}` if $DEBUG
40
+ end
41
+ assert(ok, "Output error: #{computed} != #{output}") unless $DEBUG
42
+ else
43
+ File.open(output, 'w') do |f| f.write result end
44
+ puts "New test: #{output}"
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ require 'test/unit/testsuite'
54
+ $suite = TestSuite.new 'CodeRay Demos Test'
55
+ $suite << CodeRaySuite.suite
56
+
57
+ def load_suite name
58
+ begin
59
+ require name + '/suite.rb'
60
+ rescue LoadError
61
+ $stderr.puts <<-ERR
62
+
63
+ !! Folder #{File.split(__FILE__).first + '/' + name} not found
64
+
65
+ ERR
66
+ false
67
+ end
68
+ end
69
+
70
+ if subsuite = ARGV.find { |a| break $1 if a[/^([^-].*)/] }
71
+ load_suite(subsuite) or exit
72
+ else
73
+ Dir[mydir + '/*/'].each { |suite| load_suite suite }
74
+ end
75
+
76
+ if ARGV.include? '-f'
77
+ require 'test/unit/ui/fox/testrunner'
78
+ UI::Fox::TestRunner.run $suite
79
+ else
80
+ require 'test/unit/ui/console/testrunner'
81
+ UI::Console::TestRunner.run $suite
82
+ end
@@ -1,6 +1,6 @@
1
1
  # = CodeRay Library
2
2
  #
3
- # $Id: coderay.rb 73 2005-10-29 16:09:17Z murphy $
3
+ # $Id: coderay.rb 96 2005-11-13 06:24:54Z murphy $
4
4
  #
5
5
  # CodeRay is a Ruby library for syntax highlighting.
6
6
  #
@@ -128,11 +128,17 @@
128
128
  # The scanning methods provide more flexibility; we recommend to use these.
129
129
  module CodeRay
130
130
 
131
- Version = '0.4.6'
131
+ # Version: Major.Minor.Teeny[.Revision]
132
+ # Major: 0 for pre-release
133
+ # Minor: odd for beta, even for stable
134
+ # Teeny: development state
135
+ # Revision: Subversion Revision number (generated on rake)
136
+ Version = '0.6.0'
132
137
 
133
138
  require 'coderay/tokens'
134
139
  require 'coderay/scanner'
135
140
  require 'coderay/encoder'
141
+ require 'coderay/style'
136
142
 
137
143
 
138
144
  class << self
@@ -9,7 +9,7 @@ module CodeRay
9
9
  # belonging to the given format.
10
10
  module Encoders
11
11
  extend PluginHost
12
- plugin_path 'coderay/encoders'
12
+ plugin_path File.dirname(__FILE__), 'encoders'
13
13
 
14
14
  # = Encoder
15
15
  #
@@ -0,0 +1,8 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ map :stats => :statistic,
5
+ :plain => :text
6
+
7
+ end
8
+ end
@@ -1,6 +1,7 @@
1
1
  module CodeRay module Encoders
2
2
 
3
- require 'coderay/encoders/html'
3
+ load :html
4
+
4
5
  class Div < HTML
5
6
 
6
7
  FILE_EXTENSION = 'div.html'
@@ -11,6 +12,7 @@ module CodeRay module Encoders
11
12
  :css => :style,
12
13
  :wrap => :div,
13
14
  })
15
+
14
16
  end
15
17
 
16
18
  end end
@@ -39,7 +39,7 @@ module Encoders
39
39
  # Default: :page
40
40
  #
41
41
  # === :line_numbers
42
- # Include line numbers in :table, :inline or nil (no line numbers)
42
+ # Include line numbers in :table, :inline, :list or nil (no line numbers)
43
43
  #
44
44
  # Default: nil
45
45
  #
@@ -71,6 +71,8 @@ module Encoders
71
71
  :level => :xhtml,
72
72
  :css => :class,
73
73
 
74
+ :style => :cycnus,
75
+
74
76
  :wrap => :page,
75
77
 
76
78
  :line_numbers => nil,
@@ -80,9 +82,9 @@ module Encoders
80
82
  :hint => false,
81
83
  }
82
84
 
83
- require 'coderay/encoders/helpers/html_helper'
84
- require 'coderay/encoders/helpers/html_output'
85
- require 'coderay/encoders/helpers/html_css'
85
+ helper :classes, :output, :css
86
+
87
+ attr_reader :css
86
88
 
87
89
  def initialize(*)
88
90
  super
@@ -116,7 +118,7 @@ module Encoders
116
118
  @HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
117
119
 
118
120
  @opened = [nil]
119
- @css = CSS.new
121
+ @css = CSS.new options[:style]
120
122
 
121
123
  hint = options[:hint]
122
124
  if hint and not [:debug, :info].include? hint
@@ -192,6 +194,7 @@ module Encoders
192
194
  @out << '</span>' * @opened.size
193
195
 
194
196
  @out.extend Output
197
+ @out.css = @css
195
198
  @out.numerize! options[:line_numbers], options # if options[:line_numbers]
196
199
  @out.wrap! options[:wrap] # if options[:wrap]
197
200
 
@@ -54,6 +54,7 @@ module CodeRay module Encoders
54
54
  :xml_text => 'xt',
55
55
 
56
56
  :ident => :NO_HIGHLIGHT, # 'id'
57
+ #:operator => 'op',
57
58
  :operator => :NO_HIGHLIGHT, # 'op'
58
59
  :space => :NO_HIGHLIGHT, # 'sp'
59
60
  :plain => :NO_HIGHLIGHT,
@@ -0,0 +1,66 @@
1
+ module CodeRay module Encoders
2
+
3
+ class HTML
4
+ class CSS
5
+
6
+ DEFAULT_STYLESHEET_ID = :cycnus
7
+
8
+ attr :stylesheet
9
+
10
+ def CSS.load_stylesheet style
11
+ style = DEFAULT_STYLESHEET_ID if style == :default
12
+ CodeRay::Styles[style]
13
+ end
14
+
15
+ def initialize style = :default
16
+ @classes = Hash.new
17
+ style = CSS.load_stylesheet style
18
+ @stylesheet = [
19
+ style::CSS_MAIN_STYLES,
20
+ style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ')
21
+ ].join("\n")
22
+ parse style::TOKEN_COLORS
23
+ end
24
+
25
+ def [] *styles
26
+ cl = @classes[styles.first]
27
+ return '' unless cl
28
+ style = ''
29
+ 1.upto(styles.size) do |offset|
30
+ break if style = cl[styles[offset .. -1]]
31
+ end
32
+ raise 'Style not found: %p' % [styles] if $DEBUG and style.empty?
33
+ return style
34
+ end
35
+
36
+ private
37
+
38
+ CSS_CLASS_PATTERN = /
39
+ ( (?: # $1 = classes
40
+ \s* \. [-\w]+
41
+ )+ )
42
+ \s* \{ \s*
43
+ ( [^\}]+ )? # $2 = style
44
+ \s* \} \s*
45
+ |
46
+ ( . ) # $3 = error
47
+ /mx
48
+ def parse stylesheet
49
+ stylesheet.scan CSS_CLASS_PATTERN do |classes, style, error|
50
+ raise "CSS parse error: '#{error.inspect}' not recognized" if error
51
+ styles = classes.scan(/[-\w]+/)
52
+ cl = styles.pop
53
+ @classes[cl] ||= Hash.new
54
+ @classes[cl][styles] = style.to_s.strip
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+
61
+ end end
62
+
63
+ if $0 == __FILE__
64
+ require 'pp'
65
+ pp CodeRay::Encoders::HTML::CSS.new
66
+ end
@@ -0,0 +1,112 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ class HTML
5
+
6
+ module Output
7
+
8
+ def numerize *args
9
+ clone.numerize!(*args)
10
+ end
11
+
12
+ NUMERIZABLE_WRAPPINGS = {
13
+ :table => [:div, :page],
14
+ :inline => :all,
15
+ :list => [:div, :page],
16
+ nil => :all
17
+ }
18
+
19
+ def numerize! mode = :table, options = {}
20
+ return self unless mode
21
+
22
+ options = DEFAULT_OPTIONS.merge options
23
+
24
+ start = options[:line_number_start]
25
+ unless start.is_a? Integer
26
+ raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
27
+ end
28
+
29
+ allowed_wrappings = NUMERIZABLE_WRAPPINGS[mode]
30
+ unless allowed_wrappings == :all or allowed_wrappings.include? options[:wrap]
31
+ raise ArgumentError, "Can't numerize, :wrap must be in %p, but is %p" % [NUMERIZABLE_WRAPPINGS, options[:wrap]]
32
+ end
33
+
34
+ bold_every = options[:bold_every]
35
+ bolding =
36
+ if bold_every == :no_bolding or bold_every == 0
37
+ proc { |line| line.to_s }
38
+ elsif bold_every.is_a? Integer
39
+ proc do |line|
40
+ if line % bold_every == 0
41
+ "<strong>#{line}</strong>" # every bold_every-th number in bold
42
+ else
43
+ line.to_s
44
+ end
45
+ end
46
+ else
47
+ raise ArgumentError, "Invalid value %p for :bolding; :no_bolding or Integer expected." % bolding
48
+ end
49
+
50
+ line_count = count("\n")
51
+ line_count += 1 if self[-1] != ?\n
52
+
53
+ case mode
54
+ when :inline
55
+ max_width = (start + line_count).to_s.size
56
+ line = start
57
+ gsub!(/^/) do
58
+ line_number = bolding.call line
59
+ line += 1
60
+ "<span class=\"no\">#{ line_number.rjust(max_width) }</span> "
61
+ end
62
+ #wrap! :div
63
+
64
+ when :table
65
+ # This is really ugly.
66
+ # Because even monospace fonts seem to have different heights when bold,
67
+ # I make the newline bold, both in the code and the line numbers.
68
+ # FIXME Still not working perfect for Mr. Internet Exploder
69
+ # FIXME Firefox struggles with very long codes (> 200 lines)
70
+ line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n")
71
+ line_numbers << "\n" # also for Mr. MS Internet Exploder :-/
72
+ line_numbers.gsub!(/\n/) { "<tt>\n</tt>" }
73
+
74
+ line_numbers_table_tpl = TABLE.apply('LINE_NUMBERS', line_numbers)
75
+ gsub!(/\n/) { "<tt>\n</tt>" }
76
+ wrap_in! line_numbers_table_tpl
77
+ @wrapped_in = :div
78
+
79
+ when :list
80
+ opened_tags = []
81
+ gsub!(/^.*$\n?/) do |line|
82
+ line.chomp!
83
+
84
+ open = opened_tags.join
85
+ line.scan(%r!<(/)?span[^>]*>?!) do |close,|
86
+ if close
87
+ opened_tags.pop
88
+ else
89
+ opened_tags << $&
90
+ end
91
+ end
92
+ close = '</span>' * opened_tags.size
93
+
94
+ "<li>#{open}#{line}#{close}</li>"
95
+ end
96
+ wrap_in! LIST
97
+ @wrapped_in = :div
98
+
99
+ else
100
+ raise ArgumentError, "Unknown value %p for mode: expected one of %p" %
101
+ [mode, NUMERIZABLE_WRAPPINGS.keys - [:all]]
102
+ end
103
+
104
+ self
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+
111
+ end
112
+ end