diff options
author | Kornelius Kalnbach <murphy@rubychan.de> | 2013-06-10 17:52:33 +0200 |
---|---|---|
committer | Kornelius Kalnbach <murphy@rubychan.de> | 2013-06-10 17:52:33 +0200 |
commit | cd7433c5a4d9114e8a398d4fd3eeeadecf290260 (patch) | |
tree | 5403f302e4fd459c0429bc0ebc7d120d9d3af954 /lib | |
parent | a8a17fc78987bfd4236c929fd54c58f8014fe88d (diff) | |
download | coderay-cd7433c5a4d9114e8a398d4fd3eeeadecf290260.tar.gz |
cleanup HTML encoder CSS styles/classes algorithm (#135)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/coderay/encoders/html.rb | 54 | ||||
-rw-r--r-- | lib/coderay/encoders/html/css.rb | 14 |
2 files changed, 40 insertions, 28 deletions
diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index a6c0a8e..b897f5e 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -186,7 +186,7 @@ module Encoders @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 @@ -217,7 +217,7 @@ module Encoders 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") @@ -231,22 +231,19 @@ module Encoders # 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 @@ -261,10 +258,7 @@ module Encoders 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 @@ -281,18 +275,29 @@ module Encoders 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 @@ -306,10 +311,17 @@ module Encoders 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 diff --git a/lib/coderay/encoders/html/css.rb b/lib/coderay/encoders/html/css.rb index 6de4b46..164d7f8 100644 --- a/lib/coderay/encoders/html/css.rb +++ b/lib/coderay/encoders/html/css.rb @@ -11,7 +11,7 @@ module Encoders end def initialize style = :default - @classes = Hash.new + @styles = Hash.new style = CSS.load_stylesheet style @stylesheet = [ style::CSS_MAIN_STYLES, @@ -20,12 +20,12 @@ module Encoders 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 @@ -52,8 +52,8 @@ module Encoders 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 |