coderay 1.0.9 → 1.1.0.rc1
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.
- checksums.yaml +7 -0
- data/Rakefile +2 -0
- data/bin/coderay +4 -4
- data/lib/coderay.rb +2 -3
- data/lib/coderay/encoders/debug.rb +5 -17
- data/lib/coderay/encoders/debug_lint.rb +62 -0
- data/lib/coderay/encoders/html.rb +84 -84
- data/lib/coderay/encoders/html/css.rb +7 -7
- data/lib/coderay/encoders/html/numbering.rb +24 -19
- data/lib/coderay/encoders/html/output.rb +1 -1
- data/lib/coderay/encoders/lint.rb +57 -0
- data/lib/coderay/encoders/statistic.rb +0 -1
- data/lib/coderay/encoders/terminal.rb +121 -105
- data/lib/coderay/helpers/file_type.rb +54 -47
- data/lib/coderay/helpers/plugin.rb +4 -13
- data/lib/coderay/scanner.rb +58 -26
- data/lib/coderay/scanners/c.rb +1 -1
- data/lib/coderay/scanners/cpp.rb +1 -1
- data/lib/coderay/scanners/css.rb +22 -25
- data/lib/coderay/scanners/diff.rb +53 -31
- data/lib/coderay/scanners/groovy.rb +17 -4
- data/lib/coderay/scanners/html.rb +38 -16
- data/lib/coderay/scanners/java.rb +1 -1
- data/lib/coderay/scanners/java_script.rb +30 -6
- data/lib/coderay/scanners/json.rb +15 -12
- data/lib/coderay/scanners/lua.rb +280 -0
- data/lib/coderay/scanners/php.rb +22 -4
- data/lib/coderay/scanners/python.rb +3 -3
- data/lib/coderay/scanners/raydebug.rb +8 -8
- data/lib/coderay/scanners/ruby.rb +2 -2
- data/lib/coderay/scanners/sass.rb +232 -0
- data/lib/coderay/scanners/sql.rb +7 -4
- data/lib/coderay/scanners/taskpaper.rb +36 -0
- data/lib/coderay/scanners/yaml.rb +2 -2
- data/lib/coderay/styles/alpha.rb +31 -21
- data/lib/coderay/token_kinds.rb +68 -71
- data/lib/coderay/tokens.rb +23 -77
- data/lib/coderay/version.rb +1 -1
- data/test/functional/examples.rb +3 -3
- data/test/functional/for_redcloth.rb +4 -10
- metadata +13 -14
- data/lib/coderay/helpers/gzip.rb +0 -41
@@ -1,15 +1,15 @@
|
|
1
1
|
module CodeRay
|
2
2
|
module Encoders
|
3
|
-
|
3
|
+
|
4
4
|
class HTML
|
5
|
-
|
5
|
+
|
6
6
|
module Numbering # :nodoc:
|
7
|
-
|
7
|
+
|
8
8
|
def self.number! output, mode = :table, options = {}
|
9
9
|
return self unless mode
|
10
|
-
|
10
|
+
|
11
11
|
options = DEFAULT_OPTIONS.merge options
|
12
|
-
|
12
|
+
|
13
13
|
start = options[:line_number_start]
|
14
14
|
unless start.is_a? Integer
|
15
15
|
raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
|
@@ -26,7 +26,7 @@ module Encoders
|
|
26
26
|
"<a href=\"##{anchor}\" name=\"#{anchor}\">#{line}</a>"
|
27
27
|
end
|
28
28
|
else
|
29
|
-
|
29
|
+
:to_s.to_proc
|
30
30
|
end
|
31
31
|
|
32
32
|
bold_every = options[:bold_every]
|
@@ -56,12 +56,17 @@ module Encoders
|
|
56
56
|
raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
|
57
57
|
end
|
58
58
|
|
59
|
-
|
60
|
-
position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n)
|
61
|
-
if position_of_last_newline
|
59
|
+
if position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n)
|
62
60
|
after_last_newline = output[position_of_last_newline + 1 .. -1]
|
63
61
|
ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/]
|
64
|
-
|
62
|
+
|
63
|
+
if ends_with_newline
|
64
|
+
line_count = output.count("\n")
|
65
|
+
else
|
66
|
+
line_count = output.count("\n") + 1
|
67
|
+
end
|
68
|
+
else
|
69
|
+
line_count = 1
|
65
70
|
end
|
66
71
|
|
67
72
|
case mode
|
@@ -70,34 +75,34 @@ module Encoders
|
|
70
75
|
line_number = start
|
71
76
|
output.gsub!(/^.*$\n?/) do |line|
|
72
77
|
line_number_text = bolding.call line_number
|
73
|
-
indent = ' ' * (max_width - line_number.to_s.size)
|
78
|
+
indent = ' ' * (max_width - line_number.to_s.size)
|
74
79
|
line_number += 1
|
75
80
|
"<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{line}"
|
76
81
|
end
|
77
|
-
|
82
|
+
|
78
83
|
when :table
|
79
84
|
line_numbers = (start ... start + line_count).map(&bolding).join("\n")
|
80
85
|
line_numbers << "\n"
|
81
86
|
line_numbers_table_template = Output::TABLE.apply('LINE_NUMBERS', line_numbers)
|
82
|
-
|
87
|
+
|
83
88
|
output.gsub!(/<\/div>\n/, '</div>')
|
84
89
|
output.wrap_in! line_numbers_table_template
|
85
90
|
output.wrapped_in = :div
|
86
|
-
|
91
|
+
|
87
92
|
when :list
|
88
93
|
raise NotImplementedError, 'The :list option is no longer available. Use :table.'
|
89
|
-
|
94
|
+
|
90
95
|
else
|
91
96
|
raise ArgumentError, 'Unknown value %p for mode: expected one of %p' %
|
92
97
|
[mode, [:table, :inline]]
|
93
98
|
end
|
94
|
-
|
99
|
+
|
95
100
|
output
|
96
101
|
end
|
97
|
-
|
102
|
+
|
98
103
|
end
|
99
|
-
|
104
|
+
|
100
105
|
end
|
101
|
-
|
106
|
+
|
102
107
|
end
|
103
108
|
end
|
@@ -124,7 +124,7 @@ module Encoders
|
|
124
124
|
|
125
125
|
TABLE = Template.new <<-TABLE
|
126
126
|
<table class="CodeRay"><tr>
|
127
|
-
<td class="line-numbers"
|
127
|
+
<td class="line-numbers"><pre><%LINE_NUMBERS%></pre></td>
|
128
128
|
<td class="code"><pre><%CONTENT%></pre></td>
|
129
129
|
</tr></table>
|
130
130
|
TABLE
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module CodeRay
|
2
|
+
module Encoders
|
3
|
+
|
4
|
+
# = Lint Encoder
|
5
|
+
#
|
6
|
+
# Checks for:
|
7
|
+
#
|
8
|
+
# - empty tokens
|
9
|
+
# - incorrect nesting
|
10
|
+
#
|
11
|
+
# It will raise an InvalidTokenStream exception when any of the above occurs.
|
12
|
+
#
|
13
|
+
# See also: Encoders::DebugLint
|
14
|
+
class Lint < Debug
|
15
|
+
|
16
|
+
register_for :lint
|
17
|
+
|
18
|
+
InvalidTokenStream = Class.new StandardError
|
19
|
+
EmptyToken = Class.new InvalidTokenStream
|
20
|
+
IncorrectTokenGroupNesting = Class.new InvalidTokenStream
|
21
|
+
|
22
|
+
def text_token text, kind
|
23
|
+
raise EmptyToken, 'empty token' if text.empty?
|
24
|
+
end
|
25
|
+
|
26
|
+
def begin_group kind
|
27
|
+
@opened << kind
|
28
|
+
end
|
29
|
+
|
30
|
+
def end_group kind
|
31
|
+
raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_group)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
|
32
|
+
@opened.pop
|
33
|
+
end
|
34
|
+
|
35
|
+
def begin_line kind
|
36
|
+
@opened << kind
|
37
|
+
end
|
38
|
+
|
39
|
+
def end_line kind
|
40
|
+
raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_line)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
|
41
|
+
@opened.pop
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
|
46
|
+
def setup options
|
47
|
+
@opened = []
|
48
|
+
end
|
49
|
+
|
50
|
+
def finish options
|
51
|
+
raise 'Some tokens still open at end of token stream: %p' % [@opened] unless @opened.empty?
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
@@ -19,105 +19,135 @@ module CodeRay
|
|
19
19
|
register_for :terminal
|
20
20
|
|
21
21
|
TOKEN_COLORS = {
|
22
|
-
:
|
23
|
-
|
24
|
-
:
|
25
|
-
:
|
22
|
+
:debug => "\e[1;37;44m",
|
23
|
+
|
24
|
+
:annotation => "\e[34m",
|
25
|
+
:attribute_name => "\e[35m",
|
26
|
+
:attribute_value => "\e[31m",
|
27
|
+
:binary => {
|
28
|
+
:self => "\e[31m",
|
29
|
+
:char => "\e[1;31m",
|
30
|
+
:delimiter => "\e[1;31m",
|
31
|
+
},
|
26
32
|
:char => {
|
27
|
-
:self =>
|
33
|
+
:self => "\e[35m",
|
34
|
+
:delimiter => "\e[1;35m"
|
35
|
+
},
|
36
|
+
:class => "\e[1;35;4m",
|
37
|
+
:class_variable => "\e[36m",
|
38
|
+
:color => "\e[32m",
|
39
|
+
:comment => {
|
40
|
+
:self => "\e[1;30m",
|
41
|
+
:char => "\e[37m",
|
42
|
+
:delimiter => "\e[37m",
|
28
43
|
},
|
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
|
-
:
|
44
|
+
:constant => "\e[1;34;4m",
|
45
|
+
:decorator => "\e[35m",
|
46
|
+
:definition => "\e[1;33m",
|
47
|
+
:directive => "\e[33m",
|
48
|
+
:docstring => "\e[31m",
|
49
|
+
:doctype => "\e[1;34m",
|
50
|
+
:done => "\e[1;30;2m",
|
51
|
+
:entity => "\e[31m",
|
52
|
+
:error => "\e[1;37;41m",
|
53
|
+
:exception => "\e[1;31m",
|
54
|
+
:float => "\e[1;35m",
|
55
|
+
:function => "\e[1;34m",
|
56
|
+
:global_variable => "\e[1;32m",
|
57
|
+
:hex => "\e[1;36m",
|
58
|
+
:id => "\e[1;34m",
|
59
|
+
:include => "\e[31m",
|
60
|
+
:integer => "\e[1;34m",
|
61
|
+
:imaginary => "\e[1;34m",
|
62
|
+
:important => "\e[1;31m",
|
63
|
+
:key => {
|
64
|
+
:self => "\e[35m",
|
65
|
+
:char => "\e[1;35m",
|
66
|
+
:delimiter => "\e[1;35m",
|
67
|
+
},
|
68
|
+
:keyword => "\e[32m",
|
69
|
+
:label => "\e[1;33m",
|
70
|
+
:local_variable => "\e[33m",
|
71
|
+
:namespace => "\e[1;35m",
|
72
|
+
:octal => "\e[1;34m",
|
73
|
+
:predefined => "\e[36m",
|
74
|
+
:predefined_constant => "\e[1;36m",
|
75
|
+
:predefined_type => "\e[1;32m",
|
76
|
+
:preprocessor => "\e[1;36m",
|
77
|
+
:pseudo_class => "\e[1;34m",
|
60
78
|
:regexp => {
|
61
|
-
:self =>
|
62
|
-
:
|
63
|
-
:
|
64
|
-
:
|
79
|
+
:self => "\e[35m",
|
80
|
+
:delimiter => "\e[1;35m",
|
81
|
+
:modifier => "\e[35m",
|
82
|
+
:char => "\e[1;35m",
|
65
83
|
},
|
66
|
-
:reserved =>
|
84
|
+
:reserved => "\e[32m",
|
67
85
|
:shell => {
|
68
|
-
:self =>
|
69
|
-
:
|
70
|
-
:delimiter =>
|
86
|
+
:self => "\e[33m",
|
87
|
+
:char => "\e[1;33m",
|
88
|
+
:delimiter => "\e[1;33m",
|
89
|
+
:escape => "\e[1;33m",
|
71
90
|
},
|
72
91
|
:string => {
|
73
|
-
:self =>
|
74
|
-
:modifier =>
|
75
|
-
:
|
76
|
-
:delimiter =>
|
77
|
-
:
|
92
|
+
:self => "\e[31m",
|
93
|
+
:modifier => "\e[1;31m",
|
94
|
+
:char => "\e[1;35m",
|
95
|
+
:delimiter => "\e[1;31m",
|
96
|
+
:escape => "\e[1;31m",
|
97
|
+
},
|
98
|
+
:symbol => {
|
99
|
+
:self => "\e[33m",
|
100
|
+
:delimiter => "\e[1;33m",
|
78
101
|
},
|
79
|
-
:
|
80
|
-
:
|
81
|
-
:
|
82
|
-
:
|
83
|
-
:variable => '1;34',
|
102
|
+
:tag => "\e[32m",
|
103
|
+
:type => "\e[1;34m",
|
104
|
+
:value => "\e[36m",
|
105
|
+
:variable => "\e[34m",
|
84
106
|
|
85
|
-
:insert =>
|
86
|
-
|
87
|
-
|
88
|
-
|
107
|
+
:insert => {
|
108
|
+
:self => "\e[42m",
|
109
|
+
:insert => "\e[1;32;42m",
|
110
|
+
:eyecatcher => "\e[102m",
|
111
|
+
},
|
112
|
+
:delete => {
|
113
|
+
:self => "\e[41m",
|
114
|
+
:delete => "\e[1;31;41m",
|
115
|
+
:eyecatcher => "\e[101m",
|
116
|
+
},
|
117
|
+
:change => {
|
118
|
+
:self => "\e[44m",
|
119
|
+
:change => "\e[37;44m",
|
120
|
+
},
|
121
|
+
:head => {
|
122
|
+
:self => "\e[45m",
|
123
|
+
:filename => "\e[37;45m"
|
124
|
+
},
|
89
125
|
}
|
126
|
+
|
90
127
|
TOKEN_COLORS[:keyword] = TOKEN_COLORS[:reserved]
|
91
128
|
TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
|
92
|
-
TOKEN_COLORS[:
|
93
|
-
TOKEN_COLORS[:begin_group] = TOKEN_COLORS[:end_group] =
|
94
|
-
TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
|
129
|
+
TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
|
95
130
|
|
96
131
|
protected
|
97
132
|
|
98
133
|
def setup(options)
|
99
134
|
super
|
100
135
|
@opened = []
|
101
|
-
@
|
136
|
+
@color_scopes = [TOKEN_COLORS]
|
102
137
|
end
|
103
138
|
|
104
139
|
public
|
105
140
|
|
106
141
|
def text_token text, kind
|
107
|
-
if color =
|
108
|
-
|
109
|
-
if color[:self]
|
110
|
-
color = color[:self]
|
111
|
-
else
|
112
|
-
@out << text
|
113
|
-
return
|
114
|
-
end
|
115
|
-
end
|
142
|
+
if color = @color_scopes.last[kind]
|
143
|
+
color = color[:self] if color.is_a? Hash
|
116
144
|
|
117
|
-
@out <<
|
118
|
-
@out << text.gsub("\n",
|
119
|
-
@out <<
|
120
|
-
|
145
|
+
@out << color
|
146
|
+
@out << (text.index("\n") ? text.gsub("\n", "\e[0m\n" + color) : text)
|
147
|
+
@out << "\e[0m"
|
148
|
+
if outer_color = @color_scopes.last[:self]
|
149
|
+
@out << outer_color
|
150
|
+
end
|
121
151
|
else
|
122
152
|
@out << text
|
123
153
|
end
|
@@ -130,50 +160,36 @@ module CodeRay
|
|
130
160
|
alias begin_line begin_group
|
131
161
|
|
132
162
|
def end_group kind
|
133
|
-
if @opened.
|
134
|
-
|
135
|
-
|
136
|
-
@
|
137
|
-
|
138
|
-
|
163
|
+
if @opened.pop
|
164
|
+
@color_scopes.pop
|
165
|
+
@out << "\e[0m"
|
166
|
+
if outer_color = @color_scopes.last[:self]
|
167
|
+
@out << outer_color
|
168
|
+
end
|
139
169
|
end
|
140
170
|
end
|
141
171
|
|
142
172
|
def end_line kind
|
143
|
-
|
144
|
-
|
145
|
-
else
|
146
|
-
@opened.pop
|
147
|
-
# whole lines to be highlighted,
|
148
|
-
# eg. added/modified/deleted lines in a diff
|
149
|
-
@out << "\t" * 100 + ansi_clear
|
150
|
-
@out << open_token(@opened.last)
|
151
|
-
end
|
173
|
+
@out << (@line_filler ||= "\t" * 100)
|
174
|
+
end_group kind
|
152
175
|
end
|
153
176
|
|
154
177
|
private
|
155
178
|
|
156
179
|
def open_token kind
|
157
|
-
if color =
|
158
|
-
if Hash
|
159
|
-
@
|
160
|
-
|
180
|
+
if color = @color_scopes.last[kind]
|
181
|
+
if color.is_a? Hash
|
182
|
+
@color_scopes << color
|
183
|
+
color[:self]
|
161
184
|
else
|
162
|
-
@
|
163
|
-
|
185
|
+
@color_scopes << @color_scopes.last
|
186
|
+
color
|
164
187
|
end
|
165
188
|
else
|
166
|
-
@
|
189
|
+
@color_scopes << @color_scopes.last
|
167
190
|
''
|
168
191
|
end
|
169
192
|
end
|
170
|
-
|
171
|
-
def ansi_colorize(color)
|
172
|
-
Array(color).map { |c| "\e[#{c}m" }.join
|
173
|
-
end
|
174
|
-
def ansi_clear
|
175
|
-
ansi_colorize(0)
|
176
|
-
end
|
177
193
|
end
|
178
194
|
end
|
179
|
-
end
|
195
|
+
end
|
@@ -77,53 +77,57 @@ module CodeRay
|
|
77
77
|
end
|
78
78
|
|
79
79
|
TypeFromExt = {
|
80
|
-
'c'
|
81
|
-
'cfc'
|
82
|
-
'cfm'
|
83
|
-
'clj'
|
84
|
-
'css'
|
85
|
-
'diff'
|
86
|
-
'dpr'
|
87
|
-
'erb'
|
88
|
-
'gemspec'
|
89
|
-
'groovy'
|
90
|
-
'gvy'
|
91
|
-
'h'
|
92
|
-
'haml'
|
93
|
-
'htm'
|
94
|
-
'html'
|
95
|
-
'html.erb'
|
96
|
-
'java'
|
97
|
-
'js'
|
98
|
-
'json'
|
99
|
-
'
|
100
|
-
'
|
101
|
-
'
|
102
|
-
'
|
103
|
-
'
|
104
|
-
'
|
105
|
-
'
|
106
|
-
'
|
107
|
-
'
|
108
|
-
'
|
109
|
-
'
|
110
|
-
'
|
111
|
-
'
|
112
|
-
'
|
113
|
-
'
|
114
|
-
'
|
115
|
-
'
|
116
|
-
'
|
117
|
-
'
|
118
|
-
'
|
119
|
-
|
120
|
-
'
|
121
|
-
|
122
|
-
'
|
123
|
-
'
|
124
|
-
'
|
125
|
-
'
|
126
|
-
'
|
80
|
+
'c' => :c,
|
81
|
+
'cfc' => :xml,
|
82
|
+
'cfm' => :xml,
|
83
|
+
'clj' => :clojure,
|
84
|
+
'css' => :css,
|
85
|
+
'diff' => :diff,
|
86
|
+
'dpr' => :delphi,
|
87
|
+
'erb' => :erb,
|
88
|
+
'gemspec' => :ruby,
|
89
|
+
'groovy' => :groovy,
|
90
|
+
'gvy' => :groovy,
|
91
|
+
'h' => :c,
|
92
|
+
'haml' => :haml,
|
93
|
+
'htm' => :html,
|
94
|
+
'html' => :html,
|
95
|
+
'html.erb' => :erb,
|
96
|
+
'java' => :java,
|
97
|
+
'js' => :java_script,
|
98
|
+
'json' => :json,
|
99
|
+
'lua' => :lua,
|
100
|
+
'mab' => :ruby,
|
101
|
+
'pas' => :delphi,
|
102
|
+
'patch' => :diff,
|
103
|
+
'phtml' => :php,
|
104
|
+
'php' => :php,
|
105
|
+
'php3' => :php,
|
106
|
+
'php4' => :php,
|
107
|
+
'php5' => :php,
|
108
|
+
'prawn' => :ruby,
|
109
|
+
'py' => :python,
|
110
|
+
'py3' => :python,
|
111
|
+
'pyw' => :python,
|
112
|
+
'rake' => :ruby,
|
113
|
+
'raydebug' => :raydebug,
|
114
|
+
'rb' => :ruby,
|
115
|
+
'rbw' => :ruby,
|
116
|
+
'rhtml' => :erb,
|
117
|
+
'rjs' => :ruby,
|
118
|
+
'rpdf' => :ruby,
|
119
|
+
'ru' => :ruby, # config.ru
|
120
|
+
'rxml' => :ruby,
|
121
|
+
'sass' => :sass,
|
122
|
+
'sql' => :sql,
|
123
|
+
'taskpaper' => :taskpaper,
|
124
|
+
'template' => :json, # AWS CloudFormation template
|
125
|
+
'tmproj' => :xml,
|
126
|
+
'xaml' => :xml,
|
127
|
+
'xhtml' => :html,
|
128
|
+
'xml' => :xml,
|
129
|
+
'yaml' => :yaml,
|
130
|
+
'yml' => :yaml,
|
127
131
|
}
|
128
132
|
for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
|
129
133
|
TypeFromExt[cpp_alias] = :cpp
|
@@ -136,6 +140,9 @@ module CodeRay
|
|
136
140
|
'Rakefile' => :ruby,
|
137
141
|
'Rantfile' => :ruby,
|
138
142
|
'Gemfile' => :ruby,
|
143
|
+
'Guardfile' => :ruby,
|
144
|
+
'Vagrantfile' => :ruby,
|
145
|
+
'Appraisals' => :ruby
|
139
146
|
}
|
140
147
|
|
141
148
|
end
|