Skip to content

Commit

Permalink
cleanup HTML encoder CSS styles/classes algorithm (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
korny committed Jun 10, 2013
1 parent a8a17fc commit cd7433c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
54 changes: 33 additions & 21 deletions lib/coderay/encoders/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def setup options
@last_opened = nil
@css = CSS.new options[:style]

@span_for_kind = make_span_for_kind(options[:css], options[:hint], @css)
@span_for_kinds = make_span_for_kinds(options[:css], options[:hint])

@set_last_opened = options[:hint] || options[:css] == :style
end
Expand Down Expand Up @@ -217,7 +217,7 @@ def finish options
public

def text_token text, kind
style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind]

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

# token groups, eg. strings
def begin_group kind
@out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '<span>')
@out << (@span_for_kinds[@last_opened ? [kind, *@opened] : kind] || '<span>')
@opened << kind
@last_opened = kind if @set_last_opened
end

def end_group kind
check_group_nesting 'token group', kind if $CODERAY_DEBUG
if @opened.pop
@out << '</span>'
@last_opened = @opened.last if @last_opened
end
close_span
end

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

def end_line kind
check_group_nesting 'line', kind if $CODERAY_DEBUG
if @opened.pop
@out << '</span>'
@last_opened = @opened.last if @last_opened
end
close_span
end

protected
Expand All @@ -281,18 +275,29 @@ def check_options! options
options[:break_lines] = true if options[:line_numbers] == :inline
end

def make_span_for_kind method, hint, css
def css_class_for_kinds kinds
TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first]
end

def style_for_kinds kinds
css_classes = kinds.is_a?(Array) ? kinds.map { |c| TokenKinds[c] } : [TokenKinds[kinds]]
@css.get_style_for_css_classes css_classes
end

def make_span_for_kinds method, hint
Hash.new do |h, kinds|
h[kinds.is_a?(Symbol) ? kinds : kinds.dup] = begin
css_class = TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first]
css_class = css_class_for_kinds(kinds)
title = HTML.token_path_to_hint hint, kinds if hint

if method == :style
style = css.get_style(kinds.is_a?(Array) ? kinds.map { |c| TokenKinds[c] } : [TokenKinds[kinds]])
"<span#{title}#{" style=\"#{style}\"" if style}>"
else
"<span#{title}#{" class=\"#{css_class}\"" if css_class}>"
end if css_class || title
if css_class || title
if method == :style
style = style_for_kinds(kinds)
"<span#{title}#{" style=\"#{style}\"" if style}>"
else
"<span#{title}#{" class=\"#{css_class}\"" if css_class}>"
end
end
end
end
end
Expand All @@ -306,10 +311,17 @@ def check_group_nesting name, kind
def break_lines text, style
reopen = ''
@opened.each_with_index do |k, index|
reopen << (@span_for_kind[index > 0 ? [k, *@opened[0...index]] : k] || '<span>')
reopen << (@span_for_kinds[index > 0 ? [k, *@opened[0...index]] : k] || '<span>')
end
text.gsub("\n", "#{'</span>' * @opened.size}#{'</span>' if style}\n#{reopen}#{style}")
end

def close_span
if @opened.pop
@out << '</span>'
@last_opened = @opened.last if @last_opened
end
end
end

end
Expand Down
14 changes: 7 additions & 7 deletions lib/coderay/encoders/html/css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def CSS.load_stylesheet style = nil
end

def initialize style = :default
@classes = Hash.new
@styles = Hash.new
style = CSS.load_stylesheet style
@stylesheet = [
style::CSS_MAIN_STYLES,
Expand All @@ -20,12 +20,12 @@ def initialize style = :default
parse style::TOKEN_COLORS
end

def get_style styles
cl = @classes[styles.first]
def get_style_for_css_classes css_classes
cl = @styles[css_classes.first]
return '' unless cl
style = ''
1.upto styles.size do |offset|
break if style = cl[styles[offset .. -1]]
1.upto css_classes.size do |offset|
break if style = cl[css_classes[offset .. -1]]
end
# warn 'Style not found: %p' % [styles] if style.empty?
return style
Expand All @@ -52,8 +52,8 @@ def parse stylesheet
for selector in selectors.split(',')
classes = selector.scan(/[-\w]+/)
cl = classes.pop
@classes[cl] ||= Hash.new
@classes[cl][classes] = style.to_s.strip.delete(' ').chomp(';')
@styles[cl] ||= Hash.new
@styles[cl][classes] = style.to_s.strip.delete(' ').chomp(';')
end
end
end
Expand Down

0 comments on commit cd7433c

Please sign in to comment.