Skip to content

Commit

Permalink
Revert behavior of listnum/emlistnum in htmlbuilder without highlight (
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmsnr authored and takahashim committed Oct 1, 2015
1 parent fed17a2 commit 1565936
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
34 changes: 26 additions & 8 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,18 @@ def listnum(lines, id, caption, lang = nil)
end

def listnum_body(lines, lang)
body = lines.inject(''){|i, j| i + detab(j) + "\n"}
lexer = lang
puts highlight(:body => body, :lexer => lexer, :format => 'html',
:options => {:linenos => 'inline', :nowrap => false})
if highlight?
body = lines.inject(''){|i, j| i + detab(j) + "\n"}
lexer = lang
puts highlight(:body => body, :lexer => lexer, :format => 'html',
:options => {:linenos => 'inline', :nowrap => false})
else
print '<pre class="list">'
lines.each_with_index do |line, i|
puts detab((i+1).to_s.rjust(2) + ": " + line)
end
puts '</pre>'
end
end

def emlist(lines, caption = nil, lang = nil)
Expand All @@ -491,10 +499,20 @@ def emlistnum(lines, caption = nil, lang = nil)
if caption.present?
puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
end
body = lines.inject(''){|i, j| i + detab(j) + "\n"}
lexer = lang
puts highlight(:body => body, :lexer => lexer, :format => 'html',
:options => {:linenos => 'inline', :nowrap => false})

if highlight?
body = lines.inject(''){|i, j| i + detab(j) + "\n"}
lexer = lang
puts highlight(:body => body, :lexer => lexer, :format => 'html',
:options => {:linenos => 'inline', :nowrap => false})
else
print '<pre class="emlist">'
lines.each_with_index do |line, i|
puts detab((i+1).to_s.rjust(2) + ": " + line)
end
puts '</pre>'
end

puts '</div>'
end

Expand Down
4 changes: 2 additions & 2 deletions lib/review/htmlutils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def escape_comment(str)
str.gsub('-', '&#45;')
end

def highlight_pygments?
def highlight?
@book.config["pygments"].present? ||
@book.config["highlight"] && @book.config["highlight"]["html"] == "pygments"
end
Expand All @@ -58,7 +58,7 @@ def highlight(ops)
if ops[:options] && ops[:options].kind_of?(Hash)
options.merge!(ops[:options])
end
return body if !highlight_pygments?
return body if !highlight?

begin
require 'pygments'
Expand Down
42 changes: 42 additions & 0 deletions test/test_htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,35 @@ def @chapter.list(id)
assert_equal "<div class=\"caption-code\">\n<p class=\"caption\">リスト1.1: this is <b>test</b>&lt;&amp;&gt;_</p>\n<pre class=\"list\">def foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n</pre>\n</div>\n", actual
end

def test_listnum
def @chapter.list(id)
Book::ListIndex::Item.new("samplelist",1)
end

@book.config["highlight"] = false
actual = compile_block(<<-EOS)
//listnum[samplelist][this is @<b>{test}<&>_][ruby]{
def foo(a1, a2=:test)
(1..3).times{|i| a.include?(:foo)}
return true
end
//}
EOS

expected =<<-EOS
<div class="code">
<p class="caption">リスト1.1: this is <b>test</b>&lt;&amp;&gt;_</p>
<pre class="list"> 1: def foo(a1, a2=:test)
2: (1..3).times{|i| a.include?(:foo)}
3: return true
4: end
</pre>
</div>
EOS

assert_equal expected, actual
end

def test_listnum_pygments_lang
def @chapter.list(id)
Book::ListIndex::Item.new("samplelist",1)
Expand Down Expand Up @@ -582,6 +611,19 @@ def test_emlist_with_tab
assert_equal %Q|<div class="emlist-code">\n<pre class="emlist"> lineA\n lineB\n lineC\n</pre>\n</div>\n|, actual
end

def test_emlistnum
@book.config["highlight"] = false
actual = compile_block("//emlistnum{\nlineA\nlineB\n//}\n")
expected =<<-EOS
<div class="emlistnum-code">
<pre class="emlist"> 1: lineA
2: lineB
</pre>
</div>
EOS
assert_equal expected, actual
end

def test_emlist_with_4tab
@config["tabwidth"] = 4
actual = compile_block("//emlist{\n\tlineA\n\t\tlineB\n\tlineC\n//}\n")
Expand Down

0 comments on commit 1565936

Please sign in to comment.