1 #--- insert.t ----------------------------------------------------------------- 2 # function: Test ToC insertion. 3 4 use strict; 5 use Test; 6 7 BEGIN { plan tests => 10; } 8 9 use HTML::Toc; 10 use HTML::TocGenerator; 11 use HTML::TocInsertor; 12 13 my ($output, $content, $filename); 14 my $toc = HTML::Toc->new; 15 my $tocGenerator = HTML::TocGenerator->new; 16 my $tocInsertor = HTML::TocInsertor->new; 17 18 $toc->setOptions({ 19 'doLinkToToken' => 0, 20 'levelIndent' => 0, 21 'header' => "", 22 'footer' => "", 23 }); 24 25 26 BEGIN { 27 # Create test file 28 $filename = "file$$.htm"; 29 die "$filename is already there" if -e $filename; 30 open(FILE, ">$filename") || die "Can't create $filename: $!"; 31 print FILE <<'EOT'; close(FILE); 32 <h1>Header</h1> 33 EOT 34 } 35 36 37 END { 38 # Remove test file 39 unlink($filename) or warn "Can't unlink $filename: $!"; 40 } 41 42 43 #--- 1. insert before start --------------------------------------------------- 44 45 $toc->setOptions({'insertionPoint' => 'before <h1>'}); 46 # Generate ToC 47 $tocGenerator->generate($toc, "<h1>Header</h1>"); 48 $tocInsertor->insert($toc, "<h1>Header</h1>", { 49 'output' => \$output, 50 'doGenerateToc' => 0 51 }); 52 # Test ToC 53 ok($output, "<ul>\n<li>Header\n</ul><h1>Header</h1>"); 54 55 56 #--- 2. insert after start ---------------------------------------------------- 57 58 $toc->setOptions({'insertionPoint' => 'after <h1>'}); 59 # Generate ToC 60 $tocGenerator->generate($toc, "<h1>Header</h1>"); 61 $tocInsertor->insert($toc, "<h1>Header</h1>", { 62 'output' => \$output, 63 'doGenerateToc' => 0 64 }); 65 # Test ToC 66 ok($output, "<h1><ul>\n<li>Header\n</ul>Header</h1>"); 67 68 69 #--- 3. insert before end ----------------------------------------------------- 70 71 $toc->setOptions({'insertionPoint' => 'before </h1>'}); 72 # Generate ToC 73 $tocGenerator->generate($toc, "<h1>Header</h1>"); 74 $tocInsertor->insert($toc, "<h1>Header</h1>", { 75 'output' => \$output, 76 'doGenerateToc' => 0 77 }); 78 # Test ToC 79 ok($output, "<h1>Header<ul>\n<li>Header\n</ul></h1>"); 80 81 82 #--- 4. insert after end ------------------------------------------------------ 83 84 $toc->setOptions({'insertionPoint' => 'after </h1>'}); 85 # Generate ToC 86 $tocGenerator->generate($toc, "<h1>Header</h1>"); 87 $tocInsertor->insert($toc, "<h1>Header</h1>", { 88 'output' => \$output, 89 'doGenerateToc' => 0 90 }); 91 # Test ToC 92 ok($output, "<h1>Header</h1><ul>\n<li>Header\n</ul>"); 93 94 95 #--- 5. outputFile ------------------------------------------------------------ 96 97 $toc->setOptions({'insertionPoint' => 'before <h1>'}); 98 # Generate ToC 99 $tocGenerator->generate($toc, "<h1>Header</h1>"); 100 # Insert ToC, output to file 101 $tocInsertor->insert($toc, "<h1>Header</h1>", { 102 'outputFile' => $filename, 103 'doGenerateToc' => 0 104 }); 105 # Read outputfile 106 open(FILE, "<$filename") || die "Can't open $filename: $!"; 107 $content = join('', <FILE>); 108 close(FILE); 109 # Test ToC 110 ok($output, "<ul>\n<li>Header\n</ul><h1>Header</h1>"); 111 112 113 #--- 6. empty toc ------------------------------------------------------------- 114 115 $tocGenerator->generate($toc, ""); 116 $tocInsertor->insert($toc, "", { 117 'output' => \$output, 118 'doGenerateToc' => 0 119 }); 120 ok($output, ""); 121 122 123 #--- TestAfterDeclaration() --------------------------------------------------- 124 # function: Test putting HTML comment after declaration. 125 126 sub TestAfterDeclaration { 127 # Create objects 128 my $toc = HTML::Toc->new(); 129 my $tocInsertor = HTML::TocInsertor->new(); 130 my $output; 131 132 # Set ToC options 133 $toc->setOptions({ 134 'insertionPoint' => "after <!ToC>", 135 }); 136 # Generate ToC 137 $tocInsertor->insert($toc, <<EOT, {'output' => \$output}); 138 <!ToC><body> 139 <h1>Appendix</h1> 140 <h2>Appendix Paragraph</h2> 141 <h1>Appendix</h1> 142 <h2>Appendix Paragraph</h2> 143 </body> 144 EOT 145 # Test ToC 146 ok($output, <<EOT); 147 <!ToC> 148 <!-- Table of Contents generated by Perl - HTML::Toc --> 149 <ul> 150 <li><a href=#h-1>Appendix</a> 151 <ul> 152 <li><a href=#h-1.1>Appendix Paragraph</a> 153 </ul> 154 <li><a href=#h-2>Appendix</a> 155 <ul> 156 <li><a href=#h-2.1>Appendix Paragraph</a> 157 </ul> 158 </ul> 159 <!-- End of generated Table of Contents --> 160 <body> 161 <a name=h-1><h1>Appendix</h1></a> 162 <a name=h-1.1><h2>Appendix Paragraph</h2></a> 163 <a name=h-2><h1>Appendix</h1></a> 164 <a name=h-2.1><h2>Appendix Paragraph</h2></a> 165 </body> 166 EOT 167 } # TestAfterDeclaration() 168 169 170 #--- TestNumberingStyle() ----------------------------------------------------- 171 # function: Test numberingstyle. 172 173 sub TestNumberingStyle { 174 # Create objects 175 my $toc = HTML::Toc->new(); 176 my $tocInsertor = HTML::TocInsertor->new(); 177 my $output; 178 179 # Set ToC options 180 $toc->setOptions({ 181 'numberingStyle' => 'lower-alpha', 182 'doNumberToken' => 1, 183 'tokenToToc' => [{ 184 'tokenBegin' => '<h1>', 185 }, { 186 'tokenBegin' => '<h2>', 187 'level' => 2, 188 'numberingStyle' => 'upper-alpha' 189 }, { 190 'tokenBegin' => '<h3>', 191 'level' => 3, 192 'numberingStyle' => 'decimal' 193 }] 194 }); 195 # Generate ToC 196 $tocInsertor->insert($toc, <<EOT, {'output' => \$output}); 197 <body> 198 <h1>Chapter</h1> 199 <h2>Paragraph</h2> 200 <h3>Paragraph</h3> 201 <h3>Paragraph</h3> 202 <h3>Paragraph</h3> 203 </body> 204 EOT 205 # Test ToC 206 ok($output, <<EOT); 207 <body> 208 <!-- Table of Contents generated by Perl - HTML::Toc --> 209 <ul> 210 <li><a href=#h-a>Chapter</a> 211 <ul> 212 <li><a href=#h-a.A>Paragraph</a> 213 <ul> 214 <li><a href=#h-a.A.1>Paragraph</a> 215 <li><a href=#h-a.A.2>Paragraph</a> 216 <li><a href=#h-a.A.3>Paragraph</a> 217 </ul> 218 </ul> 219 </ul> 220 <!-- End of generated Table of Contents --> 221 222 <a name=h-a><h1>a Chapter</h1></a> 223 <a name=h-a.A><h2>a.A Paragraph</h2></a> 224 <a name=h-a.A.1><h3>a.A.1 Paragraph</h3></a> 225 <a name=h-a.A.2><h3>a.A.2 Paragraph</h3></a> 226 <a name=h-a.A.3><h3>a.A.3 Paragraph</h3></a> 227 </body> 228 EOT 229 } # TestNumberingStyle() 230 231 232 #--- TestReplaceComment() ----------------------------------------------------- 233 # function: Test replacing HTML comment with ToC. 234 235 sub TestReplaceComment { 236 # Create objects 237 my $toc = HTML::Toc->new(); 238 my $tocInsertor = HTML::TocInsertor->new(); 239 my $output; 240 241 # Set ToC options 242 $toc->setOptions({ 243 'insertionPoint' => "replace <!-- ToC -->" 244 }); 245 # Generate ToC 246 $tocInsertor->insert($toc, <<EOT, {'output' => \$output}); 247 <!-- ToC --> 248 <body> 249 <h1>Appendix</h1> 250 <h2>Appendix Paragraph</h2> 251 <h1>Appendix</h1> 252 <h2>Appendix Paragraph</h2> 253 </body> 254 EOT 255 # Test ToC 256 ok($output, <<EOT); 257 258 <!-- Table of Contents generated by Perl - HTML::Toc --> 259 <ul> 260 <li><a href=#h-1>Appendix</a> 261 <ul> 262 <li><a href=#h-1.1>Appendix Paragraph</a> 263 </ul> 264 <li><a href=#h-2>Appendix</a> 265 <ul> 266 <li><a href=#h-2.1>Appendix Paragraph</a> 267 </ul> 268 </ul> 269 <!-- End of generated Table of Contents --> 270 271 <body> 272 <a name=h-1><h1>Appendix</h1></a> 273 <a name=h-1.1><h2>Appendix Paragraph</h2></a> 274 <a name=h-2><h1>Appendix</h1></a> 275 <a name=h-2.1><h2>Appendix Paragraph</h2></a> 276 </body> 277 EOT 278 } # TestReplaceComment() 279 280 281 #--- TestReplaceText() ----------------------------------------------------- 282 # function: Test replacing HTML comment with ToC. 283 284 sub TestReplaceText { 285 # Create objects 286 my $toc = HTML::Toc->new(); 287 my $tocInsertor = HTML::TocInsertor->new(); 288 my $output; 289 290 # Set ToC options 291 $toc->setOptions({ 292 'insertionPoint' => "replace ToC will be placed here[,]" 293 }); 294 # Generate ToC 295 $tocInsertor->insert($toc, <<EOT, {'output' => \$output}); 296 The ToC will be placed here, overnight. 297 <body> 298 <h1>Appendix</h1> 299 <h2>Appendix Paragraph</h2> 300 <h1>Appendix</h1> 301 <h2>Appendix Paragraph</h2> 302 </body> 303 EOT 304 # Test ToC 305 ok($output, <<EOT); 306 307 <!-- Table of Contents generated by Perl - HTML::Toc --> 308 <ul> 309 <li><a href=#h-1>Appendix</a> 310 <ul> 311 <li><a href=#h-1.1>Appendix Paragraph</a> 312 </ul> 313 <li><a href=#h-2>Appendix</a> 314 <ul> 315 <li><a href=#h-2.1>Appendix Paragraph</a> 316 </ul> 317 </ul> 318 <!-- End of generated Table of Contents --> 319 <body> 320 <a name=h-1><h1>Appendix</h1></a> 321 <a name=h-1.1><h2>Appendix Paragraph</h2></a> 322 <a name=h-2><h1>Appendix</h1></a> 323 <a name=h-2.1><h2>Appendix Paragraph</h2></a> 324 </body> 325 EOT 326 } # TestReplaceText() 327 328 329 # 7. Test 'numberingStyle' 330 TestNumberingStyle(); 331 # 8. Test replace comment 332 TestReplaceComment(); 333 # 9. Test after declaration 334 TestAfterDeclaration(); 335 # 10. Test replace text 336 TestReplaceText(); 337