To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / ac / ac319deb1c64f0f2b18947928e8e2b43b56a5313.svn-base @ 1297:0a574315af3e
History | View | Annotate | Download (8.65 KB)
| 1 |
# encoding: utf-8 |
|---|---|
| 2 |
# Encoding.default_internal = 'UTF-8' |
| 3 |
|
| 4 |
# = CodeRay Library |
| 5 |
# |
| 6 |
# CodeRay is a Ruby library for syntax highlighting. |
| 7 |
# |
| 8 |
# I try to make CodeRay easy to use and intuitive, but at the same time fully |
| 9 |
# featured, complete, fast and efficient. |
| 10 |
# |
| 11 |
# See README. |
| 12 |
# |
| 13 |
# It consists mainly of |
| 14 |
# * the main engine: CodeRay (Scanners::Scanner, Tokens, Encoders::Encoder) |
| 15 |
# * the plugin system: PluginHost, Plugin |
| 16 |
# * the scanners in CodeRay::Scanners |
| 17 |
# * the encoders in CodeRay::Encoders |
| 18 |
# * the styles in CodeRay::Styles |
| 19 |
# |
| 20 |
# Here's a fancy graphic to light up this gray docu: |
| 21 |
# |
| 22 |
# http://cycnus.de/raindark/coderay/scheme.png |
| 23 |
# |
| 24 |
# == Documentation |
| 25 |
# |
| 26 |
# See CodeRay, Encoders, Scanners, Tokens. |
| 27 |
# |
| 28 |
# == Usage |
| 29 |
# |
| 30 |
# Remember you need RubyGems to use CodeRay, unless you have it in your load |
| 31 |
# path. Run Ruby with -rubygems option if required. |
| 32 |
# |
| 33 |
# === Highlight Ruby code in a string as html |
| 34 |
# |
| 35 |
# require 'coderay' |
| 36 |
# print CodeRay.scan('puts "Hello, world!"', :ruby).html
|
| 37 |
# |
| 38 |
# # prints something like this: |
| 39 |
# puts <span class="s">"Hello, world!"</span> |
| 40 |
# |
| 41 |
# |
| 42 |
# === Highlight C code from a file in a html div |
| 43 |
# |
| 44 |
# require 'coderay' |
| 45 |
# print CodeRay.scan(File.read('ruby.h'), :c).div
|
| 46 |
# print CodeRay.scan_file('ruby.h').html.div
|
| 47 |
# |
| 48 |
# You can include this div in your page. The used CSS styles can be printed with |
| 49 |
# |
| 50 |
# % coderay_stylesheet |
| 51 |
# |
| 52 |
# === Highlight without typing too much |
| 53 |
# |
| 54 |
# If you are one of the hasty (or lazy, or extremely curious) people, just run this file: |
| 55 |
# |
| 56 |
# % ruby -rubygems /path/to/coderay/coderay.rb > example.html |
| 57 |
# |
| 58 |
# and look at the file it created in your browser. |
| 59 |
# |
| 60 |
# = CodeRay Module |
| 61 |
# |
| 62 |
# The CodeRay module provides convenience methods for the engine. |
| 63 |
# |
| 64 |
# * The +lang+ and +format+ arguments select Scanner and Encoder to use. These are |
| 65 |
# simply lower-case symbols, like <tt>:python</tt> or <tt>:html</tt>. |
| 66 |
# * All methods take an optional hash as last parameter, +options+, that is send to |
| 67 |
# the Encoder / Scanner. |
| 68 |
# * Input and language are always sorted in this order: +code+, +lang+. |
| 69 |
# (This is in alphabetical order, if you need a mnemonic ;) |
| 70 |
# |
| 71 |
# You should be able to highlight everything you want just using these methods; |
| 72 |
# so there is no need to dive into CodeRay's deep class hierarchy. |
| 73 |
# |
| 74 |
# The examples in the demo directory demonstrate common cases using this interface. |
| 75 |
# |
| 76 |
# = Basic Access Ways |
| 77 |
# |
| 78 |
# Read this to get a general view what CodeRay provides. |
| 79 |
# |
| 80 |
# == Scanning |
| 81 |
# |
| 82 |
# Scanning means analysing an input string, splitting it up into Tokens. |
| 83 |
# Each Token knows about what type it is: string, comment, class name, etc. |
| 84 |
# |
| 85 |
# Each +lang+ (language) has its own Scanner; for example, <tt>:ruby</tt> code is |
| 86 |
# handled by CodeRay::Scanners::Ruby. |
| 87 |
# |
| 88 |
# CodeRay.scan:: Scan a string in a given language into Tokens. |
| 89 |
# This is the most common method to use. |
| 90 |
# CodeRay.scan_file:: Scan a file and guess the language using FileType. |
| 91 |
# |
| 92 |
# The Tokens object you get from these methods can encode itself; see Tokens. |
| 93 |
# |
| 94 |
# == Encoding |
| 95 |
# |
| 96 |
# Encoding means compiling Tokens into an output. This can be colored HTML or |
| 97 |
# LaTeX, a textual statistic or just the number of non-whitespace tokens. |
| 98 |
# |
| 99 |
# Each Encoder provides output in a specific +format+, so you select Encoders via |
| 100 |
# formats like <tt>:html</tt> or <tt>:statistic</tt>. |
| 101 |
# |
| 102 |
# CodeRay.encode:: Scan and encode a string in a given language. |
| 103 |
# CodeRay.encode_tokens:: Encode the given tokens. |
| 104 |
# CodeRay.encode_file:: Scan a file, guess the language using FileType and encode it. |
| 105 |
# |
| 106 |
# == All-in-One Encoding |
| 107 |
# |
| 108 |
# CodeRay.encode:: Highlight a string with a given input and output format. |
| 109 |
# |
| 110 |
# == Instanciating |
| 111 |
# |
| 112 |
# You can use an Encoder instance to highlight multiple inputs. This way, the setup |
| 113 |
# for this Encoder must only be done once. |
| 114 |
# |
| 115 |
# CodeRay.encoder:: Create an Encoder instance with format and options. |
| 116 |
# CodeRay.scanner:: Create an Scanner instance for lang, with '' as default code. |
| 117 |
# |
| 118 |
# To make use of CodeRay.scanner, use CodeRay::Scanner::code=. |
| 119 |
# |
| 120 |
# The scanning methods provide more flexibility; we recommend to use these. |
| 121 |
# |
| 122 |
# == Reusing Scanners and Encoders |
| 123 |
# |
| 124 |
# If you want to re-use scanners and encoders (because that is faster), see |
| 125 |
# CodeRay::Duo for the most convenient (and recommended) interface. |
| 126 |
module CodeRay |
| 127 |
|
| 128 |
$CODERAY_DEBUG ||= false |
| 129 |
|
| 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 |
|
| 152 |
class << self |
| 153 |
|
| 154 |
# Scans the given +code+ (a String) with the Scanner for +lang+. |
| 155 |
# |
| 156 |
# This is a simple way to use CodeRay. Example: |
| 157 |
# require 'coderay' |
| 158 |
# page = CodeRay.scan("puts 'Hello, world!'", :ruby).html
|
| 159 |
# |
| 160 |
# See also demo/demo_simple. |
| 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 |
| 165 |
|
| 166 |
# Scans +filename+ (a path to a code file) with the Scanner for +lang+. |
| 167 |
# |
| 168 |
# If +lang+ is :auto or omitted, the CodeRay::FileType module is used to |
| 169 |
# determine it. If it cannot find out what type it is, it uses |
| 170 |
# CodeRay::Scanners::Text. |
| 171 |
# |
| 172 |
# Calls CodeRay.scan. |
| 173 |
# |
| 174 |
# Example: |
| 175 |
# require 'coderay' |
| 176 |
# page = CodeRay.scan_file('some_c_code.c').html
|
| 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 |
| 182 |
|
| 183 |
# Encode a string. |
| 184 |
# |
| 185 |
# This scans +code+ with the the Scanner for +lang+ and then |
| 186 |
# encodes it with the Encoder for +format+. |
| 187 |
# +options+ will be passed to the Encoder. |
| 188 |
# |
| 189 |
# See CodeRay::Encoder.encode. |
| 190 |
def encode code, lang, format, options = {}
|
| 191 |
encoder(format, options).encode code, lang, options |
| 192 |
end |
| 193 |
|
| 194 |
# Encode pre-scanned Tokens. |
| 195 |
# Use this together with CodeRay.scan: |
| 196 |
# |
| 197 |
# require 'coderay' |
| 198 |
# |
| 199 |
# # Highlight a short Ruby code example in a HTML span |
| 200 |
# tokens = CodeRay.scan '1 + 2', :ruby |
| 201 |
# puts CodeRay.encode_tokens(tokens, :span) |
| 202 |
# |
| 203 |
def encode_tokens tokens, format, options = {}
|
| 204 |
encoder(format, options).encode_tokens tokens, options |
| 205 |
end |
| 206 |
|
| 207 |
# Encodes +filename+ (a path to a code file) with the Scanner for +lang+. |
| 208 |
# |
| 209 |
# See CodeRay.scan_file. |
| 210 |
# Notice that the second argument is the output +format+, not the input language. |
| 211 |
# |
| 212 |
# Example: |
| 213 |
# require 'coderay' |
| 214 |
# page = CodeRay.encode_file 'some_c_code.c', :html |
| 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 |
| 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 |
|
| 230 |
# Highlight a file into a HTML <div>. |
| 231 |
# |
| 232 |
# CSS styles use classes, so you have to include a stylesheet |
| 233 |
# in your output. |
| 234 |
# |
| 235 |
# See encode. |
| 236 |
def highlight_file filename, options = { :css => :class }, format = :div
|
| 237 |
encode_file filename, format, options |
| 238 |
end |
| 239 |
|
| 240 |
# Finds the Encoder class for +format+ and creates an instance, passing |
| 241 |
# +options+ to it. |
| 242 |
# |
| 243 |
# Example: |
| 244 |
# require 'coderay' |
| 245 |
# |
| 246 |
# stats = CodeRay.encoder(:statistic) |
| 247 |
# stats.encode("puts 17 + 4\n", :ruby)
|
| 248 |
# |
| 249 |
# puts '%d out of %d tokens have the kind :integer.' % [ |
| 250 |
# stats.type_stats[:integer].count, |
| 251 |
# stats.real_token_count |
| 252 |
# ] |
| 253 |
# #-> 2 out of 4 tokens have the kind :integer. |
| 254 |
def encoder format, options = {}
|
| 255 |
Encoders[format].new options |
| 256 |
end |
| 257 |
|
| 258 |
# Finds the Scanner class for +lang+ and creates an instance, passing |
| 259 |
# +options+ to it. |
| 260 |
# |
| 261 |
# See Scanner.new. |
| 262 |
def scanner lang, options = {}, &block
|
| 263 |
Scanners[lang].new '', options, &block |
| 264 |
end |
| 265 |
|
| 266 |
# Extract the options for the scanner from the +options+ hash. |
| 267 |
# |
| 268 |
# Returns an empty Hash if <tt>:scanner_options</tt> is not set. |
| 269 |
# |
| 270 |
# This is used if a method like CodeRay.encode has to provide options |
| 271 |
# for Encoder _and_ scanner. |
| 272 |
def get_scanner_options options |
| 273 |
options.fetch :scanner_options, {}
|
| 274 |
end |
| 275 |
|
| 276 |
end |
| 277 |
|
| 278 |
end |