blob: 3f1adba811ff11a9addf4125f2699370ee853989 [file] [log] [blame]
#--- insert.t -----------------------------------------------------------------
# function: Test ToC insertion.
use strict;
use Test;
BEGIN { plan tests => 10; }
use HTML::Toc;
use HTML::TocGenerator;
use HTML::TocInsertor;
my ($output, $content, $filename);
my $toc = HTML::Toc->new;
my $tocGenerator = HTML::TocGenerator->new;
my $tocInsertor = HTML::TocInsertor->new;
$toc->setOptions({
'doLinkToToken' => 0,
'levelIndent' => 0,
'header' => "",
'footer' => "",
});
BEGIN {
# Create test file
$filename = "file$$.htm";
die "$filename is already there" if -e $filename;
open(FILE, ">$filename") || die "Can't create $filename: $!";
print FILE <<'EOT'; close(FILE);
<h1>Header</h1>
EOT
}
END {
# Remove test file
unlink($filename) or warn "Can't unlink $filename: $!";
}
#--- 1. insert before start ---------------------------------------------------
$toc->setOptions({'insertionPoint' => 'before <h1>'});
# Generate ToC
$tocGenerator->generate($toc, "<h1>Header</h1>");
$tocInsertor->insert($toc, "<h1>Header</h1>", {
'output' => \$output,
'doGenerateToc' => 0
});
# Test ToC
ok($output, "<ul>\n<li>Header\n</ul><h1>Header</h1>");
#--- 2. insert after start ----------------------------------------------------
$toc->setOptions({'insertionPoint' => 'after <h1>'});
# Generate ToC
$tocGenerator->generate($toc, "<h1>Header</h1>");
$tocInsertor->insert($toc, "<h1>Header</h1>", {
'output' => \$output,
'doGenerateToc' => 0
});
# Test ToC
ok($output, "<h1><ul>\n<li>Header\n</ul>Header</h1>");
#--- 3. insert before end -----------------------------------------------------
$toc->setOptions({'insertionPoint' => 'before </h1>'});
# Generate ToC
$tocGenerator->generate($toc, "<h1>Header</h1>");
$tocInsertor->insert($toc, "<h1>Header</h1>", {
'output' => \$output,
'doGenerateToc' => 0
});
# Test ToC
ok($output, "<h1>Header<ul>\n<li>Header\n</ul></h1>");
#--- 4. insert after end ------------------------------------------------------
$toc->setOptions({'insertionPoint' => 'after </h1>'});
# Generate ToC
$tocGenerator->generate($toc, "<h1>Header</h1>");
$tocInsertor->insert($toc, "<h1>Header</h1>", {
'output' => \$output,
'doGenerateToc' => 0
});
# Test ToC
ok($output, "<h1>Header</h1><ul>\n<li>Header\n</ul>");
#--- 5. outputFile ------------------------------------------------------------
$toc->setOptions({'insertionPoint' => 'before <h1>'});
# Generate ToC
$tocGenerator->generate($toc, "<h1>Header</h1>");
# Insert ToC, output to file
$tocInsertor->insert($toc, "<h1>Header</h1>", {
'outputFile' => $filename,
'doGenerateToc' => 0
});
# Read outputfile
open(FILE, "<$filename") || die "Can't open $filename: $!";
$content = join('', <FILE>);
close(FILE);
# Test ToC
ok($output, "<ul>\n<li>Header\n</ul><h1>Header</h1>");
#--- 6. empty toc -------------------------------------------------------------
$tocGenerator->generate($toc, "");
$tocInsertor->insert($toc, "", {
'output' => \$output,
'doGenerateToc' => 0
});
ok($output, "");
#--- TestAfterDeclaration() ---------------------------------------------------
# function: Test putting HTML comment after declaration.
sub TestAfterDeclaration {
# Create objects
my $toc = HTML::Toc->new();
my $tocInsertor = HTML::TocInsertor->new();
my $output;
# Set ToC options
$toc->setOptions({
'insertionPoint' => "after <!ToC>",
});
# Generate ToC
$tocInsertor->insert($toc, <<EOT, {'output' => \$output});
<!ToC><body>
<h1>Appendix</h1>
<h2>Appendix Paragraph</h2>
<h1>Appendix</h1>
<h2>Appendix Paragraph</h2>
</body>
EOT
# Test ToC
ok($output, <<EOT);
<!ToC>
<!-- Table of Contents generated by Perl - HTML::Toc -->
<ul>
<li><a href=#h-1>Appendix</a>
<ul>
<li><a href=#h-1.1>Appendix Paragraph</a>
</ul>
<li><a href=#h-2>Appendix</a>
<ul>
<li><a href=#h-2.1>Appendix Paragraph</a>
</ul>
</ul>
<!-- End of generated Table of Contents -->
<body>
<a name=h-1><h1>Appendix</h1></a>
<a name=h-1.1><h2>Appendix Paragraph</h2></a>
<a name=h-2><h1>Appendix</h1></a>
<a name=h-2.1><h2>Appendix Paragraph</h2></a>
</body>
EOT
} # TestAfterDeclaration()
#--- TestNumberingStyle() -----------------------------------------------------
# function: Test numberingstyle.
sub TestNumberingStyle {
# Create objects
my $toc = HTML::Toc->new();
my $tocInsertor = HTML::TocInsertor->new();
my $output;
# Set ToC options
$toc->setOptions({
'numberingStyle' => 'lower-alpha',
'doNumberToken' => 1,
'tokenToToc' => [{
'tokenBegin' => '<h1>',
}, {
'tokenBegin' => '<h2>',
'level' => 2,
'numberingStyle' => 'upper-alpha'
}, {
'tokenBegin' => '<h3>',
'level' => 3,
'numberingStyle' => 'decimal'
}]
});
# Generate ToC
$tocInsertor->insert($toc, <<EOT, {'output' => \$output});
<body>
<h1>Chapter</h1>
<h2>Paragraph</h2>
<h3>Paragraph</h3>
<h3>Paragraph</h3>
<h3>Paragraph</h3>
</body>
EOT
# Test ToC
ok($output, <<EOT);
<body>
<!-- Table of Contents generated by Perl - HTML::Toc -->
<ul>
<li><a href=#h-a>Chapter</a>
<ul>
<li><a href=#h-a.A>Paragraph</a>
<ul>
<li><a href=#h-a.A.1>Paragraph</a>
<li><a href=#h-a.A.2>Paragraph</a>
<li><a href=#h-a.A.3>Paragraph</a>
</ul>
</ul>
</ul>
<!-- End of generated Table of Contents -->
<a name=h-a><h1>a &nbsp;Chapter</h1></a>
<a name=h-a.A><h2>a.A &nbsp;Paragraph</h2></a>
<a name=h-a.A.1><h3>a.A.1 &nbsp;Paragraph</h3></a>
<a name=h-a.A.2><h3>a.A.2 &nbsp;Paragraph</h3></a>
<a name=h-a.A.3><h3>a.A.3 &nbsp;Paragraph</h3></a>
</body>
EOT
} # TestNumberingStyle()
#--- TestReplaceComment() -----------------------------------------------------
# function: Test replacing HTML comment with ToC.
sub TestReplaceComment {
# Create objects
my $toc = HTML::Toc->new();
my $tocInsertor = HTML::TocInsertor->new();
my $output;
# Set ToC options
$toc->setOptions({
'insertionPoint' => "replace <!-- ToC -->"
});
# Generate ToC
$tocInsertor->insert($toc, <<EOT, {'output' => \$output});
<!-- ToC -->
<body>
<h1>Appendix</h1>
<h2>Appendix Paragraph</h2>
<h1>Appendix</h1>
<h2>Appendix Paragraph</h2>
</body>
EOT
# Test ToC
ok($output, <<EOT);
<!-- Table of Contents generated by Perl - HTML::Toc -->
<ul>
<li><a href=#h-1>Appendix</a>
<ul>
<li><a href=#h-1.1>Appendix Paragraph</a>
</ul>
<li><a href=#h-2>Appendix</a>
<ul>
<li><a href=#h-2.1>Appendix Paragraph</a>
</ul>
</ul>
<!-- End of generated Table of Contents -->
<body>
<a name=h-1><h1>Appendix</h1></a>
<a name=h-1.1><h2>Appendix Paragraph</h2></a>
<a name=h-2><h1>Appendix</h1></a>
<a name=h-2.1><h2>Appendix Paragraph</h2></a>
</body>
EOT
} # TestReplaceComment()
#--- TestReplaceText() -----------------------------------------------------
# function: Test replacing HTML comment with ToC.
sub TestReplaceText {
# Create objects
my $toc = HTML::Toc->new();
my $tocInsertor = HTML::TocInsertor->new();
my $output;
# Set ToC options
$toc->setOptions({
'insertionPoint' => "replace ToC will be placed here[,]"
});
# Generate ToC
$tocInsertor->insert($toc, <<EOT, {'output' => \$output});
The ToC will be placed here, overnight.
<body>
<h1>Appendix</h1>
<h2>Appendix Paragraph</h2>
<h1>Appendix</h1>
<h2>Appendix Paragraph</h2>
</body>
EOT
# Test ToC
ok($output, <<EOT);
<!-- Table of Contents generated by Perl - HTML::Toc -->
<ul>
<li><a href=#h-1>Appendix</a>
<ul>
<li><a href=#h-1.1>Appendix Paragraph</a>
</ul>
<li><a href=#h-2>Appendix</a>
<ul>
<li><a href=#h-2.1>Appendix Paragraph</a>
</ul>
</ul>
<!-- End of generated Table of Contents -->
<body>
<a name=h-1><h1>Appendix</h1></a>
<a name=h-1.1><h2>Appendix Paragraph</h2></a>
<a name=h-2><h1>Appendix</h1></a>
<a name=h-2.1><h2>Appendix Paragraph</h2></a>
</body>
EOT
} # TestReplaceText()
# 7. Test 'numberingStyle'
TestNumberingStyle();
# 8. Test replace comment
TestReplaceComment();
# 9. Test after declaration
TestAfterDeclaration();
# 10. Test replace text
TestReplaceText();