From eee67f5e2db06e516886557059cf47e61891e32b Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 24 Nov 2015 00:57:59 +0900 Subject: [PATCH] fix caption of //list, //listnum --- lib/review/latexbuilder.rb | 49 ++++++++++++++++++-------------------- test/test_latexbuilder.rb | 2 +- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index caee98a85..787336804 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -230,9 +230,9 @@ def highlight_listings? def emlist(lines, caption = nil, lang = nil) buf = "\n" if highlight_listings? - buf << common_code_block_lst(lines, 'reviewemlistlst', 'title', caption, lang) + buf << common_code_block_lst(nil, lines, 'reviewemlistlst', 'title', caption, lang) else - buf << common_code_block(lines, 'reviewemlist', caption, lang) do |line, idx| + buf << common_code_block(nil, lines, 'reviewemlist', caption, lang) do |line, idx| detab(line) + "\n" end end @@ -242,9 +242,9 @@ def emlist(lines, caption = nil, lang = nil) def emlistnum(lines, caption = nil, lang = nil) buf = "\n" if highlight_listings? - buf << common_code_block_lst(lines, 'reviewemlistnumlst', 'title', caption, lang) + buf << common_code_block_lst(nil, lines, 'reviewemlistnumlst', 'title', caption, lang) else - buf << common_code_block(lines, 'reviewemlist', caption, lang) do |line, idx| + buf << common_code_block(nil, lines, 'reviewemlist', caption, lang) do |line, idx| detab((idx+1).to_s.rjust(2)+": " + line) + "\n" end end @@ -255,15 +255,9 @@ def emlistnum(lines, caption = nil, lang = nil) def list(lines, id, caption = nil, lang = nil) buf = "" if highlight_listings? - buf << common_code_block_lst(lines, 'reviewlistlst', 'caption', caption, lang) + buf << common_code_block_lst(id, lines, 'reviewlistlst', 'caption', caption, lang) else - begin - buf << "\n" - buf << macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}") + "\n" - rescue KeyError - error "no such list: #{id}" - end - buf << common_code_block(lines, 'reviewlist', nil, lang) do |line, idx| + buf << common_code_block(id, lines, 'reviewlist', caption, lang) do |line, idx| detab(line) + "\n" end end @@ -275,15 +269,9 @@ def list(lines, id, caption = nil, lang = nil) def listnum(lines, id, caption = nil, lang = nil) buf = "" if highlight_listings? - buf << common_code_block_lst(lines, 'reviewlistnumlst', 'caption', caption, lang) + buf << common_code_block_lst(id, lines, 'reviewlistnumlst', 'caption', caption, lang) else - begin - buf << "\n" - buf << macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}") + "\n" - rescue KeyError - error "no such list: #{id}" - end - buf << common_code_block(lines, 'reviewlist', caption, lang) do |line, idx| + buf << common_code_block(id, lines, 'reviewlist', caption, lang) do |line, idx| detab((idx+1).to_s.rjust(2)+": " + line) + "\n" end end @@ -293,20 +281,29 @@ def listnum(lines, id, caption = nil, lang = nil) def cmd(lines, caption = nil, lang = nil) buf = "" if highlight_listings? - buf << common_code_block_lst(lines, 'reviewcmdlst', 'title', caption, lang) + buf << common_code_block_lst(nil, lines, 'reviewcmdlst', 'title', caption, lang) else buf << "\n" - buf << common_code_block(lines, 'reviewcmd', caption, lang) do |line, idx| + buf << common_code_block(nil, lines, 'reviewcmd', caption, lang) do |line, idx| detab(line) + "\n" end end buf end - def common_code_block(lines, command, caption, lang) + def common_code_block(id, lines, command, caption, lang) buf = "" if caption - buf << macro(command + 'caption', "#{caption}") + "\n" + if command =~ /emlist/ || command =~ /cmd/ + buf << macro(command + 'caption', "#{caption}") + "\n" + else + begin + buf << "\n" + buf << macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}") + "\n" + rescue KeyError + error "no such list: #{id}" + end + end end body = "" lines.each_with_index do |line, idx| @@ -318,7 +315,7 @@ def common_code_block(lines, command, caption, lang) buf end - def common_code_block_lst(lines, command, title, caption, lang) + def common_code_block_lst(id, lines, command, title, caption, lang) buf = "" caption_str = (caption || "") if title == "title" && caption_str == "" @@ -340,7 +337,7 @@ def common_code_block_lst(lines, command, title, caption, lang) def source(lines, caption = nil, lang = nil) if highlight_listings? - common_code_block_lst(lines, 'reviewlistlst', 'title', caption, lang) + common_code_block_lst(nil, lines, 'reviewlistlst', 'title', caption, lang) else buf = "\n" buf << '\begin{reviewlist}' << "\n" diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index a332f784f..015ec84e7 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -279,7 +279,7 @@ def test_emlist_with_tab4 def test_listnum actual = compile_block("//listnum[test1][ruby]{\nclass Foo\n def foo\n bar\n\n buz\n end\nend\n//}\n") - assert_equal %Q|\n\\reviewlistcaption{リスト1.1: ruby}\n\\reviewlistcaption{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 + assert_equal %Q|\n\\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_lst