require 'test_helper'
require 'book_test_helper'
require 'review'
class HTMLBuidlerTest < Test::Unit::TestCase
include ReVIEW
include BookTestHelper
def setup
ReVIEW::I18n.setup
@config = ReVIEW::Configure.values
@config['secnolevel'] = 2
@config['stylesheet'] = nil
@config['htmlext'] = 'html'
@config['epubmaker'] = {}
@book = Book::Base.new('.')
@book.config = @config
img_math = ReVIEW::ImgMath.new(@config)
@builder = HTMLBuilder.new(img_math: img_math)
@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')
@skip_pygments = true # temporary suppress pygments test
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(\nA.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.write(file, "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(\nI.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.write(file, "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(\nA.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(%Q(={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(\n1.1 this is test.
\n), actual
end
def test_headline_level3
actual = compile_block("==={test} this is test.\n")
assert_equal %Q(\nthis 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(\n1.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 'Git,Hub(http://github.com&q=1)', actual
actual = compile_inline('@{http://github.com&q=1}')
assert_equal '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 '@{inline}', actual
end
def test_inline_in_table
actual = compile_block("//table{\n@{1}\t@{2}\n------------\n@{3}\t@{4}<>&\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_inline_br
actual = compile_inline('@
{}')
assert_equal '
', actual
end
def test_inline_i
actual = compile_inline('test @{inline test} test2')
assert_equal 'test inline test test2', actual
end
def test_inline_i_and_escape
actual = compile_inline('test @{inline<&;\\ test} test2')
assert_equal 'test inline<&;\\ test test2', actual
end
def test_inline_b
actual = compile_inline('test @{inline test} test2')
assert_equal 'test inline test test2', actual
end
def test_inline_b_and_escape
actual = compile_inline('test @{inline<&;\\ test} test2')
assert_equal '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
item = Book::Index::Item.new('chap1|test', [1, 1], 'te_st')
idx = Book::HeadlineIndex.new(self)
idx.add_item(item)
idx
end
@config['secnolevel'] = 2
actual = compile_inline('test @{chap1|test} test2')
assert_equal 'test 「te_st」 test2', actual
@config['secnolevel'] = 3
actual = compile_inline('test @{chap1|test} test2')
assert_equal 'test 「1.1.1 te_st」 test2', actual
@config['chapterlink'] = nil
@config['secnolevel'] = 2
actual = compile_inline('test @{chap1|test} test2')
assert_equal 'test 「te_st」 test2', actual
@config['secnolevel'] = 3
actual = compile_inline('test @{chap1|test} test2')
assert_equal '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.write(file, "locale: ja\nappendix: 付録%pR")
I18n.setup('ja')
@chapter.instance_eval do
def on_appendix?
true
end
end
def @chapter.headline_index
item = Book::Index::Item.new('test', [1], 'te_st')
idx = Book::HeadlineIndex.new(self)
idx.add_item(item)
idx
end
actual = compile_inline('test @{test} test2')
assert_equal 'test 「I.1 te_st」 test2', actual
@config['chapterlink'] = nil
actual = compile_inline('test @{test} test2')
assert_equal '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.write(file, "locale: ja\nappendix: 付録%pA")
I18n.setup('ja')
@chapter.instance_eval do
def on_appendix?
true
end
end
def @chapter.headline_index
item = Book::Index::Item.new('test', [1], 'te_st')
idx = Book::HeadlineIndex.new(self)
idx.add_item(item)
idx
end
actual = compile_inline('test @{test} test2')
assert_equal 'test 「A.1 te_st」 test2', actual
@config['chapterlink'] = nil
actual = compile_inline('test @{test} test2')
assert_equal 'test 「A.1 te_st」 test2', actual
end
end
end
def test_inline_uchar
actual = compile_inline('test @{2460} test2')
assert_equal 'test ① test2', actual
end
def test_inline_balloon
actual = compile_inline('test @{①}')
assert_equal %Q(test ①), 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['math_format'] = 'mathml'
actual = compile_inline('@{\\frac{-b \\pm \\sqrt{b^2 - 4ac\\}\\}{2a\\}}')
assert_equal %Q(), actual
end
def test_inline_mathjax
@config['math_format'] = 'mathjax'
actual = compile_inline('@{\\frac{-b \\pm \\sqrt{b^2 - 4ac\\}\\}{2a\\}}')
assert_equal %Q(\\( \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a} \\)), actual
content = <<-EOF
//texequation{
\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}
//}
EOF
actual = compile_block(content)
expected = %Q(]\n$$\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}$$\n
\n)
assert_equal expected, actual
end
def test_inline_img
def @chapter.image(_id)
item = Book::Index::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
@config['chapterlink'] = nil
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::Index::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
@config['chapterlink'] = nil
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::Index::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
@config['chapterlink'] = nil
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')
file3 = File.join(dir, 'images', 'icon3.png')
re1 = File.join(dir, 'sample1.re')
cat = File.join(dir, 'catalog.yml')
FileUtils.mkdir_p(File.join(dir, 'images'))
File.write(file1, '')
File.write(filet1, '')
File.write(file2, '')
File.write(file3, '')
File.write(cat, "CHAPS:\n - sample1.re\n")
File.write(re1, <<-EOF)
= test
tbl1 is @{tbl1}.
img2 is @{img2}.
icon3 is @{icon3}.
//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.
icon3 is .
図1.1: image 1
表1.1: table 1
図1.2: image 2
EOS
assert_equal expected, actual
@config['chapterlink'] = nil
actual = compile_block(content)
expected = <<-EOS
第1章 test
tbl1 is 表1.1.
img2 is 図1.2.
icon3 is .
図1.1: image 1
表1.1: table 1
図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")
expected = <<-EOS
foobar
buz
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block("//quote{\nfoo\nbar\n\nbuz\n//}\n")
expected = <<-EOS
foo bar
buz
EOS
assert_equal expected, actual
end
def test_memo
actual = compile_block("//memo[this is @{test}<&>_]{\ntest1\n\ntest@{2}\n//}\n")
expected = <<-EOS
this is test<&>_
test1
test2
EOS
assert_equal expected, actual
end
def test_blankline
actual = compile_block("//blankline\nfoo\n")
expected = <<-EOS
foo
EOS
assert_equal expected, actual
end
def test_noindent
actual = compile_block("//noindent\nfoo\nbar\n\nfoo2\nbar2\n")
expected = <<-EOS
foobar
foo2bar2
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block("//noindent\nfoo\nbar\n\nfoo2\nbar2\n")
expected = <<-EOS
foo bar
foo2 bar2
EOS
assert_equal expected, actual
end
def test_flushright
actual = compile_block("//flushright{\nfoo\nbar\n\nbuz\n//}\n")
expected = <<-EOS
foobar
buz
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block("//flushright{\nfoo\nbar\n\nbuz\n//}\n")
expected = <<-EOS
foo bar
buz
EOS
assert_equal expected, actual
end
def test_centering
actual = compile_block("//centering{\nfoo\nbar\n\nbuz\n//}\n")
expected = <<-EOS
foobar
buz
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block("//centering{\nfoo\nbar\n\nbuz\n//}\n")
expected = <<-EOS
foo bar
buz
EOS
assert_equal expected, actual
end
def test_image
def @chapter.image(_id)
item = Book::Index::Item.new('sampleimg', 1)
item.instance_eval { @path = './images/chap1-sampleimg.png' }
item
end
actual = compile_block("//image[sampleimg][sample photo]{\n//}\n")
expected = <<-EOS
図1.1: sample photo
EOS
assert_equal expected, actual
actual = compile_block("//image[sampleimg][]{\n//}\n")
expected = <<-EOS
図1.1:
EOS
assert_equal expected, actual
actual = compile_block("//image[sampleimg][][]{\n//}\n")
assert_equal expected, actual
@config['caption_position']['image'] = 'top'
actual = compile_block("//image[sampleimg][sample photo]{\n//}\n")
expected = <<-EOS
図1.1: sample photo
EOS
assert_equal expected, actual
end
def test_image_with_metric
def @chapter.image(_id)
item = Book::Index::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")
expected = <<-EOS
図1.1: sample photo
EOS
assert_equal expected, actual
end
def test_image_with_metric2
def @chapter.image(_id)
item = Book::Index::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")
expected = <<-EOS
図1.1: sample photo
EOS
assert_equal expected, actual
end
def test_image_with_tricky_id_kana
def @chapter.image(_id)
item = Book::Index::Item.new('123あいう', 1)
item.instance_eval { @path = './images/123あいう.png' }
item
end
@chapter.instance_eval { @name = 'ch01' }
actual = compile_block("//image[123あいう][sample photo]{\n//}\nimg: @{123あいう}\n")
expected = <<-EOS
図1.1: sample photo
img: 図1.1
EOS
assert_equal expected, actual
end
def test_indepimage
def @chapter.image(_id)
item = Book::Index::Item.new('sampleimg', 1)
item.instance_eval { @path = './images/chap1-sampleimg.png' }
item
end
actual = compile_block("//indepimage[sampleimg][sample photo]\n")
expected = <<-EOS
図: sample photo
EOS
assert_equal expected, actual
@config['caption_position']['image'] = 'top'
actual = compile_block("//indepimage[sampleimg][sample photo]\n")
expected = <<-EOS
図: sample photo
EOS
assert_equal expected, actual
end
def test_indepimage_without_caption
def @chapter.image(_id)
item = Book::Index::Item.new('sampleimg', 1)
item.instance_eval { @path = './images/chap1-sampleimg.png' }
item
end
actual = compile_block("//indepimage[sampleimg]\n")
expected = <<-EOS
EOS
assert_equal expected, actual
actual = compile_block("//indepimage[sampleimg][]\n")
assert_equal expected, actual
actual = compile_block("//indepimage[sampleimg][][]\n")
assert_equal expected, actual
end
def test_indepimage_with_metric
def @chapter.image(_id)
item = Book::Index::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")
expected = <<-EOS
図: sample photo
EOS
assert_equal expected, actual
end
def test_indepimage_with_metric2
def @chapter.image(_id)
item = Book::Index::Item.new('sampleimg', 1)
item.instance_eval { @path = './images/chap1-sampleimg.png' }
item
end
actual = compile_block(%Q(//indepimage[sampleimg][sample photo][scale=1.2, html::class="sample",latex::ignore=params]\n))
expected = <<-EOS
図: sample photo
EOS
assert_equal expected, actual
end
def test_indepimage_without_caption_but_with_metric
def @chapter.image(_id)
item = Book::Index::Item.new('sampleimg', 1)
item.instance_eval { @path = './images/chap1-sampleimg.png' }
item
end
actual = compile_block("//indepimage[sampleimg][][scale=1.2]\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_dlist
actual = compile_block(" : foo\n foo.\n bar.\n")
expected = <<-EOS
- foo
- foo.bar.
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block(" : foo\n foo.\n bar.\n")
expected = <<-EOS
- foo
- foo. bar.
EOS
assert_equal expected, actual
end
def test_dlist_with_bracket
actual = compile_block(" : foo[bar]\n foo.\n bar.\n")
expected = <<-EOS
- foo[bar]
- foo.bar.
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block(" : foo[bar]\n foo.\n bar.\n")
expected = <<-EOS
- foo[bar]
- foo. bar.
EOS
assert_equal expected, actual
end
def test_dlist_with_comment
source = " : title\n body\n\#@ comment\n\#@ comment\n : title2\n body2\n"
actual = compile_block(source)
expected = <<-EOS
- title
- body
- title2
- body2
EOS
assert_equal expected, 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")
expected = <<-EOS
- foo
- foo.
para
- foo
- foo.
- bar
- foo
- foo.
EOS
assert_equal expected, actual
end
def test_dt_inline
actual = compile_block("//footnote[bar][bar]\n\n : foo@{bar}[]<>&@$\\alpha[]$\n")
expected = <<-EOS
- foo*1[]<>&\\alpha[]
EOS
assert_equal expected, actual
end
def test_list
def @chapter.list(_id)
Book::Index::Item.new('samplelist', 1)
end
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
test2
EOS
assert_equal expected, actual
@config['caption_position']['list'] = 'bottom'
actual = compile_block("//list[samplelist][this is @{test}<&>_]{\ntest1\ntest1.5\n\ntest@{2}\n//}\n")
expected = <<-EOS
test1
test1.5
test2
リスト1.1: this is test<&>_
EOS
assert_equal expected, actual
end
def test_inline_list
def @chapter.list(_id)
Book::Index::Item.new('samplelist', 1)
end
actual = compile_block("@{samplelist}\n")
assert_equal %Q(リスト1.1
\n), actual
@config['chapterlink'] = nil
actual = compile_block("@{samplelist}\n")
assert_equal %Q(リスト1.1
\n), actual
end
def test_inline_list_href
book = ReVIEW::Book::Base.new
book.config['chapterlink'] = true
book.catalog = ReVIEW::Catalog.new('CHAPS' => %w[ch1.re ch2.re])
io1 = StringIO.new("//list[sampletest][a]{\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(book, nil, [chap1, chap2])]
builder = ReVIEW::HTMLBuilder.new
comp = ReVIEW::Compiler.new(builder)
builder.bind(comp, chap2, nil)
chap1.generate_indexes
actual = builder.inline_list('ch1|sampletest')
assert_equal %Q(リスト1.1), actual
end
def test_list_pygments
return true if @skip_pygments
def @chapter.list(_id)
Book::Index::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
return true if @skip_pygments
def @chapter.list(_id)
Book::Index::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")
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_pygments_nulllang
return true if @skip_pygments
def @chapter.list(_id)
Book::Index::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::Index::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")
expected = <<-EOS
リスト1.1: this is test<&>_
test1
test1.5
test<i>2</i>
EOS
assert_equal expected, 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::Index::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 = <<-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_nulllang
begin
require 'rouge'
rescue LoadError
$stderr.puts 'skip test_list_rouge_nulllang (cannot find Rouge)'
return true
end
def @chapter.list(_id)
Book::Index::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")
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_listnum
def @chapter.list(_id)
Book::Index::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
@config['caption_position']['list'] = 'bottom'
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: def foo(a1, a2=:test)
2: (1..3).times{|i| a.include?(:foo)}
3: return true
4: end
リスト1.1: this is test<&>_
EOS
assert_equal expected, actual
end
def test_listnum_linenum
def @chapter.list(_id)
Book::Index::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
return true if @skip_pygments
def @chapter.list(_id)
Book::Index::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
return true if @skip_pygments
def @chapter.list(_id)
Book::Index::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 = <<-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_without_lang
return true if @skip_pygments
def @chapter.list(_id)
Book::Index::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::Index::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")
expected = <<-EOS
リスト1.1: this is test<&>_
1
2
3
4
5
| def foo(a1, a2=:test)
(1..3).times{|i| a.include?(:foo)}
return true
end
|
EOS
assert_equal expected, 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::Index::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_source
actual = compile_block("//source[foo/bar/test.rb]{\nfoo\nbar\n\nbuz\n//}\n")
expected = <<-EOS
foo/bar/test.rb
foo
bar
buz
EOS
assert_equal expected, actual
@config['caption_position']['list'] = 'bottom'
actual = compile_block("//source[foo/bar/test.rb]{\nfoo\nbar\n\nbuz\n//}\n")
expected = <<-EOS
foo
bar
buz
foo/bar/test.rb
EOS
assert_equal expected, actual
end
def test_source_empty_caption
actual = compile_block("//source[]{\nfoo\nbar\n\nbuz\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_box
actual = compile_block("//box{\nfoo\nbar\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
actual = compile_block("//box[FOO]{\nfoo\nbar\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
@config['caption_position']['list'] = 'bottom'
actual = compile_block("//box[FOO]{\nfoo\nbar\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_emlist
actual = compile_block("//emlist{\nlineA\nlineB\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_emlist_pygments_lang
return true if @skip_pygments
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")
expected = <<-EOS
EOS
assert_equal expected, actual
@config['caption_position']['list'] = 'bottom'
actual = compile_block("//emlist[cap1]{\nlineA\nlineB\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_emlist_with_tab
actual = compile_block("//emlist{\n\tlineA\n\t\tlineB\n\tlineC\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_emlistnum
@book.config['highlight'] = false
actual = compile_block("//emlistnum{\nlineA\nlineB\n//}\n")
expected = <<-EOS
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
EOS
assert_equal expected, actual
@config['caption_position']['list'] = 'bottom'
actual = compile_block("//emlistnum[cap][text]{\nlineA\nlineB\n//}\n")
expected = <<-EOS
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")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_cmd
actual = compile_block("//cmd{\nlineA\nlineB\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_cmd_pygments
return true if @skip_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")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_cmd_caption
actual = compile_block("//cmd[cap1]{\nlineA\nlineB\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
@config['caption_position']['list'] = 'bottom'
actual = compile_block("//cmd[cap1]{\nlineA\nlineB\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_texequation
return true if /mswin|mingw|cygwin/ =~ RUBY_PLATFORM
return true unless system('latex -version 1>/dev/null 2>/dev/null')
mktmpbookdir('catalog.yml' => "CHAPS:\n - ch01.re\n",
'ch01.re' => "= test\n\n//texequation{\np \\land \\bm{P} q\n//}\n") do |dir, book, _files|
@book = book
@book.config = @config
@config['math_format'] = 'imgmath'
@chapter = Book::Chapter.new(@book, 1, '-', nil, StringIO.new)
location = Location.new(nil, nil)
img_math = ReVIEW::ImgMath.new(@config)
@builder = HTMLBuilder.new(img_math: img_math)
@builder.bind(@compiler, @chapter, location)
FileUtils.mkdir_p(File.join(dir, 'images'))
expected = <<-EOB
EOB
tmpio = $stderr
$stderr = StringIO.new
begin
result = compile_block("//texequation{\np \\land \\bm{P} q\n//}\n")
ensure
$stderr = tmpio
end
actual = result.gsub(/_gen_[0-9a-f]+\.png/, '_gen_XXX.png')
assert_equal expected, actual
end
end
def test_texequation_fail
# Re:VIEW 3 never fail on defer mode. This test is only for Re:VIEW 2.
return true if /mswin|mingw|cygwin/ =~ RUBY_PLATFORM
return true unless system('latex -version 1>/dev/null 2>/dev/null')
mktmpbookdir('catalog.yml' => "CHAPS:\n - ch01.re\n",
'ch01.re' => "= test\n\n//texequation{\np \\land \\bm{P}} q\n//}\n") do |dir, book, _files|
@book = book
@book.config = @config
@config['review_version'] = 2
@config['math_format'] = 'imgmath'
@chapter = Book::Chapter.new(@book, 1, '-', nil, StringIO.new)
location = Location.new(nil, nil)
img_math = ReVIEW::ImgMath.new(@config)
@builder = HTMLBuilder.new(img_math: img_math)
@builder.bind(@compiler, @chapter, location)
FileUtils.mkdir_p(File.join(dir, 'images'))
tmpio = $stderr
$stderr = StringIO.new
begin
assert_raise(ReVIEW::ApplicationError) do
_result = compile_block("//texequation{\np \\land \\bm{P}} q\n//}\n")
end
ensure
$stderr = tmpio
end
end
end
def test_bib
def @chapter.bibpaper(_id)
Book::Index::Item.new('samplebib', 1, 'sample bib')
end
assert_equal %Q([1]), compile_inline('@{samplebib}')
end
def test_bib_noramlized
def @chapter.bibpaper(_id)
Book::Index::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::Index::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::Index::Item.new('samplebib', 1, 'sample bib')
end
actual = compile_block("//bibpaper[samplebib][sample bib @{bold}]{\na\nb\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block("//bibpaper[samplebib][sample bib @{bold}]{\na\nb\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_bibpaper_normalized
def @chapter.bibpaper(_id)
Book::Index::Item.new('sample=bib', 1, 'sample bib')
end
actual = compile_block("//bibpaper[sample=bib][sample bib @{bold}]{\na\nb\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block("//bibpaper[sample=bib][sample bib @{bold}]{\na\nb\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
end
def test_bibpaper_with_anchor
def @chapter.bibpaper(_id)
Book::Index::Item.new('samplebib', 1, 'sample bib')
end
actual = compile_block("//bibpaper[samplebib][sample bib @{http://example.jp}]{\na\nb\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
actual = compile_block("//bibpaper[samplebib][sample bib @{http://example.jp}]{\na\nb\n//}\n")
expected = <<-EOS
EOS
assert_equal expected, 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
EOS
assert_equal expected, column_helper(review)
end
def test_column_2
review = <<-EOS
===[column] test
inside column
=== next level
EOS
expected = <<-EOS
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::ApplicationError) 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
next level
this is コラム「test」.
EOS
assert_equal expected, column_helper(review)
@config['chapterlink'] = nil
expected = <<-EOS
next level
this is コラム「test」.
EOS
assert_equal expected, column_helper(review)
end
def test_column_in_aother_chapter_ref
def @chapter.column_index
item = Book::Index::Item.new('chap1|column', 1, 'column_cap')
idx = Book::ColumnIndex.new
idx.add_item(item)
idx
end
actual = compile_inline('test @{chap1|column} test2')
expected = 'test コラム「column_cap」 test2'
assert_equal expected, actual
@config['chapterlink'] = nil
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 = <<-EOS
EOS
actual = compile_block(src)
assert_equal expected, actual
end
def test_ul_cont
src = <<-EOS
* AAA
-AA
* BBB
-BB
EOS
expected = <<-EOS
EOS
actual = compile_block(src)
assert_equal expected, actual
@book.config['join_lines_by_lang'] = true
expected = <<-EOS
EOS
actual = compile_block(src)
assert_equal expected, actual
end
def test_ul_nest1
src = <<-EOS
* AAA
** AA
EOS
expected = <<-EOS
EOS
actual = compile_block(src)
assert_equal expected, actual
end
def test_ul_nest2
src = <<-EOS
* AAA
** AA
* BBB
** BB
EOS
expected = <<-EOS
EOS
actual = compile_block(src)
assert_equal expected, actual
end
def test_ul_nest3
src = <<-EOS
** AAA
* AA
EOS
e = assert_raises(ReVIEW::ApplicationError) { compile_block(src) }
assert_equal ':1: error: too many *.', e.message
end
def test_ul_nest4
src = <<-EOS
* A
** AA
*** AAA
* B
** BB
EOS
expected = <<-EOS
EOS
actual = compile_block(src)
assert_equal expected, actual
end
def test_ol
src = <<-EOS
3. AAA
3. BBB
EOS
expected = <<-EOS
- AAA
- 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('@