Skip to content

Commit

Permalink
here come the white-space nazis
Browse files Browse the repository at this point in the history
  • Loading branch information
korny committed Mar 31, 2012
1 parent d68749d commit df4e2bc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
23 changes: 12 additions & 11 deletions lib/coderay/encoders/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module Encoders
# Default: 'CodeRay output'
#
# === :independent_lines
# Split multilines blocks into line-wide blocks.
# Split multiline blocks at line breaks.
# Forced to true if :line_numbers option is set to :inline.
#
# Default: false
Expand Down Expand Up @@ -105,8 +105,9 @@ class HTML < Encoder
:style => :alpha,
:wrap => nil,
:title => 'CodeRay output',

:independent_lines => false,

:independent_lines => false,

:line_numbers => nil,
:line_number_anchors => 'n',
:line_number_start => 1,
Expand Down Expand Up @@ -174,11 +175,11 @@ def setup options
@real_out = @out
@out = ''
end

options[:independent_lines] = true if options[:line_numbers] == :inline

@independent_lines = (options[:independent_lines] == true)

@HTML_ESCAPE = HTML_ESCAPE.dup
@HTML_ESCAPE["\t"] = ' ' * options[:tab_width]

Expand Down Expand Up @@ -256,9 +257,9 @@ 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 @independent_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0
close = '</span>' * c
reopen = ''
Expand All @@ -267,14 +268,14 @@ def text_token text, kind
end
text[i .. -1] = text[i .. -1].gsub("\n", "#{close}\n#{reopen}#{style}")
end

if style
@out << style << text << '</span>'
else
@out << text
end
end

# token groups, eg. strings
def begin_group kind
@out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '<span>')
Expand Down Expand Up @@ -322,4 +323,4 @@ def end_line kind
end

end
end
end
2 changes: 1 addition & 1 deletion lib/coderay/encoders/html/numbering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ def self.number! output, mode = :table, options = {}
end

end
end
end
39 changes: 19 additions & 20 deletions test/unit/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@
require 'coderay'

class HtmlTest < Test::Unit::TestCase

def test_independent_lines_option

snippets = {}

snippets[:ruby] = {}

snippets[:ruby][:in] = <<-RUBY
ruby_inside = <<-RUBY_INSIDE
This is tricky,
isn't it?
RUBY_INSIDE
RUBY

RUBY
snippets[:ruby][:expected_with_option_off] = <<-HTML_OPT_INDEPENDENT_LINES_OFF
ruby_inside = <span class=\"string\"><span class=\"delimiter\">&lt;&lt;-RUBY_INSIDE</span></span><span class=\"string\"><span class=\"content\">
This is tricky,
isn't it?</span><span class=\"delimiter\">
RUBY_INSIDE</span></span>
HTML_OPT_INDEPENDENT_LINES_OFF

HTML_OPT_INDEPENDENT_LINES_OFF
snippets[:ruby][:expected_with_option_on] = <<-HTML_OPT_INDEPENDENT_LINES_ON
ruby_inside = <span class=\"string\"><span class=\"delimiter\">&lt;&lt;-RUBY_INSIDE</span></span><span class=\"string\"><span class=\"content\"></span></span>
<span class=\"string\"><span class=\"content\">This is tricky,</span></span>
<span class=\"string\"><span class=\"content\">isn't it?</span><span class=\"delimiter\"></span></span>
<span class=\"string\"><span class=\"delimiter\">RUBY_INSIDE</span></span>
HTML_OPT_INDEPENDENT_LINES_ON

HTML_OPT_INDEPENDENT_LINES_ON
snippets[:java] = {}

snippets[:java][:in] = <<-JAVA
import java.lang.*;
Expand All @@ -51,8 +50,8 @@ def test_independent_lines_option
System.out.println(MESSAGE);
}
}
JAVA

JAVA
snippets[:java][:expected_with_option_off] = <<-HTML_OPT_INDEPENDENT_LINES_OFF
<span class=\"keyword\">import</span> <span class=\"include\">java.lang</span>.*;
Expand All @@ -71,8 +70,8 @@ def test_independent_lines_option
<span class=\"predefined-type\">System</span>.out.println(MESSAGE);
}
}
HTML_OPT_INDEPENDENT_LINES_OFF

HTML_OPT_INDEPENDENT_LINES_OFF
snippets[:java][:expected_with_option_on] = <<-HTML_OPT_INDEPENDENT_LINES_ON
<span class=\"keyword\">import</span> <span class=\"include\">java.lang</span>.*;
Expand All @@ -91,14 +90,14 @@ def test_independent_lines_option
<span class=\"predefined-type\">System</span>.out.println(MESSAGE);
}
}
HTML_OPT_INDEPENDENT_LINES_ON

snippets.entries().each do |lang, code|
HTML_OPT_INDEPENDENT_LINES_ON
for lang, code in snippets
tokens = CodeRay.scan code[:in], lang

assert_equal code[:expected_with_option_off], tokens.html
assert_equal code[:expected_with_option_off], tokens.html(:independent_lines => false)
assert_equal code[:expected_with_option_on], tokens.html(:independent_lines => true)
end
end
end
end

0 comments on commit df4e2bc

Please sign in to comment.