annotate vendor/plugins/coderay-0.9.2/lib/coderay/.svn/text-base/duo.rb.svn-base @ 866:2cd212a468b6 bug_152

Close obsolete branch bug_152
author Chris Cannam
date Wed, 11 May 2011 10:08:34 +0100
parents 513646585e45
children
rev   line source
Chris@0 1 module CodeRay
Chris@0 2
Chris@0 3 # = Duo
Chris@0 4 #
Chris@0 5 # A Duo is a convenient way to use CodeRay. You just create a Duo,
Chris@0 6 # giving it a lang (language of the input code) and a format (desired
Chris@0 7 # output format), and call Duo#highlight with the code.
Chris@0 8 #
Chris@0 9 # Duo makes it easy to re-use both scanner and encoder for a repetitive
Chris@0 10 # task. It also provides a very easy interface syntax:
Chris@0 11 #
Chris@0 12 # require 'coderay'
Chris@0 13 # CodeRay::Duo[:python, :div].highlight 'import this'
Chris@0 14 #
Chris@0 15 # Until you want to do uncommon things with CodeRay, I recommend to use
Chris@0 16 # this method, since it takes care of everything.
Chris@0 17 class Duo
Chris@0 18
Chris@0 19 attr_accessor :lang, :format, :options
Chris@0 20
Chris@0 21 # Create a new Duo, holding a lang and a format to highlight code.
Chris@0 22 #
Chris@0 23 # simple:
Chris@0 24 # CodeRay::Duo[:ruby, :page].highlight 'bla 42'
Chris@0 25 #
Chris@0 26 # streaming:
Chris@0 27 # CodeRay::Duo[:ruby, :page].highlight 'bar 23', :stream => true
Chris@0 28 #
Chris@0 29 # with options:
Chris@0 30 # CodeRay::Duo[:ruby, :html, :hint => :debug].highlight '????::??'
Chris@0 31 #
Chris@0 32 # alternative syntax without options:
Chris@0 33 # CodeRay::Duo[:ruby => :statistic].encode 'class << self; end'
Chris@0 34 #
Chris@0 35 # alternative syntax with options:
Chris@0 36 # CodeRay::Duo[{ :ruby => :statistic }, :do => :something].encode 'abc'
Chris@0 37 #
Chris@0 38 # The options are forwarded to scanner and encoder
Chris@0 39 # (see CodeRay.get_scanner_options).
Chris@0 40 def initialize lang = nil, format = nil, options = {}
Chris@0 41 if format == nil and lang.is_a? Hash and lang.size == 1
Chris@0 42 @lang = lang.keys.first
Chris@0 43 @format = lang[@lang]
Chris@0 44 else
Chris@0 45 @lang = lang
Chris@0 46 @format = format
Chris@0 47 end
Chris@0 48 @options = options
Chris@0 49 end
Chris@0 50
Chris@0 51 class << self
Chris@0 52 # To allow calls like Duo[:ruby, :html].highlight.
Chris@0 53 alias [] new
Chris@0 54 end
Chris@0 55
Chris@0 56 # The scanner of the duo. Only created once.
Chris@0 57 def scanner
Chris@0 58 @scanner ||= CodeRay.scanner @lang, CodeRay.get_scanner_options(@options)
Chris@0 59 end
Chris@0 60
Chris@0 61 # The encoder of the duo. Only created once.
Chris@0 62 def encoder
Chris@0 63 @encoder ||= CodeRay.encoder @format, @options
Chris@0 64 end
Chris@0 65
Chris@0 66 # Tokenize and highlight the code using +scanner+ and +encoder+.
Chris@0 67 #
Chris@0 68 # If the :stream option is set, the Duo will go into streaming mode,
Chris@0 69 # saving memory for the cost of time.
Chris@0 70 def encode code, options = { :stream => false }
Chris@0 71 stream = options.delete :stream
Chris@0 72 options = @options.merge options
Chris@0 73 if stream
Chris@0 74 encoder.encode_stream(code, @lang, options)
Chris@0 75 else
Chris@0 76 scanner.code = code
Chris@0 77 encoder.encode_tokens(scanner.tokenize, options)
Chris@0 78 end
Chris@0 79 end
Chris@0 80 alias highlight encode
Chris@0 81
Chris@0 82 end
Chris@0 83
Chris@0 84 end
Chris@0 85