1#--- propagate.t -------------------------------------------------------------- 2# function: Test ToC propagation. 3 4use strict; 5use Test; 6 7BEGIN { plan tests => 10; } 8 9use HTML::Toc; 10use HTML::TocGenerator; 11use HTML::TocInsertor; 12 13my ($output, $content, $filename); 14my $toc = HTML::Toc->new; 15my $tocGenerator = HTML::TocGenerator->new; 16my $tocInsertor = HTML::TocInsertor->new; 17 18$toc->setOptions({ 19 'doLinkToToken' => 0, 20 'levelIndent' => 0, 21 'insertionPoint' => 'before <h1>', 22 'header' => '', 23 'footer' => '', 24}); 25 26 27BEGIN { 28 # Create test file 29 $filename = "file$$.htm"; 30 die "$filename is already there" if -e $filename; 31 open(FILE, ">$filename") || die "Can't create $filename: $!"; 32 print FILE <<'EOT'; close(FILE); 33<h1>Header</h1> 34EOT 35} 36 37 38END { 39 # Remove test file 40 unlink($filename) or warn "Can't unlink $filename: $!"; 41} 42 43 44#--- 1. propagate ------------------------------------------------------------- 45 46$tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 47ok($output, "<ul>\n<li>Header\n</ul><h1>Header</h1>"); 48 49 50#--- 2. propagateFile --------------------------------------------------------- 51 52$tocInsertor->insertIntoFile($toc, $filename, {'output' => \$output}); 53ok($output, "<ul>\n<li>Header\n</ul><h1>Header</h1>\n"); 54 55 56#--- 3. doLinkToToken ----------------------------------------------------- 57 58$toc->setOptions({'doLinkToToken' => 1}); 59$tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 60ok("$output\n", <<'EOT'); 61<ul> 62<li><a href=#h-1>Header</a> 63</ul><a name=h-1><h1>Header</h1></a> 64EOT 65 66 67#--- 4. templateAnchorHrefBegin ----------------------------------------------- 68 69$toc->setOptions( 70 {'templateAnchorHrefBegin' => '"<$node${file}test${groupId}>"'} 71); 72$tocInsertor->insertIntoFile($toc, $filename, {'output' => \$output}); 73ok($output, "<ul>\n<li><1${filename}testh>Header</a>\n</ul><a name=h-1><h1>Header</h1></a>\n"); 74$toc->setOptions({'templateAnchorHrefBegin' => undef}); 75 76 77#--- 5. templateAnchorNameBegin ----------------------------------------------- 78 79$toc->setOptions({ 80 'templateAnchorName' => '"$node$groupId"', 81 'templateAnchorNameBegin' => '"<$anchorName>"' 82}); 83$tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 84ok($output, "<ul>\n<li><a href=#1h>Header</a>\n</ul><1h><h1>Header</h1></a>"); 85$toc->setOptions({'templateAnchorName' => undef}); 86 87 88#--- 6. templateAnchorName function ------------------------------------------- 89 90sub AssembleAnchorName { 91 # Get arguments 92 my ($aFile, $aGroupId, $aLevel, $aNode) = @_; 93 # Return value 94 return $aFile . $aGroupId . $aLevel . $aNode; 95} # AssembleAnchorName() 96 97 # Set options 98$toc->setOptions({'templateAnchorNameBegin' => \&AssembleAnchorName}); 99 # Propagate ToC 100$tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 101 # Test ToC 102ok($output, "<ul>\n<li><a href=#h-1>Header</a>\n</ul>h11<h1>Header</h1></a>"); 103 # Restore options 104$toc->setOptions({'templateAnchorNameBegin' => undef}); 105 106 107#--- 7. doNumberToken -------------------------------------------------------- 108 109 # Set options 110$toc->setOptions({'doNumberToken' => 1}); 111$tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 112ok("$output\n", <<'EOT'); 113<ul> 114<li><a href=#h-1>Header</a> 115</ul><a name=h-1><h1>1 Header</h1></a> 116EOT 117 # Reset options 118$toc->setOptions({ 119 'templateTokenNumber' => undef, 120 'doNumberToken' => 0 121}); 122 123 124#--- 8. templateTokenNumber --------------------------------------------------- 125 126 # Set options 127$toc->setOptions({ 128 'templateTokenNumber' => '"-$node-"', 129 'doNumberToken' => 1 130}); 131 # Propagate ToC 132$tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 133 # Test ToC 134ok("$output\n", <<'EOT'); 135<ul> 136<li><a href=#h-1>Header</a> 137</ul><a name=h-1><h1>-1-Header</h1></a> 138EOT 139 # Reset options 140$toc->setOptions({ 141 'doNumberToken' => 0, 142 'templateTokenNumber' => undef 143}); 144 145 146#--- 9. numberingStyle -------------------------------------------------------- 147 148 # Set options 149$toc->setOptions({ 150 'doNumberToken' => 1, 151 'tokenToToc' => [{ 152 'level' => 1, 153 'tokenBegin' => '<h1>', 154 'numberingStyle' => 'lower-alpha' 155 }] 156}); 157 # Propagate ToC 158$tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 159 # Test ToC 160ok("$output\n", <<'EOT'); 161<ul> 162<li><a href=#h-a>Header</a> 163</ul><a name=h-a><h1>a Header</h1></a> 164EOT 165 # Reset options 166$toc->setOptions({ 167 'doNumberToken' => 0, 168 'tokenToToc' => undef, 169}); 170 171 172#--- 10. declaration pass through --------------------------------------------- 173 174$tocInsertor->insert($toc, '<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><h1>Header</h1>', {'output' => \$output}); 175 # Test ToC 176ok($output, '<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><h1>Header</h1>'); 177