coderay 0.5.0.121 → 0.7.1.147

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/FOLDERS +53 -0
  2. data/README +21 -13
  3. data/bin/coderay +79 -0
  4. data/demo/demo_cache.rb +12 -0
  5. data/demo/demo_html_list.rb +12 -0
  6. data/lib/coderay.rb +11 -2
  7. data/lib/coderay/duo.rb +29 -0
  8. data/lib/coderay/encoder.rb +4 -4
  9. data/lib/coderay/encoders/_map.rb +6 -6
  10. data/lib/coderay/encoders/count.rb +3 -3
  11. data/lib/coderay/encoders/debug.rb +38 -30
  12. data/lib/coderay/encoders/div.rb +4 -2
  13. data/lib/coderay/encoders/html.rb +9 -19
  14. data/lib/coderay/encoders/html/classes.rb +5 -2
  15. data/lib/coderay/encoders/html/css.rb +5 -6
  16. data/lib/coderay/encoders/html/numerization.rb +28 -18
  17. data/lib/coderay/encoders/html/output.rb +4 -4
  18. data/lib/coderay/encoders/null.rb +16 -16
  19. data/lib/coderay/encoders/page.rb +21 -0
  20. data/lib/coderay/encoders/span.rb +6 -3
  21. data/lib/coderay/encoders/statistic.rb +4 -2
  22. data/lib/coderay/encoders/tokens.rb +35 -35
  23. data/lib/coderay/encoders/xml.rb +53 -52
  24. data/lib/coderay/encoders/yaml.rb +13 -10
  25. data/lib/coderay/helpers/filetype.rb +41 -5
  26. data/lib/coderay/helpers/gzip_simple.rb +1 -1
  27. data/lib/coderay/helpers/plugin.rb +33 -17
  28. data/lib/coderay/scanner.rb +60 -19
  29. data/lib/coderay/scanners/_map.rb +12 -8
  30. data/lib/coderay/scanners/c.rb +16 -8
  31. data/lib/coderay/scanners/delphi.rb +9 -3
  32. data/lib/coderay/scanners/html.rb +167 -0
  33. data/lib/coderay/scanners/nitro_html.rb +125 -0
  34. data/lib/coderay/scanners/plaintext.rb +4 -2
  35. data/lib/coderay/scanners/rhtml.rb +65 -0
  36. data/lib/coderay/scanners/ruby.rb +51 -39
  37. data/lib/coderay/scanners/ruby/patterns.rb +12 -9
  38. data/lib/coderay/scanners/xml.rb +18 -0
  39. data/lib/coderay/scanners/yaml.rb +85 -0
  40. data/lib/coderay/styles/_map.rb +7 -0
  41. data/lib/coderay/styles/cycnus.rb +105 -99
  42. data/lib/coderay/styles/murphy.rb +18 -18
  43. metadata +19 -6
@@ -1,19 +1,22 @@
1
1
  module CodeRay
2
- module Encoders
2
+ module Encoders
3
3
 
4
- class YAML < Encoder
4
+ # = YAML Encoder
5
+ #
6
+ # Slow.
7
+ class YAML < Encoder
5
8
 
6
- register_for :yaml
9
+ register_for :yaml
7
10
 
8
- FILE_EXTENSION = 'yaml'
9
-
10
- protected
11
- def compile tokens, options
12
- require 'yaml'
13
- @out = tokens.to_a.to_yaml
14
- end
11
+ FILE_EXTENSION = 'yaml'
15
12
 
13
+ protected
14
+ def compile tokens, options
15
+ require 'yaml'
16
+ @out = tokens.to_a.to_yaml
16
17
  end
17
18
 
18
19
  end
20
+
21
+ end
19
22
  end
@@ -6,16 +6,29 @@
6
6
  #
7
7
  # Version: 0.1 (2005.september.1)
8
8
  #
9
- # ==Documentation
10
- #
11
- # TODO
9
+ # == Documentation
12
10
  #
11
+ # # determine the type of the given
12
+ # lang = FileType[ARGV.first]
13
+ #
14
+ # # return :plaintext if the file type is unknown
15
+ # lang = FileType.fetch ARGV.first, :plaintext
16
+ #
17
+ # # try the shebang line, too
18
+ # lang = FileType.fetch ARGV.first, :plaintext, true
19
+ #
13
20
  module FileType
14
21
 
15
22
  UnknownFileType = Class.new Exception
16
23
 
17
24
  class << self
18
25
 
26
+ # Try to determine the file type of the file.
27
+ #
28
+ # +filename+ is a relative or absolute path to a file.
29
+ #
30
+ # The file itself is only accessed when +read_shebang+ is set to true.
31
+ # That means you can get filetypes from files that don't exist.
19
32
  def [] filename, read_shebang = false
20
33
  name = File.basename filename
21
34
  ext = File.extname name
@@ -43,6 +56,9 @@ module FileType
43
56
  end
44
57
 
45
58
  # This works like Hash#fetch.
59
+ #
60
+ # If the filetype cannot be found, the +default+ value
61
+ # is returned.
46
62
  def fetch filename, default = nil, read_shebang = false
47
63
  if default and block_given?
48
64
  warn 'block supersedes default value argument'
@@ -61,12 +77,17 @@ module FileType
61
77
  TypeFromExt = {
62
78
  'rb' => :ruby,
63
79
  'rbw' => :ruby,
64
- 'cpp' => :cpp,
80
+ 'rake' => :ruby,
81
+ 'cpp' => :c,
65
82
  'c' => :c,
66
83
  'h' => :c,
67
84
  'xml' => :xml,
68
85
  'htm' => :html,
69
86
  'html' => :html,
87
+ 'xhtml' => :xhtml,
88
+ 'rhtml' => :rhtml,
89
+ 'yaml' => :yaml,
90
+ 'yml' => :yaml,
70
91
  }
71
92
 
72
93
  TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
@@ -80,7 +101,7 @@ end
80
101
 
81
102
  if $0 == __FILE__
82
103
  $VERBOSE = true
83
- eval DATA.read, nil, $0, __LINE__+4
104
+ eval DATA.read, nil, $0, __LINE__+4
84
105
  end
85
106
 
86
107
  __END__
@@ -118,6 +139,7 @@ class TC_FileType < Test::Unit::TestCase
118
139
  assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
119
140
  assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
120
141
  assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
142
+ assert_equal :ruby, FileType['./lib/tasks\repository.rake']
121
143
  assert_not_equal :ruby, FileType['test_rb']
122
144
  assert_not_equal :ruby, FileType['Makefile']
123
145
  assert_not_equal :ruby, FileType['set.rb/set']
@@ -133,6 +155,20 @@ class TC_FileType < Test::Unit::TestCase
133
155
  assert_not_equal :c, FileType['~/projects/blabla/c']
134
156
  end
135
157
 
158
+ def test_html
159
+ assert_equal :html, FileType['test.htm']
160
+ assert_equal :xhtml, FileType['test.xhtml']
161
+ assert_equal :xhtml, FileType['test.html.xhtml']
162
+ assert_equal :rhtml, FileType['_form.rhtml']
163
+ end
164
+
165
+ def test_yaml
166
+ assert_equal :yaml, FileType['test.yml']
167
+ assert_equal :yaml, FileType['test.yaml']
168
+ assert_equal :yaml, FileType['my.html.yaml']
169
+ assert_not_equal :yaml, FileType['YAML']
170
+ end
171
+
136
172
  def test_shebang
137
173
  dir = './test'
138
174
  if File.directory? dir
@@ -89,7 +89,7 @@ class String
89
89
  end
90
90
 
91
91
  if $0 == __FILE__
92
- eval DATA.read, nil, $0, __LINE__+4
92
+ eval DATA.read, nil, $0, __LINE__+4
93
93
  end
94
94
 
95
95
  __END__
@@ -1,6 +1,6 @@
1
1
  # = PluginHost
2
2
  #
3
- # $Id: plugin.rb 105 2006-02-20 01:41:40Z murphy $
3
+ # $Id: plugin.rb 143 2006-06-28 23:05:18Z murphy $
4
4
  #
5
5
  # A simple subclass plugin system.
6
6
  #
@@ -56,7 +56,6 @@ module PluginHost
56
56
 
57
57
  def require_helper plugin_id, helper_name
58
58
  path = path_to File.join(plugin_id, helper_name)
59
- #$stderr.puts 'Loading helper: ' + path
60
59
  require path
61
60
  end
62
61
 
@@ -125,6 +124,20 @@ module PluginHost
125
124
  end
126
125
  end
127
126
 
127
+ # Define the default plugin to use when no plugin is found
128
+ # for a given id.
129
+ #
130
+ # See also map.
131
+ #
132
+ # class MyColorHost < PluginHost
133
+ # map :navy => :dark_blue
134
+ # default :gray
135
+ # end
136
+ def default id
137
+ id = validate_id id
138
+ plugin_hash[nil] = id
139
+ end
140
+
128
141
  # Every plugin must register itself for one or more
129
142
  # +ids+ by calling register_for, which calls this method.
130
143
  #
@@ -144,6 +157,17 @@ module PluginHost
144
157
  @plugin_hash ||= create_plugin_hash
145
158
  end
146
159
 
160
+ # Returns an array of all .rb files in the plugin path.
161
+ #
162
+ # The extension .rb is not included.
163
+ def all_plugin_names
164
+ Dir[path_to('*')].select do |file|
165
+ File.basename(file)[/^(?!_)\w+\.rb$/]
166
+ end.map do |file|
167
+ File.basename file, '.rb'
168
+ end
169
+ end
170
+
147
171
  protected
148
172
  # Created a new plugin list and stores it to @plugin_hash.
149
173
  def create_plugin_hash
@@ -152,10 +176,13 @@ protected
152
176
  id = validate_id(plugin_id)
153
177
  path = path_to id
154
178
  begin
155
- #$stderr.puts 'Loading plugin: ' + path if $DEBUG
156
179
  require path
157
180
  rescue LoadError => boom
158
- raise PluginNotFound, 'Could not load plugin %p: %s' % [id, boom]
181
+ if h.has_key? nil # default plugin
182
+ h[id] = h[nil]
183
+ else
184
+ raise PluginNotFound, 'Could not load plugin %p: %s' % [id, boom]
185
+ end
159
186
  else
160
187
  # Plugin should have registered by now
161
188
  unless h.has_key? id
@@ -188,17 +215,6 @@ protected
188
215
  end
189
216
  end
190
217
 
191
- # Returns an array of all .rb files in the plugin path.
192
- #
193
- # The extension .rb is not included.
194
- def all_plugin_names
195
- Dir[path_to('*')].select do |file|
196
- File.basename(file)[/^(?!_)\w+\.rb$/]
197
- end.map do |file|
198
- File.basename file, '.rb'
199
- end
200
- end
201
-
202
218
  # Returns the Plugin for +id+.
203
219
  # Use it like Hash#fetch.
204
220
  #
@@ -208,7 +224,7 @@ protected
208
224
  plugin_hash.fetch validate_id(id), *args, &blk
209
225
  end
210
226
 
211
- # Returns the path to the encoder for format.
227
+ # Returns the expected path to the plugin file for the given id.
212
228
  def path_to plugin_id
213
229
  File.join plugin_path, "#{plugin_id}.rb"
214
230
  end
@@ -219,7 +235,7 @@ protected
219
235
  # Raises +ArgumentError+ for all other objects, or if the
220
236
  # given String includes non-alphanumeric characters (\W).
221
237
  def validate_id id
222
- if id.is_a? Symbol
238
+ if id.is_a? Symbol or id.nil?
223
239
  id
224
240
  elsif id.is_a? String
225
241
  if id[/\w+/] == id
@@ -4,7 +4,7 @@ module CodeRay
4
4
 
5
5
  # = Scanners
6
6
  #
7
- # $Id: scanner.rb 90 2005-11-05 14:37:40Z murphy $
7
+ # $Id: scanner.rb 143 2006-06-28 23:05:18Z murphy $
8
8
  #
9
9
  # This module holds the Scanner class and its subclasses.
10
10
  # For example, the Ruby scanner is named CodeRay::Scanners::Ruby
@@ -65,6 +65,10 @@ module CodeRay
65
65
  is_a? Streamable
66
66
  end
67
67
 
68
+ def normify code
69
+ code = code.to_s.to_unix
70
+ end
71
+
68
72
  end
69
73
 
70
74
  =begin
@@ -91,44 +95,59 @@ module CodeRay
91
95
  # TokenStream with the +block+ as callback to handle the tokens.
92
96
  #
93
97
  # Else, a Tokens object is used.
94
- def initialize code, options = {}, &block
98
+ def initialize code='', options = {}, &block
95
99
  @options = self.class::DEFAULT_OPTIONS.merge options
96
100
  raise "I am only the basic Scanner class. I can't scan "\
97
101
  "anything. :( Use my subclasses." if self.class == Scanner
98
102
 
99
- # I love this hack. It seems to silence
100
- # all dos/unix/mac newline problems.
101
- super code.gsub(/\r\n?/, "\n")
103
+ super Scanner.normify(code)
102
104
 
105
+ @tokens = options[:tokens]
103
106
  if @options[:stream]
104
107
  warn "warning in CodeRay::Scanner.new: :stream is set, "\
105
108
  "but no block was given" unless block_given?
106
109
  raise NotStreamableError, self unless kind_of? Streamable
107
- @tokens = TokenStream.new(&block)
110
+ @tokens ||= TokenStream.new(&block)
108
111
  else
109
112
  warn "warning in CodeRay::Scanner.new: Block given, "\
110
113
  "but :stream is #{@options[:stream]}" if block_given?
111
- @tokens = Tokens.new
114
+ @tokens ||= Tokens.new
112
115
  end
116
+
117
+ setup
113
118
  end
114
119
 
115
120
  # More mnemonic accessor name for the input string.
116
121
  alias code string
117
122
 
123
+ def reset
124
+ super
125
+ reset_instance
126
+ end
127
+
128
+ def string= code
129
+ code = Scanner.normify(code)
130
+ super code
131
+ reset_instance
132
+ end
133
+
118
134
  # Scans the code and returns all tokens in a Tokens object.
119
- def tokenize options = {}
120
- options = @options.merge({}) #options
121
- if @options[:stream] # :stream must have been set already
122
- reset ## what is this for?
123
- scan_tokens @tokens, options
124
- @tokens
125
- else
126
- @cached_tokens ||= scan_tokens @tokens, options
127
- end
135
+ def tokenize new_string=nil, options = {}
136
+ options = @options.merge(options)
137
+ self.string = new_string if new_string
138
+ @cached_tokens =
139
+ if @options[:stream] # :stream must have been set already
140
+ reset unless new_string
141
+ scan_tokens @tokens, options
142
+ @tokens
143
+ else
144
+ scan_tokens @tokens, options
145
+ end
128
146
  end
129
147
 
130
- # You can also see tokenize as a read-only attribute
131
- alias tokens tokenize
148
+ def tokens
149
+ @cached_tokens ||= tokenize
150
+ end
132
151
 
133
152
  # Traverses the tokens.
134
153
  def each &block
@@ -148,6 +167,14 @@ module CodeRay
148
167
 
149
168
  protected
150
169
 
170
+ # Can be implemented by subclasses to do some initialization
171
+ # that has to be done once per instance.
172
+ #
173
+ # Use reset for initialization that has to be done once per
174
+ # scan.
175
+ def setup
176
+ end
177
+
151
178
  # This is the central method, and commonly the only one a
152
179
  # subclass implements.
153
180
  #
@@ -158,6 +185,11 @@ module CodeRay
158
185
  "#{self.class}#scan_tokens not implemented."
159
186
  end
160
187
 
188
+ def reset_instance
189
+ @tokens.clear unless @options[:keep_tokens]
190
+ @cached_tokens = nil
191
+ end
192
+
161
193
  # Scanner error with additional status information
162
194
  def raise_inspect msg, tokens, ambit = 30
163
195
  raise ScanError, <<-EOE % [
@@ -194,4 +226,13 @@ surrounding code:
194
226
  end
195
227
  end
196
228
 
197
- # vim:sw=2:ts=2:noet:tw=78
229
+ class String
230
+ # I love this hack. It seems to silence all dos/unix/mac newline problems.
231
+ def to_unix
232
+ if index ?\r
233
+ gsub(/\r\n?/, "\n")
234
+ else
235
+ self
236
+ end
237
+ end
238
+ end
@@ -1,10 +1,14 @@
1
1
  module CodeRay
2
- module Scanners
3
-
4
- map :cpp => :c,
5
- :plain => :plaintext,
6
- :pascal => :delphi,
7
- :irb => :ruby
8
-
9
- end
2
+ module Scanners
3
+
4
+ map :cpp => :c,
5
+ :plain => :plaintext,
6
+ :pascal => :delphi,
7
+ :irb => :ruby,
8
+ :xml => :html,
9
+ :xhtml => :nitro_html
10
+
11
+ default :plain
12
+
13
+ end
10
14
  end
@@ -1,4 +1,5 @@
1
- module CodeRay module Scanners
1
+ module CodeRay
2
+ module Scanners
2
3
 
3
4
  class C < Scanner
4
5
 
@@ -42,7 +43,9 @@ module CodeRay module Scanners
42
43
  kind = :error
43
44
  match = nil
44
45
 
45
- if state == :initial
46
+ case state
47
+
48
+ when :initial
46
49
 
47
50
  if scan(/ \s+ | \\\n /x)
48
51
  kind = :space
@@ -96,7 +99,7 @@ module CodeRay module Scanners
96
99
  getch
97
100
  end
98
101
 
99
- elsif state == :string
102
+ when :string
100
103
  if scan(/[^\\"]+/)
101
104
  kind = :content
102
105
  elsif scan(/"/)
@@ -110,10 +113,10 @@ module CodeRay module Scanners
110
113
  kind = :error
111
114
  state = :initial
112
115
  else
113
- raise "else case \" reached; %p not handled." % peek(1), tokens
116
+ raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
114
117
  end
115
118
 
116
- elsif state == :include_expected
119
+ when :include_expected
117
120
  if scan(/<[^>\n]+>?|"[^"\n\\]*(?:\\.[^"\n\\]*)*"?/)
118
121
  kind = :include
119
122
  state = :initial
@@ -128,12 +131,16 @@ module CodeRay module Scanners
128
131
  end
129
132
 
130
133
  else
131
- raise 'else-case reached', tokens
134
+ raise_inspect 'Unknown state', tokens
132
135
 
133
136
  end
134
137
 
135
138
  match ||= matched
136
- raise [match, kind], tokens if kind == :error
139
+ if $DEBUG and (not kind or kind == :error)
140
+ raise_inspect 'Error token %p in line %d' %
141
+ [[match, kind], line], tokens
142
+ end
143
+ raise_inspect 'Empty token', tokens unless match
137
144
 
138
145
  tokens << [match, kind]
139
146
 
@@ -144,4 +151,5 @@ module CodeRay module Scanners
144
151
 
145
152
  end
146
153
 
147
- end end
154
+ end
155
+ end