From 15659362d86fde7a315da0634500cf62353e6944 Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Sat, 5 Sep 2015 19:05:12 +0900 Subject: [PATCH] Revert behavior of listnum/emlistnum in htmlbuilder without highlight (#449) --- lib/review/htmlbuilder.rb | 34 +++++++++++++++++++++++-------- lib/review/htmlutils.rb | 4 ++-- test/test_htmlbuilder.rb | 42 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 10 deletions(-) diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index e34157460..0b204f009 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -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 '
'
+        lines.each_with_index do |line, i|
+          puts detab((i+1).to_s.rjust(2) + ": " + line)
+        end
+        puts '
' + end end def emlist(lines, caption = nil, lang = nil) @@ -491,10 +499,20 @@ def emlistnum(lines, caption = nil, lang = nil) if caption.present? puts %Q(

#{compile_inline(caption)}

) 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 '
'
+        lines.each_with_index do |line, i|
+          puts detab((i+1).to_s.rjust(2) + ": " + line)
+        end
+        puts '
' + end + puts '' end diff --git a/lib/review/htmlutils.rb b/lib/review/htmlutils.rb index 031e24a80..4e805b9f4 100644 --- a/lib/review/htmlutils.rb +++ b/lib/review/htmlutils.rb @@ -40,7 +40,7 @@ def escape_comment(str) str.gsub('-', '-') end - def highlight_pygments? + def highlight? @book.config["pygments"].present? || @book.config["highlight"] && @book.config["highlight"]["html"] == "pygments" end @@ -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' diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 6600bd7a9..d83c478dd 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -520,6 +520,35 @@ def @chapter.list(id) assert_equal "
\n

リスト1.1: this is test<&>_

\n
def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n
\n
\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 @{test}<&>_][ruby]{ +def foo(a1, a2=:test) + (1..3).times{|i| a.include?(:foo)} + return true +end +//} +EOS + + expected =<<-EOS +
+

リスト1.1: this is test<&>_

+
 1: def foo(a1, a2=:test)
+ 2:   (1..3).times{|i| a.include?(:foo)}
+ 3:   return true
+ 4: end
+
+
+EOS + + assert_equal expected, actual + end + def test_listnum_pygments_lang def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) @@ -582,6 +611,19 @@ def test_emlist_with_tab assert_equal %Q|
\n
        lineA\n                lineB\n        lineC\n
\n
\n|, actual end + def test_emlistnum + @book.config["highlight"] = false + actual = compile_block("//emlistnum{\nlineA\nlineB\n//}\n") + expected =<<-EOS +
+
 1: lineA
+ 2: lineB
+
+
+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")