Skip to content

Commit a8a17fc

Browse files
committed
simplify more HTML encoder methods (#135)
1 parent c39168e commit a8a17fc

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

lib/coderay/encoders/html.rb

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,16 @@ def self.token_path_to_hint hint, kinds
171171
def setup options
172172
super
173173

174-
check_options options
174+
check_options! options
175175

176176
if options[:wrap] || options[:line_numbers]
177177
@real_out = @out
178178
@out = ''
179179
end
180180

181-
options[:break_lines] = true if options[:line_numbers] == :inline
182181
@break_lines = (options[:break_lines] == true)
183182

184-
@HTML_ESCAPE = HTML_ESCAPE.dup
185-
@HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
183+
@HTML_ESCAPE = HTML_ESCAPE.merge("\t" => ' ' * options[:tab_width])
186184

187185
@opened = []
188186
@last_opened = nil
@@ -271,32 +269,31 @@ def end_line kind
271269

272270
protected
273271

274-
def check_options options
272+
def check_options! options
275273
unless [false, nil, :debug, :info, :info_long].include? options[:hint]
276274
raise ArgumentError, "Unknown value %p for :hint; expected :info, :info_long, :debug, false, or nil." % [options[:hint]]
277275
end
278276

279277
unless [:class, :style].include? options[:css]
280278
raise ArgumentError, 'Unknown value %p for :css.' % [options[:css]]
281279
end
280+
281+
options[:break_lines] = true if options[:line_numbers] == :inline
282282
end
283283

284284
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
285+
Hash.new do |h, kinds|
286+
h[kinds.is_a?(Symbol) ? kinds : kinds.dup] = begin
287+
css_class = TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first]
288+
title = HTML.token_path_to_hint hint, kinds if hint
289+
290+
if method == :style
291+
style = css.get_style(kinds.is_a?(Array) ? kinds.map { |c| TokenKinds[c] } : [TokenKinds[kinds]])
292+
"<span#{title}#{" style=\"#{style}\"" if style}>"
293+
else
294+
"<span#{title}#{" class=\"#{css_class}\"" if css_class}>"
295+
end if css_class || title
296+
end
300297
end
301298
end
302299

0 commit comments

Comments
 (0)