#--- 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);
Header
EOT
}
END {
# Remove test file
unlink($filename) or warn "Can't unlink $filename: $!";
}
#--- 1. insert before start ---------------------------------------------------
$toc->setOptions({'insertionPoint' => 'before '});
# Generate ToC
$tocGenerator->generate($toc, "Header
");
$tocInsertor->insert($toc, "Header
", {
'output' => \$output,
'doGenerateToc' => 0
});
# Test ToC
ok($output, "Header
");
#--- 2. insert after start ----------------------------------------------------
$toc->setOptions({'insertionPoint' => 'after '});
# Generate ToC
$tocGenerator->generate($toc, "Header
");
$tocInsertor->insert($toc, "Header
", {
'output' => \$output,
'doGenerateToc' => 0
});
# Test ToC
ok($output, "Header
");
#--- 3. insert before end -----------------------------------------------------
$toc->setOptions({'insertionPoint' => 'before
'});
# Generate ToC
$tocGenerator->generate($toc, "Header
");
$tocInsertor->insert($toc, "Header
", {
'output' => \$output,
'doGenerateToc' => 0
});
# Test ToC
ok($output, "Header
");
#--- 4. insert after end ------------------------------------------------------
$toc->setOptions({'insertionPoint' => 'after
'});
# Generate ToC
$tocGenerator->generate($toc, "Header
");
$tocInsertor->insert($toc, "Header
", {
'output' => \$output,
'doGenerateToc' => 0
});
# Test ToC
ok($output, "Header
");
#--- 5. outputFile ------------------------------------------------------------
$toc->setOptions({'insertionPoint' => 'before '});
# Generate ToC
$tocGenerator->generate($toc, "Header
");
# Insert ToC, output to file
$tocInsertor->insert($toc, "Header
", {
'outputFile' => $filename,
'doGenerateToc' => 0
});
# Read outputfile
open(FILE, "<$filename") || die "Can't open $filename: $!";
$content = join('', );
close(FILE);
# Test ToC
ok($output, "Header
");
#--- 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 ",
});
# Generate ToC
$tocInsertor->insert($toc, < \$output});
Appendix
Appendix Paragraph
Appendix
Appendix Paragraph
EOT
# Test ToC
ok($output, <
Appendix
Appendix Paragraph
Appendix
Appendix Paragraph
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' => '',
}, {
'tokenBegin' => '',
'level' => 2,
'numberingStyle' => 'upper-alpha'
}, {
'tokenBegin' => '