module CodeRay
def self.coderay_path *path
def self.coderay_path *path File.join CODERAY_PATH, *path end
def encode code, lang, format, options = {}
+options+ will be passed to the Encoder.
encodes it with the Encoder for +format+.
This scans +code+ with the the Scanner for +lang+ and then
Encode a string.
def encode code, lang, format, options = {} encoder(format, options).encode code, lang, options end
def encode_file filename, format, options = {}
require 'coderay'
Example:
Notice that the second argument is the output +format+, not the input language.
See CodeRay.scan_file.
Encodes +filename+ (a path to a code file) with the Scanner for +lang+.
def encode_file filename, format, options = {} tokens = scan_file filename, :auto, get_scanner_options(options) encode_tokens tokens, format, options end
def encode_tokens tokens, format, options = {}
puts CodeRay.encode_tokens(tokens, :span)
tokens = CodeRay.scan '1 + 2', :ruby
# Highlight a short Ruby code example in a HTML span
require 'coderay'
Use this together with CodeRay.scan:
Encode pre-scanned Tokens.
def encode_tokens tokens, format, options = {} encoder(format, options).encode_tokens tokens, options end
def encoder format, options = {}
]
stats.real_token_count
stats.type_stats[:integer].count,
puts '%d out of %d tokens have the kind :integer.' % [
stats.encode("puts 17 + 4\n", :ruby)
stats = CodeRay.encoder(:statistic)
require 'coderay'
Example:
+options+ to it.
Finds the Encoder class for +format+ and creates an instance, passing
def encoder format, options = {} CodeRay::Encoders[format].new options end
def get_scanner_options options
This is used if a method like CodeRay.encode has to provide options
Returns an empty Hash if :scanner_options is not set.
Extract the options for the scanner from the +options+ hash.
def get_scanner_options options options.fetch :scanner_options, {} end
def highlight code, lang, options = { :css => :class }, format = :div
in your output.
CSS styles use classes, so you have to include a stylesheet
Highlight a string into a HTML
.
def highlight code, lang, options = { :css => :class }, format = :div encode code, lang, format, options end
def highlight_file filename, options = { :css => :class }, format = :div
in your output.
CSS styles use classes, so you have to include a stylesheet
Highlight a file into a HTML
.
def highlight_file filename, options = { :css => :class }, format = :div encode_file filename, format, options end
def scan code, lang, options = {}, &block
page = CodeRay.scan("puts 'Hello, world!'", :ruby).html
require 'coderay'
This is a simple way to use CodeRay. Example:
Scans the given +code+ (a String) with the Scanner for +lang+.
def scan code, lang, options = {}, &block CodeRay::TokensProxy.new code, lang, options, block end
def scan_file filename, lang = :auto, options = {}, &block
require 'coderay'
Example:
Calls CodeRay.scan.
CodeRay::Scanners::Text.
determine it. If it cannot find out what type it is, it uses
If +lang+ is :auto or omitted, the CodeRay::FileType module is used to
Scans +filename+ (a path to a code file) with the Scanner for +lang+.
def scan_file filename, lang = :auto, options = {}, &block lang = CodeRay::FileType.fetch filename, :text, true if lang == :auto code = File.read filename scan code, lang, options, &block end
def scanner lang, options = {}, &block
+options+ to it.
Finds the Scanner class for +lang+ and creates an instance, passing
def scanner lang, options = {}, &block CodeRay::Scanners[lang].new '', options, &block end