# encoding: utf-8 require 'test_helper' require 'review' class HTMLBuidlerTest < Test::Unit::TestCase include ReVIEW def setup ReVIEW::I18n.setup @builder = HTMLBuilder.new() @config = ReVIEW::Configure.values @config.merge!({ "secnolevel" => 2, # for IDGXMLBuilder, HTMLBuilder "stylesheet" => nil, # for HTMLBuilder "htmlext" => "html", }) @book = Book::Base.new(".") @book.config = @config @compiler = ReVIEW::Compiler.new(@builder) @chapter = Book::Chapter.new(@book, 1, '-', nil, StringIO.new) location = Location.new(nil, nil) @builder.bind(@compiler, @chapter, location) I18n.setup("ja") end def test_xmlns_ops_prefix_epub3 assert_equal "epub", @builder.xmlns_ops_prefix end def test_xmlns_ops_prefix_epub2 @book.config["epubversion"] = 2 assert_equal "ops", @builder.xmlns_ops_prefix end def test_headline_level1 actual = compile_block("={test} this is test.\n") assert_equal %Q|

第1章 this is test.

\n|, actual end def test_headline_level1_postdef @chapter.instance_eval do def on_APPENDIX? true end end actual = compile_block("={test} this is test.\n") assert_equal %Q|

付録A this is test.

\n|, actual end def test_headline_level2_postdef @chapter.instance_eval do def on_APPENDIX? true end end actual = compile_block("=={test} this is test.\n") assert_equal %Q|\n

A.1 this is test.

\n|, actual end def test_headline_postdef_roman Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, "locale.yml") File.open(file, "w"){|f| f.write("locale: ja\nappendix: 付録%pR")} I18n.setup("ja") @chapter.instance_eval do def on_APPENDIX? true end end actual = compile_block("={test} this is test.\n") assert_equal %Q|

付録I this is test.

\n|, actual actual = compile_block("=={test} this is test.\n") assert_equal %Q|\n

I.1 this is test.

\n|, actual end end end def test_headline_postdef_alpha Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, "locale.yml") File.open(file, "w"){|f| f.write("locale: ja\nappendix: 付録%pA")} I18n.setup("ja") @chapter.instance_eval do def on_APPENDIX? true end end actual = compile_block("={test} this is test.\n") assert_equal %Q|

付録A this is test.

\n|, actual actual = compile_block("=={test} this is test.\n") assert_equal %Q|\n

A.1 this is test.

\n|, actual end end end def test_headline_level1_without_secno @book.config["secnolevel"] = 0 actual = compile_block("={test} this is test.\n") assert_equal %Q|

this is test.

\n|, actual end def test_headline_level1_with_tricky_id actual = compile_block("={123 あ_;} this is test.\n") assert_equal %Q|

第1章 this is test.

\n|, actual end def test_headline_level1_with_inlinetag actual = compile_block("={test} this @{is} test.<&\">\n") assert_equal %Q|

第1章 this is test.<&">

\n|, actual end def test_headline_level2 actual = compile_block("=={test} this is test.\n") assert_equal %Q|\n

1.1 this is test.

\n|, actual end def test_headline_level3 actual = compile_block("==={test} this is test.\n") assert_equal %Q|\n

this is test.

\n|, actual end def test_headline_level3_with_secno @book.config["secnolevel"] = 3 actual = compile_block("==={test} this is test.\n") assert_equal %Q|\n

1.0.1 this is test.

\n|, actual end def test_label actual = compile_block("//label[label_test]\n") assert_equal %Q|\n|, actual end def test_label_with_tricky_id actual = compile_block("//label[123 あ_;]\n") assert_equal %Q|\n|, actual end def test_href actual = compile_inline("@{http://github.com,GitHub}") assert_equal %Q|GitHub|, actual end def test_href_without_label actual = compile_inline("@{http://github.com}") assert_equal %Q|http://github.com|, actual end def test_inline_href actual = compile_inline("@{http://github.com,Git\\,Hub}") assert_equal %Q|Git,Hub|, actual @book.config["epubmaker"] ||= {} @book.config["epubmaker"]["externallink"] = false actual = compile_inline("@{http://github.com&q=1,Git\\,Hub}") assert_equal %Q|Git,Hub|, actual actual = compile_inline("@{http://github.com&q=1}") assert_equal %Q|http://github.com&q=1|, actual end def test_inline_href_epubmaker @book.config.maker = "epubmaker" actual = compile_inline("@{http://github.com,Git\\,Hub}") assert_equal %Q|Git,Hub|, actual @book.config["epubmaker"] ||= {} @book.config["epubmaker"]["externallink"] = false actual = compile_inline("@{http://github.com&q=1,Git\\,Hub}") assert_equal %Q|Git,Hub(http://github.com&q=1)|, actual actual = compile_inline("@{http://github.com&q=1}") assert_equal %Q|http://github.com&q=1|, actual @book.config["epubmaker"]["externallink"] = true actual = compile_inline("@{http://github.com&q=1,Git\\,Hub}") assert_equal %Q|Git,Hub|, actual actual = compile_inline("@{http://github.com&q=1}") assert_equal %Q|http://github.com&q=1|, actual end def test_inline_raw actual = compile_inline("@{@{inline\\}}") assert_equal %Q|@{inline}|, actual end def test_inline_in_table actual = compile_block("//table{\n@{1}\t@{2}\n------------\n@{3}\t@{4}<>&\n//}\n") assert_equal %Q|
\n\n\n\n
12
34<>&
\n
\n|, actual end def test_inline_br actual = compile_inline("@
{}") assert_equal %Q|
|, actual end def test_inline_i actual = compile_inline("test @{inline test} test2") assert_equal %Q|test inline test test2|, actual end def test_inline_i_and_escape actual = compile_inline("test @{inline<&;\\ test} test2") assert_equal %Q|test inline<&;\\ test test2|, actual end def test_inline_b actual = compile_inline("test @{inline test} test2") assert_equal %Q|test inline test test2|, actual end def test_inline_b_and_escape actual = compile_inline("test @{inline<&;\\ test} test2") assert_equal %Q|test inline<&;\\ test test2|, actual end def test_inline_tt actual = compile_inline("test @{inline test} test2") assert_equal %Q|test inline test test2|, actual end def test_inline_tti actual = compile_inline("test @{inline test} test2") assert_equal %Q|test inline test test2|, actual end def test_inline_ttb actual = compile_inline("test @{inline test} test2") assert_equal %Q|test inline test test2|, actual end def test_inline_hd_chap def @chapter.headline_index items = [Book::HeadlineIndex::Item.new("chap1|test", [1, 1], "te_st")] Book::HeadlineIndex.new(items, self) end @config["secnolevel"] = 2 actual = compile_inline("test @{chap1|test} test2") assert_equal %Q|test 「te_st」 test2|, actual @config["secnolevel"] = 3 actual = compile_inline("test @{chap1|test} test2") assert_equal %Q|test 「1.1.1 te_st」 test2|, actual end def test_inline_hd_chap_postdef_roman Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, "locale.yml") File.open(file, "w"){|f| f.write("locale: ja\nappendix: 付録%pR")} I18n.setup("ja") @chapter.instance_eval do def on_APPENDIX? true end end def @chapter.headline_index items = [Book::HeadlineIndex::Item.new("test", [1], "te_st")] Book::HeadlineIndex.new(items, self) end actual = compile_inline("test @{test} test2") assert_equal %Q|test 「I.1 te_st」 test2|, actual end end end def test_inline_hd_chap_postdef_alpha Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, "locale.yml") File.open(file, "w"){|f| f.write("locale: ja\nappendix: 付録%pA")} I18n.setup("ja") @chapter.instance_eval do def on_APPENDIX? true end end def @chapter.headline_index items = [Book::HeadlineIndex::Item.new("test", [1], "te_st")] Book::HeadlineIndex.new(items, self) end actual = compile_inline("test @{test} test2") assert_equal %Q|test 「A.1 te_st」 test2|, actual end end end def test_inline_uchar actual = compile_inline("test @{2460} test2") assert_equal %Q|test ① test2|, actual end def test_inline_ruby actual = compile_inline("@{粗雑,クルード}と思われているなら@{繊細,テクニカル}にやり、繊細と思われているなら粗雑にやる。") assert_equal "粗雑クルードと思われているなら繊細テクニカルにやり、繊細と思われているなら粗雑にやる。", actual end def test_inline_ruby_comma actual = compile_inline("@{foo\\, bar\\, buz,フー・バー・バズ}") assert_equal "foo, bar, buzフー・バー・バズ", actual end def test_inline_ref actual = compile_inline("@{外部参照<>&}") assert_equal %Q|「●● 外部参照<>&」|, actual end def test_inline_mathml begin require 'math_ml' require "math_ml/symbol/character_reference" rescue LoadError return true end @config["mathml"] = true actual = compile_inline("@{\\frac{-b \\pm \\sqrt{b^2 - 4ac\\}\\}{2a\\}}") @config["mathml"] = nil assert_equal "-b±b2-4ac2a", actual end def test_inline_img def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg", 1, 'sample photo') item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block "@{sampleimg}\n" expected = %Q|

図1.1

\n| assert_equal expected, actual end def test_inline_imgref def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg", 1, 'sample photo') item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block "@{sampleimg}\n" expected = %Q|

図1.1「sample photo」

\n| assert_equal expected, actual end def test_inline_imgref2 def @chapter.image(id) item = Book::NumberlessImageIndex::Item.new("sampleimg", 1) item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block "@{sampleimg}\n" expected = %Q|

図1.1

\n| assert_equal expected, actual end def test_inline_imgref3 Dir.mktmpdir do |dir| Dir.chdir(dir) do file1 = File.join(dir, "images", "img1.png") filet1 = File.join(dir, "images", "tbl1.png") file2 = File.join(dir, "images", "img2.png") re1 = File.join(dir, "sample1.re") cat = File.join(dir, "catalog.yml") FileUtils.mkdir_p(File.join(dir,"images")) File.open(file1, "w"){|f| f.write("")} File.open(filet1, "w"){|f| f.write("")} File.open(file2, "w"){|f| f.write("")} File.open(cat, "w"){|f| f.write("CHAPS:\n - sample1.re\n")} File.open(re1,"w"){|f| f.write(<{tbl1}. img2 is @{img2}. //image[img1][image 1]{ //} //imgtable[tbl1][table 1]{ //} //image[img2][image 2]{ //} EOF content = File.read(re1) actual = compile_block(content) expected =<<-EOS

第1章 test

tbl1 is 表1.1.

img2 is 図1.2.

image 1

図1.1: image 1

表1.1: table 1

table 1
image 2

図1.2: image 2

EOS assert_equal expected, actual end end end def test_quote actual = compile_block("//quote{\nfoo\nbar\n\nbuz\n//}\n") assert_equal %Q|

foobar

\n

buz

\n|, actual end def test_memo actual = compile_block("//memo[this is @{test}<&>_]{\ntest1\n\ntest@{2}\n//}\n") assert_equal %Q|
\n

this is test<&>_

\n

test1

\n

test2

\n
\n|, actual end def test_noindent @builder.noindent actual = compile_block("foo\nbar\n\nfoo2\nbar2\n") assert_equal %Q|

foobar

\n

foo2bar2

\n|, actual end def test_flushright actual = compile_block("//flushright{\nfoo\nbar\n\nbuz\n//}\n") assert_equal %Q|

foobar

\n

buz

\n|, actual end def test_centering actual = compile_block("//centering{\nfoo\nbar\n\nbuz\n//}\n") assert_equal %Q|

foobar

\n

buz

\n|, actual end def test_image def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg",1) item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block("//image[sampleimg][sample photo]{\n//}\n") assert_equal %Q|
\nsample photo\n

\n図1.1: sample photo\n

\n
\n|, actual end def test_image_with_metric def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg",1) item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block("//image[sampleimg][sample photo][scale=1.2]{\n//}\n") assert_equal %Q|
\nsample photo\n

\n図1.1: sample photo\n

\n
\n|, actual end def test_image_with_metric2 def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg",1) item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block("//image[sampleimg][sample photo][scale=1.2,html::class=sample,latex::ignore=params]{\n//}\n") assert_equal %Q|
\nsample photo\n

\n図1.1: sample photo\n

\n
\n|, actual end def test_image_with_tricky_id def @chapter.image(id) item = Book::ImageIndex::Item.new("123 あ_;",1) item.instance_eval{@path="./images/chap1-123 あ_;.png"} item end actual = compile_block("//image[123 あ_;][sample photo]{\n//}\n") assert_equal %Q|
\nsample photo\n

\n図1.1: sample photo\n

\n
\n|, actual end def test_indepimage def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg",1) item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block("//indepimage[sampleimg][sample photo]\n") assert_equal %Q|
\nsample photo\n

\n図: sample photo\n

\n
\n|, actual end def test_indepimage_without_caption def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg",1) item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block("//indepimage[sampleimg]\n") assert_equal %Q|
\n\n
\n|, actual end def test_indepimage_with_metric def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg",1) item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block("//indepimage[sampleimg][sample photo][scale=1.2]\n") assert_equal %Q|
\nsample photo\n

\n図: sample photo\n

\n
\n|, actual end def test_indepimage_with_metric2 def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg",1) item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block("//indepimage[sampleimg][sample photo][scale=1.2, html::class=\"sample\",latex::ignore=params]\n") assert_equal %Q|
\nsample photo\n

\n図: sample photo\n

\n
\n|, actual end def test_indepimage_without_caption_but_with_metric def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg",1) item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block("//indepimage[sampleimg][][scale=1.2]\n") assert_equal %Q|
\n\n
\n|, actual end def test_dlist actual = compile_block(": foo\n foo.\n bar.\n") assert_equal %Q|
\n
foo
\n
foo.bar.
\n
\n|, actual end def test_dlist_with_bracket actual = compile_block(": foo[bar]\n foo.\n bar.\n") assert_equal %Q|
\n
foo[bar]
\n
foo.bar.
\n
\n|, actual end def test_dlist_with_comment source = ": title\n body\n\#@ comment\n\#@ comment\n: title2\n body2\n" actual = compile_block(source) assert_equal %Q|
\n
title
\n
body
\n
title2
\n
body2
\n
\n|, actual end def test_dlist_beforeulol actual = compile_block(" : foo\n foo.\n\npara\n\n : foo\n foo.\n\n 1. bar\n\n : foo\n foo.\n\n * bar\n") assert_equal %Q|
\n
foo
\n
foo.
\n
\n

para

\n
\n
foo
\n
foo.
\n
\n
    \n
  1. bar
  2. \n
\n
\n
foo
\n
foo.
\n
\n
    \n
  • bar
  • \n
\n|, actual end def test_list def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end actual = compile_block("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") assert_equal %Q|
\n

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

\n
test1\ntest1.5\n\ntest2\n
\n
\n|, actual end def test_inline_list def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end actual = compile_block("@{sampletest}\n") assert_equal %Q|

リスト1.1

\n|, actual end def test_inline_list_href book = ReVIEW::Book::Base.load book.config["chapterlink"] = true book.catalog = ReVIEW::Catalog.new({"CHAPS"=>%w(ch1.re ch2.re)}) io1 = StringIO.new("//list[sampletest]{\nfoo\n//}\n") io2 = StringIO.new("= BAR\n") chap1 = ReVIEW::Book::Chapter.new(book, 1, 'ch1', 'ch1.re', io1) chap2 = ReVIEW::Book::Chapter.new(book, 2, 'ch2', 'ch2.re', io2) book.parts = [ReVIEW::Book::Part.new(self, nil, [chap1, chap2])] builder = ReVIEW::HTMLBuilder.new comp = ReVIEW::Compiler.new(builder) builder.bind(comp, chap2, nil) actual = builder.inline_list("ch1|sampletest") assert_equal %Q|リスト1.1|, actual end def test_list_pygments def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end begin require 'pygments' rescue LoadError $stderr.puts "skip test_list_pygments_lang (cannot find pygments.rb)" return true end @book.config["highlight"] = {} @book.config["highlight"]["html"] = "pygments" actual = compile_block("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") expected = <<-EOS

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

test1
test1.5

test<i>2</i>
EOS assert_equal expected, actual end def test_list_pygments_lang def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end begin require 'pygments' rescue LoadError $stderr.puts "skip test_list_pygments_lang (cannot find pygments.rb)" return true end @book.config["highlight"] = {} @book.config["highlight"]["html"] = "pygments" actual = compile_block("//list[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") assert_equal %Q|
\n| + %Q|

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

\n| + %Q|
def foo(a1, a2=:test)\n| +
                 %Q|  (1..3).times{\|i\| a.include?(:foo)}\n| +
                 %Q|  return true\n| +
                 %Q|end\n| +
                 %Q|
\n| + %Q|
\n|, actual end def test_list_pygments_nulllang def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end begin require 'pygments' rescue LoadError $stderr.puts "skip test_list_pygments_nulllang (cannot find pygments.rb)" return true end @book.config["highlight"] = {} @book.config["highlight"]["html"] = "pygments" actual = compile_block("//list[samplelist][this is @{test}<&>_][]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") expected = <<-EOS

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

def foo(a1, a2=:test)
  (1..3).times{|i| a.include?(:foo)}
  return true
end
EOS assert_equal expected, actual end def test_list_rouge begin require 'rouge' rescue LoadError $stderr.puts "skip test_list_rouge (cannot find Rouge)" return true end def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end @book.config["highlight"] = {} @book.config["highlight"]["html"] = "rouge" actual = compile_block("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") assert_equal %Q|
\n

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

\n
test1\ntest1.5\n\ntest<i>2</i>\n
\n
\n|, actual end def test_list_rouge_lang begin require 'rouge' rescue LoadError $stderr.puts "skip test_list_rouge_lang (cannot find Rouge)" return true end def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end @book.config["highlight"] = {} @book.config["highlight"]["html"] = "rouge" actual = compile_block("//list[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 = "
\n" + "

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

\n" + "
def foo(a1, a2=:test)\n" +
             "  (1..3).times{|i| a.include?(:foo)}\n" +
             "  return true\n" +
             "end\n" +
             "\n" +
             "
\n" + "
\n" assert_equal expected, actual end def test_list_rouge_nulllang begin require 'rouge' rescue LoadError $stderr.puts "skip test_list_rouge_nulllang (cannot find Rouge)" return true end def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end @book.config["highlight"] = {} @book.config["highlight"]["html"] = "rouge" actual = compile_block("//list[samplelist][this is @{test}<&>_][]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") 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
\n", actual end def test_list_ext def @chapter.list(id) Book::ListIndex::Item.new("samplelist.rb",1) end actual = compile_block("//list[samplelist.rb][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n") assert_equal %Q|
\n

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

\n
test1\ntest1.5\n\ntest2\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_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

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

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_listnum_pygments_lang 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("//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 = <<-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_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

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

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) 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" @book.config["highlight"]["lang"] = "ruby" actual = compile_block("//listnum[samplelist][this is @{test}<&>_]{\ndef foo(a1, a2=:test)\n (1..3).times{|i| a.include?(:foo)}\n return true\nend\n\n//}\n") 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_rouge_lang begin require 'rouge' rescue LoadError $stderr.puts "skip test_listnum_rouge_lang (cannot find Rouge)" return true end def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end @book.config["highlight"] = {} @book.config["highlight"]["html"] = "rouge" actual = compile_block("//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") assert_equal "
\n

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

\n
1\n2\n3\n4\n5\n
def foo(a1, a2=:test)\n  (1..3).times{|i| a.include?(:foo)}\n  return true\nend\n\n
\n
\n", actual end def test_listnum_rouge_lang_linenum begin require 'rouge' rescue LoadError $stderr.puts "skip test_listnum_rouge_lang_linenum (cannot find Rouge)" return true end def @chapter.list(id) Book::ListIndex::Item.new("samplelist",1) end @book.config["highlight"] = {} @book.config["highlight"]["html"] = "rouge" 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

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

100
101
102
103
104
def foo(a1, a2=:test)
  (1..3).times{|i| a.include?(:foo)}
  return true
end

EOB assert_equal expected, actual end def test_emlist actual = compile_block("//emlist{\nlineA\nlineB\n//}\n") assert_equal %Q|
\n
lineA\nlineB\n
\n
\n|, actual end def test_emlist_pygments_lang begin require 'pygments' rescue LoadError $stderr.puts "skip test_emlist_pygments_lang (cannot find pygments.rb)" return true end @book.config["highlight"] = {} @book.config["highlight"]["html"] = "pygments" actual = compile_block("//emlist[][sql]{\nSELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'\n//}\n") expected = <<-EOS
SELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'
EOS assert_equal expected, actual end def test_emlist_caption actual = compile_block("//emlist[cap1]{\nlineA\nlineB\n//}\n") assert_equal %Q|
\n

cap1

\n
lineA\nlineB\n
\n
\n|, actual end def test_emlist_with_tab actual = compile_block("//emlist{\n\tlineA\n\t\tlineB\n\tlineC\n//}\n") 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_emlistnum_lang @book.config["highlight"] = false actual = compile_block("//emlistnum[cap][text]{\nlineA\nlineB\n//}\n") expected =<<-EOS

cap

 1: lineA
 2: lineB
EOS 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

cap

1000: lineA
1001: 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") assert_equal %Q|
\n
    lineA\n        lineB\n    lineC\n
\n
\n|, actual end def test_cmd actual = compile_block("//cmd{\nlineA\nlineB\n//}\n") assert_equal %Q|
\n
lineA\nlineB\n
\n
\n|, actual end def test_cmd_pygments begin require 'pygments' rescue LoadError return true end @book.config["highlight"] = {} @book.config["highlight"]["html"] = "pygments" actual = compile_block("//cmd{\nlineA\nlineB\n//}\n") assert_equal "
\n
lineA\nlineB\n
\n
\n", actual end def test_cmd_caption actual = compile_block("//cmd[cap1]{\nlineA\nlineB\n//}\n") assert_equal %Q|
\n

cap1

\n
lineA\nlineB\n
\n
\n|, actual end def test_bib def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end assert_equal %Q|[1]|, compile_inline("@{samplebib}") end def test_bib_noramlized def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("sampleb=ib",1,"sample bib") end assert_equal %Q|[1]|, compile_inline("@{sample=bib}") end def test_bib_htmlext def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end @config["htmlext"] = "xhtml" assert_equal %Q|[1]|, compile_inline("@{samplebib}") end def test_bibpaper def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end actual = compile_block("//bibpaper[samplebib][sample bib @{bold}]{\na\nb\n//}\n") assert_equal %Q|
\n[1] sample bib bold\n

ab

\n|, actual end def test_bibpaper_normalized def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("sample=bib",1,"sample bib") end actual = compile_block("//bibpaper[sample=bib][sample bib @{bold}]{\na\nb\n//}\n") assert_equal %Q|
\n[1] sample bib bold\n

ab

\n|, actual end def test_bibpaper_with_anchor def @chapter.bibpaper(id) Book::BibpaperIndex::Item.new("samplebib",1,"sample bib") end actual = compile_block("//bibpaper[samplebib][sample bib @{http://example.jp}]{\na\nb\n//}\n") assert_equal %Q|
\n[1] sample bib http://example.jp\n

ab

\n|, actual end def column_helper(review) compile_block(review) end def test_column_1 review =<<-EOS ===[column] prev column inside prev column ===[column] test inside column ===[/column] EOS expected =<<-EOS

prev column

inside prev column

test

inside column

EOS assert_equal expected, column_helper(review) end def test_column_2 review =<<-EOS ===[column] test inside column === next level EOS expected =<<-EOS

test

inside column

next level

EOS assert_equal expected, column_helper(review) end def test_column_3 review =<<-EOS ===[column] test inside column ===[/column_dummy] EOS assert_raise(ReVIEW::CompileError) do column_helper(review) end end def test_column_ref review =<<-EOS ===[column]{foo} test inside column === next level this is @{foo}. EOS expected =<<-EOS

test

inside column

next level

this is コラム「test」.

EOS assert_equal expected, column_helper(review) end def test_column_in_aother_chapter_ref def @chapter.column_index items = [Book::ColumnIndex::Item.new("chap1|column", 1, "column_cap")] Book::ColumnIndex.new(items) end actual = compile_inline("test @{chap1|column} test2") expected = "test コラム「column_cap」 test2" assert_equal expected, actual end def test_ul src =<<-EOS * AAA * BBB EOS expected = "
    \n
  • AAA
  • \n
  • BBB
  • \n
\n" actual = compile_block(src) assert_equal expected, actual end def test_ul_cont src =<<-EOS * AAA -AA * BBB -BB EOS expected = "
    \n
  • AAA-AA
  • \n
  • BBB-BB
  • \n
\n" actual = compile_block(src) assert_equal expected, actual end def test_ul_nest1 src =<<-EOS * AAA ** AA EOS expected =<<-EOS
  • AAA
    • AA
EOS actual = compile_block(src) assert_equal expected, actual end def test_ul_nest2 src =<<-EOS * AAA ** AA * BBB ** BB EOS expected =<<-EOS
  • AAA
    • AA
  • BBB
    • BB
EOS actual = compile_block(src) assert_equal expected, actual end def test_ul_nest3 src =<<-EOS ** AAA * AA * BBB ** BB EOS expected =<<-EOS
    • AAA
  • AA
  • BBB
    • BB
EOS actual = compile_block(src) assert_equal expected, actual end def test_ul_nest4 src =<<-EOS * A ** AA *** AAA * B ** BB EOS expected =<<-EOS
  • A
    • AA
      • AAA
  • B
    • BB
EOS actual = compile_block(src) assert_equal expected, actual end def test_ul_nest5 src =<<-EOS * A ** AA **** AAAA * B ** BB EOS expected =<<-EOS
  • A
    • AA
        • AAAA
  • B
    • BB
EOS actual = compile_block(src) assert_equal expected, actual end def test_ol src =<<-EOS 3. AAA 3. BBB EOS expected =<<-EOS
  1. AAA
  2. BBB
EOS actual = compile_block(src) assert_equal expected, actual end def test_inline_raw0 assert_equal "normal", compile_inline("@{normal}") end def test_inline_raw1 assert_equal "body", compile_inline("@{|html|body}") end def test_inline_raw2 assert_equal "body", compile_inline("@{|html, latex|body}") end def test_inline_raw3 assert_equal "", compile_inline("@{|idgxml, latex|body}") end def test_inline_raw4 assert_equal "|html body", compile_inline("@{|html body}") end def test_inline_raw5 assert_equal "nor\nmal", compile_inline("@{|html|nor\\nmal}") end def test_inline_embed0 assert_equal "normal", compile_inline("@{normal}") end def test_inline_embed1 assert_equal "body", compile_inline("@{|html|body}") end def test_inline_embed3 assert_equal "", compile_inline("@{|idgxml, latex|body}") end def test_inline_embed5 assert_equal 'nor\\nmal', compile_inline("@{|html|nor\\nmal}") end def test_inline_embed_math1 assert_equal '\[ \frac{\partial f}{\partial x} =x^2+xy \]', compile_inline('@{\[ \frac{\partial f\}{\partial x\} =x^2+xy \]}') end def test_inline_embed_math1a assert_equal '\[ \frac{\partial f}{\partial x} =x^2+xy \]', compile_inline('@{\\[ \\frac{\\partial f\}{\\partial x\} =x^2+xy \\]}') end def test_inline_embed_math1b assert_equal '\[ \frac{\partial f}{\partial x} =x^2+xy \]', compile_inline('@{\\\\[ \\\\frac{\\\\partial f\}{\\\\partial x\} =x^2+xy \\\\]}') end def test_inline_embed_math1c assert_equal '\\[ \\frac{}{} \\]', compile_inline('@{\[ \\frac{\}{\} \\]}') end def test_inline_embed_n1 assert_equal '\\n', compile_inline('@{\\n}') end def test_inline_embed_n2 assert_equal '\\n', compile_inline('@{\\\\n}') end def test_inline_embed_brace_right0 assert_equal '}', compile_inline('@{\\}}') end def test_inline_embed_brace_right1 assert_equal '\\}', compile_inline('@{\\\\}}') end def test_inline_embed_brace_right2 assert_equal '\\}', compile_inline('@{\\\\\\}}') end def test_inline_embed_brace_right3 assert_equal '\\\\}', compile_inline('@{\\\\\\\\}}') end def test_block_raw0 actual = compile_block("//raw[<>!\"\\n& ]\n") expected = %Q(<>!\"\n& ) assert_equal expected, actual end def test_block_raw1 actual = compile_block("//raw[|html|<>!\"\\n& ]\n") expected = %Q(<>!\"\n& ) assert_equal expected, actual end def test_block_raw2 actual = compile_block("//raw[|html, latex|<>!\"\\n& ]\n") expected = %Q(<>!\"\n& ) assert_equal expected, actual end def test_block_raw3 actual = compile_block("//raw[|latex, idgxml|<>!\"\\n& ]\n") expected = '' assert_equal expected, actual end def test_block_raw4 actual = compile_block("//raw[|html <>!\"\\n& ]\n") expected = %Q(|html <>!\"\n& ) assert_equal expected, actual end def test_embed0 lines = '//embed{' + "\n" + ' <>!\\"\\\\n& ' + "\n" + '//}' + "\n" actual = compile_block(lines) expected = ' <>!\\"\\\\n& ' + "\n" assert_equal expected, actual end def test_embed1 actual = compile_block("//embed[|html|]{\n" + "<>!\\\"\\\\n& \n" + "//}\n") expected = %Q(<>!\\\"\\\\n& \n) assert_equal expected, actual end def test_embed2 actual = compile_block("//embed[html, latex]{\n" + "<>!\\\"\\\\n& \n" + "//}\n") expected = %Q(<>!\\\"\\\\n& \n) assert_equal expected, actual end def test_embed2a actual = compile_block("//embed[|html, latex|]{\n" + "<>!\\\"\\\\n& \n" + "//}\n") expected = %Q(<>!\\\"\\\\n& \n) assert_equal expected, actual end def test_embed2b actual = compile_block("//embed[html, latex]{\n" + '#@# comments are not ignored in //embed block' + "\n" + "<>!\\\"\\\\n& \n" + "//}\n") expected = '#@# comments are not ignored in //embed block' + "\n" + %Q(<>!\\\"\\\\n& \n) assert_equal expected, actual end def test_inline_fn fn = Book::FootnoteIndex.parse(['//footnote[foo][bar\\a\\$buz]']) @chapter.instance_eval{@footnote_index=fn} actual = compile_block("//footnote[foo][bar\\a\\$buz]\n") expected =<<-'EOS'

[*1] bar\a\$buz

EOS assert_equal expected, actual end def test_inline_fn_with_tricky_id fn = Book::FootnoteIndex.parse(['//footnote[123 あ_;][bar\\a\\$buz]']) @chapter.instance_eval{@footnote_index=fn} actual = compile_block("//footnote[123 あ_;][bar\\a\\$buz]\n") expected =<<-'EOS'

[*1] bar\a\$buz

EOS assert_equal expected, actual end def test_inline_hd book = ReVIEW::Book::Base.load book.catalog = ReVIEW::Catalog.new({"CHAPS"=>%w(ch1.re ch2.re)}) io1 = StringIO.new("= test1\n\nfoo\n\n== test1-1\n\nbar\n\n== test1-2\n\nbar\n\n") io2 = StringIO.new("= test2\n\nfoo\n\n== test2-1\n\nbar\n\n== test2-2\n\nbar\n\n") chap1 = ReVIEW::Book::Chapter.new(book, 1, 'ch1', 'ch1.re', io1) chap2 = ReVIEW::Book::Chapter.new(book, 2, 'ch2', 'ch2.re', io2) book.parts = [ReVIEW::Book::Part.new(self, nil, [chap1, chap2])] builder = ReVIEW::HTMLBuilder.new comp = ReVIEW::Compiler.new(builder) builder.bind(comp, chap2, nil) hd = builder.inline_hd("ch1|test1-1") assert_equal "「1.1 test1-1」", hd end def test_inline_hd_for_part book = ReVIEW::Book::Base.load book.catalog = ReVIEW::Catalog.new({"CHAPS"=>%w(ch1.re ch2.re)}) io1 = StringIO.new("= test1\n\nfoo\n\n== test1-1\n\nbar\n\n== test1-2\n\nbar\n\n") io2 = StringIO.new("= test2\n\nfoo\n\n== test2-1\n\nbar\n\n== test2-2\n\nbar\n\n") io_p1 = StringIO.new("= part1\n\nfoo\n\n== part1-1\n\nbar\n\n== part1-2\n\nbar\n\n") chap1 = ReVIEW::Book::Chapter.new(book, 1, 'ch1', 'ch1.re', io1) chap2 = ReVIEW::Book::Chapter.new(book, 2, 'ch2', 'ch2.re', io2) book.parts = [ReVIEW::Book::Part.new(self, 1, [chap1, chap2], "part1.re", io_p1)] builder = ReVIEW::HTMLBuilder.new comp = ReVIEW::Compiler.new(builder) builder.bind(comp, chap2, nil) hd = builder.inline_hd("part1|part1-1") assert_equal "「1.1 part1-1」", hd end def test_table actual = compile_block("//table{\naaa\tbbb\n------------\nccc\tddd<>&\n//}\n") assert_equal %Q|
\n\n\n\n
aaabbb
cccddd<>&
\n
\n|, actual end def test_inline_table def @chapter.table(id) Book::TableIndex::Item.new("sampletable",1) end actual = compile_block("@{sampletest}\n") assert_equal %Q|

表1.1

\n|, actual end def test_emtable actual = compile_block("//emtable[foo]{\naaa\tbbb\n------------\nccc\tddd<>&\n//}\n//emtable{\naaa\tbbb\n------------\nccc\tddd<>&\n//}\n") assert_equal %Q|
\n

foo

\n
\n\n\n
aaabbb
cccddd<>&
\n\n
\n\n\n\n
aaabbb
cccddd<>&
\n
\n|, actual end def test_imgtable def @chapter.image(id) item = Book::ImageIndex::Item.new("sampleimg",1, 'sample img') item.instance_eval{@path="./images/chap1-sampleimg.png"} item end actual = compile_block("//imgtable[sampleimg][test for imgtable]{\n//}\n") expected = %Q|
\n

表1.1: test for imgtable

\ntest for imgtable\n
\n| assert_equal expected, actual end def test_major_blocks actual = compile_block("//note{\nA\n\nB\n//}\n//note[caption]{\nA\n//}") expected = %Q(
\n

A

\n

B

\n
\n
\n

caption

\n

A

\n
\n) assert_equal expected, actual actual = compile_block("//memo{\nA\n\nB\n//}\n//memo[caption]{\nA\n//}") expected = %Q(
\n

A

\n

B

\n
\n
\n

caption

\n

A

\n
\n) assert_equal expected, actual actual = compile_block("//info{\nA\n\nB\n//}\n//info[caption]{\nA\n//}") expected = %Q(
\n

A

\n

B

\n
\n
\n

caption

\n

A

\n
\n) assert_equal expected, actual actual = compile_block("//important{\nA\n\nB\n//}\n//important[caption]{\nA\n//}") expected = %Q(
\n

A

\n

B

\n
\n
\n

caption

\n

A

\n
\n) assert_equal expected, actual actual = compile_block("//caution{\nA\n\nB\n//}\n//caution[caption]{\nA\n//}") expected = %Q(
\n

A

\n

B

\n
\n
\n

caption

\n

A

\n
\n) assert_equal expected, actual actual = compile_block("//notice{\nA\n\nB\n//}\n//notice[caption]{\nA\n//}") expected = %Q(
\n

A

\n

B

\n
\n
\n

caption

\n

A

\n
\n) assert_equal expected, actual actual = compile_block("//warning{\nA\n\nB\n//}\n//warning[caption]{\nA\n//}") expected = %Q(
\n

A

\n

B

\n
\n
\n

caption

\n

A

\n
\n) assert_equal expected, actual actual = compile_block("//tip{\nA\n\nB\n//}\n//tip[caption]{\nA\n//}") expected = %Q(
\n

A

\n

B

\n
\n
\n

caption

\n

A

\n
\n) assert_equal expected, actual end def test_comment actual = compile_block("//comment[コメント]") assert_equal %Q||, actual end def test_comment_for_draft @config["draft"] = true actual = compile_block("//comment[コメント]") assert_equal %Q|
コメント
\n|, actual end def test_inline_comment actual = compile_inline("test @{コメント} test2") assert_equal %Q|test test2|, actual end def test_inline_comment_for_draft @config["draft"] = true actual = compile_inline("test @{コメント} test2") assert_equal %Q|test コメント test2|, actual end end