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.
- data/lib/coderay/encoders/term.rb +118 -95
- metadata +2 -2
@@ -1,114 +1,137 @@
|
|
1
1
|
# encoders/term.rb
|
2
|
-
# By
|
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
|
6
|
-
# Check out http://
|
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
|
-
|
11
|
-
|
10
|
+
module Encoders
|
11
|
+
class Term < Encoder
|
12
|
+
register_for :term
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
67
|
+
protected
|
67
68
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
82
|
-
|
83
|
-
else
|
84
|
-
@out << text
|
75
|
+
def finish(options)
|
76
|
+
super
|
85
77
|
end
|
86
|
-
|
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
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
95
|
-
@out << col(color)
|
126
|
+
raise 'unknown token kind: %p' % [text]
|
96
127
|
end
|
97
128
|
end
|
98
|
-
end
|
99
129
|
|
100
|
-
|
101
|
-
if (@subcolors || {})[:self]
|
102
|
-
@out << col(0)
|
103
|
-
end
|
104
|
-
@subcolors = nil
|
105
|
-
end
|
130
|
+
private
|
106
131
|
|
107
|
-
|
108
|
-
|
109
|
-
|
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.
|
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-
|
12
|
+
date: 2009-05-08 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|