# encoding: utf-8
require 'test_helper'
require 'review/compiler'
require 'review/book'
require 'review/htmlbuilder'
require 'review/i18n'
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
"inencoding" => "UTF-8",
"outencoding" => "UTF-8",
"stylesheet" => nil, # for HTMLBuilder
})
@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
@book.config["epubversion"] = 3
assert_equal "epub", @builder.xmlns_ops_prefix
end
def test_xmlns_ops_prefix_epub2
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|付録1 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|\n1.1 this is test.
\n|, actual
end
def test_headline_level1_postdef_roman
@chapter.book.config["appendix_format"] = "roman"
@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
end
def test_headline_level2_postdef_roman
@chapter.book.config["appendix_format"] = "roman"
@chapter.instance_eval do
def on_APPENDIX?
true
end
end
actual = compile_block("=={test} this is test.\n")
assert_equal %Q|\nI.1 this is test.
\n|, actual
end
def test_headline_level1_postdef_alpha
@chapter.book.config["appendix_format"] = "alpha"
@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_alpha
@chapter.book.config["appendix_format"] = "alpha"
@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_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|\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
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|, 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
@chapter.book.config["appendix_format"] = "roman"
@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
def test_inline_hd_chap_postdef_alpha
@chapter.book.config["appendix_format"] = "alpha"
@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
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 "", 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 = "]図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 = "図1.1
\n"
assert_equal expected, actual
end
def test_quote
actual = compile_block("//quote{\nfoo\nbar\n\nbuz\n//}\n")
assert_equal %Q|foobar
\nbuz
\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
\nfoo2bar2
\n|, actual
end
def test_flushright
actual = compile_block("//flushright{\nfoo\nbar\n\nbuz\n//}\n")
assert_equal %Q|foobar
\nbuz
\n|, actual
end
def test_centering
actual = compile_block("//centering{\nfoo\nbar\n\nbuz\n//}\n")
assert_equal %Q|foobar
\nbuz
\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|\n
\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|\n
\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|\n
\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|\n
\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|\n
\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|\n
\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|\n
\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_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_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["pygments"] = true
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_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["pygments"] = true
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
リスト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["pygments"] = true
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", 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["pygments"] = true
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 def foo(a1, a2=:test)\n2 (1..3).times{|i| a.include?(:foo)}\n3 return true\n4 end\n
\n
\n", actual
end
def test_emlist
actual = compile_block("//emlist{\nlineA\nlineB\n//}\n")
assert_equal %Q|\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["pygments"] = true
actual = compile_block("//emlist[][sql]{\nSELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'\n//}\n")
assert_equal "\n
SELECT COUNT(*) FROM tests WHERE tests.no > 10 AND test.name LIKE 'ABC%'\n
\n
\n", actual
end
def test_emlist_caption
actual = compile_block("//emlist[cap1]{\nlineA\nlineB\n//}\n")
assert_equal %Q|\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_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|, actual
end
def test_cmd_pygments
begin
require 'pygments'
rescue LoadError
return true
end
@book.config["pygments"] = true
actual = compile_block("//cmd{\nlineA\nlineB\n//}\n")
assert_equal "\n", actual
end
def test_cmd_caption
actual = compile_block("//cmd[cap1]{\nlineA\nlineB\n//}\n")
assert_equal %Q|\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_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|, 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::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
next level
this is コラム「test」.
EOS
assert_equal expected, column_helper(review)
end
def test_ul
src =<<-EOS
* AAA
* BBB
EOS
expected = "\n"
actual = compile_block(src)
assert_equal expected, actual
end
def test_ul_cont
src =<<-EOS
* AAA
-AA
* BBB
-BB
EOS
expected = "\n"
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
* BBB
** BB
EOS
expected =<<-EOS
EOS
actual = compile_block(src)
assert_equal expected, actual
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_ul_nest5
src =<<-EOS
* A
** AA
**** AAAA
* 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_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_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'
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'
EOS
assert_equal expected, actual
end
end