Skip to content
This repository was archived by the owner on Feb 6, 2018. It is now read-only.

Commit c39168e

Browse files
committed
cleanup HTML encoder #setup (rubychan#135)
1 parent 9d41587 commit c39168e

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

lib/coderay/encoders/html.rb

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,14 @@ def self.token_path_to_hint hint, kinds
171171
def setup options
172172
super
173173

174+
check_options options
175+
174176
if options[:wrap] || options[:line_numbers]
175177
@real_out = @out
176178
@out = ''
177179
end
178180

179181
options[:break_lines] = true if options[:line_numbers] == :inline
180-
181182
@break_lines = (options[:break_lines] == true)
182183

183184
@HTML_ESCAPE = HTML_ESCAPE.dup
@@ -187,43 +188,7 @@ def setup options
187188
@last_opened = nil
188189
@css = CSS.new options[:style]
189190

190-
hint = options[:hint]
191-
if hint && ![:debug, :info, :info_long].include?(hint)
192-
raise ArgumentError, "Unknown value %p for :hint; \
193-
expected :info, :info_long, :debug, false, or nil." % hint
194-
end
195-
196-
css_classes = TokenKinds
197-
case options[:css]
198-
when :class
199-
@span_for_kind = Hash.new do |h, k|
200-
if k.is_a? ::Symbol
201-
kind = k_dup = k
202-
else
203-
kind = k.first
204-
k_dup = k.dup
205-
end
206-
if kind != :space && (hint || css_class = css_classes[kind])
207-
title = HTML.token_path_to_hint hint, k if hint
208-
css_class ||= css_classes[kind]
209-
h[k_dup] = "<span#{title}#{" class=\"#{css_class}\"" if css_class}>"
210-
else
211-
h[k_dup] = nil
212-
end
213-
end
214-
when :style
215-
@span_for_kind = Hash.new do |h, k|
216-
kind = k.is_a?(Symbol) ? k : k.first
217-
h[k.is_a?(Symbol) ? k : k.dup] =
218-
if kind != :space && (hint || css_classes[kind])
219-
title = HTML.token_path_to_hint hint, k if hint
220-
style = @css.get_style Array(k).map { |c| css_classes[c] }
221-
"<span#{title}#{" style=\"#{style}\"" if style}>"
222-
end
223-
end
224-
else
225-
raise ArgumentError, "Unknown value %p for :css." % options[:css]
226-
end
191+
@span_for_kind = make_span_for_kind(options[:css], options[:hint], @css)
227192

228193
@set_last_opened = options[:hint] || options[:css] == :style
229194
end
@@ -306,6 +271,35 @@ def end_line kind
306271

307272
protected
308273

274+
def check_options options
275+
unless [false, nil, :debug, :info, :info_long].include? options[:hint]
276+
raise ArgumentError, "Unknown value %p for :hint; expected :info, :info_long, :debug, false, or nil." % [options[:hint]]
277+
end
278+
279+
unless [:class, :style].include? options[:css]
280+
raise ArgumentError, 'Unknown value %p for :css.' % [options[:css]]
281+
end
282+
end
283+
284+
def make_span_for_kind method, hint, css
285+
css_classes = TokenKinds
286+
287+
Hash.new do |h, k|
288+
kind = k.is_a?(Symbol) ? k : k.first
289+
290+
h[k.is_a?(Symbol) ? k : k.dup] =
291+
if kind != :space && ((css_class = css_classes[kind]) || hint)
292+
title = HTML.token_path_to_hint hint, k if hint
293+
if method == :class
294+
"<span#{title}#{" class=\"#{css_class}\"" if css_class}>"
295+
else
296+
style = css.get_style k.is_a?(Array) ? k.map { |c| css_classes[c] } : [css_class]
297+
"<span#{title}#{" style=\"#{style}\"" if style}>"
298+
end
299+
end
300+
end
301+
end
302+
309303
def check_group_nesting name, kind
310304
if @opened.empty? || @opened.last != kind
311305
warn "Malformed token stream: Trying to close a #{name} (%p) that is not open. Open are: %p." % [kind, @opened[1..-1]]

0 commit comments

Comments
 (0)