coderay 0.7.2.176 → 0.7.4.196

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +0,0 @@
1
- require 'coderay'
2
-
3
- # scan this file
4
- tokens = CodeRay.scan(File.read($0) * 1, :ruby)
5
-
6
- # output it with two styles of line numbers
7
- out = tokens.div(:line_numbers => :table)
8
- out << '<hr />'
9
- out << tokens.div(:line_numbers => :inline, :line_number_start => 8)
10
-
11
- puts out.page
@@ -1,12 +0,0 @@
1
- $: << '..'
2
- require 'coderay'
3
-
4
- tokens = CodeRay.scan File.read(__FILE__), :ruby
5
- html = tokens.html(:tab_width => 2, :line_numbers => :inline, :line_number_start => -1)
6
-
7
- puts html.page
8
-
9
- commment = <<_
10
- This code must be > 10 lines
11
- because I want to test the correct adjustment of the line numbers.
12
- _
@@ -1,25 +0,0 @@
1
- require 'coderay'
2
-
3
- begin
4
- CodeRay::Encoders::YAML
5
- rescue
6
- puts 'CodeRay::Encoders::YAML is not defined; you must load it first.'
7
- end
8
-
9
- yaml_encoder = CodeRay::Encoders[:yaml]
10
- print 'Now it is loaded: '
11
- p yaml_encoder
12
- puts 'See?'
13
-
14
- tokens_encoder = require_plugin 'CodeRay::Encoders/tokens'
15
- print 'Require is also possible: '
16
- p tokens_encoder
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 }
@@ -1,25 +0,0 @@
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 }
@@ -1,205 +0,0 @@
1
- require 'coderay'
2
-
3
- c, ruby = DATA.read.split(/^---$/)
4
- DATA.rewind
5
- me = DATA.read[/.*^__END__$/m]
6
- $input = c + ruby + me
7
-
8
- require 'benchmark'
9
- time = Benchmark.realtime do
10
-
11
- # here CodeRay comes to play
12
- hl = CodeRay.encoder(:html, :tab_width => 2, :line_numbers => :table, :wrap => :div)
13
- c = hl.highlight c, :c
14
- ruby = hl.highlight ruby, :ruby
15
- me = hl.highlight me, :ruby
16
-
17
- body = %w[C Ruby Genereated\ by].zip([c, ruby, me]).map do |title, code|
18
- "<h1>#{title}</h1>\n#{code}"
19
- end.join
20
- body = hl.class::Output.new(body, hl.css, :div).page!
21
-
22
- # CodeRay also provides a simple page generator
23
- $output = body #hl.class.wrap_in_page body
24
- end
25
-
26
- File.open('test.html', 'w') do |f|
27
- f.write $output
28
- end
29
- puts 'Input: %dB, Output: %dB' % [$input.size, $output.size]
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.'
32
-
33
- __END__
34
- /**********************************************************************
35
-
36
- version.c -
37
-
38
- $Author: nobu $
39
- $Date: 2004/03/25 12:01:40 $
40
- created at: Thu Sep 30 20:08:01 JST 1993
41
-
42
- Copyright (C) 1993-2003 Yukihiro Matsumoto
43
-
44
- **********************************************************************/
45
-
46
- #include "ruby.h"
47
- #include "version.h"
48
- #include <stdio.h>
49
-
50
- const char ruby_version[] = RUBY_VERSION;
51
- const char ruby_release_date[] = RUBY_RELEASE_DATE;
52
- const char ruby_platform[] = RUBY_PLATFORM;
53
-
54
- void
55
- Init_version()
56
- {
57
- VALUE v = rb_obj_freeze(rb_str_new2(ruby_version));
58
- VALUE d = rb_obj_freeze(rb_str_new2(ruby_release_date));
59
- VALUE p = rb_obj_freeze(rb_str_new2(ruby_platform));
60
-
61
- rb_define_global_const("RUBY_VERSION", v);
62
- rb_define_global_const("RUBY_RELEASE_DATE", d);
63
- rb_define_global_const("RUBY_PLATFORM", p);
64
- }
65
-
66
- void
67
- ruby_show_version()
68
- {
69
- printf("ruby %s (%s) [%s]\n", RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_PLATFORM);
70
- }
71
-
72
- void
73
- ruby_show_copyright()
74
- {
75
- printf("ruby - Copyright (C) 1993-%d Yukihiro Matsumoto\n", RUBY_RELEASE_YEAR);
76
- exit(0);
77
- }
78
- ---
79
- #
80
- # = ostruct.rb: OpenStruct implementation
81
- #
82
- # Author:: Yukihiro Matsumoto
83
- # Documentation:: Gavin Sinclair
84
- #
85
- # OpenStruct allows the creation of data objects with arbitrary attributes.
86
- # See OpenStruct for an example.
87
- #
88
-
89
- #
90
- # OpenStruct allows you to create data objects and set arbitrary attributes.
91
- # For example:
92
- #
93
- # require 'ostruct'
94
- #
95
- # record = OpenStruct.new
96
- # record.name = "John Smith"
97
- # record.age = 70
98
- # record.pension = 300
99
- #
100
- # puts record.name # -> "John Smith"
101
- # puts record.address # -> nil
102
- #
103
- # It is like a hash with a different way to access the data. In fact, it is
104
- # implemented with a hash, and you can initialize it with one.
105
- #
106
- # hash = { "country" => "Australia", :population => 20_000_000 }
107
- # data = OpenStruct.new(hash)
108
- #
109
- # p data # -> <OpenStruct country="Australia" population=20000000>
110
- #
111
- class OpenStruct
112
- #
113
- # Create a new OpenStruct object. The optional +hash+, if given, will
114
- # generate attributes and values. For example.
115
- #
116
- # require 'ostruct'
117
- # hash = { "country" => "Australia", :population => 20_000_000 }
118
- # data = OpenStruct.new(hash)
119
- #
120
- # p data # -> <OpenStruct country="Australia" population=20000000>
121
- #
122
- # By default, the resulting OpenStruct object will have no attributes.
123
- #
124
- def initialize(hash=nil)
125
- @table = {}
126
- if hash
127
- for k,v in hash
128
- @table[k.to_sym] = v
129
- new_ostruct_member(k)
130
- end
131
- end
132
- end
133
-
134
- # Duplicate an OpenStruct object members.
135
- def initialize_copy(orig)
136
- super
137
- @table = @table.dup
138
- end
139
-
140
- def marshal_dump
141
- @table
142
- end
143
- def marshal_load(x)
144
- @table = x
145
- @table.each_key{|key| new_ostruct_member(key)}
146
- end
147
-
148
- def new_ostruct_member(name)
149
- unless self.respond_to?(name)
150
- self.instance_eval %{
151
- def #{name}; @table[:#{name}]; end
152
- def #{name}=(x); @table[:#{name}] = x; end
153
- }
154
- end
155
- end
156
-
157
- def method_missing(mid, *args) # :nodoc:
158
- mname = mid.id2name
159
- len = args.length
160
- if mname =~ /=$/
161
- if len != 1
162
- raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
163
- end
164
- if self.frozen?
165
- raise TypeError, "can't modify frozen #{self.class}", caller(1)
166
- end
167
- mname.chop!
168
- @table[mname.intern] = args[0]
169
- self.new_ostruct_member(mname)
170
- elsif len == 0
171
- @table[mid]
172
- else
173
- raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
174
- end
175
- end
176
-
177
- #
178
- # Remove the named field from the object.
179
- #
180
- def delete_field(name)
181
- @table.delete name.to_sym
182
- end
183
-
184
- #
185
- # Returns a string containing a detailed summary of the keys and values.
186
- #
187
- def inspect
188
- str = "<#{self.class}"
189
- for k,v in @table
190
- str << " #{k}=#{v.inspect}"
191
- end
192
- str << ">"
193
- end
194
-
195
- attr_reader :table # :nodoc:
196
- protected :table
197
-
198
- #
199
- # Compare this object and +other+ for equality.
200
- #
201
- def ==(other)
202
- return false unless(other.kind_of?(OpenStruct))
203
- return @table == other.table
204
- end
205
- end
@@ -1,36 +0,0 @@
1
- require 'coderay'
2
-
3
- c_code = "if (*p == '{') nest++;"
4
- puts 'C Code: ' + c_code
5
- puts
6
-
7
- c_scanner = CodeRay::Scanners[:c].new c_code
8
-
9
- puts '> print only operators:'
10
- for text, kind in c_scanner
11
- print text if kind == :operator
12
- end
13
- puts
14
- puts '-' * 30
15
- puts
16
-
17
- ruby_code = %q!ruby_code(:can, BE, %r[q[ui]te #{ /comple/x },] => $-s, &?\xee)!
18
- puts 'Ruby Code: ' + ruby_code
19
- puts
20
-
21
- ruby_scanner = CodeRay::Scanners[:ruby].new ruby_code
22
-
23
- puts '> has a string?'
24
- puts ruby_scanner.
25
- any? { |text, kind| kind == :string }
26
- puts
27
-
28
- puts '> number of regexps?'
29
- puts ruby_scanner.
30
- select { |token| token == [:open, :regexp] }.size
31
- puts
32
-
33
- puts '> has a string?'
34
- puts ruby_scanner.
35
- reject { |text, kind| not text.is_a? String }.
36
- map { |text, kind| %("#{text}" (#{kind})) }.join(', ')
@@ -1,110 +0,0 @@
1
- # CodeRay dynamic highlighter
2
-
3
- unless ARGV.grep(/-[hv]|--(help|version)/).empty?
4
- puts <<-USAGE
5
- CodeRay Server 0.5
6
- $Id: demo_server.rb 52 2006-03-15 23:14:27Z 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
22
-
23
- require 'webrick'
24
- require 'pathname'
25
-
26
- class << File
27
- alias dir? directory?
28
- end
29
-
30
- require 'erb'
31
- include ERB::Util
32
- def url_decode s
33
- s.to_s.gsub(/%([0-9a-f]{2})/i) { [$1.hex].pack 'C' }
34
- end
35
-
36
- class String
37
- def to_link name = File.basename(self)
38
- "<a href=\"?path=#{url_encode self}\">#{name}</a>"
39
- end
40
- end
41
-
42
- require 'coderay'
43
- class CodeRayServlet < WEBrick::HTTPServlet::AbstractServlet
44
-
45
- STYLE = 'style="font-family: sans-serif; color: navy;"'
46
- BANNER = '<p><img src="http://rd.cYcnus.de/coderay/coderay-banner" style="border: 0" alt="Highlighted by CodeRay"/></p>'
47
-
48
- def do_GET req, res
49
- q = req.query_string || ''
50
- args = Hash[*q.scan(/(.*?)=(.*?)(?:&|$)/).flatten].each_value { |v| v.replace url_decode(v) }
51
- path = args.fetch 'path', '.'
52
-
53
- backlinks = '<p>current path: %s<br />' % html_escape(path) +
54
- (Pathname.new(path) + '..').cleanpath.to_s.to_link('up') + ' - ' +
55
- '.'.to_link('current') + '</p>'
56
-
57
- res.body =
58
- if File.dir? path
59
- path = Pathname.new(path).cleanpath.to_s
60
- dirs, files = Dir[File.join(path, '*')].sort.partition { |p| File.dir? p }
61
-
62
- page = "<html><head></head><body #{STYLE}>"
63
- page << backlinks
64
-
65
- page << '<dl>'
66
- page << "<dt>Directories</dt>\n" + dirs.map do |p|
67
- "<dd>#{p.to_link}</dd>\n"
68
- end.join << "\n"
69
- page << "<dt>Files</dt>\n" + files.map do |p|
70
- "<dd>#{p.to_link}</dd>\n"
71
- end.join << "\n"
72
- page << "</dl>\n"
73
- page << "#{BANNER}</body></html>"
74
-
75
- elsif File.exist? path
76
- if $DEBUG
77
- $".delete_if { |f| f =~ /coderay/ }
78
- require 'coderay'
79
- end
80
- div = CodeRay.scan_file(path).html :tab_width => 8, :wrap => :div, :hint => :info
81
- div.replace <<-DIV
82
- <div #{STYLE}>
83
- #{backlinks}
84
- #{div}
85
- </div>
86
- #{BANNER}
87
- DIV
88
- div.page
89
- end
90
-
91
- res['Content-Type'] = 'text/html'
92
- end
93
- end
94
-
95
- # This port is taken by "qip_msgd" - I don't know that. Do you?
96
- module CodeRay
97
- PORT = 0xC0DE / 20
98
- end
99
-
100
- server = WEBrick::HTTPServer.new :Port => CodeRay::PORT
101
-
102
- server.mount '/', CodeRayServlet
103
-
104
- server.mount_proc '/version' do |req, res|
105
- res.body = 'CodeRay::Version = ' + CodeRay::Version
106
- res['Content-Type'] = "text/plain"
107
- end
108
-
109
- trap("INT") { server.shutdown }
110
- server.start
@@ -1,10 +0,0 @@
1
-
2
- # Load CodeRay
3
- # If this doesn't work, try ruby -rubygems.
4
- require 'coderay'
5
-
6
- # Generate HTML page for Ruby code.
7
- page = CodeRay.scan("puts 'Hello, world!'", :ruby).span
8
-
9
- # Print it
10
- puts page
@@ -1,25 +0,0 @@
1
- require 'coderay'
2
-
3
- code = File.read($0) * 500
4
- puts "Size of code: %d KB" % [code.size / 1024]
5
-
6
- puts "Use your system's memory tracker to see how much RAM this takes."
7
- print 'Press some key to continue...'; gets
8
-
9
- require 'benchmark'
10
- e = CodeRay.encoder(:div)
11
- for do_stream in [true, false]
12
- puts "Scanning and encoding in %s mode, please wait..." %
13
- [do_stream ? 'streaming' : 'normal']
14
- output = ''
15
- time = Benchmark.realtime do
16
- if do_stream
17
- output = e.encode_stream(code, :ruby)
18
- else
19
- output = e.encode_tokens(t = CodeRay.scan(code, :ruby))
20
- end
21
- end
22
- puts 'Finished after %4.2f seconds.' % time
23
- puts "Size of output: %d KB" % [output.size / 1024]
24
- print 'Press some key to continue...'; gets
25
- end