Chris@909
|
1 module CodeRay
|
Chris@909
|
2
|
Chris@909
|
3 # = Duo
|
Chris@909
|
4 #
|
Chris@909
|
5 # A Duo is a convenient way to use CodeRay. You just create a Duo,
|
Chris@909
|
6 # giving it a lang (language of the input code) and a format (desired
|
Chris@909
|
7 # output format), and call Duo#highlight with the code.
|
Chris@909
|
8 #
|
Chris@909
|
9 # Duo makes it easy to re-use both scanner and encoder for a repetitive
|
Chris@909
|
10 # task. It also provides a very easy interface syntax:
|
Chris@909
|
11 #
|
Chris@909
|
12 # require 'coderay'
|
Chris@909
|
13 # CodeRay::Duo[:python, :div].highlight 'import this'
|
Chris@909
|
14 #
|
Chris@909
|
15 # Until you want to do uncommon things with CodeRay, I recommend to use
|
Chris@909
|
16 # this method, since it takes care of everything.
|
Chris@909
|
17 class Duo
|
Chris@909
|
18
|
Chris@909
|
19 attr_accessor :lang, :format, :options
|
Chris@909
|
20
|
Chris@909
|
21 # Create a new Duo, holding a lang and a format to highlight code.
|
Chris@909
|
22 #
|
Chris@909
|
23 # simple:
|
Chris@909
|
24 # CodeRay::Duo[:ruby, :html].highlight 'bla 42'
|
Chris@909
|
25 #
|
Chris@909
|
26 # with options:
|
Chris@909
|
27 # CodeRay::Duo[:ruby, :html, :hint => :debug].highlight '????::??'
|
Chris@909
|
28 #
|
Chris@909
|
29 # alternative syntax without options:
|
Chris@909
|
30 # CodeRay::Duo[:ruby => :statistic].encode 'class << self; end'
|
Chris@909
|
31 #
|
Chris@909
|
32 # alternative syntax with options:
|
Chris@909
|
33 # CodeRay::Duo[{ :ruby => :statistic }, :do => :something].encode 'abc'
|
Chris@909
|
34 #
|
Chris@909
|
35 # The options are forwarded to scanner and encoder
|
Chris@909
|
36 # (see CodeRay.get_scanner_options).
|
Chris@909
|
37 def initialize lang = nil, format = nil, options = {}
|
Chris@909
|
38 if format.nil? && lang.is_a?(Hash) && lang.size == 1
|
Chris@909
|
39 @lang = lang.keys.first
|
Chris@909
|
40 @format = lang[@lang]
|
Chris@909
|
41 else
|
Chris@909
|
42 @lang = lang
|
Chris@909
|
43 @format = format
|
Chris@909
|
44 end
|
Chris@909
|
45 @options = options
|
Chris@909
|
46 end
|
Chris@909
|
47
|
Chris@909
|
48 class << self
|
Chris@909
|
49 # To allow calls like Duo[:ruby, :html].highlight.
|
Chris@909
|
50 alias [] new
|
Chris@909
|
51 end
|
Chris@909
|
52
|
Chris@909
|
53 # The scanner of the duo. Only created once.
|
Chris@909
|
54 def scanner
|
Chris@909
|
55 @scanner ||= CodeRay.scanner @lang, CodeRay.get_scanner_options(@options)
|
Chris@909
|
56 end
|
Chris@909
|
57
|
Chris@909
|
58 # The encoder of the duo. Only created once.
|
Chris@909
|
59 def encoder
|
Chris@909
|
60 @encoder ||= CodeRay.encoder @format, @options
|
Chris@909
|
61 end
|
Chris@909
|
62
|
Chris@909
|
63 # Tokenize and highlight the code using +scanner+ and +encoder+.
|
Chris@909
|
64 def encode code, options = {}
|
Chris@909
|
65 options = @options.merge options
|
Chris@909
|
66 encoder.encode(code, @lang, options)
|
Chris@909
|
67 end
|
Chris@909
|
68 alias highlight encode
|
Chris@909
|
69
|
Chris@909
|
70 # Allows to use Duo like a proc object:
|
Chris@909
|
71 #
|
Chris@909
|
72 # CodeRay::Duo[:python => :yaml].call(code)
|
Chris@909
|
73 #
|
Chris@909
|
74 # or, in Ruby 1.9 and later:
|
Chris@909
|
75 #
|
Chris@909
|
76 # CodeRay::Duo[:python => :yaml].(code)
|
Chris@909
|
77 alias call encode
|
Chris@909
|
78
|
Chris@909
|
79 end
|
Chris@909
|
80
|
Chris@909
|
81 end
|