annotate vendor/gems/coderay-0.9.7/lib/coderay.rb @ 1176:7d9db6065048 bug_352

Close obsolete branch bug_352
author Chris Cannam
date Wed, 01 Feb 2012 16:09:00 +0000
parents 0579821a129a
children
rev   line source
Chris@210 1 # = CodeRay Library
Chris@210 2 #
Chris@210 3 # CodeRay is a Ruby library for syntax highlighting.
Chris@210 4 #
Chris@210 5 # I try to make CodeRay easy to use and intuitive, but at the same time fully featured, complete,
Chris@210 6 # fast and efficient.
Chris@210 7 #
Chris@210 8 # See README.
Chris@210 9 #
Chris@210 10 # It consists mainly of
Chris@210 11 # * the main engine: CodeRay (Scanners::Scanner, Tokens/TokenStream, Encoders::Encoder), PluginHost
Chris@210 12 # * the scanners in CodeRay::Scanners
Chris@210 13 # * the encoders in CodeRay::Encoders
Chris@210 14 #
Chris@210 15 # Here's a fancy graphic to light up this gray docu:
Chris@210 16 #
Chris@210 17 # http://cycnus.de/raindark/coderay/scheme.png
Chris@210 18 #
Chris@210 19 # == Documentation
Chris@210 20 #
Chris@210 21 # See CodeRay, Encoders, Scanners, Tokens.
Chris@210 22 #
Chris@210 23 # == Usage
Chris@210 24 #
Chris@210 25 # Remember you need RubyGems to use CodeRay, unless you have it in your load path. Run Ruby with
Chris@210 26 # -rubygems option if required.
Chris@210 27 #
Chris@210 28 # === Highlight Ruby code in a string as html
Chris@210 29 #
Chris@210 30 # require 'coderay'
Chris@210 31 # print CodeRay.scan('puts "Hello, world!"', :ruby).html
Chris@210 32 #
Chris@210 33 # # prints something like this:
Chris@210 34 # puts <span class="s">&quot;Hello, world!&quot;</span>
Chris@210 35 #
Chris@210 36 #
Chris@210 37 # === Highlight C code from a file in a html div
Chris@210 38 #
Chris@210 39 # require 'coderay'
Chris@210 40 # print CodeRay.scan(File.read('ruby.h'), :c).div
Chris@210 41 # print CodeRay.scan_file('ruby.h').html.div
Chris@210 42 #
Chris@210 43 # You can include this div in your page. The used CSS styles can be printed with
Chris@210 44 #
Chris@210 45 # % coderay_stylesheet
Chris@210 46 #
Chris@210 47 # === Highlight without typing too much
Chris@210 48 #
Chris@210 49 # If you are one of the hasty (or lazy, or extremely curious) people, just run this file:
Chris@210 50 #
Chris@210 51 # % ruby -rubygems /path/to/coderay/coderay.rb > example.html
Chris@210 52 #
Chris@210 53 # and look at the file it created in your browser.
Chris@210 54 #
Chris@210 55 # = CodeRay Module
Chris@210 56 #
Chris@210 57 # The CodeRay module provides convenience methods for the engine.
Chris@210 58 #
Chris@210 59 # * The +lang+ and +format+ arguments select Scanner and Encoder to use. These are
Chris@210 60 # simply lower-case symbols, like <tt>:python</tt> or <tt>:html</tt>.
Chris@210 61 # * All methods take an optional hash as last parameter, +options+, that is send to
Chris@210 62 # the Encoder / Scanner.
Chris@210 63 # * Input and language are always sorted in this order: +code+, +lang+.
Chris@210 64 # (This is in alphabetical order, if you need a mnemonic ;)
Chris@210 65 #
Chris@210 66 # You should be able to highlight everything you want just using these methods;
Chris@210 67 # so there is no need to dive into CodeRay's deep class hierarchy.
Chris@210 68 #
Chris@210 69 # The examples in the demo directory demonstrate common cases using this interface.
Chris@210 70 #
Chris@210 71 # = Basic Access Ways
Chris@210 72 #
Chris@210 73 # Read this to get a general view what CodeRay provides.
Chris@210 74 #
Chris@210 75 # == Scanning
Chris@210 76 #
Chris@210 77 # Scanning means analysing an input string, splitting it up into Tokens.
Chris@210 78 # Each Token knows about what type it is: string, comment, class name, etc.
Chris@210 79 #
Chris@210 80 # Each +lang+ (language) has its own Scanner; for example, <tt>:ruby</tt> code is
Chris@210 81 # handled by CodeRay::Scanners::Ruby.
Chris@210 82 #
Chris@210 83 # CodeRay.scan:: Scan a string in a given language into Tokens.
Chris@210 84 # This is the most common method to use.
Chris@210 85 # CodeRay.scan_file:: Scan a file and guess the language using FileType.
Chris@210 86 #
Chris@210 87 # The Tokens object you get from these methods can encode itself; see Tokens.
Chris@210 88 #
Chris@210 89 # == Encoding
Chris@210 90 #
Chris@210 91 # Encoding means compiling Tokens into an output. This can be colored HTML or
Chris@210 92 # LaTeX, a textual statistic or just the number of non-whitespace tokens.
Chris@210 93 #
Chris@210 94 # Each Encoder provides output in a specific +format+, so you select Encoders via
Chris@210 95 # formats like <tt>:html</tt> or <tt>:statistic</tt>.
Chris@210 96 #
Chris@210 97 # CodeRay.encode:: Scan and encode a string in a given language.
Chris@210 98 # CodeRay.encode_tokens:: Encode the given tokens.
Chris@210 99 # CodeRay.encode_file:: Scan a file, guess the language using FileType and encode it.
Chris@210 100 #
Chris@210 101 # == Streaming
Chris@210 102 #
Chris@210 103 # Streaming saves RAM by running Scanner and Encoder in some sort of
Chris@210 104 # pipe mode; see TokenStream.
Chris@210 105 #
Chris@210 106 # CodeRay.scan_stream:: Scan in stream mode.
Chris@210 107 #
Chris@210 108 # == All-in-One Encoding
Chris@210 109 #
Chris@210 110 # CodeRay.encode:: Highlight a string with a given input and output format.
Chris@210 111 #
Chris@210 112 # == Instanciating
Chris@210 113 #
Chris@210 114 # You can use an Encoder instance to highlight multiple inputs. This way, the setup
Chris@210 115 # for this Encoder must only be done once.
Chris@210 116 #
Chris@210 117 # CodeRay.encoder:: Create an Encoder instance with format and options.
Chris@210 118 # CodeRay.scanner:: Create an Scanner instance for lang, with '' as default code.
Chris@210 119 #
Chris@210 120 # To make use of CodeRay.scanner, use CodeRay::Scanner::code=.
Chris@210 121 #
Chris@210 122 # The scanning methods provide more flexibility; we recommend to use these.
Chris@210 123 #
Chris@210 124 # == Reusing Scanners and Encoders
Chris@210 125 #
Chris@210 126 # If you want to re-use scanners and encoders (because that is faster), see
Chris@210 127 # CodeRay::Duo for the most convenient (and recommended) interface.
Chris@210 128 module CodeRay
Chris@210 129
Chris@210 130 $CODERAY_DEBUG ||= false
Chris@210 131
Chris@210 132 # Version: Major.Minor.Teeny[.Revision]
Chris@210 133 # Major: 0 for pre-stable, 1 for stable
Chris@210 134 # Minor: feature milestone
Chris@210 135 # Teeny: development state, 0 for pre-release
Chris@210 136 # Revision: Subversion Revision number (generated on rake gem:make)
Chris@210 137 VERSION = '0.9.7'
Chris@210 138
Chris@210 139 require 'coderay/tokens'
Chris@210 140 require 'coderay/token_classes'
Chris@210 141 require 'coderay/scanner'
Chris@210 142 require 'coderay/encoder'
Chris@210 143 require 'coderay/duo'
Chris@210 144 require 'coderay/style'
Chris@210 145
Chris@210 146
Chris@210 147 class << self
Chris@210 148
Chris@210 149 # Scans the given +code+ (a String) with the Scanner for +lang+.
Chris@210 150 #
Chris@210 151 # This is a simple way to use CodeRay. Example:
Chris@210 152 # require 'coderay'
Chris@210 153 # page = CodeRay.scan("puts 'Hello, world!'", :ruby).html
Chris@210 154 #
Chris@210 155 # See also demo/demo_simple.
Chris@210 156 def scan code, lang, options = {}, &block
Chris@210 157 scanner = Scanners[lang].new code, options, &block
Chris@210 158 scanner.tokenize
Chris@210 159 end
Chris@210 160
Chris@210 161 # Scans +filename+ (a path to a code file) with the Scanner for +lang+.
Chris@210 162 #
Chris@210 163 # If +lang+ is :auto or omitted, the CodeRay::FileType module is used to
Chris@210 164 # determine it. If it cannot find out what type it is, it uses
Chris@210 165 # CodeRay::Scanners::Plaintext.
Chris@210 166 #
Chris@210 167 # Calls CodeRay.scan.
Chris@210 168 #
Chris@210 169 # Example:
Chris@210 170 # require 'coderay'
Chris@210 171 # page = CodeRay.scan_file('some_c_code.c').html
Chris@210 172 def scan_file filename, lang = :auto, options = {}, &block
Chris@210 173 file = IO.read filename
Chris@210 174 if lang == :auto
Chris@210 175 require 'coderay/helpers/file_type'
Chris@210 176 lang = FileType.fetch filename, :plaintext, true
Chris@210 177 end
Chris@210 178 scan file, lang, options = {}, &block
Chris@210 179 end
Chris@210 180
Chris@210 181 # Scan the +code+ (a string) with the scanner for +lang+.
Chris@210 182 #
Chris@210 183 # Calls scan.
Chris@210 184 #
Chris@210 185 # See CodeRay.scan.
Chris@210 186 def scan_stream code, lang, options = {}, &block
Chris@210 187 options[:stream] = true
Chris@210 188 scan code, lang, options, &block
Chris@210 189 end
Chris@210 190
Chris@210 191 # Encode a string in Streaming mode.
Chris@210 192 #
Chris@210 193 # This starts scanning +code+ with the the Scanner for +lang+
Chris@210 194 # while encodes the output with the Encoder for +format+.
Chris@210 195 # +options+ will be passed to the Encoder.
Chris@210 196 #
Chris@210 197 # See CodeRay::Encoder.encode_stream
Chris@210 198 def encode_stream code, lang, format, options = {}
Chris@210 199 encoder(format, options).encode_stream code, lang, options
Chris@210 200 end
Chris@210 201
Chris@210 202 # Encode a string.
Chris@210 203 #
Chris@210 204 # This scans +code+ with the the Scanner for +lang+ and then
Chris@210 205 # encodes it with the Encoder for +format+.
Chris@210 206 # +options+ will be passed to the Encoder.
Chris@210 207 #
Chris@210 208 # See CodeRay::Encoder.encode
Chris@210 209 def encode code, lang, format, options = {}
Chris@210 210 encoder(format, options).encode code, lang, options
Chris@210 211 end
Chris@210 212
Chris@210 213 # Highlight a string into a HTML <div>.
Chris@210 214 #
Chris@210 215 # CSS styles use classes, so you have to include a stylesheet
Chris@210 216 # in your output.
Chris@210 217 #
Chris@210 218 # See encode.
Chris@210 219 def highlight code, lang, options = { :css => :class }, format = :div
Chris@210 220 encode code, lang, format, options
Chris@210 221 end
Chris@210 222
Chris@210 223 # Encode pre-scanned Tokens.
Chris@210 224 # Use this together with CodeRay.scan:
Chris@210 225 #
Chris@210 226 # require 'coderay'
Chris@210 227 #
Chris@210 228 # # Highlight a short Ruby code example in a HTML span
Chris@210 229 # tokens = CodeRay.scan '1 + 2', :ruby
Chris@210 230 # puts CodeRay.encode_tokens(tokens, :span)
Chris@210 231 #
Chris@210 232 def encode_tokens tokens, format, options = {}
Chris@210 233 encoder(format, options).encode_tokens tokens, options
Chris@210 234 end
Chris@210 235
Chris@210 236 # Encodes +filename+ (a path to a code file) with the Scanner for +lang+.
Chris@210 237 #
Chris@210 238 # See CodeRay.scan_file.
Chris@210 239 # Notice that the second argument is the output +format+, not the input language.
Chris@210 240 #
Chris@210 241 # Example:
Chris@210 242 # require 'coderay'
Chris@210 243 # page = CodeRay.encode_file 'some_c_code.c', :html
Chris@210 244 def encode_file filename, format, options = {}
Chris@210 245 tokens = scan_file filename, :auto, get_scanner_options(options)
Chris@210 246 encode_tokens tokens, format, options
Chris@210 247 end
Chris@210 248
Chris@210 249 # Highlight a file into a HTML <div>.
Chris@210 250 #
Chris@210 251 # CSS styles use classes, so you have to include a stylesheet
Chris@210 252 # in your output.
Chris@210 253 #
Chris@210 254 # See encode.
Chris@210 255 def highlight_file filename, options = { :css => :class }, format = :div
Chris@210 256 encode_file filename, format, options
Chris@210 257 end
Chris@210 258
Chris@210 259 # Finds the Encoder class for +format+ and creates an instance, passing
Chris@210 260 # +options+ to it.
Chris@210 261 #
Chris@210 262 # Example:
Chris@210 263 # require 'coderay'
Chris@210 264 #
Chris@210 265 # stats = CodeRay.encoder(:statistic)
Chris@210 266 # stats.encode("puts 17 + 4\n", :ruby)
Chris@210 267 #
Chris@210 268 # puts '%d out of %d tokens have the kind :integer.' % [
Chris@210 269 # stats.type_stats[:integer].count,
Chris@210 270 # stats.real_token_count
Chris@210 271 # ]
Chris@210 272 # #-> 2 out of 4 tokens have the kind :integer.
Chris@210 273 def encoder format, options = {}
Chris@210 274 Encoders[format].new options
Chris@210 275 end
Chris@210 276
Chris@210 277 # Finds the Scanner class for +lang+ and creates an instance, passing
Chris@210 278 # +options+ to it.
Chris@210 279 #
Chris@210 280 # See Scanner.new.
Chris@210 281 def scanner lang, options = {}
Chris@210 282 Scanners[lang].new '', options
Chris@210 283 end
Chris@210 284
Chris@210 285 # Extract the options for the scanner from the +options+ hash.
Chris@210 286 #
Chris@210 287 # Returns an empty Hash if <tt>:scanner_options</tt> is not set.
Chris@210 288 #
Chris@210 289 # This is used if a method like CodeRay.encode has to provide options
Chris@210 290 # for Encoder _and_ scanner.
Chris@210 291 def get_scanner_options options
Chris@210 292 options.fetch :scanner_options, {}
Chris@210 293 end
Chris@210 294
Chris@210 295 end
Chris@210 296
Chris@210 297 # This Exception is raised when you try to stream with something that is not
Chris@210 298 # capable of streaming.
Chris@210 299 class NotStreamableError < Exception
Chris@210 300 def initialize obj
Chris@210 301 @obj = obj
Chris@210 302 end
Chris@210 303
Chris@210 304 def to_s
Chris@210 305 '%s is not Streamable!' % @obj.class
Chris@210 306 end
Chris@210 307 end
Chris@210 308
Chris@210 309 # A dummy module that is included by subclasses of CodeRay::Scanner an CodeRay::Encoder
Chris@210 310 # to show that they are able to handle streams.
Chris@210 311 module Streamable
Chris@210 312 end
Chris@210 313
Chris@210 314 end
Chris@210 315
Chris@210 316 # Run a test script.
Chris@210 317 if $0 == __FILE__
Chris@210 318 $stderr.print 'Press key to print demo.'; gets
Chris@210 319 # Just use this file as an example of Ruby code.
Chris@210 320 code = File.read(__FILE__)[/module CodeRay.*/m]
Chris@210 321 print CodeRay.scan(code, :ruby).html
Chris@210 322 end