coderay 0.7.4.215 → 0.8.260

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/LICENSE +421 -257
  2. data/README +24 -13
  3. data/bin/coderay +9 -4
  4. data/lib/coderay.rb +19 -17
  5. data/lib/coderay/duo.rb +67 -9
  6. data/lib/coderay/encoder.rb +18 -9
  7. data/lib/coderay/encoders/_map.rb +2 -1
  8. data/lib/coderay/encoders/debug.rb +14 -11
  9. data/lib/coderay/encoders/html.rb +44 -17
  10. data/lib/coderay/encoders/html/css.rb +13 -8
  11. data/lib/coderay/encoders/html/numerization.rb +8 -6
  12. data/lib/coderay/encoders/html/output.rb +3 -1
  13. data/lib/coderay/encoders/statistic.rb +2 -6
  14. data/lib/coderay/encoders/text.rb +2 -3
  15. data/lib/coderay/encoders/tokens.rb +3 -3
  16. data/lib/coderay/encoders/xml.rb +1 -2
  17. data/lib/coderay/for_redcloth.rb +72 -0
  18. data/lib/coderay/helpers/file_type.rb +38 -9
  19. data/lib/coderay/helpers/gzip_simple.rb +1 -0
  20. data/lib/coderay/helpers/plugin.rb +15 -8
  21. data/lib/coderay/helpers/word_list.rb +4 -0
  22. data/lib/coderay/scanner.rb +30 -13
  23. data/lib/coderay/scanners/_map.rb +1 -1
  24. data/lib/coderay/scanners/c.rb +3 -1
  25. data/lib/coderay/scanners/css.rb +181 -0
  26. data/lib/coderay/scanners/debug.rb +1 -0
  27. data/lib/coderay/scanners/delphi.rb +1 -0
  28. data/lib/coderay/scanners/diff.rb +104 -0
  29. data/lib/coderay/scanners/java.rb +179 -0
  30. data/lib/coderay/scanners/java/builtin_types.rb +419 -0
  31. data/lib/coderay/scanners/java_script.rb +187 -0
  32. data/lib/coderay/scanners/json.rb +106 -0
  33. data/lib/coderay/scanners/nitro_xhtml.rb +5 -4
  34. data/lib/coderay/scanners/plaintext.rb +2 -0
  35. data/lib/coderay/scanners/rhtml.rb +2 -2
  36. data/lib/coderay/scanners/ruby.rb +64 -50
  37. data/lib/coderay/scanners/ruby/patterns.rb +15 -19
  38. data/lib/coderay/scanners/scheme.rb +142 -0
  39. data/lib/coderay/scanners/sql.Keith.rb +143 -0
  40. data/lib/coderay/scanners/sql.rb +154 -0
  41. data/lib/coderay/scanners/xml.rb +1 -0
  42. data/lib/coderay/styles/cycnus.rb +30 -9
  43. data/lib/coderay/styles/murphy.rb +15 -2
  44. data/lib/coderay/{encoders/html/classes.rb → token_classes.rb} +14 -9
  45. data/lib/coderay/tokens.rb +33 -14
  46. data/lib/term/ansicolor.rb +220 -0
  47. metadata +62 -44
data/README CHANGED
@@ -18,12 +18,12 @@ 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.7.4 (2006.october.20)
21
+ Version: 0.8.0
22
22
  Author:: murphy (Kornelius Kalnbach)
23
23
  Contact:: murphy rubychan de
24
24
  Website:: coderay.rubychan.de[http://coderay.rubychan.de]
25
25
  License:: GNU LGPL; see LICENSE file in the main directory.
26
- Subversion:: $Id: README 206 2006-10-19 19:46:56Z murphy $
26
+ Subversion:: $Id: README 247 2008-08-28 05:06:01Z murphy $
27
27
 
28
28
  -----
29
29
 
@@ -40,8 +40,10 @@ Since CodeRay is still in beta stage, nightly buildy may be useful:
40
40
 
41
41
  === Dependencies
42
42
 
43
- CodeRay needs Ruby 1.8 and the strscan[http://www.ruby-doc.org/stdlib/libdoc/strscan/rdoc/index.htm] library
44
- (part of the standard library.) It should also run with Ruby 1.9 and yarv.
43
+ CodeRay needs Ruby 1.8 and the
44
+ strscan[http://www.ruby-doc.org/stdlib/libdoc/strscan/rdoc/index.htm]
45
+ library (part of the standard library.) It should also run with Ruby 1.9 and
46
+ yarv.
45
47
 
46
48
 
47
49
  == Example Usage
@@ -67,7 +69,8 @@ Please report errors in this documentation to <coderay cycnus de>.
67
69
 
68
70
  === Special Thanks to
69
71
 
70
- * licenser (Heinz N. Gies) for ending my QBasic career, inventing the Coder project and the input/output plugin system.
72
+ * licenser (Heinz N. Gies) for ending my QBasic career, inventing the Coder
73
+ project and the input/output plugin system.
71
74
  CodeRay would not exist without him.
72
75
 
73
76
  === Thanks to
@@ -88,6 +91,8 @@ Please report errors in this documentation to <coderay cycnus de>.
88
91
  * Daniel and Dethix from ruby-mine.de
89
92
  * Dookie (who is no longer with us...) and Leonidas from
90
93
  http://www.python-forum.de
94
+ * Andreas Schwarz for finding out that CaseIgnoringWordList was not case
95
+ ignoring! Such things really make you write tests.
91
96
  * matz and all Ruby gods and gurus
92
97
  * The inventors of: the computer, the internet, the true color display, HTML &
93
98
  CSS, VIM, RUBY, pizza, microwaves, guitars, scouting, programming, anime,
@@ -97,21 +102,27 @@ Where would we be without all those people?
97
102
 
98
103
  === Created using
99
104
 
100
- * Ruby[http://ruby-lang.org/en/]
101
- * Chihiro (my Sony VAIO laptop) and Seras (my Athlon 2200+ tower)
102
- * VIM[http://vim.org]
105
+ * Ruby[http://ruby-lang.org/]
106
+ * Chihiro (my Sony VAIO laptop), Henrietta (my new MacBook) and
107
+ Seras (my Athlon 2200+ tower)
108
+ * VIM[http://vim.org] and TextMate[http://macromates.com]
103
109
  * RDE[http://homepage2.nifty.com/sakazuki/rde_e.html]
104
- * Microsoft Windows (yes, I confess!)
105
- * Firefox[http://www.mozilla.org/products/firefox/] and Thunderbird[http://www.mozilla.org/products/thunderbird/]
110
+ * Microsoft Windows (yes, I confess!) and MacOS X
111
+ * Firefox[http://www.mozilla.org/products/firefox/] and
112
+ Thunderbird[http://www.mozilla.org/products/thunderbird/]
106
113
  * Rake[http://rake.rubyforge.org/]
107
114
  * RubyGems[http://docs.rubygems.org/]
108
- * {Subversion/TortoiseSVN}[http://tortoisesvn.tigris.org/] using Apache via XAMPP[http://www.apachefriends.org/en/xampp.html]
115
+ * {Subversion/TortoiseSVN}[http://tortoisesvn.tigris.org/] using Apache via
116
+ XAMPP[http://www.apachefriends.org/en/xampp.html]
109
117
  * RDoc (though I'm quite unsatisfied with it)
110
- * GNUWin32, MinGW and some other tools to make the shell under windows a bit more useful
118
+ * GNUWin32, MinGW and some other tools to make the shell under windows a bit
119
+ more useful
120
+ * Term::ANSIColor[http://term-ansicolor.rubyforge.org/]
111
121
 
112
122
  ---
113
123
 
114
124
  * As you can see, CodeRay was created under heavy use of *free* software.
115
125
  * So CodeRay is also *free*.
116
- * If you use CodeRay to create software, think about making this software *free*, too.
126
+ * If you use CodeRay to create software, think about making this software
127
+ *free*, too.
117
128
  * Thanks :)
@@ -29,11 +29,11 @@ Example:
29
29
  if first[/-(\w+)/] == first
30
30
  lang = $1.to_sym
31
31
  input = $stdin.read
32
- tokens = CodeRay.scan input, lang
32
+ tokens = :scan
33
33
  elsif first == '-'
34
34
  lang = $1.to_sym
35
35
  input = $stdin.read
36
- tokens = CodeRay.scan input, lang
36
+ tokens = :scan
37
37
  else
38
38
  file = first
39
39
  tokens = CodeRay.scan_file file
@@ -54,8 +54,13 @@ Example:
54
54
  $stderr.puts 'No format given; setting to default (HTML Page)'
55
55
  format = :page
56
56
  end
57
-
58
- output = tokens.encode format
57
+
58
+ # TODO: allow streaming
59
+ if tokens == :scan
60
+ output = CodeRay::Duo[lang => format].highlight input #, :stream => true
61
+ else
62
+ output = tokens.encode format
63
+ end
59
64
  out = $stdout
60
65
  if output_filename
61
66
  output_filename += '.' + CodeRay::Encoders[format]::FILE_EXTENSION
@@ -1,6 +1,6 @@
1
1
  # = CodeRay Library
2
2
  #
3
- # $Id: coderay.rb 213 2006-10-20 07:12:31Z murphy $
3
+ # $Id: coderay.rb 260 2008-10-08 20:18:31Z murphy $
4
4
  #
5
5
  # CodeRay is a Ruby library for syntax highlighting.
6
6
  #
@@ -24,8 +24,8 @@
24
24
  #
25
25
  # == Usage
26
26
  #
27
- # Remember you need RubyGems to use CodeRay. Run Ruby with -rubygems option
28
- # if required.
27
+ # Remember you need RubyGems to use CodeRay, unless you have it in your load path. Run Ruby with
28
+ # -rubygems option if required.
29
29
  #
30
30
  # === Highlight Ruby code in a string as html
31
31
  #
@@ -44,19 +44,15 @@
44
44
  #
45
45
  # You can include this div in your page. The used CSS styles can be printed with
46
46
  #
47
- # % ruby -rcoderay -e "print CodeRay::Encoders[:html]::CSS"
47
+ # % coderay_stylesheet
48
48
  #
49
49
  # === Highlight without typing too much
50
- #
50
+ #
51
51
  # If you are one of the hasty (or lazy, or extremely curious) people, just run this file:
52
- #
53
- # % ruby -rubygems coderay.rb
54
52
  #
55
- # If the output was to fast for you, try
53
+ # % ruby -rubygems /path/to/coderay/coderay.rb > example.html
56
54
  #
57
- # % ruby -rubygems coderay.rb > example.html
58
- #
59
- # and look at the file it created.
55
+ # and look at the file it created in your browser.
60
56
  #
61
57
  # = CodeRay Module
62
58
  #
@@ -111,7 +107,7 @@
111
107
  #
112
108
  # CodeRay.scan_stream:: Scan in stream mode.
113
109
  #
114
- # == All-in-One Encoding
110
+ # == All-in-One Encoding
115
111
  #
116
112
  # CodeRay.encode:: Highlight a string with a given input and output format.
117
113
  #
@@ -121,11 +117,16 @@
121
117
  # for this Encoder must only be done once.
122
118
  #
123
119
  # CodeRay.encoder:: Create an Encoder instance with format and options.
120
+ # CodeRay.scanner:: Create an Scanner instance for lang, with '' as default code.
124
121
  #
125
- # There is no CodeRay.scanner method because Scanners are bound to an input string
126
- # on creation; you can't re-use them with another string.
122
+ # To make use of CodeRay.scanner, use CodeRay::Scanner::code=.
127
123
  #
128
- # The scanning methods provide more flexibility; we recommend to use these.
124
+ # The scanning methods provide more flexibility; we recommend to use these.
125
+ #
126
+ # == Reusing Scanners and Encoders
127
+ #
128
+ # If you want to re-use scanners and encoders (because that is faster), see
129
+ # CodeRay::Duo for the most convenient (and recommended) interface.
129
130
  module CodeRay
130
131
 
131
132
  # Version: Major.Minor.Teeny[.Revision]
@@ -133,7 +134,7 @@ module CodeRay
133
134
  # Minor: odd for beta, even for stable
134
135
  # Teeny: development state
135
136
  # Revision: Subversion Revision number (generated on rake)
136
- VERSION = '0.7.4'
137
+ VERSION = '0.8'
137
138
 
138
139
  require 'coderay/tokens'
139
140
  require 'coderay/scanner'
@@ -314,6 +315,7 @@ end
314
315
  # Run a test script.
315
316
  if $0 == __FILE__
316
317
  $stderr.print 'Press key to print demo.'; gets
317
- code = File.read($0)[/module CodeRay.*/m]
318
+ # Just use this file as an example of Ruby code.
319
+ code = File.read(__FILE__)[/module CodeRay.*/m]
318
320
  print CodeRay.scan(code, :ruby).html
319
321
  end
@@ -4,26 +4,84 @@ module CodeRay
4
4
  #
5
5
  # $Id: scanner.rb 123 2006-03-21 14:46:34Z murphy $
6
6
  #
7
- # TODO: Doc.
7
+ # A Duo is a convenient way to use CodeRay. You just create a Duo,
8
+ # giving it a lang (language of the input code) and a format (desired
9
+ # output format), and call Duo#highlight with the code.
10
+ #
11
+ # Duo makes it easy to re-use both scanner and encoder for a repetitive
12
+ # task. It also provides a very easy interface syntax:
13
+ #
14
+ # require 'coderay'
15
+ # CodeRay::Duo[:python, :div].highlight 'import this'
16
+ #
17
+ # Until you want to do uncommon things with CodeRay, I recommend to use
18
+ # this method, since it takes care of everything.
8
19
  class Duo
9
20
 
10
- attr_accessor :scanner, :encoder
11
-
12
- def initialize lang, format, options = {}
13
- @scanner = CodeRay.scanner lang, CodeRay.get_scanner_options(options)
14
- @encoder = CodeRay.encoder format, options
21
+ attr_accessor :lang, :format, :options
22
+
23
+ # Create a new Duo, holding a lang and a format to highlight code.
24
+ #
25
+ # simple:
26
+ # CodeRay::Duo[:ruby, :page].highlight 'bla 42'
27
+ #
28
+ # streaming:
29
+ # CodeRay::Duo[:ruby, :page].highlight 'bar 23', :stream => true
30
+ #
31
+ # with options:
32
+ # CodeRay::Duo[:ruby, :html, :hint => :debug].highlight '????::??'
33
+ #
34
+ # alternative syntax without options:
35
+ # CodeRay::Duo[:ruby => :statistic].encode 'class << self; end'
36
+ #
37
+ # alternative syntax with options:
38
+ # CodeRay::Duo[{ :ruby => :statistic }, :do => :something].encode 'abc'
39
+ #
40
+ # The options are forwarded to scanner and encoder
41
+ # (see CodeRay.get_scanner_options).
42
+ def initialize lang = nil, format = nil, options = {}
43
+ if format == nil and lang.is_a? Hash and lang.size == 1
44
+ @lang = lang.keys.first
45
+ @format = lang[@lang]
46
+ else
47
+ @lang = lang
48
+ @format = format
49
+ end
50
+ @options = options
15
51
  end
16
52
 
17
53
  class << self
54
+ # To allow calls like Duo[:ruby, :html].highlight.
18
55
  alias [] new
19
56
  end
20
57
 
21
- def encode code
22
- @scanner.string = code
23
- @encoder.encode_tokens(scanner.tokenize)
58
+ # The scanner of the duo. Only created once.
59
+ def scanner
60
+ @scanner ||= CodeRay.scanner @lang, CodeRay.get_scanner_options(@options)
61
+ end
62
+
63
+ # The encoder of the duo. Only created once.
64
+ def encoder
65
+ @encoder ||= CodeRay.encoder @format, @options
66
+ end
67
+
68
+ # Tokenize and highlight the code using +scanner+ and +encoder+.
69
+ #
70
+ # If the :stream option is set, the Duo will go into streaming mode,
71
+ # saving memory for the cost of time.
72
+ def encode code, options = { :stream => false }
73
+ stream = options.delete :stream
74
+ options = @options.merge options
75
+ if stream
76
+ encoder.encode_stream(code, @lang, options)
77
+ else
78
+ scanner.code = code
79
+ encoder.encode_tokens(scanner.tokenize, options)
80
+ end
24
81
  end
25
82
  alias highlight encode
26
83
 
27
84
  end
28
85
 
29
86
  end
87
+
@@ -1,3 +1,5 @@
1
+ require "stringio"
2
+
1
3
  module CodeRay
2
4
 
3
5
  # This module holds the Encoder class and its subclasses.
@@ -40,7 +42,7 @@ module CodeRay
40
42
  # downcase class name instead.
41
43
  def const_missing sym
42
44
  if sym == :FILE_EXTENSION
43
- sym.to_s.downcase
45
+ plugin_id
44
46
  else
45
47
  super
46
48
  end
@@ -130,13 +132,15 @@ module CodeRay
130
132
  # By default, it calls text_token or block_token, depending on
131
133
  # whether +text+ is a String.
132
134
  def token text, kind
133
- if text.instance_of? ::String # Ruby 1.9: :open.is_a? String
134
- text_token text, kind
135
- elsif text.is_a? ::Symbol
136
- block_token text, kind
137
- else
138
- raise 'Unknown token text type: %p' % text
139
- end
135
+ out =
136
+ if text.is_a? ::String # Ruby 1.9: watch out, :open.is_a? String is true
137
+ text_token text, kind
138
+ elsif text.is_a? ::Symbol
139
+ block_token text, kind
140
+ else
141
+ raise 'Unknown token text type: %p' % text
142
+ end
143
+ @out << out if defined?(@out) && @out
140
144
  end
141
145
 
142
146
  def text_token text, kind
@@ -148,6 +152,10 @@ module CodeRay
148
152
  open_token kind
149
153
  when :close
150
154
  close_token kind
155
+ when :begin_line
156
+ begin_line kind
157
+ when :end_line
158
+ end_line kind
151
159
  else
152
160
  raise 'unknown block action: %p' % action
153
161
  end
@@ -164,7 +172,8 @@ module CodeRay
164
172
  # The already created +tokens+ object must be used; it can be a
165
173
  # TokenStream or a Tokens object.
166
174
  def compile tokens, options
167
- tokens.each(&self)
175
+ tokens.each { |text, kind| token text, kind } # FIXME for Ruby 1.9?
176
+ #tokens.each(&self)
168
177
  end
169
178
 
170
179
  end
@@ -2,7 +2,8 @@ module CodeRay
2
2
  module Encoders
3
3
 
4
4
  map :stats => :statistic,
5
- :plain => :text
5
+ :plain => :text,
6
+ :tex => :latex
6
7
 
7
8
  end
8
9
  end
@@ -19,17 +19,12 @@ module Encoders
19
19
 
20
20
  protected
21
21
  def text_token text, kind
22
- @out <<
23
- if kind == :space
24
- text
25
- else
26
- text = text.gsub(/[)\\]/, '\\\\\0')
27
- "#{kind}(#{text})"
28
- end
29
- end
30
-
31
- def block_token action, kind
32
- @out << super
22
+ if kind == :space
23
+ text
24
+ else
25
+ text = text.gsub(/[)\\]/, '\\\\\0') # escape ) and \
26
+ "#{kind}(#{text})"
27
+ end
33
28
  end
34
29
 
35
30
  def open_token kind
@@ -40,6 +35,14 @@ module Encoders
40
35
  ">"
41
36
  end
42
37
 
38
+ def begin_line kind
39
+ "#{kind}["
40
+ end
41
+
42
+ def end_line kind
43
+ "]"
44
+ end
45
+
43
46
  end
44
47
 
45
48
  end
@@ -86,7 +86,7 @@ module Encoders
86
86
  :hint => false,
87
87
  }
88
88
 
89
- helper :classes, :output, :css
89
+ helper :output, :css
90
90
 
91
91
  attr_reader :css
92
92
 
@@ -119,15 +119,14 @@ module Encoders
119
119
  end
120
120
  }
121
121
 
122
- TRANSPARENT_TOKEN_KINDS = Set[
122
+ TRANSPARENT_TOKEN_KINDS = [
123
123
  :delimiter, :modifier, :content, :escape, :inline_delimiter,
124
- ]
124
+ ].to_set
125
125
 
126
126
  # Generate a hint about the given +classes+ in a +hint+ style.
127
127
  #
128
128
  # +hint+ may be :info, :info_long or :debug.
129
129
  def self.token_path_to_hint hint, classes
130
- return '' unless hint
131
130
  title =
132
131
  case hint
133
132
  when :info
@@ -137,7 +136,7 @@ module Encoders
137
136
  when :debug
138
137
  classes.inspect
139
138
  end
140
- " title=\"#{title}\""
139
+ title ? " title=\"#{title}\"" : ''
141
140
  end
142
141
 
143
142
  def setup options
@@ -159,29 +158,32 @@ module Encoders
159
158
 
160
159
  when :class
161
160
  @css_style = Hash.new do |h, k|
162
- if k.is_a? Array
163
- type = k.first
164
- else
165
- type = k
166
- end
167
- c = ClassOfKind[type]
161
+ c = CodeRay::Tokens::ClassOfKind[k.first]
168
162
  if c == :NO_HIGHLIGHT and not hint
169
- h[k] = false
163
+ h[k.dup] = false
170
164
  else
171
- title = HTML.token_path_to_hint hint, (k[1..-1] << k.first)
172
- h[k] = '<span%s class="%s">' % [title, c]
165
+ title = if hint
166
+ HTML.token_path_to_hint(hint, k[1..-1] << k.first)
167
+ else
168
+ ''
169
+ end
170
+ if c == :NO_HIGHLIGHT
171
+ h[k.dup] = '<span%s>' % [title]
172
+ else
173
+ h[k.dup] = '<span%s class="%s">' % [title, c]
174
+ end
173
175
  end
174
176
  end
175
177
 
176
178
  when :style
177
179
  @css_style = Hash.new do |h, k|
178
- if k.is_a? Array
180
+ if k.is_a? ::Array
179
181
  styles = k.dup
180
182
  else
181
183
  styles = [k]
182
184
  end
183
185
  type = styles.first
184
- classes = styles.map { |c| ClassOfKind[c] }
186
+ classes = styles.map { |c| Tokens::ClassOfKind[c] }
185
187
  if classes.first == :NO_HIGHLIGHT and not hint
186
188
  h[k] = false
187
189
  else
@@ -224,13 +226,16 @@ module Encoders
224
226
  text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
225
227
  end
226
228
  @opened[0] = type
227
- if style = @css_style[@opened]
229
+ if text != "\n" && style = @css_style[@opened]
228
230
  @out << style << text << '</span>'
229
231
  else
230
232
  @out << text
231
233
  end
232
234
  else
235
+
233
236
  case text
237
+
238
+ # token groups, eg. strings
234
239
  when :open
235
240
  @opened[0] = type
236
241
  @out << (@css_style[@opened] || '<span>')
@@ -246,6 +251,28 @@ module Encoders
246
251
  @out << '</span>'
247
252
  @opened.pop
248
253
  end
254
+
255
+ # whole lines to be highlighted, eg. a deleted line in a diff
256
+ when :begin_line
257
+ @opened[0] = type
258
+ if style = @css_style[@opened]
259
+ @out << style.sub('<span', '<div')
260
+ else
261
+ @out << '<div>'
262
+ end
263
+ @opened << type
264
+ when :end_line
265
+ if @opened.empty?
266
+ # nothing to close
267
+ else
268
+ if $DEBUG and (@opened.size == 1 or @opened.last != type)
269
+ raise 'Malformed token stream: Trying to close a line (%p) \
270
+ that is not open. Open are: %p.' % [type, @opened[1..-1]]
271
+ end
272
+ @out << '</div>'
273
+ @opened.pop
274
+ end
275
+
249
276
  when nil
250
277
  raise 'Token with nil as text was given: %p' % [[text, type]]
251
278
  else