coderay-beta 0.9.1

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.
Files changed (67) hide show
  1. data/FOLDERS +53 -0
  2. data/LICENSE +504 -0
  3. data/bin/coderay +82 -0
  4. data/bin/coderay_stylesheet +4 -0
  5. data/lib/README +129 -0
  6. data/lib/coderay.rb +320 -0
  7. data/lib/coderay/duo.rb +85 -0
  8. data/lib/coderay/encoder.rb +213 -0
  9. data/lib/coderay/encoders/_map.rb +11 -0
  10. data/lib/coderay/encoders/comment_filter.rb +43 -0
  11. data/lib/coderay/encoders/count.rb +21 -0
  12. data/lib/coderay/encoders/debug.rb +49 -0
  13. data/lib/coderay/encoders/div.rb +19 -0
  14. data/lib/coderay/encoders/filter.rb +75 -0
  15. data/lib/coderay/encoders/html.rb +305 -0
  16. data/lib/coderay/encoders/html/css.rb +70 -0
  17. data/lib/coderay/encoders/html/numerization.rb +133 -0
  18. data/lib/coderay/encoders/html/output.rb +206 -0
  19. data/lib/coderay/encoders/json.rb +69 -0
  20. data/lib/coderay/encoders/lines_of_code.rb +90 -0
  21. data/lib/coderay/encoders/null.rb +26 -0
  22. data/lib/coderay/encoders/page.rb +20 -0
  23. data/lib/coderay/encoders/span.rb +19 -0
  24. data/lib/coderay/encoders/statistic.rb +77 -0
  25. data/lib/coderay/encoders/term.rb +137 -0
  26. data/lib/coderay/encoders/text.rb +32 -0
  27. data/lib/coderay/encoders/token_class_filter.rb +84 -0
  28. data/lib/coderay/encoders/xml.rb +71 -0
  29. data/lib/coderay/encoders/yaml.rb +22 -0
  30. data/lib/coderay/for_redcloth.rb +85 -0
  31. data/lib/coderay/helpers/file_type.rb +240 -0
  32. data/lib/coderay/helpers/gzip_simple.rb +123 -0
  33. data/lib/coderay/helpers/plugin.rb +349 -0
  34. data/lib/coderay/helpers/word_list.rb +138 -0
  35. data/lib/coderay/scanner.rb +284 -0
  36. data/lib/coderay/scanners/_map.rb +23 -0
  37. data/lib/coderay/scanners/c.rb +203 -0
  38. data/lib/coderay/scanners/cpp.rb +228 -0
  39. data/lib/coderay/scanners/css.rb +210 -0
  40. data/lib/coderay/scanners/debug.rb +62 -0
  41. data/lib/coderay/scanners/delphi.rb +150 -0
  42. data/lib/coderay/scanners/diff.rb +105 -0
  43. data/lib/coderay/scanners/groovy.rb +263 -0
  44. data/lib/coderay/scanners/html.rb +182 -0
  45. data/lib/coderay/scanners/java.rb +176 -0
  46. data/lib/coderay/scanners/java/builtin_types.rb +419 -0
  47. data/lib/coderay/scanners/java_script.rb +224 -0
  48. data/lib/coderay/scanners/json.rb +112 -0
  49. data/lib/coderay/scanners/nitro_xhtml.rb +136 -0
  50. data/lib/coderay/scanners/php.rb +526 -0
  51. data/lib/coderay/scanners/plaintext.rb +21 -0
  52. data/lib/coderay/scanners/python.rb +285 -0
  53. data/lib/coderay/scanners/rhtml.rb +74 -0
  54. data/lib/coderay/scanners/ruby.rb +404 -0
  55. data/lib/coderay/scanners/ruby/patterns.rb +238 -0
  56. data/lib/coderay/scanners/scheme.rb +145 -0
  57. data/lib/coderay/scanners/sql.rb +162 -0
  58. data/lib/coderay/scanners/xml.rb +17 -0
  59. data/lib/coderay/scanners/yaml.rb +144 -0
  60. data/lib/coderay/style.rb +20 -0
  61. data/lib/coderay/styles/_map.rb +7 -0
  62. data/lib/coderay/styles/cycnus.rb +151 -0
  63. data/lib/coderay/styles/murphy.rb +132 -0
  64. data/lib/coderay/token_classes.rb +86 -0
  65. data/lib/coderay/tokens.rb +391 -0
  66. data/lib/term/ansicolor.rb +220 -0
  67. metadata +123 -0
@@ -0,0 +1,22 @@
1
+ module CodeRay
2
+ module Encoders
3
+
4
+ # = YAML Encoder
5
+ #
6
+ # Slow.
7
+ class YAML < Encoder
8
+
9
+ register_for :yaml
10
+
11
+ FILE_EXTENSION = 'yaml'
12
+
13
+ protected
14
+ def compile tokens, options
15
+ require 'yaml'
16
+ @out = tokens.to_a.to_yaml
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,85 @@
1
+ module CodeRay
2
+
3
+ # A little hack to enable CodeRay highlighting in RedCloth.
4
+ #
5
+ # Usage:
6
+ # require 'coderay'
7
+ # require 'coderay/for_redcloth'
8
+ # RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
9
+ #
10
+ # Make sure you have RedCloth 4.0.3 activated, for example by calling
11
+ # require 'rubygems'
12
+ # before RedCloth is loaded and before calling CodeRay.for_redcloth.
13
+ module ForRedCloth
14
+
15
+ def self.install
16
+ gem 'RedCloth', '>= 4.0.3' rescue nil
17
+ require 'redcloth'
18
+ unless RedCloth::VERSION.to_s >= '4.0.3'
19
+ raise 'CodeRay.for_redcloth needs RedCloth version 4.0.3 or later.'
20
+ end
21
+ RedCloth::TextileDoc.send :include, ForRedCloth::TextileDoc
22
+ RedCloth::Formatters::HTML.module_eval do
23
+ def unescape(html)
24
+ replacements = {
25
+ '&amp;' => '&',
26
+ '&quot;' => '"',
27
+ '&gt;' => '>',
28
+ '&lt;' => '<',
29
+ }
30
+ html.gsub(/&(?:amp|quot|[gl]t);/) { |entity| replacements[entity] }
31
+ end
32
+ undef code, bc_open, bc_close, escape_pre
33
+ def code(opts) # :nodoc:
34
+ opts[:block] = true
35
+ if !opts[:lang] && RedCloth::VERSION.to_s >= '4.2.0'
36
+ # simulating pre-4.2 behavior
37
+ if opts[:text].sub!(/\A\[(\w+)\]/, '')
38
+ if CodeRay::Scanners[$1].plugin_id == 'plaintext'
39
+ opts[:text] = $& + opts[:text]
40
+ else
41
+ opts[:lang] = $1
42
+ end
43
+ end
44
+ end
45
+ if opts[:lang] && !filter_coderay
46
+ require 'coderay'
47
+ @in_bc ||= nil
48
+ format = @in_bc ? :div : :span
49
+ opts[:text] = unescape(opts[:text]) unless @in_bc
50
+ highlighted_code = CodeRay.encode opts[:text], opts[:lang], format, :stream => true
51
+ highlighted_code.sub!(/\A<(span|div)/) { |m| m + pba(@in_bc || opts) }
52
+ highlighted_code
53
+ else
54
+ "<code#{pba(opts)}>#{opts[:text]}</code>"
55
+ end
56
+ end
57
+ def bc_open(opts) # :nodoc:
58
+ opts[:block] = true
59
+ @in_bc = opts
60
+ opts[:lang] ? '' : "<pre#{pba(opts)}>"
61
+ end
62
+ def bc_close(opts) # :nodoc:
63
+ opts = @in_bc
64
+ @in_bc = nil
65
+ opts[:lang] ? '' : "</pre>\n"
66
+ end
67
+ def escape_pre(text)
68
+ if @in_bc ||= nil
69
+ text
70
+ else
71
+ html_esc(text, :html_escape_preformatted)
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ module TextileDoc # :nodoc:
78
+ attr_accessor :filter_coderay
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ CodeRay::ForRedCloth.install
@@ -0,0 +1,240 @@
1
+ #!/usr/bin/env ruby
2
+ module CodeRay
3
+
4
+ # = FileType
5
+ #
6
+ # A simple filetype recognizer.
7
+ #
8
+ # Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de>
9
+ #
10
+ # License:: LGPL / ask the author
11
+ # Version:: 0.1 (2005-09-01)
12
+ #
13
+ # == Documentation
14
+ #
15
+ # # determine the type of the given
16
+ # lang = FileType[ARGV.first]
17
+ #
18
+ # # return :plaintext if the file type is unknown
19
+ # lang = FileType.fetch ARGV.first, :plaintext
20
+ #
21
+ # # try the shebang line, too
22
+ # lang = FileType.fetch ARGV.first, :plaintext, true
23
+ module FileType
24
+
25
+ UnknownFileType = Class.new Exception
26
+
27
+ class << self
28
+
29
+ # Try to determine the file type of the file.
30
+ #
31
+ # +filename+ is a relative or absolute path to a file.
32
+ #
33
+ # The file itself is only accessed when +read_shebang+ is set to true.
34
+ # That means you can get filetypes from files that don't exist.
35
+ def [] filename, read_shebang = false
36
+ name = File.basename filename
37
+ ext = File.extname(name).sub(/^\./, '') # from last dot, delete the leading dot
38
+ ext2 = filename.to_s[/\.(.*)/, 1] # from first dot
39
+
40
+ type =
41
+ TypeFromExt[ext.downcase] ||
42
+ (TypeFromExt[ext2.downcase] if ext2) ||
43
+ TypeFromName[name] ||
44
+ TypeFromName[name.downcase]
45
+ type ||= shebang(filename) if read_shebang
46
+
47
+ type
48
+ end
49
+
50
+ def shebang filename
51
+ begin
52
+ File.open filename, 'r' do |f|
53
+ if first_line = f.gets
54
+ if type = first_line[TypeFromShebang]
55
+ type.to_sym
56
+ end
57
+ end
58
+ end
59
+ rescue IOError
60
+ nil
61
+ end
62
+ end
63
+
64
+ # This works like Hash#fetch.
65
+ #
66
+ # If the filetype cannot be found, the +default+ value
67
+ # is returned.
68
+ def fetch filename, default = nil, read_shebang = false
69
+ if default and block_given?
70
+ warn 'block supersedes default value argument'
71
+ end
72
+
73
+ unless type = self[filename, read_shebang]
74
+ return yield if block_given?
75
+ return default if default
76
+ raise UnknownFileType, 'Could not determine type of %p.' % filename
77
+ end
78
+ type
79
+ end
80
+
81
+ end
82
+
83
+ TypeFromExt = {
84
+ 'c' => :c,
85
+ 'cpp' => :cpp,
86
+ 'css' => :css,
87
+ 'diff' => :diff,
88
+ 'dpr' => :delphi,
89
+ 'groovy' => :groovy,
90
+ 'gvy' => :groovy,
91
+ 'h' => :c,
92
+ 'htm' => :html,
93
+ 'html' => :html,
94
+ 'html.erb' => :rhtml,
95
+ 'java' => :java,
96
+ 'js' => :java_script,
97
+ 'json' => :json,
98
+ 'mab' => :ruby,
99
+ 'pas' => :delphi,
100
+ 'patch' => :diff,
101
+ 'php' => :php,
102
+ 'php3' => :php,
103
+ 'php4' => :php,
104
+ 'php5' => :php,
105
+ 'py' => :python,
106
+ 'py3' => :python,
107
+ 'pyw' => :python,
108
+ 'rake' => :ruby,
109
+ 'raydebug' => :debug,
110
+ 'rb' => :ruby,
111
+ 'rbw' => :ruby,
112
+ 'rhtml' => :rhtml,
113
+ 'rxml' => :ruby,
114
+ 'sch' => :scheme,
115
+ 'sql' => :sql,
116
+ 'ss' => :scheme,
117
+ 'xhtml' => :xhtml,
118
+ 'xml' => :xml,
119
+ 'yaml' => :yaml,
120
+ 'yml' => :yaml,
121
+ }
122
+
123
+ TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
124
+
125
+ TypeFromName = {
126
+ 'Rakefile' => :ruby,
127
+ 'Rantfile' => :ruby,
128
+ }
129
+
130
+ end
131
+
132
+ end
133
+
134
+ if $0 == __FILE__
135
+ $VERBOSE = true
136
+ eval DATA.read, nil, $0, __LINE__ + 4
137
+ end
138
+
139
+ __END__
140
+ require 'test/unit'
141
+
142
+ class FileTypeTests < Test::Unit::TestCase
143
+
144
+ include CodeRay
145
+
146
+ def test_fetch
147
+ assert_raise FileType::UnknownFileType do
148
+ FileType.fetch ''
149
+ end
150
+
151
+ assert_throws :not_found do
152
+ FileType.fetch '.' do
153
+ throw :not_found
154
+ end
155
+ end
156
+
157
+ assert_equal :default, FileType.fetch('c', :default)
158
+
159
+ stderr, fake_stderr = $stderr, Object.new
160
+ $err = ''
161
+ def fake_stderr.write x
162
+ $err << x
163
+ end
164
+ $stderr = fake_stderr
165
+ FileType.fetch('c', :default) { }
166
+ assert_equal "block supersedes default value argument\n", $err
167
+ $stderr = stderr
168
+ end
169
+
170
+ def test_ruby
171
+ assert_equal :ruby, FileType['test.rb']
172
+ assert_equal :ruby, FileType['test.java.rb']
173
+ assert_equal :java, FileType['test.rb.java']
174
+ assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
175
+ assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
176
+ assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
177
+ assert_equal :ruby, FileType['./lib/tasks\repository.rake']
178
+ assert_not_equal :ruby, FileType['test_rb']
179
+ assert_not_equal :ruby, FileType['Makefile']
180
+ assert_not_equal :ruby, FileType['set.rb/set']
181
+ assert_not_equal :ruby, FileType['~/projects/blabla/rb']
182
+ end
183
+
184
+ def test_c
185
+ assert_equal :c, FileType['test.c']
186
+ assert_equal :c, FileType['C:\\Program Files\\x\\y\\c\\test.h']
187
+ assert_not_equal :c, FileType['test_c']
188
+ assert_not_equal :c, FileType['Makefile']
189
+ assert_not_equal :c, FileType['set.h/set']
190
+ assert_not_equal :c, FileType['~/projects/blabla/c']
191
+ end
192
+
193
+ def test_html
194
+ assert_equal :html, FileType['test.htm']
195
+ assert_equal :xhtml, FileType['test.xhtml']
196
+ assert_equal :xhtml, FileType['test.html.xhtml']
197
+ assert_equal :rhtml, FileType['_form.rhtml']
198
+ assert_equal :rhtml, FileType['_form.html.erb']
199
+ end
200
+
201
+ def test_yaml
202
+ assert_equal :yaml, FileType['test.yml']
203
+ assert_equal :yaml, FileType['test.yaml']
204
+ assert_equal :yaml, FileType['my.html.yaml']
205
+ assert_not_equal :yaml, FileType['YAML']
206
+ end
207
+
208
+ def test_pathname
209
+ require 'pathname'
210
+ pn = Pathname.new 'test.rb'
211
+ assert_equal :ruby, FileType[pn]
212
+ dir = Pathname.new '/etc/var/blubb'
213
+ assert_equal :ruby, FileType[dir + pn]
214
+ assert_equal :cpp, FileType[dir + 'test.cpp']
215
+ end
216
+
217
+ def test_no_shebang
218
+ dir = './test'
219
+ if File.directory? dir
220
+ Dir.chdir dir do
221
+ assert_equal :c, FileType['test.c']
222
+ end
223
+ end
224
+ end
225
+
226
+ def test_shebang_empty_file
227
+ require 'tmpdir'
228
+ tmpfile = File.join(Dir.tmpdir, 'bla')
229
+ File.open(tmpfile, 'w') { } # touch
230
+ assert_equal nil, FileType[tmpfile]
231
+ end
232
+
233
+ def test_shebang
234
+ require 'tmpdir'
235
+ tmpfile = File.join(Dir.tmpdir, 'bla')
236
+ File.open(tmpfile, 'w') { |f| f.puts '#!/usr/bin/env ruby' }
237
+ assert_equal :ruby, FileType[tmpfile, true]
238
+ end
239
+
240
+ end
@@ -0,0 +1,123 @@
1
+ # =GZip Simple
2
+ #
3
+ # A simplified interface to the gzip library +zlib+ (from the Ruby Standard Library.)
4
+ #
5
+ # Author: murphy (mail to murphy rubychan de)
6
+ #
7
+ # Version: 0.2 (2005.may.28)
8
+ #
9
+ # ==Documentation
10
+ #
11
+ # See +GZip+ module and the +String+ extensions.
12
+ #
13
+ module GZip
14
+
15
+ require 'zlib'
16
+
17
+ # The default zipping level. 7 zips good and fast.
18
+ DEFAULT_GZIP_LEVEL = 7
19
+
20
+ # Unzips the given string +s+.
21
+ #
22
+ # Example:
23
+ # require 'gzip_simple'
24
+ # print GZip.gunzip(File.read('adresses.gz'))
25
+ def GZip.gunzip s
26
+ Zlib::Inflate.inflate s
27
+ end
28
+
29
+ # Zips the given string +s+.
30
+ #
31
+ # Example:
32
+ # require 'gzip_simple'
33
+ # File.open('adresses.gz', 'w') do |file
34
+ # file.write GZip.gzip('Mum: 0123 456 789', 9)
35
+ # end
36
+ #
37
+ # If you provide a +level+, you can control how strong
38
+ # the string is compressed:
39
+ # - 0: no compression, only convert to gzip format
40
+ # - 1: compress fast
41
+ # - 7: compress more, but still fast (default)
42
+ # - 8: compress more, slower
43
+ # - 9: compress best, very slow
44
+ def GZip.gzip s, level = DEFAULT_GZIP_LEVEL
45
+ Zlib::Deflate.new(level).deflate s, Zlib::FINISH
46
+ end
47
+ end
48
+
49
+
50
+ # String extensions to use the GZip module.
51
+ #
52
+ # The methods gzip and gunzip provide an even more simple
53
+ # interface to the ZLib:
54
+ #
55
+ # # create a big string
56
+ # x = 'a' * 1000
57
+ #
58
+ # # zip it
59
+ # x_gz = x.gzip
60
+ #
61
+ # # test the result
62
+ # puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size]
63
+ # #-> Zipped 1000 bytes to 19 bytes.
64
+ #
65
+ # # unzipping works
66
+ # p x_gz.gunzip == x #-> true
67
+ class String
68
+ # Returns the string, unzipped.
69
+ # See GZip.gunzip
70
+ def gunzip
71
+ GZip.gunzip self
72
+ end
73
+ # Replaces the string with its unzipped value.
74
+ # See GZip.gunzip
75
+ def gunzip!
76
+ replace gunzip
77
+ end
78
+
79
+ # Returns the string, zipped.
80
+ # +level+ is the gzip compression level, see GZip.gzip.
81
+ def gzip level = GZip::DEFAULT_GZIP_LEVEL
82
+ GZip.gzip self, level
83
+ end
84
+ # Replaces the string with its zipped value.
85
+ # See GZip.gzip.
86
+ def gzip!(*args)
87
+ replace gzip(*args)
88
+ end
89
+ end
90
+
91
+ if $0 == __FILE__
92
+ eval DATA.read, nil, $0, __LINE__+4
93
+ end
94
+
95
+ __END__
96
+ #CODE
97
+
98
+ # Testing / Benchmark
99
+ x = 'a' * 1000
100
+ x_gz = x.gzip
101
+ puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size] #-> Zipped 1000 bytes to 19 bytes.
102
+ p x_gz.gunzip == x #-> true
103
+
104
+ require 'benchmark'
105
+
106
+ INFO = 'packed to %0.3f%%' # :nodoc:
107
+
108
+ x = Array.new(100000) { rand(255).chr + 'aaaaaaaaa' + rand(255).chr }.join
109
+ Benchmark.bm(10) do |bm|
110
+ for level in 0..9
111
+ bm.report "zip #{level}" do
112
+ $x = x.gzip level
113
+ end
114
+ puts INFO % [100.0 * $x.size / x.size]
115
+ end
116
+ bm.report 'zip' do
117
+ $x = x.gzip
118
+ end
119
+ puts INFO % [100.0 * $x.size / x.size]
120
+ bm.report 'unzip' do
121
+ $x.gunzip
122
+ end
123
+ end