coderay 0.9.8 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{lib/README → README_INDEX.rdoc} +10 -21
- data/Rakefile +6 -6
- data/bin/coderay +193 -64
- data/lib/coderay.rb +61 -105
- data/lib/coderay/duo.rb +17 -21
- data/lib/coderay/encoder.rb +100 -112
- data/lib/coderay/encoders/_map.rb +12 -7
- data/lib/coderay/encoders/comment_filter.rb +12 -30
- data/lib/coderay/encoders/count.rb +29 -11
- data/lib/coderay/encoders/debug.rb +32 -20
- data/lib/coderay/encoders/div.rb +13 -9
- data/lib/coderay/encoders/filter.rb +34 -51
- data/lib/coderay/encoders/html.rb +155 -161
- data/lib/coderay/encoders/html/css.rb +4 -9
- data/lib/coderay/encoders/html/numbering.rb +115 -0
- data/lib/coderay/encoders/html/output.rb +22 -70
- data/lib/coderay/encoders/json.rb +59 -45
- data/lib/coderay/encoders/lines_of_code.rb +12 -57
- data/lib/coderay/encoders/null.rb +6 -14
- data/lib/coderay/encoders/page.rb +13 -9
- data/lib/coderay/encoders/span.rb +13 -9
- data/lib/coderay/encoders/statistic.rb +58 -39
- data/lib/coderay/encoders/terminal.rb +179 -0
- data/lib/coderay/encoders/text.rb +31 -17
- data/lib/coderay/encoders/token_kind_filter.rb +111 -0
- data/lib/coderay/encoders/xml.rb +19 -18
- data/lib/coderay/encoders/yaml.rb +37 -9
- data/lib/coderay/for_redcloth.rb +4 -4
- data/lib/coderay/helpers/file_type.rb +127 -246
- data/lib/coderay/helpers/gzip.rb +41 -0
- data/lib/coderay/helpers/plugin.rb +241 -306
- data/lib/coderay/helpers/word_list.rb +65 -126
- data/lib/coderay/scanner.rb +173 -156
- data/lib/coderay/scanners/_map.rb +18 -17
- data/lib/coderay/scanners/c.rb +63 -77
- data/lib/coderay/scanners/clojure.rb +217 -0
- data/lib/coderay/scanners/cpp.rb +71 -84
- data/lib/coderay/scanners/css.rb +103 -120
- data/lib/coderay/scanners/debug.rb +47 -44
- data/lib/coderay/scanners/delphi.rb +70 -76
- data/lib/coderay/scanners/diff.rb +141 -50
- data/lib/coderay/scanners/erb.rb +81 -0
- data/lib/coderay/scanners/groovy.rb +104 -113
- data/lib/coderay/scanners/haml.rb +168 -0
- data/lib/coderay/scanners/html.rb +181 -110
- data/lib/coderay/scanners/java.rb +73 -75
- data/lib/coderay/scanners/java/builtin_types.rb +2 -0
- data/lib/coderay/scanners/java_script.rb +90 -101
- data/lib/coderay/scanners/json.rb +40 -53
- data/lib/coderay/scanners/php.rb +123 -147
- data/lib/coderay/scanners/python.rb +93 -91
- data/lib/coderay/scanners/raydebug.rb +66 -0
- data/lib/coderay/scanners/ruby.rb +343 -326
- data/lib/coderay/scanners/ruby/patterns.rb +40 -106
- data/lib/coderay/scanners/ruby/string_state.rb +71 -0
- data/lib/coderay/scanners/sql.rb +80 -66
- data/lib/coderay/scanners/text.rb +26 -0
- data/lib/coderay/scanners/xml.rb +1 -1
- data/lib/coderay/scanners/yaml.rb +74 -73
- data/lib/coderay/style.rb +10 -7
- data/lib/coderay/styles/_map.rb +3 -3
- data/lib/coderay/styles/alpha.rb +143 -0
- data/lib/coderay/token_kinds.rb +90 -0
- data/lib/coderay/tokens.rb +102 -277
- data/lib/coderay/tokens_proxy.rb +55 -0
- data/lib/coderay/version.rb +3 -0
- data/test/functional/basic.rb +200 -18
- data/test/functional/examples.rb +130 -0
- data/test/functional/for_redcloth.rb +15 -8
- data/test/functional/suite.rb +9 -6
- metadata +103 -123
- data/FOLDERS +0 -53
- data/bin/coderay_stylesheet +0 -4
- data/lib/coderay/encoders/html/numerization.rb +0 -133
- data/lib/coderay/encoders/term.rb +0 -158
- data/lib/coderay/encoders/token_class_filter.rb +0 -84
- data/lib/coderay/helpers/gzip_simple.rb +0 -123
- data/lib/coderay/scanners/nitro_xhtml.rb +0 -136
- data/lib/coderay/scanners/plaintext.rb +0 -20
- data/lib/coderay/scanners/rhtml.rb +0 -78
- data/lib/coderay/scanners/scheme.rb +0 -145
- data/lib/coderay/styles/cycnus.rb +0 -152
- data/lib/coderay/styles/murphy.rb +0 -134
- data/lib/coderay/token_classes.rb +0 -86
- data/test/functional/load_plugin_scanner.rb +0 -11
- data/test/functional/vhdl.rb +0 -126
- data/test/functional/word_list.rb +0 -79
@@ -1,57 +1,45 @@
|
|
1
1
|
= CodeRay
|
2
2
|
|
3
|
-
|
4
|
-
coderay.rubychan.de[http://coderay.rubychan.de/doc/]
|
3
|
+
Tired of blue'n'gray? Try the original version of this documentation on
|
4
|
+
coderay.rubychan.de[http://coderay.rubychan.de/doc/] :-)
|
5
5
|
|
6
6
|
== About
|
7
|
+
|
7
8
|
CodeRay is a Ruby library for syntax highlighting.
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
And with line numbers.
|
10
|
+
You put your code in, and you get it back colored; Keywords, strings,
|
11
|
+
floats, comments - all in different colors. And with line numbers.
|
12
12
|
|
13
13
|
*Syntax* *Highlighting*...
|
14
14
|
* makes code easier to read and maintain
|
15
15
|
* lets you detect syntax errors faster
|
16
16
|
* helps you to understand the syntax of a language
|
17
17
|
* looks nice
|
18
|
-
* is what everybody
|
18
|
+
* is what everybody wants to have on their website
|
19
19
|
* solves all your problems and makes the girls run after you
|
20
20
|
|
21
|
-
Version: 0.9.8
|
22
|
-
Author:: murphy (Kornelius Kalnbach)
|
23
|
-
Contact:: murphy rubychan de
|
24
|
-
Website:: coderay.rubychan.de[http://coderay.rubychan.de]
|
25
|
-
License:: GNU LGPL; see LICENSE file in the main directory.
|
26
21
|
|
27
22
|
== Installation
|
28
23
|
|
29
|
-
You need RubyGems[http://rubyforge.org/frs/?group_id=126].
|
30
|
-
|
31
24
|
% gem install coderay
|
32
25
|
|
33
26
|
|
34
27
|
=== Dependencies
|
35
28
|
|
36
|
-
CodeRay needs Ruby 1.8.
|
29
|
+
CodeRay needs Ruby 1.8.7+ or 1.9.2+. It also runs on Rubinius and JRuby.
|
37
30
|
|
38
31
|
|
39
32
|
== Example Usage
|
40
|
-
(Forgive me, but this is not highlighted.)
|
41
33
|
|
42
34
|
require 'coderay'
|
43
35
|
|
44
|
-
|
45
|
-
page = tokens.html :line_numbers => :inline, :wrap => :page
|
46
|
-
puts page
|
36
|
+
html = CodeRay.scan("puts 'Hello, world!'", :ruby).div(:line_numbers => :table)
|
47
37
|
|
48
38
|
|
49
39
|
== Documentation
|
50
40
|
|
51
41
|
See CodeRay.
|
52
42
|
|
53
|
-
Please report errors in this documentation to <murphy rubychan de>.
|
54
|
-
|
55
43
|
|
56
44
|
== Credits
|
57
45
|
|
@@ -94,7 +82,6 @@ Please report errors in this documentation to <murphy rubychan de>.
|
|
94
82
|
* Rob Aldred for the terminal encoder
|
95
83
|
* Trans for pointing out $DEBUG dependencies
|
96
84
|
* Flameeyes for finding that Term::ANSIColor was obsolete
|
97
|
-
* Etienne Massip for reporting a serious bug in JavaScript scanner
|
98
85
|
* matz and all Ruby gods and gurus
|
99
86
|
* The inventors of: the computer, the internet, the true color display, HTML &
|
100
87
|
CSS, VIM, Ruby, pizza, microwaves, guitars, scouting, programming, anime,
|
@@ -124,6 +111,8 @@ Where would we be without all those people?
|
|
124
111
|
less useless
|
125
112
|
* Term::ANSIColor[http://term-ansicolor.rubyforge.org/]
|
126
113
|
* PLEAC[http://pleac.sourceforge.net/] code examples
|
114
|
+
* Github
|
115
|
+
* Travis CI (http://travis-ci.org/rubychan/github)
|
127
116
|
|
128
117
|
=== Free
|
129
118
|
|
data/Rakefile
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
|
1
|
+
$:.unshift File.dirname(__FILE__) unless $:.include? '.'
|
2
2
|
|
3
3
|
ROOT = '.'
|
4
4
|
LIB_ROOT = File.join ROOT, 'lib'
|
5
|
-
EXTRA_RDOC_FILES = %w(lib/README FOLDERS)
|
6
5
|
|
7
6
|
task :default => :test
|
8
7
|
|
@@ -15,20 +14,21 @@ if File.directory? 'rake_tasks'
|
|
15
14
|
|
16
15
|
else
|
17
16
|
|
18
|
-
# fallback tasks when rake_tasks folder is not present
|
17
|
+
# fallback tasks when rake_tasks folder is not present (eg. in the distribution package)
|
19
18
|
desc 'Run CodeRay tests (basic)'
|
20
19
|
task :test do
|
21
20
|
ruby './test/functional/suite.rb'
|
22
21
|
ruby './test/functional/for_redcloth.rb'
|
23
22
|
end
|
24
23
|
|
24
|
+
gem 'rdoc' if defined? gem
|
25
|
+
require 'rdoc/task'
|
25
26
|
desc 'Generate documentation for CodeRay'
|
26
27
|
Rake::RDocTask.new :doc do |rd|
|
27
28
|
rd.title = 'CodeRay Documentation'
|
28
|
-
rd.main = '
|
29
|
+
rd.main = 'README_INDEX.rdoc'
|
29
30
|
rd.rdoc_files.add Dir['lib']
|
30
|
-
rd.rdoc_files.add
|
31
|
-
rd.rdoc_files.add 'FOLDERS'
|
31
|
+
rd.rdoc_files.add rd.main
|
32
32
|
rd.rdoc_dir = 'doc'
|
33
33
|
end
|
34
34
|
|
data/bin/coderay
CHANGED
@@ -1,86 +1,215 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# CodeRay Executable
|
3
|
-
#
|
4
|
-
# Version: 0.2
|
5
|
-
# Author: murphy
|
6
|
-
|
7
2
|
require 'coderay'
|
8
3
|
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
$options, args = ARGV.partition { |arg| arg[/^-[hv]$|--\w+/] }
|
5
|
+
subcommand = args.first if /^\w/ === args.first
|
6
|
+
subcommand = nil if subcommand && File.exist?(subcommand)
|
7
|
+
args.delete subcommand
|
12
8
|
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
def option? *options
|
10
|
+
!($options & options).empty?
|
11
|
+
end
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
def tty?
|
14
|
+
$stdout.tty? || option?('--tty')
|
15
|
+
end
|
20
16
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
coderay foo.rb > foo.html # HTML page output to file, based on extension
|
25
|
-
coderay -ruby < foo.rb # colorized output to terminal, based on lang
|
26
|
-
coderay -ruby -loc < foo.rb # print LOC count, based on lang
|
27
|
-
coderay -ruby -page foo.rb # HTML page output to terminal, based on lang and format
|
28
|
-
coderay -ruby -page foo.rb > foo.html # HTML page output to file, based on lang and format
|
17
|
+
def version
|
18
|
+
puts <<-USAGE
|
19
|
+
CodeRay #{CodeRay::VERSION}
|
29
20
|
USAGE
|
30
21
|
end
|
31
22
|
|
32
|
-
|
23
|
+
def help
|
24
|
+
puts <<-HELP
|
25
|
+
This is CodeRay #{CodeRay::VERSION}, a syntax highlighting tool for selected languages.
|
33
26
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
27
|
+
usage:
|
28
|
+
coderay [-language] [input] [-format] [output]
|
29
|
+
|
30
|
+
defaults:
|
31
|
+
language detect from input file name or shebang; fall back to plain text
|
32
|
+
input STDIN
|
33
|
+
format detect from output file name or use terminal; fall back to HTML
|
34
|
+
output STDOUT
|
35
|
+
|
36
|
+
common:
|
37
|
+
coderay file.rb # highlight file to terminal
|
38
|
+
coderay file.rb > file.html # highlight file to HTML page
|
39
|
+
coderay file.rb -div > file.html # highlight file to HTML snippet
|
40
|
+
|
41
|
+
configure output:
|
42
|
+
coderay file.py output.json # output tokens as JSON
|
43
|
+
coderay file.py -loc # count lines of code in Python file
|
44
|
+
|
45
|
+
configure input:
|
46
|
+
coderay -python file # specify the input language
|
47
|
+
coderay -ruby # take input from STDIN
|
48
|
+
|
49
|
+
more:
|
50
|
+
coderay stylesheet [style] # print CSS stylesheet
|
51
|
+
HELP
|
45
52
|
end
|
46
53
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
def commands
|
55
|
+
puts <<-COMMANDS
|
56
|
+
general:
|
57
|
+
highlight code highlighting (default command, optional)
|
58
|
+
stylesheet print the CSS stylesheet with the given name (aliases: style, css)
|
59
|
+
|
60
|
+
about:
|
61
|
+
list [of] list all available plugins (or just the scanners|encoders|styles|filetypes)
|
62
|
+
commands print this list
|
63
|
+
help show some help
|
64
|
+
version print CodeRay version
|
65
|
+
COMMANDS
|
66
|
+
end
|
67
|
+
|
68
|
+
def print_list_of plugin_host
|
69
|
+
plugins = plugin_host.all_plugins.map do |plugin|
|
70
|
+
info = " #{plugin.plugin_id}: #{plugin.title}"
|
71
|
+
|
72
|
+
aliases = (plugin.aliases - [:default]).map { |key| "-#{key}" }.sort_by { |key| key.size }
|
73
|
+
if plugin.respond_to?(:file_extension) || !aliases.empty?
|
74
|
+
additional_info = []
|
75
|
+
additional_info << aliases.join(', ') unless aliases.empty?
|
76
|
+
info << " (#{additional_info.join('; ')})"
|
57
77
|
end
|
58
|
-
|
78
|
+
|
79
|
+
info << ' <-- default' if plugin.aliases.include? :default
|
80
|
+
|
81
|
+
info
|
59
82
|
end
|
60
|
-
|
61
|
-
|
62
|
-
|
83
|
+
puts plugins.sort
|
84
|
+
end
|
85
|
+
|
86
|
+
if option? '-v', '--version'
|
87
|
+
version
|
88
|
+
end
|
89
|
+
|
90
|
+
if option? '-h', '--help'
|
91
|
+
help
|
63
92
|
end
|
64
93
|
|
65
|
-
|
66
|
-
|
67
|
-
|
94
|
+
case subcommand
|
95
|
+
when 'highlight', nil
|
96
|
+
if ARGV.empty?
|
97
|
+
version
|
98
|
+
help
|
68
99
|
else
|
69
|
-
|
100
|
+
signature = args.map { |arg| arg[/^-/] ? '-' : 'f' }.join
|
101
|
+
names = args.map { |arg| arg.sub(/^-/, '') }
|
102
|
+
case signature
|
103
|
+
when /^$/
|
104
|
+
exit
|
105
|
+
when /^ff?$/
|
106
|
+
input_file, output_file, = *names
|
107
|
+
when /^f-f?$/
|
108
|
+
input_file, output_format, output_file, = *names
|
109
|
+
when /^-ff?$/
|
110
|
+
input_lang, input_file, output_file, = *names
|
111
|
+
when /^-f-f?$/
|
112
|
+
input_lang, input_file, output_format, output_file, = *names
|
113
|
+
when /^--?f?$/
|
114
|
+
input_lang, output_format, output_file, = *names
|
115
|
+
else
|
116
|
+
$stdout = $stderr
|
117
|
+
help
|
118
|
+
puts
|
119
|
+
puts "Unknown parameter order: #{args.join ' '}, expected: [-language] [input] [-format] [output]"
|
120
|
+
exit 1
|
121
|
+
end
|
122
|
+
|
123
|
+
if input_file
|
124
|
+
input_lang ||= CodeRay::FileType.fetch input_file, :text, true
|
125
|
+
end
|
126
|
+
|
127
|
+
if output_file
|
128
|
+
output_format ||= CodeRay::FileType[output_file]
|
129
|
+
else
|
130
|
+
output_format ||= :terminal
|
131
|
+
end
|
132
|
+
|
133
|
+
output_format = :page if output_format.to_s == 'html'
|
134
|
+
|
135
|
+
if input_file
|
136
|
+
input = File.read input_file
|
137
|
+
else
|
138
|
+
input = $stdin.read
|
139
|
+
end
|
140
|
+
|
141
|
+
begin
|
142
|
+
file =
|
143
|
+
if output_file
|
144
|
+
File.open output_file, 'w'
|
145
|
+
else
|
146
|
+
$stdout.sync = true
|
147
|
+
$stdout
|
148
|
+
end
|
149
|
+
CodeRay.encode(input, input_lang, output_format, :out => file)
|
150
|
+
file.puts
|
151
|
+
rescue CodeRay::PluginHost::PluginNotFound => boom
|
152
|
+
$stdout = $stderr
|
153
|
+
if boom.message[/CodeRay::(\w+)s could not load plugin :?(.*?): /]
|
154
|
+
puts "I don't know the #$1 \"#$2\"."
|
155
|
+
else
|
156
|
+
puts boom.message
|
157
|
+
end
|
158
|
+
# puts "I don't know this plugin: #{boom.message[/Could not load plugin (.*?): /, 1]}."
|
159
|
+
rescue CodeRay::Scanners::Scanner::ScanError # FIXME: rescue Errno::EPIPE
|
160
|
+
# this is sometimes raised by pagers; ignore [TODO: wtf?]
|
161
|
+
ensure
|
162
|
+
file.close if output_file
|
163
|
+
end
|
164
|
+
end
|
165
|
+
when 'li', 'list'
|
166
|
+
arg = args.first && args.first.downcase
|
167
|
+
if [nil, 's', 'sc', 'scanner', 'scanners'].include? arg
|
168
|
+
puts 'input languages (Scanners):'
|
169
|
+
print_list_of CodeRay::Scanners
|
170
|
+
end
|
171
|
+
|
172
|
+
if [nil, 'e', 'en', 'enc', 'encoder', 'encoders'].include? arg
|
173
|
+
puts 'output formats (Encoders):'
|
174
|
+
print_list_of CodeRay::Encoders
|
70
175
|
end
|
176
|
+
|
177
|
+
if [nil, 'st', 'style', 'styles'].include? arg
|
178
|
+
puts 'CSS themes for HTML output (Styles):'
|
179
|
+
print_list_of CodeRay::Styles
|
180
|
+
end
|
181
|
+
|
182
|
+
if [nil, 'f', 'ft', 'file', 'filetype', 'filetypes'].include? arg
|
183
|
+
puts 'recognized file types:'
|
184
|
+
|
185
|
+
filetypes = Hash.new { |h, k| h[k] = [] }
|
186
|
+
CodeRay::FileType::TypeFromExt.inject filetypes do |types, (ext, type)|
|
187
|
+
types[type.to_s] << ".#{ext}"
|
188
|
+
types
|
189
|
+
end
|
190
|
+
CodeRay::FileType::TypeFromName.inject filetypes do |types, (name, type)|
|
191
|
+
types[type.to_s] << name
|
192
|
+
types
|
193
|
+
end
|
194
|
+
|
195
|
+
filetypes.sort.each do |type, exts|
|
196
|
+
puts " #{type}: #{exts.sort_by { |ext| ext.size }.join(', ')}"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
when 'stylesheet', 'style', 'css'
|
200
|
+
puts CodeRay::Encoders[:html]::CSS.new(args.first).stylesheet
|
201
|
+
when 'commands'
|
202
|
+
commands
|
203
|
+
when 'help'
|
204
|
+
help
|
71
205
|
else
|
72
|
-
|
73
|
-
|
206
|
+
$stdout = $stderr
|
207
|
+
help
|
208
|
+
puts
|
209
|
+
if subcommand[/\A\w+\z/]
|
210
|
+
puts "Unknown command: #{subcommand}"
|
74
211
|
else
|
75
|
-
|
76
|
-
format = :page
|
212
|
+
puts "File not found: #{subcommand}"
|
77
213
|
end
|
214
|
+
exit 1
|
78
215
|
end
|
79
|
-
|
80
|
-
if tokens == :scan
|
81
|
-
output = CodeRay::Duo[lang => format].highlight input
|
82
|
-
else
|
83
|
-
output = tokens.encode format
|
84
|
-
end
|
85
|
-
out = $stdout
|
86
|
-
out.puts output
|
data/lib/coderay.rb
CHANGED
@@ -1,16 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# Encoding.default_internal = 'UTF-8'
|
3
|
+
|
1
4
|
# = CodeRay Library
|
2
5
|
#
|
3
6
|
# CodeRay is a Ruby library for syntax highlighting.
|
4
7
|
#
|
5
|
-
# I try to make CodeRay easy to use and intuitive, but at the same time fully
|
6
|
-
# fast and efficient.
|
8
|
+
# I try to make CodeRay easy to use and intuitive, but at the same time fully
|
9
|
+
# featured, complete, fast and efficient.
|
7
10
|
#
|
8
11
|
# See README.
|
9
12
|
#
|
10
13
|
# It consists mainly of
|
11
|
-
# * the main engine: CodeRay (Scanners::Scanner, Tokens
|
14
|
+
# * the main engine: CodeRay (Scanners::Scanner, Tokens, Encoders::Encoder)
|
15
|
+
# * the plugin system: PluginHost, Plugin
|
12
16
|
# * the scanners in CodeRay::Scanners
|
13
17
|
# * the encoders in CodeRay::Encoders
|
18
|
+
# * the styles in CodeRay::Styles
|
14
19
|
#
|
15
20
|
# Here's a fancy graphic to light up this gray docu:
|
16
21
|
#
|
@@ -22,8 +27,8 @@
|
|
22
27
|
#
|
23
28
|
# == Usage
|
24
29
|
#
|
25
|
-
# Remember you need RubyGems to use CodeRay, unless you have it in your load
|
26
|
-
# -rubygems option if required.
|
30
|
+
# Remember you need RubyGems to use CodeRay, unless you have it in your load
|
31
|
+
# path. Run Ruby with -rubygems option if required.
|
27
32
|
#
|
28
33
|
# === Highlight Ruby code in a string as html
|
29
34
|
#
|
@@ -98,13 +103,6 @@
|
|
98
103
|
# CodeRay.encode_tokens:: Encode the given tokens.
|
99
104
|
# CodeRay.encode_file:: Scan a file, guess the language using FileType and encode it.
|
100
105
|
#
|
101
|
-
# == Streaming
|
102
|
-
#
|
103
|
-
# Streaming saves RAM by running Scanner and Encoder in some sort of
|
104
|
-
# pipe mode; see TokenStream.
|
105
|
-
#
|
106
|
-
# CodeRay.scan_stream:: Scan in stream mode.
|
107
|
-
#
|
108
106
|
# == All-in-One Encoding
|
109
107
|
#
|
110
108
|
# CodeRay.encode:: Highlight a string with a given input and output format.
|
@@ -129,23 +127,30 @@ module CodeRay
|
|
129
127
|
|
130
128
|
$CODERAY_DEBUG ||= false
|
131
129
|
|
132
|
-
|
133
|
-
|
134
|
-
#
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
130
|
+
require 'coderay/version'
|
131
|
+
|
132
|
+
# helpers
|
133
|
+
autoload :FileType, 'coderay/helpers/file_type'
|
134
|
+
|
135
|
+
# Tokens
|
136
|
+
autoload :Tokens, 'coderay/tokens'
|
137
|
+
autoload :TokensProxy, 'coderay/tokens_proxy'
|
138
|
+
autoload :TokenKinds, 'coderay/token_kinds'
|
139
|
+
|
140
|
+
# Plugin system
|
141
|
+
autoload :PluginHost, 'coderay/helpers/plugin'
|
142
|
+
autoload :Plugin, 'coderay/helpers/plugin'
|
143
|
+
|
144
|
+
# Plugins
|
145
|
+
autoload :Scanners, 'coderay/scanner'
|
146
|
+
autoload :Encoders, 'coderay/encoder'
|
147
|
+
autoload :Styles, 'coderay/style'
|
148
|
+
|
149
|
+
# Convenience access and reusable Encoder/Scanner pair
|
150
|
+
autoload :Duo, 'coderay/duo'
|
151
|
+
|
147
152
|
class << self
|
148
|
-
|
153
|
+
|
149
154
|
# Scans the given +code+ (a String) with the Scanner for +lang+.
|
150
155
|
#
|
151
156
|
# This is a simple way to use CodeRay. Example:
|
@@ -154,15 +159,15 @@ module CodeRay
|
|
154
159
|
#
|
155
160
|
# See also demo/demo_simple.
|
156
161
|
def scan code, lang, options = {}, &block
|
157
|
-
|
158
|
-
|
162
|
+
# FIXME: return a proxy for direct-stream encoding
|
163
|
+
TokensProxy.new code, lang, options, block
|
159
164
|
end
|
160
|
-
|
165
|
+
|
161
166
|
# Scans +filename+ (a path to a code file) with the Scanner for +lang+.
|
162
167
|
#
|
163
168
|
# If +lang+ is :auto or omitted, the CodeRay::FileType module is used to
|
164
169
|
# determine it. If it cannot find out what type it is, it uses
|
165
|
-
# CodeRay::Scanners::
|
170
|
+
# CodeRay::Scanners::Text.
|
166
171
|
#
|
167
172
|
# Calls CodeRay.scan.
|
168
173
|
#
|
@@ -170,56 +175,22 @@ module CodeRay
|
|
170
175
|
# require 'coderay'
|
171
176
|
# page = CodeRay.scan_file('some_c_code.c').html
|
172
177
|
def scan_file filename, lang = :auto, options = {}, &block
|
173
|
-
|
174
|
-
|
175
|
-
require 'coderay/helpers/file_type'
|
176
|
-
lang = FileType.fetch filename, :plaintext, true
|
177
|
-
end
|
178
|
-
scan file, lang, options = {}, &block
|
179
|
-
end
|
180
|
-
|
181
|
-
# Scan the +code+ (a string) with the scanner for +lang+.
|
182
|
-
#
|
183
|
-
# Calls scan.
|
184
|
-
#
|
185
|
-
# See CodeRay.scan.
|
186
|
-
def scan_stream code, lang, options = {}, &block
|
187
|
-
options[:stream] = true
|
178
|
+
lang = FileType.fetch filename, :text, true if lang == :auto
|
179
|
+
code = File.read filename
|
188
180
|
scan code, lang, options, &block
|
189
181
|
end
|
190
|
-
|
191
|
-
# Encode a string in Streaming mode.
|
192
|
-
#
|
193
|
-
# This starts scanning +code+ with the the Scanner for +lang+
|
194
|
-
# while encodes the output with the Encoder for +format+.
|
195
|
-
# +options+ will be passed to the Encoder.
|
196
|
-
#
|
197
|
-
# See CodeRay::Encoder.encode_stream
|
198
|
-
def encode_stream code, lang, format, options = {}
|
199
|
-
encoder(format, options).encode_stream code, lang, options
|
200
|
-
end
|
201
|
-
|
182
|
+
|
202
183
|
# Encode a string.
|
203
184
|
#
|
204
185
|
# This scans +code+ with the the Scanner for +lang+ and then
|
205
186
|
# encodes it with the Encoder for +format+.
|
206
187
|
# +options+ will be passed to the Encoder.
|
207
188
|
#
|
208
|
-
# See CodeRay::Encoder.encode
|
189
|
+
# See CodeRay::Encoder.encode.
|
209
190
|
def encode code, lang, format, options = {}
|
210
191
|
encoder(format, options).encode code, lang, options
|
211
192
|
end
|
212
|
-
|
213
|
-
# Highlight a string into a HTML <div>.
|
214
|
-
#
|
215
|
-
# CSS styles use classes, so you have to include a stylesheet
|
216
|
-
# in your output.
|
217
|
-
#
|
218
|
-
# See encode.
|
219
|
-
def highlight code, lang, options = { :css => :class }, format = :div
|
220
|
-
encode code, lang, format, options
|
221
|
-
end
|
222
|
-
|
193
|
+
|
223
194
|
# Encode pre-scanned Tokens.
|
224
195
|
# Use this together with CodeRay.scan:
|
225
196
|
#
|
@@ -232,7 +203,7 @@ module CodeRay
|
|
232
203
|
def encode_tokens tokens, format, options = {}
|
233
204
|
encoder(format, options).encode_tokens tokens, options
|
234
205
|
end
|
235
|
-
|
206
|
+
|
236
207
|
# Encodes +filename+ (a path to a code file) with the Scanner for +lang+.
|
237
208
|
#
|
238
209
|
# See CodeRay.scan_file.
|
@@ -245,7 +216,17 @@ module CodeRay
|
|
245
216
|
tokens = scan_file filename, :auto, get_scanner_options(options)
|
246
217
|
encode_tokens tokens, format, options
|
247
218
|
end
|
248
|
-
|
219
|
+
|
220
|
+
# Highlight a string into a HTML <div>.
|
221
|
+
#
|
222
|
+
# CSS styles use classes, so you have to include a stylesheet
|
223
|
+
# in your output.
|
224
|
+
#
|
225
|
+
# See encode.
|
226
|
+
def highlight code, lang, options = { :css => :class }, format = :div
|
227
|
+
encode code, lang, format, options
|
228
|
+
end
|
229
|
+
|
249
230
|
# Highlight a file into a HTML <div>.
|
250
231
|
#
|
251
232
|
# CSS styles use classes, so you have to include a stylesheet
|
@@ -255,7 +236,7 @@ module CodeRay
|
|
255
236
|
def highlight_file filename, options = { :css => :class }, format = :div
|
256
237
|
encode_file filename, format, options
|
257
238
|
end
|
258
|
-
|
239
|
+
|
259
240
|
# Finds the Encoder class for +format+ and creates an instance, passing
|
260
241
|
# +options+ to it.
|
261
242
|
#
|
@@ -273,15 +254,15 @@ module CodeRay
|
|
273
254
|
def encoder format, options = {}
|
274
255
|
Encoders[format].new options
|
275
256
|
end
|
276
|
-
|
257
|
+
|
277
258
|
# Finds the Scanner class for +lang+ and creates an instance, passing
|
278
259
|
# +options+ to it.
|
279
260
|
#
|
280
261
|
# See Scanner.new.
|
281
|
-
def scanner lang, options = {}
|
282
|
-
Scanners[lang].new '', options
|
262
|
+
def scanner lang, options = {}, &block
|
263
|
+
Scanners[lang].new '', options, &block
|
283
264
|
end
|
284
|
-
|
265
|
+
|
285
266
|
# Extract the options for the scanner from the +options+ hash.
|
286
267
|
#
|
287
268
|
# Returns an empty Hash if <tt>:scanner_options</tt> is not set.
|
@@ -291,32 +272,7 @@ module CodeRay
|
|
291
272
|
def get_scanner_options options
|
292
273
|
options.fetch :scanner_options, {}
|
293
274
|
end
|
294
|
-
|
275
|
+
|
295
276
|
end
|
296
|
-
|
297
|
-
# This Exception is raised when you try to stream with something that is not
|
298
|
-
# capable of streaming.
|
299
|
-
class NotStreamableError < Exception
|
300
|
-
def initialize obj
|
301
|
-
@obj = obj
|
302
|
-
end
|
303
|
-
|
304
|
-
def to_s
|
305
|
-
'%s is not Streamable!' % @obj.class
|
306
|
-
end
|
307
|
-
end
|
308
|
-
|
309
|
-
# A dummy module that is included by subclasses of CodeRay::Scanner an CodeRay::Encoder
|
310
|
-
# to show that they are able to handle streams.
|
311
|
-
module Streamable
|
312
|
-
end
|
313
|
-
|
314
|
-
end
|
315
|
-
|
316
|
-
# Run a test script.
|
317
|
-
if $0 == __FILE__
|
318
|
-
$stderr.print 'Press key to print demo.'; gets
|
319
|
-
# Just use this file as an example of Ruby code.
|
320
|
-
code = File.read(__FILE__)[/module CodeRay.*/m]
|
321
|
-
print CodeRay.scan(code, :ruby).html
|
277
|
+
|
322
278
|
end
|