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.
- data/FOLDERS +53 -0
- data/LICENSE +504 -0
- data/bin/coderay +82 -0
- data/bin/coderay_stylesheet +4 -0
- data/lib/README +129 -0
- data/lib/coderay.rb +320 -0
- data/lib/coderay/duo.rb +85 -0
- data/lib/coderay/encoder.rb +213 -0
- data/lib/coderay/encoders/_map.rb +11 -0
- data/lib/coderay/encoders/comment_filter.rb +43 -0
- data/lib/coderay/encoders/count.rb +21 -0
- data/lib/coderay/encoders/debug.rb +49 -0
- data/lib/coderay/encoders/div.rb +19 -0
- data/lib/coderay/encoders/filter.rb +75 -0
- data/lib/coderay/encoders/html.rb +305 -0
- data/lib/coderay/encoders/html/css.rb +70 -0
- data/lib/coderay/encoders/html/numerization.rb +133 -0
- data/lib/coderay/encoders/html/output.rb +206 -0
- data/lib/coderay/encoders/json.rb +69 -0
- data/lib/coderay/encoders/lines_of_code.rb +90 -0
- data/lib/coderay/encoders/null.rb +26 -0
- data/lib/coderay/encoders/page.rb +20 -0
- data/lib/coderay/encoders/span.rb +19 -0
- data/lib/coderay/encoders/statistic.rb +77 -0
- data/lib/coderay/encoders/term.rb +137 -0
- data/lib/coderay/encoders/text.rb +32 -0
- data/lib/coderay/encoders/token_class_filter.rb +84 -0
- data/lib/coderay/encoders/xml.rb +71 -0
- data/lib/coderay/encoders/yaml.rb +22 -0
- data/lib/coderay/for_redcloth.rb +85 -0
- data/lib/coderay/helpers/file_type.rb +240 -0
- data/lib/coderay/helpers/gzip_simple.rb +123 -0
- data/lib/coderay/helpers/plugin.rb +349 -0
- data/lib/coderay/helpers/word_list.rb +138 -0
- data/lib/coderay/scanner.rb +284 -0
- data/lib/coderay/scanners/_map.rb +23 -0
- data/lib/coderay/scanners/c.rb +203 -0
- data/lib/coderay/scanners/cpp.rb +228 -0
- data/lib/coderay/scanners/css.rb +210 -0
- data/lib/coderay/scanners/debug.rb +62 -0
- data/lib/coderay/scanners/delphi.rb +150 -0
- data/lib/coderay/scanners/diff.rb +105 -0
- data/lib/coderay/scanners/groovy.rb +263 -0
- data/lib/coderay/scanners/html.rb +182 -0
- data/lib/coderay/scanners/java.rb +176 -0
- data/lib/coderay/scanners/java/builtin_types.rb +419 -0
- data/lib/coderay/scanners/java_script.rb +224 -0
- data/lib/coderay/scanners/json.rb +112 -0
- data/lib/coderay/scanners/nitro_xhtml.rb +136 -0
- data/lib/coderay/scanners/php.rb +526 -0
- data/lib/coderay/scanners/plaintext.rb +21 -0
- data/lib/coderay/scanners/python.rb +285 -0
- data/lib/coderay/scanners/rhtml.rb +74 -0
- data/lib/coderay/scanners/ruby.rb +404 -0
- data/lib/coderay/scanners/ruby/patterns.rb +238 -0
- data/lib/coderay/scanners/scheme.rb +145 -0
- data/lib/coderay/scanners/sql.rb +162 -0
- data/lib/coderay/scanners/xml.rb +17 -0
- data/lib/coderay/scanners/yaml.rb +144 -0
- data/lib/coderay/style.rb +20 -0
- data/lib/coderay/styles/_map.rb +7 -0
- data/lib/coderay/styles/cycnus.rb +151 -0
- data/lib/coderay/styles/murphy.rb +132 -0
- data/lib/coderay/token_classes.rb +86 -0
- data/lib/coderay/tokens.rb +391 -0
- data/lib/term/ansicolor.rb +220 -0
- metadata +123 -0
data/bin/coderay
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# CodeRay Executable
|
3
|
+
#
|
4
|
+
# Version: 0.1
|
5
|
+
# Author: murphy
|
6
|
+
|
7
|
+
def err msg
|
8
|
+
$stderr.puts msg
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
require 'coderay'
|
13
|
+
|
14
|
+
if ARGV.empty?
|
15
|
+
puts <<-USAGE
|
16
|
+
CodeRay #{CodeRay::VERSION} (http://coderay.rubychan.de)
|
17
|
+
Usage:
|
18
|
+
coderay -<lang> [-<format>] < file > output
|
19
|
+
coderay file [-<format>]
|
20
|
+
Example:
|
21
|
+
coderay -ruby -statistic < foo.rb
|
22
|
+
coderay codegen.c # generates codegen.c.html
|
23
|
+
USAGE
|
24
|
+
end
|
25
|
+
|
26
|
+
first, second = ARGV
|
27
|
+
|
28
|
+
if first
|
29
|
+
if first[/-(\w+)/] == first
|
30
|
+
lang = $1
|
31
|
+
input = $stdin.read
|
32
|
+
tokens = :scan
|
33
|
+
elsif first == '-'
|
34
|
+
lang = $1
|
35
|
+
input = $stdin.read
|
36
|
+
tokens = :scan
|
37
|
+
else
|
38
|
+
file = first
|
39
|
+
tokens = CodeRay.scan_file file
|
40
|
+
output_filename, output_ext = file, /#{Regexp.escape(File.extname(file))}$/
|
41
|
+
end
|
42
|
+
else
|
43
|
+
puts 'No lang/file given.'
|
44
|
+
exit 1
|
45
|
+
end
|
46
|
+
|
47
|
+
if second
|
48
|
+
if second[/-(\w+)/] == second
|
49
|
+
format = $1
|
50
|
+
else
|
51
|
+
raise 'Invalid format (must be -xxx).'
|
52
|
+
end
|
53
|
+
else
|
54
|
+
$stderr.puts 'No format given; setting to default (HTML Page)'
|
55
|
+
format = :page
|
56
|
+
end
|
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
|
64
|
+
out = $stdout
|
65
|
+
if output_filename
|
66
|
+
output_filename += '.' + CodeRay::Encoders[format]::FILE_EXTENSION
|
67
|
+
if File.exist? output_filename
|
68
|
+
err 'File %s already exists.' % output_filename
|
69
|
+
exit
|
70
|
+
else
|
71
|
+
out = File.open output_filename, 'w'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
out.print output
|
75
|
+
|
76
|
+
rescue => boom
|
77
|
+
err "Error: #{boom.message}\n"
|
78
|
+
err boom.backtrace
|
79
|
+
err '-' * 50
|
80
|
+
err ARGV
|
81
|
+
exit 1
|
82
|
+
end
|
data/lib/README
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
= CodeRay
|
2
|
+
|
3
|
+
[- Tired of blue'n'gray? Try the original version of this documentation on
|
4
|
+
coderay.rubychan.de[http://coderay.rubychan.de/doc/] (use Ctrl+Click to open it in its own frame.) -]
|
5
|
+
|
6
|
+
== About
|
7
|
+
CodeRay is a Ruby library for syntax highlighting.
|
8
|
+
|
9
|
+
Syntax highlighting means: You put your code in, and you get it back colored;
|
10
|
+
Keywords, strings, floats, comments - all in different colors.
|
11
|
+
And with line numbers.
|
12
|
+
|
13
|
+
*Syntax* *Highlighting*...
|
14
|
+
* makes code easier to read and maintain
|
15
|
+
* lets you detect syntax errors faster
|
16
|
+
* helps you to understand the syntax of a language
|
17
|
+
* looks nice
|
18
|
+
* is what everybody should have on their website
|
19
|
+
* solves all your problems and makes the girls run after you
|
20
|
+
|
21
|
+
Version: 0.9.1
|
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
|
+
|
27
|
+
== Installation
|
28
|
+
|
29
|
+
You need RubyGems[http://rubyforge.org/frs/?group_id=126].
|
30
|
+
|
31
|
+
% gem install coderay
|
32
|
+
|
33
|
+
|
34
|
+
=== Dependencies
|
35
|
+
|
36
|
+
CodeRay needs Ruby 1.8.6 or later. It also runs with Ruby 1.9.1+ and JRuby 1.1+.
|
37
|
+
|
38
|
+
|
39
|
+
== Example Usage
|
40
|
+
(Forgive me, but this is not highlighted.)
|
41
|
+
|
42
|
+
require 'coderay'
|
43
|
+
|
44
|
+
tokens = CodeRay.scan "puts 'Hello, world!'", :ruby
|
45
|
+
page = tokens.html :line_numbers => :inline, :wrap => :page
|
46
|
+
puts page
|
47
|
+
|
48
|
+
|
49
|
+
== Documentation
|
50
|
+
|
51
|
+
See CodeRay.
|
52
|
+
|
53
|
+
Please report errors in this documentation to <murphy rubychan de>.
|
54
|
+
|
55
|
+
|
56
|
+
== Credits
|
57
|
+
|
58
|
+
=== Special Thanks to
|
59
|
+
|
60
|
+
* licenser (Heinz N. Gies) for ending my QBasic career, inventing the Coder
|
61
|
+
project and the input/output plugin system.
|
62
|
+
CodeRay would not exist without him.
|
63
|
+
* bovi (Daniel Bovensiepen) for helping me out on various occasions.
|
64
|
+
|
65
|
+
=== Thanks to
|
66
|
+
|
67
|
+
* Caleb Clausen for writing RubyLexer (see
|
68
|
+
http://rubyforge.org/projects/rubylexer) and lots of very interesting mail
|
69
|
+
traffic
|
70
|
+
* birkenfeld (Georg Brandl) and mitsuhiku (Arnim Ronacher) for PyKleur, now pygments.
|
71
|
+
You guys rock!
|
72
|
+
* Jamis Buck for writing Syntax (see http://rubyforge.org/projects/syntax)
|
73
|
+
I got some useful ideas from it.
|
74
|
+
* Doug Kearns and everyone else who worked on ruby.vim - it not only helped me
|
75
|
+
coding CodeRay, but also gave me a wonderful target to reach for the Ruby
|
76
|
+
scanner.
|
77
|
+
* everyone who uses CodeBB on http://www.rubyforen.de and http://www.python-forum.de
|
78
|
+
* iGEL, magichisoka, manveru, WoNáDo and everyone I forgot from rubyforen.de
|
79
|
+
* Dethix from ruby-mine.de
|
80
|
+
* zickzackw
|
81
|
+
* Dookie (who is no longer with us...) and Leonidas from http://www.python-forum.de
|
82
|
+
* Andreas Schwarz for finding out that CaseIgnoringWordList was not case
|
83
|
+
ignoring! Such things really make you write tests.
|
84
|
+
* closure for the first version of the Scheme scanner.
|
85
|
+
* Stefan Walk for the first version of the JavaScript scanner.
|
86
|
+
* Josh Goebel for another version of the JavaScript scanner and a Diff scanner.
|
87
|
+
* Jonathan Younger for pointing out the licence confusion caused by wrong LICENSE file.
|
88
|
+
* Jeremy Hinegardner for finding the shebang-on-empty-file bug in FileType.
|
89
|
+
* Charles Oliver Nutter and Yehuda Katz for helping me benchmark CodeRay on JRuby.
|
90
|
+
* Andreas Neuhaus for pointing out a markup bug in coderay/for_redcloth.
|
91
|
+
* 0xf30fc7 for the FileType patch concerning Delphi file extensions.
|
92
|
+
* The folks at redmine.org - thank you for using and fixing CodeRay!
|
93
|
+
* matz and all Ruby gods and gurus
|
94
|
+
* The inventors of: the computer, the internet, the true color display, HTML &
|
95
|
+
CSS, VIM, Ruby, pizza, microwaves, guitars, scouting, programming, anime,
|
96
|
+
manga, coke and green ice tea.
|
97
|
+
|
98
|
+
Where would we be without all those people?
|
99
|
+
|
100
|
+
=== Created using
|
101
|
+
|
102
|
+
* Ruby[http://ruby-lang.org/]
|
103
|
+
* Chihiro (my Sony VAIO laptop); Henrietta (my old MacBook);
|
104
|
+
Triella, born Rico (my new MacBook); as well as
|
105
|
+
Seras and Hikari (my PCs)
|
106
|
+
* RDE[http://homepage2.nifty.com/sakazuki/rde_e.html],
|
107
|
+
VIM[http://vim.org] and TextMate[http://macromates.com]
|
108
|
+
* Subversion[http://subversion.tigris.org/]
|
109
|
+
* Redmine[http://redmine.org/]
|
110
|
+
* Firefox[http://www.mozilla.org/products/firefox/],
|
111
|
+
Firebug[http://getfirebug.com/], Safari[http://www.apple.com/safari/], and
|
112
|
+
Thunderbird[http://www.mozilla.org/products/thunderbird/]
|
113
|
+
* RubyGems[http://docs.rubygems.org/] and Rake[http://rake.rubyforge.org/]
|
114
|
+
* TortoiseSVN[http://tortoisesvn.tigris.org/] using Apache via
|
115
|
+
XAMPP[http://www.apachefriends.org/en/xampp.html]
|
116
|
+
* RDoc (though I'm quite unsatisfied with it)
|
117
|
+
* Microsoft Windows (yes, I confess!) and MacOS X
|
118
|
+
* GNUWin32, MinGW and some other tools to make the shell under windows a bit
|
119
|
+
less useless
|
120
|
+
* Term::ANSIColor[http://term-ansicolor.rubyforge.org/]
|
121
|
+
* PLEAC[http://pleac.sourceforge.net/] code examples
|
122
|
+
|
123
|
+
=== Free
|
124
|
+
|
125
|
+
* As you can see, CodeRay was created under heavy use of *free* software.
|
126
|
+
* So CodeRay is also *free*.
|
127
|
+
* If you use CodeRay to create software, think about making this software
|
128
|
+
*free*, too.
|
129
|
+
* Thanks :)
|
data/lib/coderay.rb
ADDED
@@ -0,0 +1,320 @@
|
|
1
|
+
# = CodeRay Library
|
2
|
+
#
|
3
|
+
# CodeRay is a Ruby library for syntax highlighting.
|
4
|
+
#
|
5
|
+
# I try to make CodeRay easy to use and intuitive, but at the same time fully featured, complete,
|
6
|
+
# fast and efficient.
|
7
|
+
#
|
8
|
+
# See README.
|
9
|
+
#
|
10
|
+
# It consists mainly of
|
11
|
+
# * the main engine: CodeRay (Scanners::Scanner, Tokens/TokenStream, Encoders::Encoder), PluginHost
|
12
|
+
# * the scanners in CodeRay::Scanners
|
13
|
+
# * the encoders in CodeRay::Encoders
|
14
|
+
#
|
15
|
+
# Here's a fancy graphic to light up this gray docu:
|
16
|
+
#
|
17
|
+
# http://rd.cYcnus.de/coderay/scheme.png
|
18
|
+
#
|
19
|
+
# == Documentation
|
20
|
+
#
|
21
|
+
# See CodeRay, Encoders, Scanners, Tokens.
|
22
|
+
#
|
23
|
+
# == Usage
|
24
|
+
#
|
25
|
+
# Remember you need RubyGems to use CodeRay, unless you have it in your load path. Run Ruby with
|
26
|
+
# -rubygems option if required.
|
27
|
+
#
|
28
|
+
# === Highlight Ruby code in a string as html
|
29
|
+
#
|
30
|
+
# require 'coderay'
|
31
|
+
# print CodeRay.scan('puts "Hello, world!"', :ruby).html
|
32
|
+
#
|
33
|
+
# # prints something like this:
|
34
|
+
# puts <span class="s">"Hello, world!"</span>
|
35
|
+
#
|
36
|
+
#
|
37
|
+
# === Highlight C code from a file in a html div
|
38
|
+
#
|
39
|
+
# require 'coderay'
|
40
|
+
# print CodeRay.scan(File.read('ruby.h'), :c).div
|
41
|
+
# print CodeRay.scan_file('ruby.h').html.div
|
42
|
+
#
|
43
|
+
# You can include this div in your page. The used CSS styles can be printed with
|
44
|
+
#
|
45
|
+
# % coderay_stylesheet
|
46
|
+
#
|
47
|
+
# === Highlight without typing too much
|
48
|
+
#
|
49
|
+
# If you are one of the hasty (or lazy, or extremely curious) people, just run this file:
|
50
|
+
#
|
51
|
+
# % ruby -rubygems /path/to/coderay/coderay.rb > example.html
|
52
|
+
#
|
53
|
+
# and look at the file it created in your browser.
|
54
|
+
#
|
55
|
+
# = CodeRay Module
|
56
|
+
#
|
57
|
+
# The CodeRay module provides convenience methods for the engine.
|
58
|
+
#
|
59
|
+
# * The +lang+ and +format+ arguments select Scanner and Encoder to use. These are
|
60
|
+
# simply lower-case symbols, like <tt>:python</tt> or <tt>:html</tt>.
|
61
|
+
# * All methods take an optional hash as last parameter, +options+, that is send to
|
62
|
+
# the Encoder / Scanner.
|
63
|
+
# * Input and language are always sorted in this order: +code+, +lang+.
|
64
|
+
# (This is in alphabetical order, if you need a mnemonic ;)
|
65
|
+
#
|
66
|
+
# You should be able to highlight everything you want just using these methods;
|
67
|
+
# so there is no need to dive into CodeRay's deep class hierarchy.
|
68
|
+
#
|
69
|
+
# The examples in the demo directory demonstrate common cases using this interface.
|
70
|
+
#
|
71
|
+
# = Basic Access Ways
|
72
|
+
#
|
73
|
+
# Read this to get a general view what CodeRay provides.
|
74
|
+
#
|
75
|
+
# == Scanning
|
76
|
+
#
|
77
|
+
# Scanning means analysing an input string, splitting it up into Tokens.
|
78
|
+
# Each Token knows about what type it is: string, comment, class name, etc.
|
79
|
+
#
|
80
|
+
# Each +lang+ (language) has its own Scanner; for example, <tt>:ruby</tt> code is
|
81
|
+
# handled by CodeRay::Scanners::Ruby.
|
82
|
+
#
|
83
|
+
# CodeRay.scan:: Scan a string in a given language into Tokens.
|
84
|
+
# This is the most common method to use.
|
85
|
+
# CodeRay.scan_file:: Scan a file and guess the language using FileType.
|
86
|
+
#
|
87
|
+
# The Tokens object you get from these methods can encode itself; see Tokens.
|
88
|
+
#
|
89
|
+
# == Encoding
|
90
|
+
#
|
91
|
+
# Encoding means compiling Tokens into an output. This can be colored HTML or
|
92
|
+
# LaTeX, a textual statistic or just the number of non-whitespace tokens.
|
93
|
+
#
|
94
|
+
# Each Encoder provides output in a specific +format+, so you select Encoders via
|
95
|
+
# formats like <tt>:html</tt> or <tt>:statistic</tt>.
|
96
|
+
#
|
97
|
+
# CodeRay.encode:: Scan and encode a string in a given language.
|
98
|
+
# CodeRay.encode_tokens:: Encode the given tokens.
|
99
|
+
# CodeRay.encode_file:: Scan a file, guess the language using FileType and encode it.
|
100
|
+
#
|
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
|
+
# == All-in-One Encoding
|
109
|
+
#
|
110
|
+
# CodeRay.encode:: Highlight a string with a given input and output format.
|
111
|
+
#
|
112
|
+
# == Instanciating
|
113
|
+
#
|
114
|
+
# You can use an Encoder instance to highlight multiple inputs. This way, the setup
|
115
|
+
# for this Encoder must only be done once.
|
116
|
+
#
|
117
|
+
# CodeRay.encoder:: Create an Encoder instance with format and options.
|
118
|
+
# CodeRay.scanner:: Create an Scanner instance for lang, with '' as default code.
|
119
|
+
#
|
120
|
+
# To make use of CodeRay.scanner, use CodeRay::Scanner::code=.
|
121
|
+
#
|
122
|
+
# The scanning methods provide more flexibility; we recommend to use these.
|
123
|
+
#
|
124
|
+
# == Reusing Scanners and Encoders
|
125
|
+
#
|
126
|
+
# If you want to re-use scanners and encoders (because that is faster), see
|
127
|
+
# CodeRay::Duo for the most convenient (and recommended) interface.
|
128
|
+
module CodeRay
|
129
|
+
|
130
|
+
# Version: Major.Minor.Teeny[.Revision]
|
131
|
+
# Major: 0 for pre-stable, 1 for stable
|
132
|
+
# Minor: feature milestone
|
133
|
+
# Teeny: development state, 0 for pre-release
|
134
|
+
# Revision: Subversion Revision number (generated on rake gem:make)
|
135
|
+
VERSION = '0.9.1'
|
136
|
+
|
137
|
+
require 'coderay/tokens'
|
138
|
+
require 'coderay/token_classes'
|
139
|
+
require 'coderay/scanner'
|
140
|
+
require 'coderay/encoder'
|
141
|
+
require 'coderay/duo'
|
142
|
+
require 'coderay/style'
|
143
|
+
|
144
|
+
|
145
|
+
class << self
|
146
|
+
|
147
|
+
# Scans the given +code+ (a String) with the Scanner for +lang+.
|
148
|
+
#
|
149
|
+
# This is a simple way to use CodeRay. Example:
|
150
|
+
# require 'coderay'
|
151
|
+
# page = CodeRay.scan("puts 'Hello, world!'", :ruby).html
|
152
|
+
#
|
153
|
+
# See also demo/demo_simple.
|
154
|
+
def scan code, lang, options = {}, &block
|
155
|
+
scanner = Scanners[lang].new code, options, &block
|
156
|
+
scanner.tokenize
|
157
|
+
end
|
158
|
+
|
159
|
+
# Scans +filename+ (a path to a code file) with the Scanner for +lang+.
|
160
|
+
#
|
161
|
+
# If +lang+ is :auto or omitted, the CodeRay::FileType module is used to
|
162
|
+
# determine it. If it cannot find out what type it is, it uses
|
163
|
+
# CodeRay::Scanners::Plaintext.
|
164
|
+
#
|
165
|
+
# Calls CodeRay.scan.
|
166
|
+
#
|
167
|
+
# Example:
|
168
|
+
# require 'coderay'
|
169
|
+
# page = CodeRay.scan_file('some_c_code.c').html
|
170
|
+
def scan_file filename, lang = :auto, options = {}, &block
|
171
|
+
file = IO.read filename
|
172
|
+
if lang == :auto
|
173
|
+
require 'coderay/helpers/file_type'
|
174
|
+
lang = FileType.fetch filename, :plaintext, true
|
175
|
+
end
|
176
|
+
scan file, lang, options = {}, &block
|
177
|
+
end
|
178
|
+
|
179
|
+
# Scan the +code+ (a string) with the scanner for +lang+.
|
180
|
+
#
|
181
|
+
# Calls scan.
|
182
|
+
#
|
183
|
+
# See CodeRay.scan.
|
184
|
+
def scan_stream code, lang, options = {}, &block
|
185
|
+
options[:stream] = true
|
186
|
+
scan code, lang, options, &block
|
187
|
+
end
|
188
|
+
|
189
|
+
# Encode a string in Streaming mode.
|
190
|
+
#
|
191
|
+
# This starts scanning +code+ with the the Scanner for +lang+
|
192
|
+
# while encodes the output with the Encoder for +format+.
|
193
|
+
# +options+ will be passed to the Encoder.
|
194
|
+
#
|
195
|
+
# See CodeRay::Encoder.encode_stream
|
196
|
+
def encode_stream code, lang, format, options = {}
|
197
|
+
encoder(format, options).encode_stream code, lang, options
|
198
|
+
end
|
199
|
+
|
200
|
+
# Encode a string.
|
201
|
+
#
|
202
|
+
# This scans +code+ with the the Scanner for +lang+ and then
|
203
|
+
# encodes it with the Encoder for +format+.
|
204
|
+
# +options+ will be passed to the Encoder.
|
205
|
+
#
|
206
|
+
# See CodeRay::Encoder.encode
|
207
|
+
def encode code, lang, format, options = {}
|
208
|
+
encoder(format, options).encode code, lang, options
|
209
|
+
end
|
210
|
+
|
211
|
+
# Highlight a string into a HTML <div>.
|
212
|
+
#
|
213
|
+
# CSS styles use classes, so you have to include a stylesheet
|
214
|
+
# in your output.
|
215
|
+
#
|
216
|
+
# See encode.
|
217
|
+
def highlight code, lang, options = { :css => :class }, format = :div
|
218
|
+
encode code, lang, format, options
|
219
|
+
end
|
220
|
+
|
221
|
+
# Encode pre-scanned Tokens.
|
222
|
+
# Use this together with CodeRay.scan:
|
223
|
+
#
|
224
|
+
# require 'coderay'
|
225
|
+
#
|
226
|
+
# # Highlight a short Ruby code example in a HTML span
|
227
|
+
# tokens = CodeRay.scan '1 + 2', :ruby
|
228
|
+
# puts CodeRay.encode_tokens(tokens, :span)
|
229
|
+
#
|
230
|
+
def encode_tokens tokens, format, options = {}
|
231
|
+
encoder(format, options).encode_tokens tokens, options
|
232
|
+
end
|
233
|
+
|
234
|
+
# Encodes +filename+ (a path to a code file) with the Scanner for +lang+.
|
235
|
+
#
|
236
|
+
# See CodeRay.scan_file.
|
237
|
+
# Notice that the second argument is the output +format+, not the input language.
|
238
|
+
#
|
239
|
+
# Example:
|
240
|
+
# require 'coderay'
|
241
|
+
# page = CodeRay.encode_file 'some_c_code.c', :html
|
242
|
+
def encode_file filename, format, options = {}
|
243
|
+
tokens = scan_file filename, :auto, get_scanner_options(options)
|
244
|
+
encode_tokens tokens, format, options
|
245
|
+
end
|
246
|
+
|
247
|
+
# Highlight a file into a HTML <div>.
|
248
|
+
#
|
249
|
+
# CSS styles use classes, so you have to include a stylesheet
|
250
|
+
# in your output.
|
251
|
+
#
|
252
|
+
# See encode.
|
253
|
+
def highlight_file filename, options = { :css => :class }, format = :div
|
254
|
+
encode_file filename, format, options
|
255
|
+
end
|
256
|
+
|
257
|
+
# Finds the Encoder class for +format+ and creates an instance, passing
|
258
|
+
# +options+ to it.
|
259
|
+
#
|
260
|
+
# Example:
|
261
|
+
# require 'coderay'
|
262
|
+
#
|
263
|
+
# stats = CodeRay.encoder(:statistic)
|
264
|
+
# stats.encode("puts 17 + 4\n", :ruby)
|
265
|
+
#
|
266
|
+
# puts '%d out of %d tokens have the kind :integer.' % [
|
267
|
+
# stats.type_stats[:integer].count,
|
268
|
+
# stats.real_token_count
|
269
|
+
# ]
|
270
|
+
# #-> 2 out of 4 tokens have the kind :integer.
|
271
|
+
def encoder format, options = {}
|
272
|
+
Encoders[format].new options
|
273
|
+
end
|
274
|
+
|
275
|
+
# Finds the Scanner class for +lang+ and creates an instance, passing
|
276
|
+
# +options+ to it.
|
277
|
+
#
|
278
|
+
# See Scanner.new.
|
279
|
+
def scanner lang, options = {}
|
280
|
+
Scanners[lang].new '', options
|
281
|
+
end
|
282
|
+
|
283
|
+
# Extract the options for the scanner from the +options+ hash.
|
284
|
+
#
|
285
|
+
# Returns an empty Hash if <tt>:scanner_options</tt> is not set.
|
286
|
+
#
|
287
|
+
# This is used if a method like CodeRay.encode has to provide options
|
288
|
+
# for Encoder _and_ scanner.
|
289
|
+
def get_scanner_options options
|
290
|
+
options.fetch :scanner_options, {}
|
291
|
+
end
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
# This Exception is raised when you try to stream with something that is not
|
296
|
+
# capable of streaming.
|
297
|
+
class NotStreamableError < Exception
|
298
|
+
def initialize obj
|
299
|
+
@obj = obj
|
300
|
+
end
|
301
|
+
|
302
|
+
def to_s
|
303
|
+
'%s is not Streamable!' % @obj.class
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
# A dummy module that is included by subclasses of CodeRay::Scanner an CodeRay::Encoder
|
308
|
+
# to show that they are able to handle streams.
|
309
|
+
module Streamable
|
310
|
+
end
|
311
|
+
|
312
|
+
end
|
313
|
+
|
314
|
+
# Run a test script.
|
315
|
+
if $0 == __FILE__
|
316
|
+
$stderr.print 'Press key to print demo.'; gets
|
317
|
+
# Just use this file as an example of Ruby code.
|
318
|
+
code = File.read(__FILE__)[/module CodeRay.*/m]
|
319
|
+
print CodeRay.scan(code, :ruby).html
|
320
|
+
end
|