Subject: | insertionPoint replaces too much text; documentation is a tad misleading |
Two issues:
1. An insertionPoint of "replace just_the_toc_placeholder" will replace
all text around just_the_toc_placeholder, up to surrounding HTML
elements. While normally there is no bare text around the ToC
placeholder, this is somewhat of a problem because it the replacing
includes whitespace surrounding just_the_toc_placeholder.
2. The documentation reads "syntax: [<before|after|replace>] <token>".
The square brackets may imply that "before", "after" and "replace" are
optional. In fact, specifying one of them is mandatory. A better
phrasing may be:
syntax: "before" | "after" | "replace" <token>
Test case attached.
Subject: | insertionPoint.t |
#!/usr/bin/perl -w
use strict;
use HTML::Toc;
use HTML::TocInsertor;
use Test::More tests => 3;
use Test::Differences;
my $toc = HTML::Toc->new();
my $tocInsertor = HTML::TocInsertor->new();
my $output;
# ------------------------------------------------------------------------
# --- insertionPoint should preserve text around the Toc placeholder
# ------------------------------------------------------------------------
my $content = <<HTML;
<p>this text is in a p tag and is preserved</p>
but this text should be preserved as well
{{toc}}
so should this text
<h1>Chapter 1</h1>
Some text here
<h1>Chapter 2</h1>
Second chapter
HTML
# http://search.cpan.org/dist/HTML-Toc/Toc.pod#HTML::TocInsertor::insert()
$toc->setOptions({
insertionPoint => 'replace {{toc}}',
doLinkToId => 0,
});
$tocInsertor->insert($toc, $content, {output => \$output});
eq_or_diff($output, <<'EOT', 'insertionPoint should preserve text around ToC placeholder');
<p>this text is in a p tag and is preserved</p>
but this text should be preserved as well
<!-- Table of Contents generated by Perl - HTML::Toc -->
<ul>
<li><a href="#h-1">Chapter 1</a></li>
<li><a href="#h-2">Chapter 2</a></li>
</ul>
<!-- End of generated Table of Contents -->
so should this text
<h1><a name="h-1"></a>Chapter 1</h1>
Some text here
<h1><a name="h-2"></a>Chapter 2</h1>
Second chapter
EOT
# ------------------------------------------------------------------------
# --- insertionPoint should preserve text around the Toc placeholder
# ------------------------------------------------------------------------
$content = <<HTML;
<p>this text is in a p tag and is preserved</p>
but this text should be preserved as well
ToC will be placed here
so should this text
<h1>Chapter 1</h1>
Some text here
<h1>Chapter 2</h1>
Second chapter
HTML
# http://search.cpan.org/dist/HTML-Toc/Toc.pod#HTML::TocInsertor::insert()
$toc->setOptions({
insertionPoint => 'replace ToC will be placed here',
doLinkToId => 0,
});
$tocInsertor->insert($toc, $content, {output => \$output});
eq_or_diff($output, <<'EOT', '"syntax: [<before|after|replace>] <token>" implies that "before", "after" and "replace" are optional. In fact, they are not.');
<p>this text is in a p tag and is preserved</p>
but this text should be preserved as well
<!-- Table of Contents generated by Perl - HTML::Toc -->
<ul>
<li><a href="#h-1">Chapter 1</a></li>
<li><a href="#h-2">Chapter 2</a></li>
</ul>
<!-- End of generated Table of Contents -->
so should this text
<h1><a name="h-1"></a>Chapter 1</h1>
Some text here
<h1><a name="h-2"></a>Chapter 2</h1>
Second chapter
EOT
# ---------------------------------------------------------------------------
# --- insertionPoint without a "before", "after" or "replace" should not work
# ---------------------------------------------------------------------------
$toc->setOptions({
insertionPoint => 'ToC will be placed here',
doLinkToId => 0,
});
$tocInsertor->insert($toc, $content, {output => \$output});
eq_or_diff($output, $content, '"syntax: [<before|after|replace>] <token>" implies that "before", "after" and "replace" are optional. In fact, one of them is mandatory.');