encoding: utf-8
TokenKinds | = | Hash.new do |h, k| warn 'Undefined Token kind: %p' % [k] if $CODERAY_DEBUG | A Hash of all known token kinds and their associated CSS classes. | |
VERSION | = | '1.0.4' |
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.
# File lib/coderay.rb, line 190 190: def encode code, lang, format, options = {} 191: encoder(format, options).encode code, lang, options 192: end
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
# File lib/coderay.rb, line 215 215: def encode_file filename, format, options = {} 216: tokens = scan_file filename, :auto, get_scanner_options(options) 217: encode_tokens tokens, format, options 218: end
Encode pre-scanned Tokens. Use this together with CodeRay.scan:
require 'coderay' # Highlight a short Ruby code example in a HTML span tokens = CodeRay.scan '1 + 2', :ruby puts CodeRay.encode_tokens(tokens, :span)
# File lib/coderay.rb, line 203 203: def encode_tokens tokens, format, options = {} 204: encoder(format, options).encode_tokens tokens, options 205: end
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.
# File lib/coderay.rb, line 254 254: def encoder format, options = {} 255: Encoders[format].new options 256: end
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.
# File lib/coderay.rb, line 272 272: def get_scanner_options options 273: options.fetch :scanner_options, {} 274: end
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.
# File lib/coderay.rb, line 161 161: def scan code, lang, options = {}, &block 162: # FIXME: return a proxy for direct-stream encoding 163: TokensProxy.new code, lang, options, block 164: end
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::Text.
Calls CodeRay.scan.
Example:
require 'coderay' page = CodeRay.scan_file('some_c_code.c').html
# File lib/coderay.rb, line 177 177: def scan_file filename, lang = :auto, options = {}, &block 178: lang = FileType.fetch filename, :text, true if lang == :auto 179: code = File.read filename 180: scan code, lang, options, &block 181: end