coderay 0.4.3.48 → 0.4.5.73

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ require 'coderay'
2
+
3
+ puts CodeRay.highlight('puts "Hello, World!"', :ruby)
4
+
5
+ output = CodeRay.highlight_file($0, :line_numbers => :table)
6
+ puts <<HTML
7
+ <html>
8
+ <head>
9
+ #{output.stylesheet true}
10
+ <body>
11
+ #{output}
12
+ </body>
13
+ </html>
14
+ HTML
data/demo/demo_server.rb CHANGED
@@ -1,10 +1,24 @@
1
1
  # CodeRay dynamic highlighter
2
- #
3
- # Usage: start this and your browser.
4
- #
5
- # Go to http://localhost:49374/?<path to the file>
6
- # (mnemonic: 49374 = Four-Nine-Three-Seven-Four = For No Token Shall Fall)
7
- # and you should get the highlighted version.
2
+
3
+ unless ARGV.grep(/-[hv]|--(help|version)/).empty?
4
+ puts <<-USAGE
5
+ CodeRay Server 0.5
6
+ $Id: demo_server.rb 57 2005-10-22 01:16:34Z murphy $
7
+
8
+ Usage:
9
+ 1) Start this and your browser.
10
+ 2) Go to http://localhost:2468/?<path to the file>
11
+ and you should get the highlighted version.
12
+
13
+ Parameters:
14
+ -d Debug mode; reload CodeRay engine for every file.
15
+ (prepare for MANY "already initialized" and "method redefined"
16
+ messages - ingore it.)
17
+
18
+ ... More to come.
19
+ USAGE
20
+ exit
21
+ end
8
22
 
9
23
  require 'webrick'
10
24
  require 'pathname'
@@ -29,7 +43,7 @@ require 'coderay'
29
43
  class CodeRayServlet < WEBrick::HTTPServlet::AbstractServlet
30
44
 
31
45
  STYLE = 'style="font-family: sans-serif; color: navy;"'
32
- BANNER = '<p><img src="http://rd.cYcnus.de/coderay/coderay-banner" style="border: 0" alt="HIghlighted by CodeRay"/></p>'
46
+ BANNER = '<p><img src="http://rd.cYcnus.de/coderay/coderay-banner" style="border: 0" alt="Highlighted by CodeRay"/></p>'
33
47
 
34
48
  def do_GET req, res
35
49
  q = req.query_string || ''
@@ -59,6 +73,10 @@ class CodeRayServlet < WEBrick::HTTPServlet::AbstractServlet
59
73
  page << "#{BANNER}</body></html>"
60
74
 
61
75
  elsif File.exist? path
76
+ if $DEBUG
77
+ $".delete_if { |f| f =~ /coderay/ }
78
+ require 'coderay'
79
+ end
62
80
  div = CodeRay.scan_file(path).html :tab_width => 8, :wrap => :div
63
81
  div.replace <<-DIV
64
82
  <div #{STYLE}>
@@ -74,7 +92,7 @@ class CodeRayServlet < WEBrick::HTTPServlet::AbstractServlet
74
92
  end
75
93
  end
76
94
 
77
- # this is taken by "qip_msgd" - I don't know that.
95
+ # This port is taken by "qip_msgd" - I don't know that. Do you?
78
96
  module CodeRay
79
97
  PORT = 0xC0DE / 20
80
98
  end
data/lib/coderay.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # = CodeRay Library
2
2
  #
3
- # $Id: coderay.rb 44 2005-10-01 00:59:05Z murphy $
3
+ # $Id: coderay.rb 73 2005-10-29 16:09:17Z murphy $
4
4
  #
5
5
  # CodeRay is a Ruby library for syntax highlighting.
6
6
  #
@@ -128,7 +128,7 @@
128
128
  # The scanning methods provide more flexibility; we recommend to use these.
129
129
  module CodeRay
130
130
 
131
- Version = '0.4.4'
131
+ Version = '0.4.6'
132
132
 
133
133
  require 'coderay/tokens'
134
134
  require 'coderay/scanner'
@@ -201,6 +201,16 @@ module CodeRay
201
201
  encoder(format, options).encode code, lang, options
202
202
  end
203
203
 
204
+ # Highlight a string into a HTML <div>.
205
+ #
206
+ # CSS styles use classes, so you have to include a stylesheet
207
+ # in your output.
208
+ #
209
+ # See encode.
210
+ def highlight code, lang, options = { :css => :class }, format = :div
211
+ encode code, lang, format, options
212
+ end
213
+
204
214
  # Encode pre-scanned Tokens.
205
215
  # Use this together with CodeRay.scan:
206
216
  #
@@ -223,10 +233,20 @@ module CodeRay
223
233
  # require 'coderay'
224
234
  # page = CodeRay.encode_file 'some_c_code.c', :html
225
235
  def encode_file filename, format, options = {}
226
- tokens = scan_file filename, auto, get_scanner_options(options)
236
+ tokens = scan_file filename, :auto, get_scanner_options(options)
227
237
  encode_tokens tokens, format, options
228
238
  end
229
239
 
240
+ # Highlight a file into a HTML <div>.
241
+ #
242
+ # CSS styles use classes, so you have to include a stylesheet
243
+ # in your output.
244
+ #
245
+ # See encode.
246
+ def highlight_file filename, options = { :css => :class }, format = :div
247
+ encode_file filename, format, options
248
+ end
249
+
230
250
  # Finds the Encoder class for +format+ and creates an instance, passing
231
251
  # +options+ to it.
232
252
  #
@@ -109,6 +109,11 @@ module CodeRay
109
109
  method(:token).to_proc
110
110
  end
111
111
 
112
+ # Return the default file extension for outputs of this encoder.
113
+ def file_extension
114
+ self.class::FILE_EXTENSION
115
+ end
116
+
112
117
  protected
113
118
 
114
119
  # Called with merged options before encoding starts.
@@ -122,11 +127,28 @@ module CodeRay
122
127
  # Called with +text+ and +kind+ of the currently scanned token.
123
128
  # For simple scanners, it's enougth to implement this method.
124
129
  #
125
- # Raises a NotImplementedError exception if it is not overwritten
126
- # in subclass.
130
+ # By default, it calls text_token or block_token, depending on
131
+ # whether +text+ is a String.
127
132
  def token text, kind
128
- raise NotImplementedError,
129
- "#{self.class}#token not implemented."
133
+ if text.is_a? String
134
+ text_token text, kind
135
+ else
136
+ block_token text, kind
137
+ end
138
+ end
139
+
140
+ def text_token text, kind
141
+ end
142
+
143
+ def block_token action, kind
144
+ case action
145
+ when :open
146
+ open_token kind
147
+ when :close
148
+ close_token kind
149
+ else
150
+ raise 'unknown block action: %p' % action
151
+ end
130
152
  end
131
153
 
132
154
  # Called with merged options after encoding starts.
@@ -0,0 +1,38 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # = Debug Encoder
5
+ class Debug < Encoder
6
+
7
+ include Streamable
8
+ register_for :debug
9
+
10
+ FILE_EXTENSION = 'raydebug'
11
+
12
+ protected
13
+ def text_token text, kind
14
+ @out <<
15
+ if kind == :space
16
+ text
17
+ else
18
+ text = text.gsub(/[)\\]/, '\\\\\0')
19
+ "#{kind}(#{text})"
20
+ end
21
+ end
22
+
23
+ def block_token action, kind
24
+ @out << super
25
+ end
26
+
27
+ def open_token kind
28
+ "#{kind}<"
29
+ end
30
+
31
+ def close_token kind
32
+ ">"
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -102,6 +102,7 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top; }
102
102
  .hx { color:#058; font-weight:bold; }
103
103
  .i { color:#00D; font-weight:bold; }
104
104
  .ic { color:#B44; font-weight:bold; }
105
+ .il { }
105
106
  .in { color:#B2B; font-weight:bold; }
106
107
  .iv { color:#33B; }
107
108
  .la { color:#970; font-weight:bold; }
@@ -28,6 +28,7 @@ module CodeRay module Encoders
28
28
  :global_variable => 'gv',
29
29
  :hex => 'hx',
30
30
  :include => 'ic',
31
+ :inline => 'il',
31
32
  :instance_variable => 'iv',
32
33
  :integer => 'i',
33
34
  :interpreted => 'in',
@@ -13,6 +13,8 @@ module CodeRay
13
13
  # TODO: more doc.
14
14
  module Output
15
15
 
16
+ attr_accessor :wrapped_in
17
+
16
18
  class << self
17
19
 
18
20
  # This makes Output look like a class.
@@ -33,8 +35,18 @@ module CodeRay
33
35
  warn "The Output module is intended to extend instances of String, not #{o.class}." unless o.respond_to? :to_str
34
36
  end
35
37
 
38
+ def stylesheet in_tag = false
39
+ ss = CSS::DEFAULT_STYLESHEET
40
+ ss = <<-CSS if in_tag
41
+ <style type="text/css">
42
+ #{ss}
43
+ </style>
44
+ CSS
45
+ ss
46
+ end
47
+
36
48
  def page_template_for_css css = :default
37
- css = CSS::DEFAULT_STYLESHEET if css == :default
49
+ css = stylesheet if css == :default
38
50
  PAGE.apply 'CSS', css
39
51
  end
40
52
 
@@ -53,11 +65,6 @@ module CodeRay
53
65
 
54
66
  wrapper :div, :span, :page
55
67
 
56
- def wrapped_in
57
- @wrapped_in ||= nil
58
- end
59
- attr_writer :wrapped_in
60
-
61
68
  def wrapped_in? element
62
69
  wrapped_in == element
63
70
  end
@@ -97,19 +104,28 @@ module CodeRay
97
104
  clone.wrap!(*args)
98
105
  end
99
106
 
107
+ NUMERIZABLE_WRAPPINGS = {
108
+ :table => [:div, :page],
109
+ :inline => :all,
110
+ nil => :all
111
+ }
112
+
100
113
  def numerize! mode = :table, options = {}
101
114
  return self unless mode
102
115
 
103
- start = options.fetch :line_number_start, DEFAULT_OPTIONS[:line_number_start]
116
+ options = DEFAULT_OPTIONS.merge options
117
+
118
+ start = options[:line_number_start]
104
119
  unless start.is_a? Integer
105
120
  raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
106
121
  end
107
122
 
108
- unless NUMERIZABLE_WRAPPINGS.include? options[:wrap]
123
+ allowed_wrappings = NUMERIZABLE_WRAPPINGS[mode]
124
+ unless allowed_wrappings == :all or allowed_wrappings.include? options[:wrap]
109
125
  raise ArgumentError, "Can't numerize, :wrap must be in %p, but is %p" % [NUMERIZABLE_WRAPPINGS, options[:wrap]]
110
126
  end
111
127
 
112
- bold_every = options.fetch :bold_every, DEFAULT_OPTIONS[:bold_every]
128
+ bold_every = options[:bold_every]
113
129
  bolding =
114
130
  if bold_every == :no_bolding or bold_every == 0
115
131
  proc { |line| line.to_s }
@@ -137,7 +153,7 @@ module CodeRay
137
153
  line += 1
138
154
  "<span class=\"no\">#{ line_number.rjust(max_width) }</span> "
139
155
  end
140
- wrap! :div
156
+ #wrap! :div
141
157
 
142
158
  when :table
143
159
  # This is really ugly.
@@ -164,6 +180,10 @@ module CodeRay
164
180
  clone.numerize!(*args)
165
181
  end
166
182
 
183
+ def stylesheet in_tag = false
184
+ Output.stylesheet in_tag
185
+ end
186
+
167
187
  class Template < String
168
188
 
169
189
  def self.wrap! str, template, target
@@ -207,9 +227,10 @@ module CodeRay
207
227
  DIV_TABLE = <<-`DIV_TABLE`
208
228
  <table class="CodeRay"> <tr>
209
229
  <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><%LINE_NUMBERS%></pre></td>
210
- <td class="code"><pre title="double click to expand" ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><%CONTENT%></pre></td>
230
+ <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><%CONTENT%></pre></td>
211
231
  </tr> </table>
212
232
  DIV_TABLE
233
+ # title="double click to expand"
213
234
 
214
235
  PAGE = <<-`PAGE`
215
236
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
@@ -1,6 +1,63 @@
1
1
  module CodeRay
2
2
  module Encoders
3
3
 
4
+ # = HTML Encoder
5
+ #
6
+ # This is CodeRay's most important highlighter:
7
+ # It provides save, fast XHTML generation and CSS support.
8
+ #
9
+ # == Usage
10
+ #
11
+ # require 'coderay'
12
+ # puts CodeRay.scan('Some /code/', :ruby).html #-> a HTML page
13
+ # puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span) #-> <span class="CodeRay"><span class="co">Some</span> /code/</span>
14
+ # puts CodeRay.scan('Some /code/', :ruby).span #-> the same
15
+ #
16
+ # puts CodeRay.scan('Some code', :ruby).html(
17
+ # :wrap => nil,
18
+ # :line_numbers => :inline,
19
+ # :css => :style
20
+ # )
21
+ # #-> <span class="no">1</span> <span style="color:#036; font-weight:bold;">Some</span> code
22
+ #
23
+ # == Options
24
+ #
25
+ # === :tab_width
26
+ # Convert \t characters to +n+ spaces (a number.)
27
+ # Default: 8
28
+ #
29
+ # === :css
30
+ # How to include the styles; can be :class or :style.
31
+ #
32
+ # Default: :class
33
+ #
34
+ # === :wrap
35
+ # Wrap in :page, :div, :span or nil.
36
+ #
37
+ # You can also use Encoders::Div and Encoders::Span.
38
+ #
39
+ # Default: :page
40
+ #
41
+ # === :line_numbers
42
+ # Include line numbers in :table, :inline or nil (no line numbers)
43
+ #
44
+ # Default: nil
45
+ #
46
+ # === :line_number_start
47
+ # Where to start with line number counting.
48
+ #
49
+ # Default: 1
50
+ #
51
+ # === :bold_every
52
+ # Make every +n+-th number appear bold.
53
+ #
54
+ # Default: 10
55
+ #
56
+ # === :hint
57
+ # Include some information into the output using the title attribute.
58
+ # Can be :info (show token type on mouse-over) or :debug.
59
+ #
60
+ # Default: false
4
61
  class HTML < Encoder
5
62
 
6
63
  include Streamable
@@ -19,8 +76,9 @@ module Encoders
19
76
  :line_numbers => nil,
20
77
  :line_number_start => 1,
21
78
  :bold_every => 10,
79
+
80
+ :hint => false,
22
81
  }
23
- NUMERIZABLE_WRAPPINGS = [:div, :page]
24
82
 
25
83
  require 'coderay/encoders/helpers/html_helper'
26
84
  require 'coderay/encoders/helpers/html_output'
@@ -47,12 +105,9 @@ module Encoders
47
105
  ansi_chars = Array(0x7f..0xff)
48
106
  ansi_chars.each { |i| HTML_ESCAPE[i.chr] = '&#%d;' % i }
49
107
  # \x9 (\t) and \xA (\n) not included
50
- HTML_ESCAPE_PATTERN = /[&"><\0-\x8\xB-\x1f\x7f-\xff]/
108
+ HTML_ESCAPE_PATTERN = /[\t&"><\xB-\x1f\x7f-\xff\0-\x8]/
51
109
 
52
110
  def setup options
53
- if options[:line_numbers] and not NUMERIZABLE_WRAPPINGS.include? options[:wrap]
54
- warn ':line_numbers wanted, but :wrap is %p' % options[:wrap]
55
- end
56
111
  super
57
112
  return if options == @last_options
58
113
  @last_options = options
@@ -63,6 +118,11 @@ module Encoders
63
118
  @opened = [nil]
64
119
  @css = CSS.new
65
120
 
121
+ hint = options[:hint]
122
+ if hint and not [:debug, :info].include? hint
123
+ raise ArgumentError, "Unknown value %p for :hint; expected :info, :debug, false or nil." % hint
124
+ end
125
+
66
126
  case options[:css]
67
127
 
68
128
  when :class
@@ -73,15 +133,20 @@ module Encoders
73
133
  type = k
74
134
  end
75
135
  c = ClassOfKind[type]
76
- if c == :NO_HIGHLIGHT
136
+ if c == :NO_HIGHLIGHT and not hint
77
137
  h[k] = false
78
138
  else
79
- if options[:debug]
80
- debug_info = ' title="%p"' % [ k ]
139
+ title = if hint
140
+ if hint == :debug
141
+ ' title="%p"' % [ k ]
142
+ elsif hint == :info
143
+ path = (k[1..-1] << k.first).map { |kind| kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize } }
144
+ " title=\"#{path.join('/')}\""
145
+ end
81
146
  else
82
- debug_info = ''
147
+ ''
83
148
  end
84
- h[k] = '<span%s class="%s">' % [debug_info, c]
149
+ h[k] = '<span%s class="%s">' % [title, c]
85
150
  end
86
151
  end
87
152
 
@@ -92,19 +157,24 @@ module Encoders
92
157
  else
93
158
  styles = [k]
94
159
  end
160
+ type = styles.first
95
161
  styles.map! { |c| ClassOfKind[c] }
96
- if styles.first == :NO_HIGHLIGHT
162
+ if styles.first == :NO_HIGHLIGHT and not hint
97
163
  h[k] = false
98
164
  else
99
- if options[:debug]
100
- debug_info = ' title="%s"' % [ styles.inspect.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } ]
165
+ title = if hint
166
+ if hint == :debug
167
+ ' title="%p"' % [ styles ]
168
+ elsif hint == :info and styles.size == 1
169
+ " title=\"#{type.to_s.gsub(/_/, " ").capitalize}\""
170
+ end
101
171
  else
102
- debug_info = ''
172
+ ''
103
173
  end
104
174
  style = @css[*styles]
105
175
  h[k] =
106
176
  if style
107
- '<span%s style="%s">' % [debug_info, style]
177
+ '<span%s style="%s">' % [title, style]
108
178
  else
109
179
  false
110
180
  end
@@ -112,7 +182,7 @@ module Encoders
112
182
  end
113
183
 
114
184
  else
115
- raise "Unknown value %p for :css." % options[:css]
185
+ raise ArgumentError, "Unknown value %p for :css." % options[:css]
116
186
 
117
187
  end
118
188
  end
@@ -1,12 +1,20 @@
1
1
  module CodeRay
2
2
  module Encoders
3
3
 
4
+ # = Null Encoder
5
+ #
6
+ # Does nothing and returns an empty string.
4
7
  class Null < Encoder
5
8
 
6
9
  include Streamable
7
10
  register_for :null
8
11
 
9
- protected
12
+ # Defined for faster processing
13
+ def to_proc
14
+ proc {}
15
+ end
16
+
17
+ protected
10
18
 
11
19
  def token(*)
12
20
  # do nothing
@@ -16,5 +24,3 @@ module CodeRay
16
24
 
17
25
  end
18
26
  end
19
-
20
-
@@ -22,17 +22,22 @@ module CodeRay module Encoders
22
22
  super
23
23
  end
24
24
 
25
- def token text, type
25
+ def text_token text, kind
26
+ @real_token_count += 1 unless kind == :space
27
+ @type_stats[kind].count += 1
28
+ @type_stats[kind].size += text.size
29
+ @type_stats['TOTAL'].size += text.size
30
+ end
31
+
32
+ # TODO Hierarchy handling
33
+ def block_token action, kind
34
+ #@content_type = kind
35
+ @type_stats['open/close'].count += 1
36
+ end
37
+
38
+ def token text, kind
39
+ super
26
40
  @type_stats['TOTAL'].count += 1
27
- if text.is_a? String
28
- @real_token_count += 1 unless type == :space
29
- @type_stats[type].count += 1
30
- @type_stats[type].size += text.size
31
- @type_stats['TOTAL'].size += text.size
32
- else
33
- @content_type = type
34
- @type_stats['open/close'].count += 1
35
- end
36
41
  end
37
42
 
38
43
  STATS = <<-STATS
@@ -0,0 +1,70 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # = Debug Encoder
5
+ class XML < Encoder
6
+
7
+ include Streamable
8
+ register_for :xml
9
+
10
+ FILE_EXTENSION = 'xml'
11
+
12
+ require 'rexml/document'
13
+
14
+ DEFAULT_OPTIONS = {
15
+ :tab_width => 8,
16
+ :pretty => -1,
17
+ :transitive => false,
18
+ }
19
+
20
+ protected
21
+
22
+ def setup options
23
+ @out = ''
24
+ @doc = REXML::Document.new
25
+ @doc << REXML::XMLDecl.new
26
+ @tab_width = options[:tab_width]
27
+ @root = @node = @doc.add_element('coderay-tokens')
28
+ end
29
+
30
+ def finish options
31
+ @doc.write @out, options[:pretty], options[:transitive], true
32
+ @out
33
+ end
34
+
35
+ def text_token text, kind
36
+ if kind == :space
37
+ token = @node
38
+ else
39
+ token = @node.add_element kind.to_s
40
+ end
41
+ text.scan(/(\x20+)|(\t+)|(\n)|[^\x20\t\n]+/) do |space, tab, nl|
42
+ case
43
+ when space
44
+ token << REXML::Text.new(space, true)
45
+ when tab
46
+ token << REXML::Text.new(tab, true)
47
+ when nl
48
+ token << REXML::Text.new(nl, true)
49
+ else
50
+ token << REXML::Text.new($&)
51
+ end
52
+ end
53
+ end
54
+
55
+ def open_token kind
56
+ @node = @node.add_element kind.to_s
57
+ end
58
+
59
+ def close_token kind
60
+ if @node == @root
61
+ raise 'no token to close!'
62
+ end
63
+ @node = @node.parent
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ end
70
+
@@ -1,6 +1,6 @@
1
1
  # = PluginHost
2
2
  #
3
- # $Id: plugin.rb 47 2005-10-01 06:04:52Z murphy $
3
+ # $Id: plugin.rb 59 2005-10-29 04:55:15Z murphy $
4
4
  #
5
5
  # A simple subclass plugin system.
6
6
  #
@@ -90,10 +90,10 @@ module PluginHost
90
90
  id = validate_id(plugin_id)
91
91
  path = path_to id
92
92
  begin
93
- $stderr.puts 'Loading plugin: ' + path if $DEBUG
93
+ #$stderr.puts 'Loading plugin: ' + path if $DEBUG
94
94
  require path
95
- rescue LoadError
96
- raise PluginNotFound, "Plugin #{id.inspect} not found."
95
+ rescue LoadError => boom
96
+ raise PluginNotFound, 'Could not load plugin %p: %s' % [id, boom]
97
97
  else
98
98
  # Plugin should have registered by now
99
99
  unless h.has_key? id
@@ -151,7 +151,7 @@ module PluginHost
151
151
  end
152
152
 
153
153
  # Alias for +[]+.
154
- alias load_plugin []
154
+ alias load []
155
155
 
156
156
  # Returns the Plugin for +id+.
157
157
  # Use it like Hash#fetch.
@@ -239,7 +239,7 @@ def require_plugin path
239
239
  host = PluginHost.host_by_id(host_id)
240
240
  raise PluginHost::HostNotFound,
241
241
  "No host for #{host_id.inspect} found." unless host
242
- host.load_plugin plugin_id
242
+ host.load plugin_id
243
243
  end
244
244
 
245
245
 
@@ -12,9 +12,11 @@ module CodeRay module Scanners
12
12
  ]
13
13
 
14
14
  DEF_KEYWORDS = %w[ def ]
15
+ UNDEF_KEYWORDS = %w[ undef ]
15
16
  MODULE_KEYWORDS = %w[class module]
16
17
  DEF_NEW_STATE = WordList.new(:initial).
17
18
  add(DEF_KEYWORDS, :def_expected).
19
+ add(UNDEF_KEYWORDS, :undef_expected).
18
20
  add(MODULE_KEYWORDS, :module_expected)
19
21
 
20
22
  IDENTS_ALLOWING_REGEXP = %w[
@@ -32,13 +34,11 @@ module CodeRay module Scanners
32
34
  add(RESERVED_WORDS, :reserved).
33
35
  add(PREDEFINED_CONSTANTS, :pre_constant)
34
36
 
35
- # IDENT = /[a-zA-Z_][a-zA-Z_0-9]*/
36
37
  IDENT = /[a-z_][\w_]*/i
37
38
 
38
39
  METHOD_NAME = / #{IDENT} [?!]? /ox
39
- METHOD_NAME_EX = /
40
- #{IDENT}[?!=]? # common methods: split, foo=, empty?, gsub!
41
- | \*\*? # multiplication and power
40
+ METHOD_NAME_OPERATOR = /
41
+ \*\*? # multiplication and power
42
42
  | [-+]@? # plus, minus
43
43
  | [\/%&|^`~] # division, modulo or format strings, &and, |or, ^xor, `system`, tilde
44
44
  | \[\]=? # array getter and setter
@@ -46,10 +46,11 @@ module CodeRay module Scanners
46
46
  | <=?>? | >=? # comparison, rocket operator
47
47
  | ===? # simple equality and case equality
48
48
  /ox
49
+ METHOD_NAME_EX = / #{IDENT} [?!=]? | #{METHOD_NAME_OPERATOR} /ox
49
50
  INSTANCE_VARIABLE = / @ #{IDENT} /ox
50
51
  CLASS_VARIABLE = / @@ #{IDENT} /ox
51
52
  OBJECT_VARIABLE = / @@? #{IDENT} /ox
52
- GLOBAL_VARIABLE = / \$ (?: #{IDENT} | [1-9] | 0[a-zA-Z_0-9]* | [~&+`'=\/,;_.<>!@$?*":\\] | -[a-zA-Z_0-9] ) /ox
53
+ GLOBAL_VARIABLE = / \$ (?: #{IDENT} | [1-9]\d* | 0\w* | [~&+`'=\/,;_.<>!@$?*":\\] | -[a-zA-Z_0-9] ) /ox
53
54
  PREFIX_VARIABLE = / #{GLOBAL_VARIABLE} |#{OBJECT_VARIABLE} /ox
54
55
  VARIABLE = / @?@? #{IDENT} | #{GLOBAL_VARIABLE} /ox
55
56
 
@@ -60,19 +61,17 @@ module CodeRay module Scanners
60
61
  QUOTE_TO_TYPE.default = :string
61
62
 
62
63
  REGEXP_MODIFIERS = /[mixounse]*/
63
- REGEXP_SYMBOLS = /
64
- [|?*+?(){}\[\].^$]
65
- /x
64
+ REGEXP_SYMBOLS = /[|?*+?(){}\[\].^$]/
66
65
 
67
- DECIMAL = /\d+(?:_\d+)*/ # doesn't recognize 09 as octal error
66
+ DECIMAL = /\d+(?:_\d+)*/
68
67
  OCTAL = /0_?[0-7]+(?:_[0-7]+)*/
69
68
  HEXADECIMAL = /0x[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*/
70
69
  BINARY = /0b[01]+(?:_[01]+)*/
71
70
 
72
71
  EXPONENT = / [eE] [+-]? #{DECIMAL} /ox
73
- FLOAT_OR_INT = / #{DECIMAL} (?: #{EXPONENT} | \. #{DECIMAL} #{EXPONENT}? )? /ox
74
- FLOAT = / #{DECIMAL} (?: #{EXPONENT} | \. #{DECIMAL} #{EXPONENT}? ) /ox
75
- NUMERIC = / #{OCTAL} | #{HEXADECIMAL} | #{BINARY} | #{FLOAT_OR_INT} /ox
72
+ FLOAT_SUFFIX = / #{EXPONENT} | \. #{DECIMAL} #{EXPONENT}? /ox
73
+ FLOAT_OR_INT = / #{DECIMAL} (?: #{FLOAT_SUFFIX} () )? /ox
74
+ NUMERIC = / (?=0) (?: #{OCTAL} | #{HEXADECIMAL} | #{BINARY} ) | #{FLOAT_OR_INT} /ox
76
75
 
77
76
  SYMBOL = /
78
77
  :
@@ -103,7 +102,7 @@ module CodeRay module Scanners
103
102
  )
104
103
  /mx
105
104
 
106
- # NOTE: This is not completel correct, but
105
+ # NOTE: This is not completely correct, but
107
106
  # nobody needs heredoc delimiters ending with \n.
108
107
  HEREDOC_OPEN = /
109
108
  << (-)? # $1 = float
@@ -115,7 +114,7 @@ module CodeRay module Scanners
115
114
  )
116
115
  /mx
117
116
 
118
- RDOC = /
117
+ RUBYDOC = /
119
118
  =begin (?!\S)
120
119
  .*?
121
120
  (?: \Z | ^=end (?!\S) [^\n]* )
@@ -127,6 +126,8 @@ module CodeRay module Scanners
127
126
  (?: \Z | (?=^\#CODE) )
128
127
  /mx
129
128
 
129
+ RUBYDOC_OR_DATA = / #{RUBYDOC} | #{DATA} /xo
130
+
130
131
  RDOC_DATA_START = / ^=begin (?!\S) | ^__END__$ /x
131
132
 
132
133
  FANCY_START = / % ( [qQwWxsr] | (?![\w\s=]) ) (.) /mox
@@ -144,7 +145,7 @@ module CodeRay module Scanners
144
145
  FancyStringType['W'] = FancyStringType[''] = FancyStringType['Q']
145
146
 
146
147
  class StringState < Struct.new :type, :interpreted, :delim, :heredoc,
147
- :paren, :paren_depth, :pattern
148
+ :paren, :paren_depth, :pattern, :next_state
148
149
 
149
150
  CLOSING_PAREN = Hash[ *%w[
150
151
  ( )
@@ -186,7 +187,7 @@ module CodeRay module Scanners
186
187
  delim_pattern = / \n #{ '(?>[\ \t]*)' if indented } #{ Regexp.new delim_pattern } $ /x
187
188
  h[k] =
188
189
  if interpreted
189
- / (?= #{delim_pattern}() | \\ | \# [{$@] ) /mx
190
+ / (?= #{delim_pattern}() | \\ | \# [{$@] ) /mx # $1 set == end of heredoc
190
191
  else
191
192
  / (?= #{delim_pattern}() | \\ ) /mx
192
193
  end
@@ -203,7 +204,7 @@ module CodeRay module Scanners
203
204
  else
204
205
  pattern = STRING_PATTERN[ [delim, interpreted] ]
205
206
  end
206
- super kind, interpreted, delim, heredoc, paren, paren_depth, pattern
207
+ super kind, interpreted, delim, heredoc, paren, paren_depth, pattern, :initial
207
208
  end
208
209
  end unless defined? StringState
209
210
 
@@ -43,7 +43,6 @@ module CodeRay module Scanners
43
43
 
44
44
  if state.instance_of? StringState
45
45
  # {{{
46
-
47
46
  match = scan_until(state.pattern) || scan_until(/\z/)
48
47
  tokens << [match, :content] unless match.empty?
49
48
  break if eos?
@@ -52,7 +51,7 @@ module CodeRay module Scanners
52
51
  match = getch + scan_until(/$/)
53
52
  tokens << [match, :delimiter]
54
53
  tokens << [:close, state.type]
55
- state = :initial
54
+ state = state.next_state
56
55
  next
57
56
  end
58
57
 
@@ -103,7 +102,7 @@ module CodeRay module Scanners
103
102
  end
104
103
  tokens << [:close, state.type]
105
104
  fancy_allowed = regexp_allowed = false
106
- state = :initial
105
+ state = state.next_state
107
106
 
108
107
  when '\\'
109
108
  if state.interpreted
@@ -128,13 +127,14 @@ module CodeRay module Scanners
128
127
  fancy_allowed = regexp_allowed = true
129
128
  state = :initial
130
129
  depth = 1
131
- tokens << [match + getch, :escape]
130
+ tokens << [:open, :inline]
131
+ tokens << [match + getch, :delimiter]
132
132
  when ?$, ?@
133
133
  tokens << [match, :escape]
134
134
  last_state = state # scan one token as normal code, then return here
135
135
  state = :initial
136
136
  else
137
- raise "else-case # reached; #%p not handled" % peek(1), tokens
137
+ raise 'else-case # reached; #%p not handled' % peek(1), tokens
138
138
  end
139
139
 
140
140
  when state.paren
@@ -145,7 +145,7 @@ module CodeRay module Scanners
145
145
  tokens << [match, :function]
146
146
 
147
147
  else
148
- raise "else-case \" reached; %p not handled, state = %p" % [match, state], tokens
148
+ raise 'else-case " reached; %p not handled, state = %p' % [match, state], tokens
149
149
 
150
150
  end
151
151
  next
@@ -153,7 +153,7 @@ module CodeRay module Scanners
153
153
  else
154
154
  # {{{
155
155
  if match = scan(/ [ \t\f]+ | \\? \n | \# .* /x) or
156
- ( bol? and match = scan(/ #{DATA} | #{RDOC} /ox) )
156
+ ( bol? and match = scan(/#{RUBYDOC_OR_DATA}/o) )
157
157
  fancy_allowed = true
158
158
  case m = match[0]
159
159
  when ?\s, ?\t, ?\f
@@ -161,7 +161,10 @@ module CodeRay module Scanners
161
161
  type = :space
162
162
  when ?\n, ?\\
163
163
  type = :space
164
- regexp_allowed = m == ?\n
164
+ if m == ?\n
165
+ regexp_allowed = true
166
+ state = :initial if state == :undef_comma_expected
167
+ end
165
168
  if heredocs
166
169
  unscan # heredoc scanning needs \n at start
167
170
  state = heredocs.shift
@@ -175,14 +178,32 @@ module CodeRay module Scanners
175
178
  type = :comment
176
179
  regexp_allowed = true
177
180
  else
178
- raise "else-case _ reached, because case %p was not handled" % [matched[0].chr], tokens
181
+ raise 'else-case _ reached, because case %p was not handled' % [matched[0].chr], tokens
179
182
  end
180
183
  tokens << [match, type]
181
184
  next
182
185
 
183
186
  elsif state == :initial
184
- if match = scan(/ \.\.?\.? | [-+*=>;,|&!\(\)\[\]~^]+ | [\{\}] | :: /x)
185
- if match !~ / [.\)\]\}] \z/x or match =~ /\.\.\.?/
187
+
188
+ # IDENTS #
189
+ if match = scan(/#{METHOD_NAME}/o)
190
+ if last_token_dot
191
+ type = if match[/^[A-Z]/] then :constant else :ident end
192
+ else
193
+ type = IDENT_KIND[match]
194
+ if type == :ident and match[/^[A-Z]/] and not match[/[!?]$/]
195
+ type = :constant
196
+ elsif type == :reserved
197
+ state = DEF_NEW_STATE[match]
198
+ end
199
+ end
200
+ ## experimental!
201
+ fancy_allowed = regexp_allowed = :set if REGEXP_ALLOWED[match] or check(/\s+(?:%\S|\/\S)/)
202
+
203
+ # OPERATORS #
204
+ elsif (not last_token_dot and match = scan(/ ==?=? | \.\.?\.? | [\(\)\[\]\{\}] | :: | , /x)) or
205
+ (last_token_dot and match = scan(/#{METHOD_NAME_OPERATOR}/o))
206
+ if match !~ / [.\)\]\}] /x or match =~ /\.\.\.?/
186
207
  regexp_allowed = fancy_allowed = :set
187
208
  end
188
209
  last_token_dot = :set if match == '.' or match == '::'
@@ -195,33 +216,22 @@ module CodeRay module Scanners
195
216
  depth -= 1
196
217
  if depth == 0
197
218
  state, depth, heredocs = states.pop
198
- type = :escape
219
+ tokens << [match, :delimiter]
220
+ type = :inline
221
+ match = :close
199
222
  end
200
223
  end
201
224
  end
202
225
 
203
- elsif match = scan(/#{METHOD_NAME}/o)
204
- if last_token_dot
205
- type = if match[/^[A-Z]/] then :constant else :ident end
206
- else
207
- type = IDENT_KIND[match]
208
- if type == :ident and match[/^[A-Z]/]
209
- type = :constant
210
- elsif type == :reserved
211
- state = DEF_NEW_STATE[match]
212
- end
213
- end
214
- fancy_allowed = regexp_allowed = REGEXP_ALLOWED[match]
215
-
216
226
  elsif match = scan(/ ['"] /mx)
217
227
  tokens << [:open, :string]
218
228
  type = :delimiter
219
- state = StringState.new :string, match != '\'', match # important for streaming
229
+ state = StringState.new :string, match == '"', match # important for streaming
220
230
 
221
231
  elsif match = scan(/#{INSTANCE_VARIABLE}/o)
222
232
  type = :instance_variable
223
-
224
- elsif regexp_allowed and match = scan(/ \/ /mx)
233
+
234
+ elsif regexp_allowed and match = scan(/\//)
225
235
  tokens << [:open, :regexp]
226
236
  type = :delimiter
227
237
  interpreted = true
@@ -232,15 +242,23 @@ module CodeRay module Scanners
232
242
  end
233
243
 
234
244
  elsif match = scan(/#{NUMERIC}/o)
235
- type = if match[/#{FLOAT}/o] then :float else :integer end
245
+ type = if self[1] then :float else :integer end
236
246
 
237
- elsif fancy_allowed and match = scan(/#{SYMBOL}/o)
238
- case match[1]
247
+ elsif match = scan(/#{SYMBOL}/o)
248
+ case delim = match[1]
239
249
  when ?', ?"
240
250
  tokens << [:open, :symbol]
241
- state = StringState.new :symbol, match[1] == ?", match[1,1]
251
+ tokens << [':', :symbol]
252
+ match = delim.chr
253
+ type = :delimiter
254
+ state = StringState.new :symbol, delim == ?", match
255
+ else
256
+ type = :symbol
242
257
  end
243
- type = :symbol
258
+
259
+ elsif match = scan(/ [-+!~^]=? | [*|&]{1,2}=? | >>? /x)
260
+ regexp_allowed = fancy_allowed = :set
261
+ type = :operator
244
262
 
245
263
  elsif fancy_allowed and match = scan(/#{HEREDOC_OPEN}/o)
246
264
  indented = self[1] == '-'
@@ -265,7 +283,7 @@ module CodeRay module Scanners
265
283
  elsif fancy_allowed and match = scan(/#{CHARACTER}/o)
266
284
  type = :integer
267
285
 
268
- elsif match = scan(/ [\/%<?:] /x)
286
+ elsif match = scan(/ [\/%]=? | <(?:<|=>?)? | [?:;] /x)
269
287
  regexp_allowed = fancy_allowed = :set
270
288
  type = :operator
271
289
 
@@ -290,25 +308,55 @@ module CodeRay module Scanners
290
308
  end
291
309
 
292
310
  elsif state == :def_expected
293
- if match = scan(/ (?: #{VARIABLE} (?: ::#{IDENT} )* \. )? #{METHOD_NAME_EX} /ox)
311
+ state = :initial
312
+ if match = scan(/(?>#{METHOD_NAME_EX})(?!\.|::)/o)
294
313
  type = :method
295
314
  else
296
- match = getch
315
+ next
316
+ end
317
+
318
+ elsif state == :undef_expected
319
+ state = :undef_comma_expected
320
+ if match = scan(/#{METHOD_NAME_EX}/o)
321
+ type = :method
322
+ elsif match = scan(/#{SYMBOL}/o)
323
+ case delim = match[1]
324
+ when ?', ?"
325
+ tokens << [:open, :symbol]
326
+ tokens << [':', :symbol]
327
+ match = delim.chr
328
+ type = :delimiter
329
+ state = StringState.new :symbol, delim == ?", match
330
+ state.next_state = :undef_comma_expected
331
+ else
332
+ type = :symbol
333
+ end
334
+ else
335
+ state = :initial
336
+ next
337
+ end
338
+
339
+ elsif state == :undef_comma_expected
340
+ if match = scan(/,/)
341
+ type = :operator
342
+ state = :undef_expected
343
+ else
344
+ state = :initial
345
+ next
297
346
  end
298
- state = :initial
299
347
 
300
348
  elsif state == :module_expected
301
349
  if match = scan(/<</)
302
350
  type = :operator
303
351
  else
352
+ state = :initial
304
353
  if match = scan(/ (?:#{IDENT}::)* #{IDENT} /ox)
305
354
  type = :class
306
355
  else
307
- match = getch
356
+ next
308
357
  end
309
358
  end
310
- state = :initial
311
-
359
+
312
360
  end
313
361
 
314
362
  regexp_allowed = regexp_allowed == :set
@@ -316,7 +364,7 @@ module CodeRay module Scanners
316
364
  last_token_dot = last_token_dot == :set
317
365
 
318
366
  if $DEBUG
319
- raise_inspect 'error token %p in line %d' % [tokens.last, line], tokens if not type or type == :error
367
+ raise_inspect 'error token %p in line %d' % [[match, type], line], tokens if not type or type == :error
320
368
  end
321
369
 
322
370
  tokens << [match, type]
@@ -94,15 +94,14 @@ module CodeRay
94
94
  false
95
95
  end
96
96
 
97
- alias :orig_each :each
98
97
  # Iterates over all tokens.
99
98
  #
100
99
  # If a filter is given, only tokens of that kind are yielded.
101
100
  def each kind_filter = nil, &block
102
101
  unless kind_filter
103
- orig_each(&block)
102
+ super(&block)
104
103
  else
105
- orig_each do |text, kind|
104
+ super do |text, kind|
106
105
  next unless kind == kind_filter
107
106
  yield text, kind
108
107
  end
@@ -115,7 +114,7 @@ module CodeRay
115
114
  # Example:
116
115
  # tokens.each_text_token { |text, kind| text.replace html_escape(text) }
117
116
  def each_text_token
118
- orig_each do |text, kind|
117
+ each do |text, kind|
119
118
  next unless text.respond_to? :to_str
120
119
  yield text, kind
121
120
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: coderay
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.4.3.48
7
- date: 2005-10-01
6
+ version: 0.4.5.73
7
+ date: 2005-10-29
8
8
  summary: CodeRay is a fast syntax highlighter engine for many languages.
9
9
  require_paths:
10
10
  - lib
@@ -31,47 +31,50 @@ authors:
31
31
  - murphy
32
32
  files:
33
33
  - "./lib/coderay.rb"
34
- - "./lib/coderay/encoder.rb"
35
- - "./lib/coderay/scanner.rb"
36
34
  - "./lib/coderay/tokens.rb"
37
- - "./lib/coderay/encoders/yaml.rb"
35
+ - "./lib/coderay/scanner.rb"
36
+ - "./lib/coderay/encoder.rb"
37
+ - "./lib/coderay/encoders/xml.rb"
38
+ - "./lib/coderay/encoders/span.rb"
38
39
  - "./lib/coderay/encoders/div.rb"
40
+ - "./lib/coderay/encoders/yaml.rb"
39
41
  - "./lib/coderay/encoders/tokens.rb"
42
+ - "./lib/coderay/encoders/text.rb"
40
43
  - "./lib/coderay/encoders/statistic.rb"
44
+ - "./lib/coderay/encoders/count.rb"
41
45
  - "./lib/coderay/encoders/null.rb"
42
- - "./lib/coderay/encoders/span.rb"
46
+ - "./lib/coderay/encoders/debug.rb"
43
47
  - "./lib/coderay/encoders/html.rb"
44
- - "./lib/coderay/encoders/text.rb"
45
- - "./lib/coderay/encoders/count.rb"
46
48
  - "./lib/coderay/encoders/helpers/html_helper.rb"
47
- - "./lib/coderay/encoders/helpers/html_output.rb"
48
49
  - "./lib/coderay/encoders/helpers/html_css.rb"
49
- - "./lib/coderay/helpers/plugin.rb"
50
+ - "./lib/coderay/encoders/helpers/html_output.rb"
50
51
  - "./lib/coderay/helpers/filetype.rb"
51
- - "./lib/coderay/helpers/gzip_simple.rb"
52
+ - "./lib/coderay/helpers/plugin.rb"
52
53
  - "./lib/coderay/helpers/scanner_helper.rb"
54
+ - "./lib/coderay/helpers/gzip_simple.rb"
53
55
  - "./lib/coderay/scanners/c.rb"
54
56
  - "./lib/coderay/scanners/delphi.rb"
55
57
  - "./lib/coderay/scanners/plaintext.rb"
56
58
  - "./lib/coderay/scanners/ruby.rb"
57
59
  - "./lib/coderay/scanners/helpers/ruby_helper.rb"
58
60
  - "./demo/demo_global_vars2.rb"
61
+ - "./demo/demo_stream2.rb"
62
+ - "./demo/demo_load_encoder.rb"
59
63
  - "./demo/demo_encoder.rb"
60
64
  - "./demo/demo_div.rb"
65
+ - "./demo/demo_html2.rb"
66
+ - "./demo/demo_css.rb"
61
67
  - "./demo/demo_count.rb"
62
68
  - "./demo/demo_simple.rb"
63
- - "./demo/demo_tokens.rb"
64
- - "./demo/demo_css.rb"
65
- - "./demo/demo_html.rb"
66
- - "./demo/demo_dump.rb"
67
- - "./demo/demo_stream2.rb"
68
- - "./demo/demo_load_encoder.rb"
69
- - "./demo/demo_html2.rb"
70
69
  - "./demo/demo_scanner.rb"
71
70
  - "./demo/demo_stream.rb"
72
71
  - "./demo/demo_global_vars.rb"
73
72
  - "./demo/demo_more.rb"
73
+ - "./demo/demo_tokens.rb"
74
+ - "./demo/demo_html.rb"
75
+ - "./demo/demo_dump.rb"
74
76
  - "./demo/demo_server.rb"
77
+ - "./demo/demo_highlight.rb"
75
78
  - "./README"
76
79
  - "./LICENSE"
77
80
  test_files: []