Module: CodeRay
- Defined in:
- lib/coderay.rb,
lib/coderay/duo.rb,
lib/coderay/style.rb,
lib/coderay/tokens.rb,
lib/coderay/encoder.rb,
lib/coderay/scanner.rb,
lib/coderay/scanners/c.rb,
lib/coderay/styles/_map.rb,
lib/coderay/encoders/div.rb,
lib/coderay/encoders/xml.rb,
lib/coderay/for_redcloth.rb,
lib/coderay/scanners/cpp.rb,
lib/coderay/scanners/css.rb,
lib/coderay/scanners/sql.rb,
lib/coderay/scanners/xml.rb,
lib/coderay/encoders/_map.rb,
lib/coderay/encoders/html.rb,
lib/coderay/encoders/json.rb,
lib/coderay/encoders/null.rb,
lib/coderay/encoders/page.rb,
lib/coderay/encoders/span.rb,
lib/coderay/encoders/term.rb,
lib/coderay/encoders/text.rb,
lib/coderay/encoders/yaml.rb,
lib/coderay/scanners/_map.rb,
lib/coderay/scanners/diff.rb,
lib/coderay/scanners/html.rb,
lib/coderay/scanners/java.rb,
lib/coderay/scanners/json.rb,
lib/coderay/scanners/ruby.rb,
lib/coderay/scanners/yaml.rb,
lib/coderay/styles/cycnus.rb,
lib/coderay/styles/murphy.rb,
lib/coderay/token_classes.rb,
lib/coderay/encoders/count.rb,
lib/coderay/encoders/debug.rb,
lib/coderay/helpers/plugin.rb,
lib/coderay/scanners/debug.rb,
lib/coderay/scanners/rhtml.rb,
lib/coderay/encoders/filter.rb,
lib/coderay/scanners/delphi.rb,
lib/coderay/scanners/groovy.rb,
lib/coderay/scanners/python.rb,
lib/coderay/scanners/scheme.rb,
lib/coderay/encoders/html/css.rb,
lib/coderay/helpers/file_type.rb,
lib/coderay/helpers/word_list.rb,
lib/coderay/encoders/statistic.rb,
lib/coderay/scanners/plaintext.rb,
lib/coderay/encoders/html/output.rb,
lib/coderay/scanners/java_script.rb,
lib/coderay/scanners/nitro_xhtml.rb,
lib/coderay/encoders/lines_of_code.rb,
lib/coderay/scanners/ruby/patterns.rb,
lib/coderay/encoders/comment_filter.rb,
lib/coderay/encoders/html/numerization.rb,
lib/coderay/encoders/token_class_filter.rb,
lib/coderay/scanners/java/builtin_types.rb
Overview
encoders/term.rb By Rob Aldred (robaldred.co.uk) Based on idea by Nathan Weizenbaum (nex-3.com) MIT License (www.opensource.org/licenses/mit-license.php)
A CodeRay encoder that outputs code highlighted for a color terminal. Check out robaldred.co.uk
Defined Under Namespace
Modules: Encoders, FileType, ForRedCloth, Plugin, PluginHost, Scanners, Streamable, Styles Classes: CaseIgnoringWordList, Duo, NotStreamableError, TokenStream, Tokens, WordList
Constant Summary collapse
- VERSION =
Version: Major.Minor.Teeny[.Revision] Major: 0 for pre-stable, 1 for stable Minor: feature milestone Teeny: development state, 0 for pre-release Revision: Subversion Revision number (generated on rake gem:make)
'0.9.8'
Class Method Summary collapse
-
.encode(code, lang, format, options = {}) ⇒ Object
Encode a string.
-
.encode_file(filename, format, options = {}) ⇒ Object
Encodes
filename(a path to a code file) with the Scanner forlang. -
.encode_stream(code, lang, format, options = {}) ⇒ Object
Encode a string in Streaming mode.
-
.encode_tokens(tokens, format, options = {}) ⇒ Object
Encode pre-scanned Tokens.
-
.encoder(format, options = {}) ⇒ Object
Finds the Encoder class for
formatand creates an instance, passingoptionsto it. -
.get_scanner_options(options) ⇒ Object
Extract the options for the scanner from the
optionshash. -
.highlight(code, lang, options = { :css => :class }, format = :div) ⇒ Object
Highlight a string into a HTML <div>.
-
.highlight_file(filename, options = { :css => :class }, format = :div) ⇒ Object
Highlight a file into a HTML <div>.
-
.require_plugin(path) ⇒ Object
Convenience method for plugin loading.
-
.scan(code, lang, options = {}, &block) ⇒ Object
Scans the given
code(a String) with the Scanner forlang. -
.scan_file(filename, lang = :auto, options = {}, &block) ⇒ Object
Scans
filename(a path to a code file) with the Scanner forlang. -
.scan_stream(code, lang, options = {}, &block) ⇒ Object
Scan the
code(a string) with the scanner forlang. -
.scanner(lang, options = {}) ⇒ Object
Finds the Scanner class for
langand creates an instance, passingoptionsto it.
Class Method Details
.encode(code, lang, format, options = {}) ⇒ Object
Encode a string.
This scans code with the the Scanner for lang and then encodes it with the Encoder for format. options will be passed to the Encoder.
See CodeRay::Encoder.encode
209 210 211 |
# File 'lib/coderay.rb', line 209 def encode code, lang, format, = {} encoder(format, ).encode code, lang, end |
.encode_file(filename, format, options = {}) ⇒ Object
Encodes filename (a path to a code file) with the Scanner for lang.
See CodeRay.scan_file. Notice that the second argument is the output format, not the input language.
Example:
require 'coderay'
page = CodeRay.encode_file 'some_c_code.c', :html
244 245 246 247 |
# File 'lib/coderay.rb', line 244 def encode_file filename, format, = {} tokens = scan_file filename, :auto, () encode_tokens tokens, format, end |
.encode_stream(code, lang, format, options = {}) ⇒ Object
Encode a string in Streaming mode.
This starts scanning code with the the Scanner for lang while encodes the output with the Encoder for format. options will be passed to the Encoder.
See CodeRay::Encoder.encode_stream
198 199 200 |
# File 'lib/coderay.rb', line 198 def encode_stream code, lang, format, = {} encoder(format, ).encode_stream code, lang, end |
.encode_tokens(tokens, format, options = {}) ⇒ Object
232 233 234 |
# File 'lib/coderay.rb', line 232 def encode_tokens tokens, format, = {} encoder(format, ).encode_tokens tokens, end |
.encoder(format, options = {}) ⇒ Object
Finds the Encoder class for format and creates an instance, passing options to it.
Example:
require 'coderay'
stats = CodeRay.encoder(:statistic)
stats.encode("puts 17 + 4\n", :ruby)
puts '%d out of %d tokens have the kind :integer.' % [
stats.type_stats[:integer].count,
stats.real_token_count
]
#-> 2 out of 4 tokens have the kind :integer.
273 274 275 |
# File 'lib/coderay.rb', line 273 def encoder format, = {} Encoders[format].new end |
.get_scanner_options(options) ⇒ Object
Extract the options for the scanner from the options hash.
Returns an empty Hash if :scanner_options is not set.
This is used if a method like CodeRay.encode has to provide options for Encoder and scanner.
291 292 293 |
# File 'lib/coderay.rb', line 291 def .fetch :scanner_options, {} end |
.highlight(code, lang, options = { :css => :class }, format = :div) ⇒ Object
Highlight a string into a HTML <div>.
CSS styles use classes, so you have to include a stylesheet in your output.
See encode.
219 220 221 |
# File 'lib/coderay.rb', line 219 def highlight code, lang, = { :css => :class }, format = :div encode code, lang, format, end |
.highlight_file(filename, options = { :css => :class }, format = :div) ⇒ Object
Highlight a file into a HTML <div>.
CSS styles use classes, so you have to include a stylesheet in your output.
See encode.
255 256 257 |
# File 'lib/coderay.rb', line 255 def highlight_file filename, = { :css => :class }, format = :div encode_file filename, format, end |
.require_plugin(path) ⇒ Object
Convenience method for plugin loading. The syntax used is:
CodeRay.require_plugin '<Host ID>/<Plugin ID>'
Returns the loaded plugin.
341 342 343 344 345 346 347 |
# File 'lib/coderay/helpers/plugin.rb', line 341 def self.require_plugin path host_id, plugin_id = path.split '/', 2 host = PluginHost.host_by_id(host_id) raise PluginHost::HostNotFound, "No host for #{host_id.inspect} found." unless host host.load plugin_id end |
.scan(code, lang, options = {}, &block) ⇒ Object
Scans the given code (a String) with the Scanner for lang.
This is a simple way to use CodeRay. Example:
require 'coderay'
page = CodeRay.scan("puts 'Hello, world!'", :ruby).html
See also demo/demo_simple.
156 157 158 159 |
# File 'lib/coderay.rb', line 156 def scan code, lang, = {}, &block scanner = Scanners[lang].new code, , &block scanner.tokenize end |
.scan_file(filename, lang = :auto, options = {}, &block) ⇒ Object
Scans filename (a path to a code file) with the Scanner for lang.
If lang is :auto or omitted, the CodeRay::FileType module is used to determine it. If it cannot find out what type it is, it uses CodeRay::Scanners::Plaintext.
Calls CodeRay.scan.
Example:
require 'coderay'
page = CodeRay.scan_file('some_c_code.c').html
172 173 174 175 176 177 178 179 |
# File 'lib/coderay.rb', line 172 def scan_file filename, lang = :auto, = {}, &block file = IO.read filename if lang == :auto require 'coderay/helpers/file_type' lang = FileType.fetch filename, :plaintext, true end scan file, lang, = {}, &block end |
.scan_stream(code, lang, options = {}, &block) ⇒ Object
Scan the code (a string) with the scanner for lang.
Calls scan.
See CodeRay.scan.
186 187 188 189 |
# File 'lib/coderay.rb', line 186 def scan_stream code, lang, = {}, &block [:stream] = true scan code, lang, , &block end |
.scanner(lang, options = {}) ⇒ Object
Finds the Scanner class for lang and creates an instance, passing options to it.
See Scanner.new.
281 282 283 |
# File 'lib/coderay.rb', line 281 def scanner lang, = {} Scanners[lang].new '', end |