raldred-coderay 0.9.339 → 0.9.3391

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/coderay/encoders/term.rb +118 -95
  2. metadata +2 -2
@@ -1,114 +1,137 @@
1
1
  # encoders/term.rb
2
- # By Nathan Weizenbaum (http://nex-3.com)
2
+ # By Rob Aldred (http://robaldred.co.uk)
3
+ # Based on idea by Nathan Weizenbaum (http://nex-3.com)
3
4
  # MIT License (http://www.opensource.org/licenses/mit-license.php)
4
5
  #
5
- # A CodeRay highlighter that outputs code highlighted for a color terminal.
6
- # Check out http://nex-3.com/posts/37-terminal-syntax-highlighting
6
+ # A CodeRay encoder that outputs code highlighted for a color terminal.
7
+ # Check out http://robaldred.co.uk
7
8
 
8
9
  module CodeRay
9
- module Encoders
10
- class Term < Encoder
11
- register_for :term
10
+ module Encoders
11
+ class Term < Encoder
12
+ register_for :term
12
13
 
13
- TOKEN_COLORS = {
14
- :attribute_name => '34',
15
- :attribute_name_fat => '34',
16
- :attribute_value => '31',
17
- :attribute_value_fat => '31',
18
- :bin => '1;35',
19
- :char => {:self => '36', :delimiter => '34'},
20
- :class => '1;35',
21
- :class_variable => '36',
22
- :color => '32',
23
- :comment => '37',
24
- :constant => ['34', '4'],
25
- :definition => '1;32',
26
- :directive => ['32', '4'],
27
- :doc => '46',
28
- :doc_string => ['31', '4'],
29
- :entity => '33',
30
- :error => ['1;33', '41'],
31
- :exception => '1;31',
32
- :float => '1;35',
33
- :function => '1;34',
34
- :global_variable => '42',
35
- :hex => '1;36',
36
- :include => '33',
37
- :integer => '1;34',
38
- :interpreted => '1;35',
39
- :label => '1;4',
40
- :local_variable => '33',
41
- :oct => '1;35',
42
- :operator_name => '1;29',
43
- :pre_constant => '1;36',
44
- :pre_type => '1;30',
45
- :predefined => ['4', '1;34'],
46
- :preprocessor => '36',
47
- :regexp => {
48
- :content => '31',
49
- :delimiter => '1;29',
50
- :modifier => '35',
51
- :function => '1;29'
52
- },
53
- :reserved => '1;31',
54
- :shell => {:self => '42', :content => '1;29'},
55
- :string => '32',
56
- :symbol => '1;32',
57
- :tag => '34',
58
- :tag_fat => '1;34',
59
- :tag_special => ['34', '4'],
60
- :type => '1;34',
61
- :variable => '34'
62
- }
63
- TOKEN_COLORS[:procedure] = TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
64
- TOKEN_COLORS[:open] = TOKEN_COLORS[:close] = TOKEN_COLORS[:nesting_delimiter] = TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
14
+ TOKEN_COLORS = {
15
+ :attribute_name => '33',
16
+ :attribute_name_fat => '33',
17
+ :attribute_value => '31',
18
+ :attribute_value_fat => '31',
19
+ :bin => '1;35',
20
+ :char => {:self => '36', :delimiter => '34'},
21
+ :class => '1;35',
22
+ :class_variable => '36',
23
+ :color => '32',
24
+ :comment => '37',
25
+ :constant => ['34', '4'],
26
+ :definition => '1;32',
27
+ :directive => ['32', '4'],
28
+ :doc => '46',
29
+ :doc_string => ['31', '4'],
30
+ :entity => '33',
31
+ :error => ['1;33', '41'],
32
+ :exception => '1;31',
33
+ :float => '1;35',
34
+ :function => '1;34',
35
+ :global_variable => '42',
36
+ :hex => '1;36',
37
+ :include => '33',
38
+ :integer => '1;34',
39
+ :interpreted => '1;35',
40
+ :label => '1;4',
41
+ :local_variable => '33',
42
+ :oct => '1;35',
43
+ :operator_name => '1;29',
44
+ :pre_constant => '1;36',
45
+ :pre_type => '1;30',
46
+ :predefined => ['4', '1;34'],
47
+ :preprocessor => '36',
48
+ :regexp => {
49
+ :content => '31',
50
+ :delimiter => '1;29',
51
+ :modifier => '35',
52
+ :function => '1;29'
53
+ },
54
+ :reserved => '1;31',
55
+ :shell => {:self => '42', :content => '1;29'},
56
+ :string => '32',
57
+ :symbol => '1;32',
58
+ :tag => '34',
59
+ :tag_fat => '1;34',
60
+ :tag_special => ['34', '4'],
61
+ :type => '1;34',
62
+ :variable => '34'
63
+ }
64
+ TOKEN_COLORS[:procedure] = TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
65
+ TOKEN_COLORS[:open] = TOKEN_COLORS[:close] = TOKEN_COLORS[:nesting_delimiter] = TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
65
66
 
66
- protected
67
+ protected
67
68
 
68
- def setup(options)
69
- @out = ''
70
- @subcolors = nil
71
- end
72
-
73
- def finish(options)
74
- @out
75
- end
76
-
77
- def text_token(text, kind)
78
- if color = (@subcolors || TOKEN_COLORS)[kind]
79
- color = color[:self] || return if Hash === color
69
+ def setup(options)
70
+ @out = ''
71
+ @opened = [nil]
72
+ @subcolors = nil
73
+ end
80
74
 
81
- @out << col(color) + text.gsub("\n", col(0) + "\n" + col(color)) + col(0)
82
- @out << col(@subcolors[:self]) if @subcolors && @subcolors[:self]
83
- else
84
- @out << text
75
+ def finish(options)
76
+ super
85
77
  end
86
- end
78
+
79
+ def token text, type = :plain
80
+ case text
81
+
82
+ when nil
83
+ # raise 'Token with nil as text was given: %p' % [[text, type]]
84
+
85
+ when String
86
+
87
+ if color = (@subcolors || TOKEN_COLORS)[type]
88
+ color = color[:self] || return if Hash === color
87
89
 
88
- def open_token(kind)
89
- if color = TOKEN_COLORS[kind]
90
- if Hash === color
91
- @subcolors = color
92
- @out << col(color[:self]) if color[:self]
90
+ @out << col(color) + text.gsub("\n", col(0) + "\n" + col(color)) + col(0)
91
+ @out << col(@subcolors[:self]) if @subcolors && @subcolors[:self]
92
+ else
93
+ @out << text
94
+ end
95
+
96
+ # token groups, eg. strings
97
+ when :open
98
+ @opened[0] = type
99
+ if color = TOKEN_COLORS[type]
100
+ if Hash === color
101
+ @subcolors = color
102
+ @out << col(color[:self]) if color[:self]
103
+ else
104
+ @subcolors = {}
105
+ @out << col(color)
106
+ end
107
+ end
108
+ @opened << type
109
+ when :close
110
+ if @opened.empty?
111
+ # nothing to close
112
+ else
113
+ if (@subcolors || {})[:self]
114
+ @out << col(0)
115
+ end
116
+ @subcolors = nil
117
+ @opened.pop
118
+ end
119
+
120
+ # whole lines to be highlighted, eg. a added/modified/deleted lines in a diff
121
+ when :begin_line
122
+
123
+ when :end_line
124
+
93
125
  else
94
- @subcolors = {}
95
- @out << col(color)
126
+ raise 'unknown token kind: %p' % [text]
96
127
  end
97
128
  end
98
- end
99
129
 
100
- def close_token(kind)
101
- if (@subcolors || {})[:self]
102
- @out << col(0)
103
- end
104
- @subcolors = nil
105
- end
130
+ private
106
131
 
107
- private
108
-
109
- def col(color)
110
- Array(color).map { |c| "\e[#{c}m" }.join
132
+ def col(color)
133
+ Array(color).map { |c| "\e[#{c}m" }.join
134
+ end
111
135
  end
112
136
  end
113
- end
114
137
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raldred-coderay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.339
4
+ version: 0.9.3391
5
5
  platform: ruby
6
6
  authors:
7
7
  - murphy
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-05 00:00:00 -07:00
12
+ date: 2009-05-08 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15