giga-fast-rb 0.0.1
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/coderay-1.1.3/MIT-LICENSE +22 -0
- data/coderay-1.1.3/README_INDEX.rdoc +123 -0
- data/coderay-1.1.3/bin/coderay +215 -0
- data/coderay-1.1.3/lib/coderay/duo.rb +81 -0
- data/coderay-1.1.3/lib/coderay/encoders/_map.rb +17 -0
- data/coderay-1.1.3/lib/coderay/encoders/comment_filter.rb +25 -0
- data/coderay-1.1.3/lib/coderay/encoders/count.rb +39 -0
- data/coderay-1.1.3/lib/coderay/encoders/debug.rb +49 -0
- data/coderay-1.1.3/lib/coderay/encoders/debug_lint.rb +63 -0
- data/coderay-1.1.3/lib/coderay/encoders/div.rb +23 -0
- data/coderay-1.1.3/lib/coderay/encoders/encoder.rb +190 -0
- data/coderay-1.1.3/lib/coderay/encoders/filter.rb +58 -0
- data/coderay-1.1.3/lib/coderay/encoders/html/css.rb +65 -0
- data/coderay-1.1.3/lib/coderay/encoders/html/numbering.rb +108 -0
- data/coderay-1.1.3/lib/coderay/encoders/html/output.rb +164 -0
- data/coderay-1.1.3/lib/coderay/encoders/html.rb +333 -0
- data/coderay-1.1.3/lib/coderay/encoders/json.rb +83 -0
- data/coderay-1.1.3/lib/coderay/encoders/lines_of_code.rb +45 -0
- data/coderay-1.1.3/lib/coderay/encoders/lint.rb +59 -0
- data/coderay-1.1.3/lib/coderay/encoders/null.rb +18 -0
- data/coderay-1.1.3/lib/coderay/encoders/page.rb +24 -0
- data/coderay-1.1.3/lib/coderay/encoders/span.rb +23 -0
- data/coderay-1.1.3/lib/coderay/encoders/statistic.rb +95 -0
- data/coderay-1.1.3/lib/coderay/encoders/terminal.rb +195 -0
- data/coderay-1.1.3/lib/coderay/encoders/text.rb +46 -0
- data/coderay-1.1.3/lib/coderay/encoders/token_kind_filter.rb +111 -0
- data/coderay-1.1.3/lib/coderay/encoders/xml.rb +72 -0
- data/coderay-1.1.3/lib/coderay/encoders/yaml.rb +50 -0
- data/coderay-1.1.3/lib/coderay/encoders.rb +18 -0
- data/coderay-1.1.3/lib/coderay/for_redcloth.rb +95 -0
- data/coderay-1.1.3/lib/coderay/helpers/file_type.rb +151 -0
- data/coderay-1.1.3/lib/coderay/helpers/plugin.rb +55 -0
- data/coderay-1.1.3/lib/coderay/helpers/plugin_host.rb +221 -0
- data/coderay-1.1.3/lib/coderay/helpers/word_list.rb +72 -0
- data/coderay-1.1.3/lib/coderay/scanners/_map.rb +24 -0
- data/coderay-1.1.3/lib/coderay/scanners/c.rb +189 -0
- data/coderay-1.1.3/lib/coderay/scanners/clojure.rb +217 -0
- data/coderay-1.1.3/lib/coderay/scanners/cpp.rb +217 -0
- data/coderay-1.1.3/lib/coderay/scanners/css.rb +196 -0
- data/coderay-1.1.3/lib/coderay/scanners/debug.rb +75 -0
- data/coderay-1.1.3/lib/coderay/scanners/delphi.rb +144 -0
- data/coderay-1.1.3/lib/coderay/scanners/diff.rb +221 -0
- data/coderay-1.1.3/lib/coderay/scanners/erb.rb +81 -0
- data/coderay-1.1.3/lib/coderay/scanners/go.rb +208 -0
- data/coderay-1.1.3/lib/coderay/scanners/groovy.rb +268 -0
- data/coderay-1.1.3/lib/coderay/scanners/haml.rb +168 -0
- data/coderay-1.1.3/lib/coderay/scanners/html.rb +275 -0
- data/coderay-1.1.3/lib/coderay/scanners/java/builtin_types.rb +421 -0
- data/coderay-1.1.3/lib/coderay/scanners/java.rb +174 -0
- data/coderay-1.1.3/lib/coderay/scanners/java_script.rb +236 -0
- data/coderay-1.1.3/lib/coderay/scanners/json.rb +98 -0
- data/coderay-1.1.3/lib/coderay/scanners/lua.rb +280 -0
- data/coderay-1.1.3/lib/coderay/scanners/php.rb +527 -0
- data/coderay-1.1.3/lib/coderay/scanners/python.rb +287 -0
- data/coderay-1.1.3/lib/coderay/scanners/raydebug.rb +75 -0
- data/coderay-1.1.3/lib/coderay/scanners/ruby/patterns.rb +178 -0
- data/coderay-1.1.3/lib/coderay/scanners/ruby/string_state.rb +79 -0
- data/coderay-1.1.3/lib/coderay/scanners/ruby.rb +477 -0
- data/coderay-1.1.3/lib/coderay/scanners/sass.rb +232 -0
- data/coderay-1.1.3/lib/coderay/scanners/scanner.rb +337 -0
- data/coderay-1.1.3/lib/coderay/scanners/sql.rb +169 -0
- data/coderay-1.1.3/lib/coderay/scanners/taskpaper.rb +36 -0
- data/coderay-1.1.3/lib/coderay/scanners/text.rb +26 -0
- data/coderay-1.1.3/lib/coderay/scanners/xml.rb +17 -0
- data/coderay-1.1.3/lib/coderay/scanners/yaml.rb +140 -0
- data/coderay-1.1.3/lib/coderay/scanners.rb +27 -0
- data/coderay-1.1.3/lib/coderay/styles/_map.rb +7 -0
- data/coderay-1.1.3/lib/coderay/styles/alpha.rb +153 -0
- data/coderay-1.1.3/lib/coderay/styles/style.rb +18 -0
- data/coderay-1.1.3/lib/coderay/styles.rb +15 -0
- data/coderay-1.1.3/lib/coderay/token_kinds.rb +85 -0
- data/coderay-1.1.3/lib/coderay/tokens.rb +164 -0
- data/coderay-1.1.3/lib/coderay/tokens_proxy.rb +55 -0
- data/coderay-1.1.3/lib/coderay/version.rb +3 -0
- data/coderay-1.1.3/lib/coderay.rb +284 -0
- data/giga-fast-rb.gemspec +12 -0
- metadata +117 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
require 'set'
|
|
2
|
+
|
|
3
|
+
module CodeRay
|
|
4
|
+
module Encoders
|
|
5
|
+
|
|
6
|
+
# = HTML Encoder
|
|
7
|
+
#
|
|
8
|
+
# This is CodeRay's most important highlighter:
|
|
9
|
+
# It provides save, fast XHTML generation and CSS support.
|
|
10
|
+
#
|
|
11
|
+
# == Usage
|
|
12
|
+
#
|
|
13
|
+
# require 'coderay'
|
|
14
|
+
# puts CodeRay.scan('Some /code/', :ruby).html #-> a HTML page
|
|
15
|
+
# puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span)
|
|
16
|
+
# #-> <span class="CodeRay"><span class="co">Some</span> /code/</span>
|
|
17
|
+
# puts CodeRay.scan('Some /code/', :ruby).span #-> the same
|
|
18
|
+
#
|
|
19
|
+
# puts CodeRay.scan('Some code', :ruby).html(
|
|
20
|
+
# :wrap => nil,
|
|
21
|
+
# :line_numbers => :inline,
|
|
22
|
+
# :css => :style
|
|
23
|
+
# )
|
|
24
|
+
#
|
|
25
|
+
# == Options
|
|
26
|
+
#
|
|
27
|
+
# === :tab_width
|
|
28
|
+
# Convert \t characters to +n+ spaces (a number or false.)
|
|
29
|
+
# false will keep tab characters untouched.
|
|
30
|
+
#
|
|
31
|
+
# Default: 8
|
|
32
|
+
#
|
|
33
|
+
# === :css
|
|
34
|
+
# How to include the styles; can be :class or :style.
|
|
35
|
+
#
|
|
36
|
+
# Default: :class
|
|
37
|
+
#
|
|
38
|
+
# === :wrap
|
|
39
|
+
# Wrap in :page, :div, :span or nil.
|
|
40
|
+
#
|
|
41
|
+
# You can also use Encoders::Div and Encoders::Span.
|
|
42
|
+
#
|
|
43
|
+
# Default: nil
|
|
44
|
+
#
|
|
45
|
+
# === :title
|
|
46
|
+
#
|
|
47
|
+
# The title of the HTML page (works only when :wrap is set to :page.)
|
|
48
|
+
#
|
|
49
|
+
# Default: 'CodeRay output'
|
|
50
|
+
#
|
|
51
|
+
# === :break_lines
|
|
52
|
+
#
|
|
53
|
+
# Split multiline blocks at line breaks.
|
|
54
|
+
# Forced to true if :line_numbers option is set to :inline.
|
|
55
|
+
#
|
|
56
|
+
# Default: false
|
|
57
|
+
#
|
|
58
|
+
# === :line_numbers
|
|
59
|
+
# Include line numbers in :table, :inline, or nil (no line numbers)
|
|
60
|
+
#
|
|
61
|
+
# Default: nil
|
|
62
|
+
#
|
|
63
|
+
# === :line_number_anchors
|
|
64
|
+
# Adds anchors and links to the line numbers. Can be false (off), true (on),
|
|
65
|
+
# or a prefix string that will be prepended to the anchor name.
|
|
66
|
+
#
|
|
67
|
+
# The prefix must consist only of letters, digits, and underscores.
|
|
68
|
+
#
|
|
69
|
+
# Default: true, default prefix name: "line"
|
|
70
|
+
#
|
|
71
|
+
# === :line_number_start
|
|
72
|
+
# Where to start with line number counting.
|
|
73
|
+
#
|
|
74
|
+
# Default: 1
|
|
75
|
+
#
|
|
76
|
+
# === :bold_every
|
|
77
|
+
# Make every +n+-th number appear bold.
|
|
78
|
+
#
|
|
79
|
+
# Default: 10
|
|
80
|
+
#
|
|
81
|
+
# === :highlight_lines
|
|
82
|
+
#
|
|
83
|
+
# Highlights certain line numbers.
|
|
84
|
+
# Can be any Enumerable, typically just an Array or Range, of numbers.
|
|
85
|
+
#
|
|
86
|
+
# Bolding is deactivated when :highlight_lines is set. It only makes sense
|
|
87
|
+
# in combination with :line_numbers.
|
|
88
|
+
#
|
|
89
|
+
# Default: nil
|
|
90
|
+
#
|
|
91
|
+
# === :hint
|
|
92
|
+
# Include some information into the output using the title attribute.
|
|
93
|
+
# Can be :info (show token kind on mouse-over), :info_long (with full path)
|
|
94
|
+
# or :debug (via inspect).
|
|
95
|
+
#
|
|
96
|
+
# Default: false
|
|
97
|
+
class HTML < Encoder
|
|
98
|
+
|
|
99
|
+
register_for :html
|
|
100
|
+
|
|
101
|
+
FILE_EXTENSION = 'snippet.html'
|
|
102
|
+
|
|
103
|
+
DEFAULT_OPTIONS = {
|
|
104
|
+
:tab_width => 8,
|
|
105
|
+
|
|
106
|
+
:css => :class,
|
|
107
|
+
:style => :alpha,
|
|
108
|
+
:wrap => nil,
|
|
109
|
+
:title => 'CodeRay output',
|
|
110
|
+
|
|
111
|
+
:break_lines => false,
|
|
112
|
+
|
|
113
|
+
:line_numbers => nil,
|
|
114
|
+
:line_number_anchors => 'n',
|
|
115
|
+
:line_number_start => 1,
|
|
116
|
+
:bold_every => 10,
|
|
117
|
+
:highlight_lines => nil,
|
|
118
|
+
|
|
119
|
+
:hint => false,
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
autoload :Output, CodeRay.coderay_path('encoders', 'html', 'output')
|
|
123
|
+
autoload :CSS, CodeRay.coderay_path('encoders', 'html', 'css')
|
|
124
|
+
autoload :Numbering, CodeRay.coderay_path('encoders', 'html', 'numbering')
|
|
125
|
+
|
|
126
|
+
attr_reader :css
|
|
127
|
+
|
|
128
|
+
protected
|
|
129
|
+
|
|
130
|
+
def self.make_html_escape_hash
|
|
131
|
+
{
|
|
132
|
+
'&' => '&',
|
|
133
|
+
'"' => '"',
|
|
134
|
+
'>' => '>',
|
|
135
|
+
'<' => '<',
|
|
136
|
+
# "\t" => will be set to ' ' * options[:tab_width] during setup
|
|
137
|
+
}.tap do |hash|
|
|
138
|
+
# Escape ASCII control codes except \x9 == \t and \xA == \n.
|
|
139
|
+
(Array(0x00..0x8) + Array(0xB..0x1F)).each { |invalid| hash[invalid.chr] = ' ' }
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
HTML_ESCAPE = make_html_escape_hash
|
|
144
|
+
HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1F]/
|
|
145
|
+
|
|
146
|
+
TOKEN_KIND_TO_INFO = Hash.new do |h, kind|
|
|
147
|
+
h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize }
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
TRANSPARENT_TOKEN_KINDS = Set[
|
|
151
|
+
:delimiter, :modifier, :content, :escape, :inline_delimiter,
|
|
152
|
+
]
|
|
153
|
+
|
|
154
|
+
# Generate a hint about the given +kinds+ in a +hint+ style.
|
|
155
|
+
#
|
|
156
|
+
# +hint+ may be :info, :info_long or :debug.
|
|
157
|
+
def self.token_path_to_hint hint, kinds
|
|
158
|
+
kinds = Array kinds
|
|
159
|
+
title =
|
|
160
|
+
case hint
|
|
161
|
+
when :info
|
|
162
|
+
kinds = kinds[1..-1] if TRANSPARENT_TOKEN_KINDS.include? kinds.first
|
|
163
|
+
TOKEN_KIND_TO_INFO[kinds.first]
|
|
164
|
+
when :info_long
|
|
165
|
+
kinds.reverse.map { |kind| TOKEN_KIND_TO_INFO[kind] }.join('/')
|
|
166
|
+
when :debug
|
|
167
|
+
kinds.inspect
|
|
168
|
+
end
|
|
169
|
+
title ? " title=\"#{title}\"" : ''
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def setup options
|
|
173
|
+
super
|
|
174
|
+
|
|
175
|
+
check_options! options
|
|
176
|
+
|
|
177
|
+
if options[:wrap] || options[:line_numbers]
|
|
178
|
+
@real_out = @out
|
|
179
|
+
@out = ''.dup
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
@break_lines = (options[:break_lines] == true)
|
|
183
|
+
|
|
184
|
+
@HTML_ESCAPE = HTML_ESCAPE.merge("\t" => options[:tab_width] ? ' ' * options[:tab_width] : "\t")
|
|
185
|
+
|
|
186
|
+
@opened = []
|
|
187
|
+
@last_opened = nil
|
|
188
|
+
@css = CSS.new options[:style]
|
|
189
|
+
|
|
190
|
+
@span_for_kinds = make_span_for_kinds(options[:css], options[:hint])
|
|
191
|
+
|
|
192
|
+
@set_last_opened = options[:hint] || options[:css] == :style
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def finish options
|
|
196
|
+
unless @opened.empty?
|
|
197
|
+
@out << '</span>' while @opened.pop
|
|
198
|
+
@last_opened = nil
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
if @out.respond_to? :to_str
|
|
202
|
+
@out.extend Output
|
|
203
|
+
@out.css = @css
|
|
204
|
+
if options[:line_numbers]
|
|
205
|
+
Numbering.number! @out, options[:line_numbers], options
|
|
206
|
+
end
|
|
207
|
+
@out.wrap! options[:wrap]
|
|
208
|
+
@out.apply_title! options[:title]
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
if defined?(@real_out) && @real_out
|
|
212
|
+
@real_out << @out
|
|
213
|
+
@out = @real_out
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
super
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
public
|
|
220
|
+
|
|
221
|
+
def text_token text, kind
|
|
222
|
+
style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]
|
|
223
|
+
|
|
224
|
+
text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } if text =~ /#{HTML_ESCAPE_PATTERN}/o
|
|
225
|
+
text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")
|
|
226
|
+
|
|
227
|
+
if style
|
|
228
|
+
@out << style << text << '</span>'
|
|
229
|
+
else
|
|
230
|
+
@out << text
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# token groups, eg. strings
|
|
235
|
+
def begin_group kind
|
|
236
|
+
@out << (@span_for_kinds[@last_opened ? [kind, *@opened] : kind] || '<span>')
|
|
237
|
+
@opened << kind
|
|
238
|
+
@last_opened = kind if @set_last_opened
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def end_group kind
|
|
242
|
+
check_group_nesting 'token group', kind if $CODERAY_DEBUG
|
|
243
|
+
close_span
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# whole lines to be highlighted, eg. a deleted line in a diff
|
|
247
|
+
def begin_line kind
|
|
248
|
+
if style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]
|
|
249
|
+
if style['class="']
|
|
250
|
+
@out << style.sub('class="', 'class="line ')
|
|
251
|
+
else
|
|
252
|
+
@out << style.sub('>', ' class="line">')
|
|
253
|
+
end
|
|
254
|
+
else
|
|
255
|
+
@out << '<span class="line">'
|
|
256
|
+
end
|
|
257
|
+
@opened << kind
|
|
258
|
+
@last_opened = kind if @options[:css] == :style
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def end_line kind
|
|
262
|
+
check_group_nesting 'line', kind if $CODERAY_DEBUG
|
|
263
|
+
close_span
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
protected
|
|
267
|
+
|
|
268
|
+
def check_options! options
|
|
269
|
+
unless [false, nil, :debug, :info, :info_long].include? options[:hint]
|
|
270
|
+
raise ArgumentError, "Unknown value %p for :hint; expected :info, :info_long, :debug, false, or nil." % [options[:hint]]
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
unless [:class, :style].include? options[:css]
|
|
274
|
+
raise ArgumentError, 'Unknown value %p for :css.' % [options[:css]]
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
options[:break_lines] = true if options[:line_numbers] == :inline
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def css_class_for_kinds kinds
|
|
281
|
+
TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first]
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def style_for_kinds kinds
|
|
285
|
+
css_classes = kinds.is_a?(Array) ? kinds.map { |c| TokenKinds[c] } : [TokenKinds[kinds]]
|
|
286
|
+
@css.get_style_for_css_classes css_classes
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def make_span_for_kinds method, hint
|
|
290
|
+
Hash.new do |h, kinds|
|
|
291
|
+
begin
|
|
292
|
+
css_class = css_class_for_kinds(kinds)
|
|
293
|
+
title = HTML.token_path_to_hint hint, kinds if hint
|
|
294
|
+
|
|
295
|
+
if css_class || title
|
|
296
|
+
if method == :style
|
|
297
|
+
style = style_for_kinds(kinds)
|
|
298
|
+
"<span#{title}#{" style=\"#{style}\"" if style}>"
|
|
299
|
+
else
|
|
300
|
+
"<span#{title}#{" class=\"#{css_class}\"" if css_class}>"
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
end.tap do |span|
|
|
304
|
+
h.clear if h.size >= 100
|
|
305
|
+
h[kinds] = span
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def check_group_nesting name, kind
|
|
311
|
+
if @opened.empty? || @opened.last != kind
|
|
312
|
+
warn "Malformed token stream: Trying to close a #{name} (%p) that is not open. Open are: %p." % [kind, @opened[1..-1]]
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
def break_lines text, style
|
|
317
|
+
reopen = ''.dup
|
|
318
|
+
@opened.each_with_index do |kind, index|
|
|
319
|
+
reopen << (@span_for_kinds[index > 0 ? [kind, *@opened[0...index]] : kind] || '<span>')
|
|
320
|
+
end
|
|
321
|
+
text.gsub("\n", "#{'</span>' * @opened.size}#{'</span>' if style}\n#{reopen}#{style}")
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def close_span
|
|
325
|
+
if @opened.pop
|
|
326
|
+
@out << '</span>'
|
|
327
|
+
@last_opened = @opened.last if @last_opened
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
end
|
|
333
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# A simple JSON Encoder.
|
|
5
|
+
#
|
|
6
|
+
# Example:
|
|
7
|
+
# CodeRay.scan('puts "Hello world!"', :ruby).json
|
|
8
|
+
# yields
|
|
9
|
+
# [
|
|
10
|
+
# {"type"=>"text", "text"=>"puts", "kind"=>"ident"},
|
|
11
|
+
# {"type"=>"text", "text"=>" ", "kind"=>"space"},
|
|
12
|
+
# {"type"=>"block", "action"=>"open", "kind"=>"string"},
|
|
13
|
+
# {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
|
|
14
|
+
# {"type"=>"text", "text"=>"Hello world!", "kind"=>"content"},
|
|
15
|
+
# {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
|
|
16
|
+
# {"type"=>"block", "action"=>"close", "kind"=>"string"},
|
|
17
|
+
# ]
|
|
18
|
+
class JSON < Encoder
|
|
19
|
+
|
|
20
|
+
begin
|
|
21
|
+
require 'json'
|
|
22
|
+
rescue LoadError
|
|
23
|
+
begin
|
|
24
|
+
require 'rubygems' unless defined? Gem
|
|
25
|
+
gem 'json'
|
|
26
|
+
require 'json'
|
|
27
|
+
rescue LoadError
|
|
28
|
+
$stderr.puts "The JSON encoder needs the JSON library.\n" \
|
|
29
|
+
"Please gem install json."
|
|
30
|
+
raise
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
register_for :json
|
|
35
|
+
FILE_EXTENSION = 'json'
|
|
36
|
+
|
|
37
|
+
protected
|
|
38
|
+
def setup options
|
|
39
|
+
super
|
|
40
|
+
|
|
41
|
+
@first = true
|
|
42
|
+
@out << '['
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def finish options
|
|
46
|
+
@out << ']'
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def append data
|
|
50
|
+
if @first
|
|
51
|
+
@first = false
|
|
52
|
+
else
|
|
53
|
+
@out << ','
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
@out << data.to_json
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
public
|
|
60
|
+
def text_token text, kind
|
|
61
|
+
append :type => 'text', :text => text, :kind => kind
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def begin_group kind
|
|
65
|
+
append :type => 'block', :action => 'open', :kind => kind
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def end_group kind
|
|
69
|
+
append :type => 'block', :action => 'close', :kind => kind
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def begin_line kind
|
|
73
|
+
append :type => 'block', :action => 'begin_line', :kind => kind
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def end_line kind
|
|
77
|
+
append :type => 'block', :action => 'end_line', :kind => kind
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# Counts the LoC (Lines of Code). Returns an Integer >= 0.
|
|
5
|
+
#
|
|
6
|
+
# Alias: +loc+
|
|
7
|
+
#
|
|
8
|
+
# Everything that is not comment, markup, doctype/shebang, or an empty line,
|
|
9
|
+
# is considered to be code.
|
|
10
|
+
#
|
|
11
|
+
# For example,
|
|
12
|
+
# * HTML files not containing JavaScript have 0 LoC
|
|
13
|
+
# * in a Java class without comments, LoC is the number of non-empty lines
|
|
14
|
+
#
|
|
15
|
+
# A Scanner class should define the token kinds that are not code in the
|
|
16
|
+
# KINDS_NOT_LOC constant, which defaults to [:comment, :doctype].
|
|
17
|
+
class LinesOfCode < TokenKindFilter
|
|
18
|
+
|
|
19
|
+
register_for :lines_of_code
|
|
20
|
+
|
|
21
|
+
NON_EMPTY_LINE = /^\s*\S.*$/
|
|
22
|
+
|
|
23
|
+
protected
|
|
24
|
+
|
|
25
|
+
def setup options
|
|
26
|
+
if scanner
|
|
27
|
+
kinds_not_loc = scanner.class::KINDS_NOT_LOC
|
|
28
|
+
else
|
|
29
|
+
warn "Tokens have no associated scanner, counting all nonempty lines." if $VERBOSE
|
|
30
|
+
kinds_not_loc = CodeRay::Scanners::Scanner::KINDS_NOT_LOC
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
options[:exclude] = kinds_not_loc
|
|
34
|
+
|
|
35
|
+
super options
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def finish options
|
|
39
|
+
output @tokens.text.scan(NON_EMPTY_LINE).size
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
UnknownTokenKind = Class.new InvalidTokenStream
|
|
21
|
+
IncorrectTokenGroupNesting = Class.new InvalidTokenStream
|
|
22
|
+
|
|
23
|
+
def text_token text, kind
|
|
24
|
+
raise EmptyToken, 'empty token for %p' % [kind] if text.empty?
|
|
25
|
+
raise UnknownTokenKind, 'unknown token kind %p (text was %p)' % [kind, text] unless TokenKinds.has_key? kind
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def begin_group kind
|
|
29
|
+
@opened << kind
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def end_group kind
|
|
33
|
+
raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_group)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
|
|
34
|
+
@opened.pop
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def begin_line kind
|
|
38
|
+
@opened << kind
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def end_line kind
|
|
42
|
+
raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_line)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
|
|
43
|
+
@opened.pop
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
protected
|
|
47
|
+
|
|
48
|
+
def setup options
|
|
49
|
+
@opened = []
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def finish options
|
|
53
|
+
raise 'Some tokens still open at end of token stream: %p' % [@opened] unless @opened.empty?
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
load :html
|
|
5
|
+
|
|
6
|
+
# Wraps the output into a HTML page, using CSS classes and
|
|
7
|
+
# line numbers in the table format by default.
|
|
8
|
+
#
|
|
9
|
+
# See Encoders::HTML for available options.
|
|
10
|
+
class Page < HTML
|
|
11
|
+
|
|
12
|
+
FILE_EXTENSION = 'html'
|
|
13
|
+
|
|
14
|
+
register_for :page
|
|
15
|
+
|
|
16
|
+
DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge \
|
|
17
|
+
:css => :class,
|
|
18
|
+
:wrap => :page,
|
|
19
|
+
:line_numbers => :table
|
|
20
|
+
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
load :html
|
|
5
|
+
|
|
6
|
+
# Wraps HTML output into a SPAN element, using inline styles by default.
|
|
7
|
+
#
|
|
8
|
+
# See Encoders::HTML for available options.
|
|
9
|
+
class Span < HTML
|
|
10
|
+
|
|
11
|
+
FILE_EXTENSION = 'span.html'
|
|
12
|
+
|
|
13
|
+
register_for :span
|
|
14
|
+
|
|
15
|
+
DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge \
|
|
16
|
+
:css => :style,
|
|
17
|
+
:wrap => :span,
|
|
18
|
+
:line_numbers => false
|
|
19
|
+
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# Makes a statistic for the given tokens.
|
|
5
|
+
#
|
|
6
|
+
# Alias: +stats+
|
|
7
|
+
class Statistic < Encoder
|
|
8
|
+
|
|
9
|
+
register_for :statistic
|
|
10
|
+
|
|
11
|
+
attr_reader :type_stats, :real_token_count # :nodoc:
|
|
12
|
+
|
|
13
|
+
TypeStats = Struct.new :count, :size # :nodoc:
|
|
14
|
+
|
|
15
|
+
protected
|
|
16
|
+
|
|
17
|
+
def setup options
|
|
18
|
+
super
|
|
19
|
+
|
|
20
|
+
@type_stats = Hash.new { |h, k| h[k] = TypeStats.new 0, 0 }
|
|
21
|
+
@real_token_count = 0
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
STATS = <<-STATS # :nodoc:
|
|
25
|
+
|
|
26
|
+
Code Statistics
|
|
27
|
+
|
|
28
|
+
Tokens %8d
|
|
29
|
+
Non-Whitespace %8d
|
|
30
|
+
Bytes Total %8d
|
|
31
|
+
|
|
32
|
+
Token Types (%d):
|
|
33
|
+
type count ratio size (average)
|
|
34
|
+
-------------------------------------------------------------
|
|
35
|
+
%s
|
|
36
|
+
STATS
|
|
37
|
+
|
|
38
|
+
TOKEN_TYPES_ROW = <<-TKR # :nodoc:
|
|
39
|
+
%-20s %8d %6.2f %% %5.1f
|
|
40
|
+
TKR
|
|
41
|
+
|
|
42
|
+
def finish options
|
|
43
|
+
all = @type_stats['TOTAL']
|
|
44
|
+
all_count, all_size = all.count, all.size
|
|
45
|
+
@type_stats.each do |type, stat|
|
|
46
|
+
stat.size /= stat.count.to_f
|
|
47
|
+
end
|
|
48
|
+
types_stats = @type_stats.sort_by { |k, v| [-v.count, k.to_s] }.map do |k, v|
|
|
49
|
+
TOKEN_TYPES_ROW % [k, v.count, 100.0 * v.count / all_count, v.size]
|
|
50
|
+
end.join
|
|
51
|
+
@out << STATS % [
|
|
52
|
+
all_count, @real_token_count, all_size,
|
|
53
|
+
@type_stats.delete_if { |k, v| k.is_a? String }.size,
|
|
54
|
+
types_stats
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
super
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
public
|
|
61
|
+
|
|
62
|
+
def text_token text, kind
|
|
63
|
+
@real_token_count += 1 unless kind == :space
|
|
64
|
+
@type_stats[kind].count += 1
|
|
65
|
+
@type_stats[kind].size += text.size
|
|
66
|
+
@type_stats['TOTAL'].size += text.size
|
|
67
|
+
@type_stats['TOTAL'].count += 1
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def begin_group kind
|
|
71
|
+
block_token ':begin_group', kind
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def end_group kind
|
|
75
|
+
block_token ':end_group', kind
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def begin_line kind
|
|
79
|
+
block_token ':begin_line', kind
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def end_line kind
|
|
83
|
+
block_token ':end_line', kind
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def block_token action, kind
|
|
87
|
+
@type_stats['TOTAL'].count += 1
|
|
88
|
+
@type_stats[action].count += 1
|
|
89
|
+
@type_stats[kind].count += 1
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
end
|
|
95
|
+
end
|