diff --git a/.rubocop.yml b/.rubocop.yml index 72d5cc39f..a9999406a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -85,6 +85,9 @@ Style/SafeNavigation: Style/EmptyMethod: Enabled: false +Style/HashSyntax: + Enabled: false + ##################### Metrics ################################## Metrics/ClassLength: diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 7da04f0d0..ffe3b0499 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -83,6 +83,21 @@ def headline_prefix(level) end private :headline_prefix + ## for //firstlinenum[num] + def firstlinenum(num) + @first_line_num = num.to_i + end + + def get_line_num + if !@first_line_num + return 1 + end + line_num = @first_line_num + @first_line_num = nil + + line_num + end + def list(lines, id, caption, lang = nil) begin list_header id, caption, lang diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index e51b4e123..2ac24f828 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -175,6 +175,7 @@ def inline_defined?(name) defsingle :tsize, 1 defsingle :include, 1 defsingle :olnum, 1 + defsingle :firstlinenum, 1 definline :chapref definline :chap diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 5e3ad6ba1..918ca8acd 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -54,6 +54,7 @@ def builder_init_file @column = 0 @sec_counter = SecCounter.new(5, @chapter) @nonum_counter = 0 + @first_line_num = nil @body_ext = nil @toc = nil end @@ -492,14 +493,16 @@ def listnum_body(lines, lang) if highlight? body = lines.inject(''){|i, j| i + detab(j) + "\n"} lexer = lang + first_line_number = get_line_num puts highlight(:body => body, :lexer => lexer, :format => 'html', - :options => {:linenos => 'inline', :nowrap => false}) + :options => {:linenos => 'inline', :nowrap => false, :linenostart => first_line_number}) else class_names = ["list"] class_names.push("language-#{lang}") unless lang.blank? print %Q[
] + first_line_num = get_line_num lines.each_with_index do |line, i| - puts detab((i+1).to_s.rjust(2) + ": " + line) + puts detab((i+first_line_num).to_s.rjust(2) + ": " + line) end puts '' end @@ -529,14 +532,16 @@ def emlistnum(lines, caption = nil, lang = nil) if highlight? body = lines.inject(''){|i, j| i + detab(j) + "\n"} lexer = lang + first_line_number = get_line_num puts highlight(:body => body, :lexer => lexer, :format => 'html', - :options => {:linenos => 'inline', :nowrap => false}) + :options => {:linenos => 'inline', :nowrap => false, :linenostart => first_line_number}) else class_names = ["emlist"] class_names.push("language-#{lang}") unless lang.blank? print %Q[
] + first_line_num = get_line_num lines.each_with_index do |line, i| - puts detab((i+1).to_s.rjust(2) + ": " + line) + puts detab((i+first_line_num).to_s.rjust(2) + ": " + line) end puts '' end diff --git a/lib/review/idgxmlbuilder.rb b/lib/review/idgxmlbuilder.rb index 9b5379d93..e65510fdd 100644 --- a/lib/review/idgxmlbuilder.rb +++ b/lib/review/idgxmlbuilder.rb @@ -64,6 +64,7 @@ def builder_init_file @column = 0 @noindent = nil @ol_num = nil + @first_line_num = nil @rootelement = "doc" @secttags = nil @tsize = nil @@ -362,8 +363,9 @@ def emlist(lines, caption = nil, lang = nil) def emlistnum(lines, caption = nil, lang = nil) _lines = [] + first_line_num = get_line_num lines.each_with_index do |line, i| - _lines << detab("" + (i + 1).to_s.rjust(2) + ": " + line) + _lines << detab("" + (i + first_line_num).to_s.rjust(2) + ": " + line) end quotedlist _lines, 'emlistnum', caption end @@ -371,6 +373,7 @@ def emlistnum(lines, caption = nil, lang = nil) def listnum_body(lines, lang) print %Q(
) no = 1 + first_line_num = get_line_num lines.each_with_index do |line, i| unless @book.config["listinfo"].nil? print "" end - print detab("" + (i + 1).to_s.rjust(2) + ": " + line) + print detab("" + (i + first_line_num).to_s.rjust(2) + ": " + line) print "\n" print " " unless @book.config["listinfo"].nil? no += 1 diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 52f087aaa..9bccc0bf1 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -37,6 +37,7 @@ def builder_init_file @tsize = nil @table_caption = nil @ol_num = nil + @first_line_num = nil @sec_counter = SecCounter.new(5, @chapter) initialize_metachars(@book.config["texcommand"]) end @@ -261,11 +262,12 @@ def emlist(lines, caption = nil, lang = nil) def emlistnum(lines, caption = nil, lang = nil) blank + first_line_num = get_line_num if highlight_listings? - common_code_block_lst(nil, lines, 'reviewemlistnumlst', 'title', caption, lang) + common_code_block_lst(nil, lines, 'reviewemlistnumlst', 'title', caption, lang, first_line_num: first_line_num) else common_code_block(nil, lines, 'reviewemlist', caption, lang) do |line, idx| - detab((idx+1).to_s.rjust(2)+": " + line) + "\n" + detab((idx+first_line_num).to_s.rjust(2)+": " + line) + "\n" end end end @@ -283,11 +285,12 @@ def list(lines, id, caption, lang = nil) ## override Builder#listnum def listnum(lines, id, caption, lang = nil) + first_line_num = get_line_num if highlight_listings? - common_code_block_lst(id, lines, 'reviewlistnumlst', 'caption', caption, lang) + common_code_block_lst(id, lines, 'reviewlistnumlst', 'caption', caption, lang, first_line_num: first_line_num) else common_code_block(id, lines, 'reviewlist', caption, lang) do |line, idx| - detab((idx+1).to_s.rjust(2)+": " + line) + "\n" + detab((idx+first_line_num).to_s.rjust(2)+": " + line) + "\n" end end end @@ -325,11 +328,22 @@ def common_code_block(id, lines, command, caption, lang) blank end - def common_code_block_lst(id, lines, command, title, caption, lang) + def common_code_block_lst(id, lines, command, title, caption, lang, first_line_num: 1) + if title == "title" && caption.blank? + print "\\vspace{-1.5em}" + end + body = lines.inject(''){|i, j| i + detab(unescape_latex(j)) + "\n"} + args = make_code_block_args(title, caption, lang, first_line_num: first_line_num) + puts "\\begin{"+command+"}[" + args + "]" + print body + puts "\\end{"+ command + "}" + blank + end + + def make_code_block_args(title, caption, lang, first_line_num: 1) caption_str = compile_inline((caption || "")) if title == "title" && caption_str == "" caption_str = "\\relax" ## dummy charactor to remove lstname - print "\\vspace{-1.5em}" end if @book.config["highlight"] && @book.config["highlight"]["lang"] lexer = @book.config["highlight"]["lang"] # default setting @@ -337,11 +351,11 @@ def common_code_block_lst(id, lines, command, title, caption, lang) lexer = "" end lexer = lang if lang.present? - body = lines.inject(''){|i, j| i + detab(unescape_latex(j)) + "\n"} - puts "\\begin{"+command+"}["+title+"={"+caption_str+"},language={"+ lexer+"}]" - print body - puts "\\end{"+ command + "}" - blank + args = title + "={" + caption_str + "},language={" + lexer + "}" + if first_line_num != 1 + args += ",firstnumber=#{first_line_num}" + end + args end def source(lines, caption, lang = nil) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index adc52b656..f5fa08ed0 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -655,6 +655,36 @@ def foo(a1, a2=:test) assert_equal expected, actual end + def test_listnum_linenum + def @chapter.list(id) + Book::ListIndex::Item.new("samplelist",1) + end + + @book.config["highlight"] = false + actual = compile_block(<<-EOS) +//firstlinenum[100] +//listnum[samplelist][this is @{test}<&>_][ruby]{ +def foo(a1, a2=:test) + (1..3).times{|i| a.include?(:foo)} + return true +end +//} +EOS + + expected =<<-EOS +++EOS + + assert_equal expected, actual + end + def test_listnum_pygments_lang def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) @@ -682,6 +712,34 @@ def @chapter.list(id) assert_equal expected, actual end + def test_listnum_pygments_lang_linenum + def @chapter.list(id) + Book::ListIndex::Item.new("samplelist",1) + end + begin + require 'pygments' + rescue LoadError + $stderr.puts "skip test_listnum_pygments_lang (cannot find pygments.rb)" + return true + end + @book.config["highlight"] = {} + @book.config["highlight"]["html"] = "pygments" + actual = compile_block("//firstlinenum[100]\n//listnum[samplelist][this is @{test}<&>_][ruby]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") + + expected = <<-EOB + +100: def foo(a1, a2=:test) +101: (1..3).times{|i| a.include?(:foo)} +102: return true +103: end ++++EOB + + assert_equal expected, actual + end + def test_listnum_pygments_lang_without_lang def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) @@ -772,6 +830,20 @@ def test_emlistnum_lang assert_equal expected, actual end + def test_emlistnum_lang_linenum + @book.config["highlight"] = false + actual = compile_block("//firstlinenum[1000]\n//emlistnum[cap][text]{\nlineA\nlineB\n//}\n") + expected =<<-EOS + ++100 def foo(a1, a2=:test) +101 (1..3).times{|i| a.include?(:foo)} +102 return true +103 end +++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") diff --git a/test/test_idgxmlbuilder.rb b/test/test_idgxmlbuilder.rb index 4b4ed9be0..65298435d 100644 --- a/test/test_idgxmlbuilder.rb +++ b/test/test_idgxmlbuilder.rb @@ -269,6 +269,22 @@ def @chapter.list(id) assert_equal %Q| +1000: lineA +1001: lineB ++|, actual end + def test_listnum + def @chapter.list(id) + Book::ListIndex::Item.new("samplelist",1) + end + actual = compile_block("//listnum[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") + assert_equal %Q| リスト1.1 this is test<&>_ test1\ntest1.5\n\ntest2\n|, actual + end + + def test_listnum_linenum + def @chapter.list(id) + Book::ListIndex::Item.new("samplelist",1) + end + actual = compile_block("//firstlinenum[100]\n//listnum[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") + assert_equal %Q| リスト1.1 this is test<&>_ 1: test1\n 2: test1.5\n 3: \n 4: test2\n|, actual + end + def test_list_listinfo def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index bb8c6e094..b296ee58b 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -311,6 +311,11 @@ def test_listnum assert_equal %Q|\\reviewlistcaption{リスト1.1: ruby}\n\\begin{reviewlist}\n 1: class Foo\n 2: def foo\n 3: bar\n 4: \n 5: buz\n 6: end\n 7: end\n\\end{reviewlist}\n|, actual end + def test_listnum_linenum + actual = compile_block("//firstlinenum[100]\n//listnum[test1][ruby]{\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n//}\n") + assert_equal %Q|\\reviewlistcaption{リスト1.1: ruby}\n\\begin{reviewlist}\n100: class Foo\n101: def foo\n102: bar\n103: \n104: buz\n105: end\n106: end\n\\end{reviewlist}\n|, actual + end + def test_listnum_lst @book.config["highlight"] = {} @book.config["highlight"]["latex"] = "listings" @@ -318,6 +323,13 @@ def test_listnum_lst assert_equal %Q|\\begin{reviewlistnumlst}[caption={ruby},language={}]\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n\\end{reviewlistnumlst}\n|, actual end + def test_listnum_lst_linenum + @book.config["highlight"] = {} + @book.config["highlight"]["latex"] = "listings" + actual = compile_block("//firstlinenum[100]\n//listnum[test1][ruby]{\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n//}\n") + assert_equal %Q|\\begin{reviewlistnumlst}[caption={ruby},language={},firstnumber=100]\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n\\end{reviewlistnumlst}\n|, actual + end + def test_source actual = compile_block("//source[foo/bar/test.rb]{\nfoo\nbar\n\nbuz\n//}\n") assert_equal %Q|\\reviewsourcecaption{foo/bar/test.rb}\n\\begin{reviewsource}\nfoo\nbar\n\nbuz\n\\end{reviewsource}\n|, actual リスト1.1 this is test<&>_ 100: test1\n101: test1.5\n102: \n103: test2\n