comparison vendor/plugins/coderay-0.9.2/lib/coderay/encoders/.svn/text-base/term.rb.svn-base @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
1 # encoders/term.rb
2 # By Rob Aldred (http://robaldred.co.uk)
3 # Based on idea by Nathan Weizenbaum (http://nex-3.com)
4 # MIT License (http://www.opensource.org/licenses/mit-license.php)
5 #
6 # A CodeRay encoder that outputs code highlighted for a color terminal.
7 # Check out http://robaldred.co.uk
8
9 module CodeRay
10 module Encoders
11 class Term < Encoder
12 register_for :term
13
14 TOKEN_COLORS = {
15 :annotation => '35',
16 :attribute_name => '33',
17 :attribute_name_fat => '33',
18 :attribute_value => '31',
19 :attribute_value_fat => '31',
20 :bin => '1;35',
21 :char => {:self => '36', :delimiter => '34'},
22 :class => '1;35',
23 :class_variable => '36',
24 :color => '32',
25 :comment => '37',
26 :complex => '34',
27 :constant => ['34', '4'],
28 :decoration => '35',
29 :definition => '1;32',
30 :directive => ['32', '4'],
31 :doc => '46',
32 :doctype => '1;30',
33 :doc_string => ['31', '4'],
34 :entity => '33',
35 :error => ['1;33', '41'],
36 :exception => '1;31',
37 :float => '1;35',
38 :function => '1;34',
39 :global_variable => '42',
40 :hex => '1;36',
41 :important => '1;31',
42 :include => '33',
43 :integer => '1;34',
44 :interpreted => '1;35',
45 :key => '35',
46 :label => '1;4',
47 :local_variable => '33',
48 :oct => '1;35',
49 :operator_name => '1;29',
50 :pre_constant => '1;36',
51 :pre_type => '1;30',
52 :predefined => ['4', '1;34'],
53 :preprocessor => '36',
54 :pseudo_class => '34',
55 :regexp => {
56 :content => '31',
57 :delimiter => '1;29',
58 :modifier => '35',
59 :function => '1;29'
60 },
61 :reserved => '1;31',
62 :shell => {
63 :self => '42',
64 :content => '1;29',
65 :delimiter => '37',
66 },
67 :string => {
68 :self => '32',
69 :modifier => '1;32',
70 :escape => '1;36',
71 :delimiter => '1;32',
72 },
73 :symbol => '1;32',
74 :tag => '34',
75 :tag_fat => '1;34',
76 :tag_special => ['34', '4'],
77 :type => '1;34',
78 :value => '36',
79 :variable => '34',
80 :insert => '42',
81 :delete => '41',
82 :change => '44',
83 :head => '45',
84 }
85 TOKEN_COLORS[:keyword] = TOKEN_COLORS[:reserved]
86 TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
87 TOKEN_COLORS[:imaginary] = TOKEN_COLORS[:complex]
88 TOKEN_COLORS[:open] = TOKEN_COLORS[:close] = TOKEN_COLORS[:nesting_delimiter] = TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
89
90 protected
91
92 def setup(options)
93 @out = ''
94 @opened = [nil]
95 @subcolors = nil
96 end
97
98 def finish(options)
99 super
100 end
101
102 def token text, type = :plain
103 case text
104
105 when nil
106 # raise 'Token with nil as text was given: %p' % [[text, type]]
107
108 when String
109
110 if color = (@subcolors || TOKEN_COLORS)[type]
111 color = color[:self] || return if Hash === color
112
113 @out << col(color) + text.gsub("\n", col(0) + "\n" + col(color)) + col(0)
114 @out << col(@subcolors[:self]) if @subcolors && @subcolors[:self]
115 else
116 @out << text
117 end
118
119 # token groups, eg. strings
120 when :open
121 @opened[0] = type
122 if color = TOKEN_COLORS[type]
123 if Hash === color
124 @subcolors = color
125 @out << col(color[:self]) if color[:self]
126 else
127 @subcolors = {}
128 @out << col(color)
129 end
130 end
131 @opened << type
132 when :close
133 if @opened.empty?
134 # nothing to close
135 else
136 @out << col(0) if (@subcolors || {})[:self]
137 @subcolors = nil
138 @opened.pop
139 end
140
141 # whole lines to be highlighted, eg. a added/modified/deleted lines in a diff
142 when :begin_line
143
144 when :end_line
145
146 else
147 raise 'unknown token kind: %p' % [text]
148 end
149 end
150
151 private
152
153 def col(color)
154 Array(color).map { |c| "\e[#{c}m" }.join
155 end
156 end
157 end
158 end