From 8a95c0a59eab55caaa4aff142f6448ede91db4d7 Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Sun, 9 Jun 2013 21:11:29 +0200 Subject: whitespace --- lib/coderay/encoders/html.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lib/coderay/encoders/html.rb') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 635a4d8..0fd1317 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -286,8 +286,7 @@ module Encoders def end_group kind if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind) - warn 'Malformed token stream: Trying to close a token (%p) ' \ - 'that is not open. Open are: %p.' % [kind, @opened[1..-1]] + warn 'Malformed token stream: Trying to close a token group (%p) that is not open. Open are: %p.' % [kind, @opened[1..-1]] end if @opened.pop @out << '' @@ -312,8 +311,7 @@ module Encoders def end_line kind if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind) - warn 'Malformed token stream: Trying to close a line (%p) ' \ - 'that is not open. Open are: %p.' % [kind, @opened[1..-1]] + warn 'Malformed token stream: Trying to close a line (%p) that is not open. Open are: %p.' % [kind, @opened[1..-1]] end if @opened.pop @out << '' -- cgit v1.2.1 From 9d41587459ec6de14993e62c41666b0cc2692e9f Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Mon, 10 Jun 2013 16:06:08 +0200 Subject: cleanup HTML encoder (#135) --- lib/coderay/encoders/html.rb | 66 ++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'lib/coderay/encoders/html.rb') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 0fd1317..2c66aab 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -126,22 +126,21 @@ module Encoders protected - HTML_ESCAPE = { #:nodoc: - '&' => '&', - '"' => '"', - '>' => '>', - '<' => '<', - } + def self.make_html_escape_hash + { + '&' => '&', + '"' => '"', + '>' => '>', + '<' => '<', + # "\t" => will be set to ' ' * options[:tab_width] during setup + }.tap do |hash| + # Escape ASCII control codes except \x9 == \t and \xA == \n. + (Array(0x00..0x8) + Array(0xB..0x1F)).each { |invalid| hash[invalid.chr] = ' ' } + end + end - # This was to prevent illegal HTML. - # Strange chars should still be avoided in codes. - evil_chars = Array(0x00...0x20) - [?\n, ?\t, ?\s] - evil_chars.each { |i| HTML_ESCAPE[i.chr] = ' ' } - #ansi_chars = Array(0x7f..0xff) - #ansi_chars.each { |i| HTML_ESCAPE[i.chr] = '&#%d;' % i } - # \x9 (\t) and \xA (\n) not included - #HTML_ESCAPE_PATTERN = /[\t&"><\0-\x8\xB-\x1f\x7f-\xff]/ - HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1f]/ + HTML_ESCAPE = make_html_escape_hash + HTML_ESCAPE_PATTERN = /[\t"&><\0-\x8\xB-\x1F]/ TOKEN_KIND_TO_INFO = Hash.new do |h, kind| h[kind] = kind.to_s.gsub(/_/, ' ').gsub(/\b\w/) { $&.capitalize } @@ -255,20 +254,10 @@ module Encoders public def text_token text, kind - if text =~ /#{HTML_ESCAPE_PATTERN}/o - text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } - end - style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] - if @break_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0 - close = '' * c - reopen = '' - @opened.each_with_index do |k, index| - reopen << (@span_for_kind[index > 0 ? [k, *@opened[0 ... index ]] : k] || '') - end - text[i .. -1] = text[i .. -1].gsub("\n", "#{close}\n#{reopen}#{style}") - end + 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") if style @out << style << text << '' @@ -285,9 +274,7 @@ module Encoders end def end_group kind - if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind) - warn 'Malformed token stream: Trying to close a token group (%p) that is not open. Open are: %p.' % [kind, @opened[1..-1]] - end + check_group_nesting 'token group', kind if $CODERAY_DEBUG if @opened.pop @out << '' @last_opened = @opened.last if @last_opened @@ -310,15 +297,28 @@ module Encoders end def end_line kind - if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind) - warn 'Malformed token stream: Trying to close a line (%p) that is not open. Open are: %p.' % [kind, @opened[1..-1]] - end + check_group_nesting 'line', kind if $CODERAY_DEBUG if @opened.pop @out << '' @last_opened = @opened.last if @last_opened end end + protected + + def check_group_nesting name, kind + if @opened.empty? || @opened.last != kind + warn "Malformed token stream: Trying to close a #{name} (%p) that is not open. Open are: %p." % [kind, @opened[1..-1]] + end + end + + def break_lines text, style + reopen = '' + @opened.each_with_index do |k, index| + reopen << (@span_for_kind[index > 0 ? [k, *@opened[0...index]] : k] || '') + end + text.gsub("\n", "#{'' * @opened.size}#{'' if style}\n#{reopen}#{style}") + end end end -- cgit v1.2.1 From c39168e8a8f8313a4e3b6a9f070f14b2cf5e18a7 Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Mon, 10 Jun 2013 17:17:07 +0200 Subject: cleanup HTML encoder #setup (#135) --- lib/coderay/encoders/html.rb | 70 ++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 38 deletions(-) (limited to 'lib/coderay/encoders/html.rb') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 2c66aab..f0f5d62 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -171,13 +171,14 @@ module Encoders def setup options super + check_options options + if options[:wrap] || options[:line_numbers] @real_out = @out @out = '' end options[:break_lines] = true if options[:line_numbers] == :inline - @break_lines = (options[:break_lines] == true) @HTML_ESCAPE = HTML_ESCAPE.dup @@ -187,43 +188,7 @@ module Encoders @last_opened = nil @css = CSS.new options[:style] - hint = options[:hint] - if hint && ![:debug, :info, :info_long].include?(hint) - raise ArgumentError, "Unknown value %p for :hint; \ - expected :info, :info_long, :debug, false, or nil." % hint - end - - css_classes = TokenKinds - case options[:css] - when :class - @span_for_kind = Hash.new do |h, k| - if k.is_a? ::Symbol - kind = k_dup = k - else - kind = k.first - k_dup = k.dup - end - if kind != :space && (hint || css_class = css_classes[kind]) - title = HTML.token_path_to_hint hint, k if hint - css_class ||= css_classes[kind] - h[k_dup] = "" - else - h[k_dup] = nil - end - end - when :style - @span_for_kind = Hash.new do |h, k| - kind = k.is_a?(Symbol) ? k : k.first - h[k.is_a?(Symbol) ? k : k.dup] = - if kind != :space && (hint || css_classes[kind]) - title = HTML.token_path_to_hint hint, k if hint - style = @css.get_style Array(k).map { |c| css_classes[c] } - "" - end - end - else - raise ArgumentError, "Unknown value %p for :css." % options[:css] - end + @span_for_kind = make_span_for_kind(options[:css], options[:hint], @css) @set_last_opened = options[:hint] || options[:css] == :style end @@ -306,6 +271,35 @@ module Encoders protected + def check_options options + unless [false, nil, :debug, :info, :info_long].include? options[:hint] + raise ArgumentError, "Unknown value %p for :hint; expected :info, :info_long, :debug, false, or nil." % [options[:hint]] + end + + unless [:class, :style].include? options[:css] + raise ArgumentError, 'Unknown value %p for :css.' % [options[:css]] + end + end + + def make_span_for_kind method, hint, css + css_classes = TokenKinds + + Hash.new do |h, k| + kind = k.is_a?(Symbol) ? k : k.first + + h[k.is_a?(Symbol) ? k : k.dup] = + if kind != :space && ((css_class = css_classes[kind]) || hint) + title = HTML.token_path_to_hint hint, k if hint + if method == :class + "" + else + style = css.get_style k.is_a?(Array) ? k.map { |c| css_classes[c] } : [css_class] + "" + end + end + end + end + def check_group_nesting name, kind if @opened.empty? || @opened.last != kind warn "Malformed token stream: Trying to close a #{name} (%p) that is not open. Open are: %p." % [kind, @opened[1..-1]] -- cgit v1.2.1 From a8a17fc78987bfd4236c929fd54c58f8014fe88d Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Mon, 10 Jun 2013 17:35:17 +0200 Subject: simplify more HTML encoder methods (#135) --- lib/coderay/encoders/html.rb | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'lib/coderay/encoders/html.rb') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index f0f5d62..a6c0a8e 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -171,18 +171,16 @@ module Encoders def setup options super - check_options options + check_options! options if options[:wrap] || options[:line_numbers] @real_out = @out @out = '' end - options[:break_lines] = true if options[:line_numbers] == :inline @break_lines = (options[:break_lines] == true) - @HTML_ESCAPE = HTML_ESCAPE.dup - @HTML_ESCAPE["\t"] = ' ' * options[:tab_width] + @HTML_ESCAPE = HTML_ESCAPE.merge("\t" => ' ' * options[:tab_width]) @opened = [] @last_opened = nil @@ -271,7 +269,7 @@ module Encoders protected - def check_options options + def check_options! options unless [false, nil, :debug, :info, :info_long].include? options[:hint] raise ArgumentError, "Unknown value %p for :hint; expected :info, :info_long, :debug, false, or nil." % [options[:hint]] end @@ -279,24 +277,23 @@ module Encoders unless [:class, :style].include? options[:css] raise ArgumentError, 'Unknown value %p for :css.' % [options[:css]] end + + options[:break_lines] = true if options[:line_numbers] == :inline end def make_span_for_kind method, hint, css - css_classes = TokenKinds - - Hash.new do |h, k| - kind = k.is_a?(Symbol) ? k : k.first - - h[k.is_a?(Symbol) ? k : k.dup] = - if kind != :space && ((css_class = css_classes[kind]) || hint) - title = HTML.token_path_to_hint hint, k if hint - if method == :class - "" - else - style = css.get_style k.is_a?(Array) ? k.map { |c| css_classes[c] } : [css_class] - "" - end - end + Hash.new do |h, kinds| + h[kinds.is_a?(Symbol) ? kinds : kinds.dup] = begin + css_class = TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first] + 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]]) + "" + else + "" + end if css_class || title + end end end -- cgit v1.2.1 From cd7433c5a4d9114e8a398d4fd3eeeadecf290260 Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Mon, 10 Jun 2013 17:52:33 +0200 Subject: cleanup HTML encoder CSS styles/classes algorithm (#135) --- lib/coderay/encoders/html.rb | 54 +++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 21 deletions(-) (limited to 'lib/coderay/encoders/html.rb') 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] || '') + @out << (@span_for_kinds[@last_opened ? [kind, *@opened] : kind] || '') @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 << '' - @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 << '' - @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]]) - "" - else - "" - end if css_class || title + if css_class || title + if method == :style + style = style_for_kinds(kinds) + "" + else + "" + 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] || '') + reopen << (@span_for_kinds[index > 0 ? [k, *@opened[0...index]] : k] || '') end text.gsub("\n", "#{'' * @opened.size}#{'' if style}\n#{reopen}#{style}") end + + def close_span + if @opened.pop + @out << '' + @last_opened = @opened.last if @last_opened + end + end end end -- cgit v1.2.1