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

Commit cd7433c

Browse files
committed
cleanup HTML encoder CSS styles/classes algorithm (rubychan#135)
1 parent a8a17fc commit cd7433c

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

lib/coderay/encoders/html.rb

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def setup options
186186
@last_opened = nil
187187
@css = CSS.new options[:style]
188188

189-
@span_for_kind = make_span_for_kind(options[:css], options[:hint], @css)
189+
@span_for_kinds = make_span_for_kinds(options[:css], options[:hint])
190190

191191
@set_last_opened = options[:hint] || options[:css] == :style
192192
end
@@ -217,7 +217,7 @@ def finish options
217217
public
218218

219219
def text_token text, kind
220-
style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
220+
style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]
221221

222222
text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } if text =~ /#{HTML_ESCAPE_PATTERN}/o
223223
text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n")
@@ -231,22 +231,19 @@ def text_token text, kind
231231

232232
# token groups, eg. strings
233233
def begin_group kind
234-
@out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '<span>')
234+
@out << (@span_for_kinds[@last_opened ? [kind, *@opened] : kind] || '<span>')
235235
@opened << kind
236236
@last_opened = kind if @set_last_opened
237237
end
238238

239239
def end_group kind
240240
check_group_nesting 'token group', kind if $CODERAY_DEBUG
241-
if @opened.pop
242-
@out << '</span>'
243-
@last_opened = @opened.last if @last_opened
244-
end
241+
close_span
245242
end
246243

247244
# whole lines to be highlighted, eg. a deleted line in a diff
248245
def begin_line kind
249-
if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
246+
if style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]
250247
if style['class="']
251248
@out << style.sub('class="', 'class="line ')
252249
else
@@ -261,10 +258,7 @@ def begin_line kind
261258

262259
def end_line kind
263260
check_group_nesting 'line', kind if $CODERAY_DEBUG
264-
if @opened.pop
265-
@out << '</span>'
266-
@last_opened = @opened.last if @last_opened
267-
end
261+
close_span
268262
end
269263

270264
protected
@@ -281,18 +275,29 @@ def check_options! options
281275
options[:break_lines] = true if options[:line_numbers] == :inline
282276
end
283277

284-
def make_span_for_kind method, hint, css
278+
def css_class_for_kinds kinds
279+
TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first]
280+
end
281+
282+
def style_for_kinds kinds
283+
css_classes = kinds.is_a?(Array) ? kinds.map { |c| TokenKinds[c] } : [TokenKinds[kinds]]
284+
@css.get_style_for_css_classes css_classes
285+
end
286+
287+
def make_span_for_kinds method, hint
285288
Hash.new do |h, kinds|
286289
h[kinds.is_a?(Symbol) ? kinds : kinds.dup] = begin
287-
css_class = TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first]
290+
css_class = css_class_for_kinds(kinds)
288291
title = HTML.token_path_to_hint hint, kinds if hint
289292

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
293+
if css_class || title
294+
if method == :style
295+
style = style_for_kinds(kinds)
296+
"<span#{title}#{" style=\"#{style}\"" if style}>"
297+
else
298+
"<span#{title}#{" class=\"#{css_class}\"" if css_class}>"
299+
end
300+
end
296301
end
297302
end
298303
end
@@ -306,10 +311,17 @@ def check_group_nesting name, kind
306311
def break_lines text, style
307312
reopen = ''
308313
@opened.each_with_index do |k, index|
309-
reopen << (@span_for_kind[index > 0 ? [k, *@opened[0...index]] : k] || '<span>')
314+
reopen << (@span_for_kinds[index > 0 ? [k, *@opened[0...index]] : k] || '<span>')
310315
end
311316
text.gsub("\n", "#{'</span>' * @opened.size}#{'</span>' if style}\n#{reopen}#{style}")
312317
end
318+
319+
def close_span
320+
if @opened.pop
321+
@out << '</span>'
322+
@last_opened = @opened.last if @last_opened
323+
end
324+
end
313325
end
314326

315327
end

lib/coderay/encoders/html/css.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def CSS.load_stylesheet style = nil
1111
end
1212

1313
def initialize style = :default
14-
@classes = Hash.new
14+
@styles = Hash.new
1515
style = CSS.load_stylesheet style
1616
@stylesheet = [
1717
style::CSS_MAIN_STYLES,
@@ -20,12 +20,12 @@ def initialize style = :default
2020
parse style::TOKEN_COLORS
2121
end
2222

23-
def get_style styles
24-
cl = @classes[styles.first]
23+
def get_style_for_css_classes css_classes
24+
cl = @styles[css_classes.first]
2525
return '' unless cl
2626
style = ''
27-
1.upto styles.size do |offset|
28-
break if style = cl[styles[offset .. -1]]
27+
1.upto css_classes.size do |offset|
28+
break if style = cl[css_classes[offset .. -1]]
2929
end
3030
# warn 'Style not found: %p' % [styles] if style.empty?
3131
return style
@@ -52,8 +52,8 @@ def parse stylesheet
5252
for selector in selectors.split(',')
5353
classes = selector.scan(/[-\w]+/)
5454
cl = classes.pop
55-
@classes[cl] ||= Hash.new
56-
@classes[cl][classes] = style.to_s.strip.delete(' ').chomp(';')
55+
@styles[cl] ||= Hash.new
56+
@styles[cl][classes] = style.to_s.strip.delete(' ').chomp(';')
5757
end
5858
end
5959
end

0 commit comments

Comments
 (0)