summaryrefslogtreecommitdiff
path: root/examples/includes/HTML-Toc-0.91/TocGenerator.pm
blob: 8c49194cdfba9644fc2018c493a6491a0455aa73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
#=== HTML::TocGenerator =======================================================
# function: Generate 'HTML::Toc' table of contents.
# note:     - 'TT' is an abbrevation of 'TocToken'.


package HTML::TocGenerator;


use strict;
use HTML::Parser;


BEGIN {
	use vars qw(@ISA $VERSION);

	$VERSION = '0.91';

	@ISA = qw(HTML::Parser);
}


	# Warnings
use constant WARNING_NESTED_ANCHOR_PS_WITHIN_PS               => 1;
use constant WARNING_TOC_ATTRIBUTE_PS_NOT_AVAILABLE_WITHIN_PS => 2;


use constant TOC_TOKEN_ID       => 0;
use constant TOC_TOKEN_INCLUDE  => 1;
use constant TOC_TOKEN_EXCLUDE  => 2;
use constant TOC_TOKEN_TOKENS   => 3;
use constant TOC_TOKEN_GROUP    => 4;
use constant TOC_TOKEN_TOC      => 5;

	# Token types
use constant TT_TAG_BEGIN                => 0;
use constant TT_TAG_END                  => 1;
use constant TT_TAG_TYPE_END             => 2;
use constant TT_INCLUDE_ATTRIBUTES_BEGIN => 3;
use constant TT_EXCLUDE_ATTRIBUTES_BEGIN => 4;
use constant TT_INCLUDE_ATTRIBUTES_END   => 5;
use constant TT_EXCLUDE_ATTRIBUTES_END   => 6;
use constant TT_GROUP                    => 7;
use constant TT_TOC                      => 8;
use constant TT_ATTRIBUTES_TOC           => 9;


use constant CONTAINMENT_INCLUDE => 0;
use constant CONTAINMENT_EXCLUDE => 1;

use constant TEMPLATE_ANCHOR            => '$groupId."-".$node';
use constant TEMPLATE_ANCHOR_HREF       => 
					'"<a href=#".' . TEMPLATE_ANCHOR . '.">"';
use constant TEMPLATE_ANCHOR_HREF_FILE  => 
					'"<a href=".$file."#".' . TEMPLATE_ANCHOR . '.">"';
use constant TEMPLATE_ANCHOR_NAME       => 
					'"<a name=".' . TEMPLATE_ANCHOR . '.">"';

use constant TEMPLATE_TOKEN_NUMBER      => '"$node &nbsp"';


use constant TT_TOKENTYPE_START        => 0;
use constant TT_TOKENTYPE_END          => 1;
use constant TT_TOKENTYPE_TEXT         => 2;
use constant TT_TOKENTYPE_COMMENT      => 3;
use constant TT_TOKENTYPE_DECLARATION  => 4;


END {}


#--- HTML::TocGenerator::new() ------------------------------------------------
# function: Constructor

sub new {
		# Get arguments
	my ($aType) = @_;
	my $self = $aType->SUPER::new;
		# Bias to not generate ToC
	$self->{_doGenerateToc} = 0;
		# Bias to not use global groups
	$self->{_doUseGroupsGlobal} = 0;
		# Output
	$self->{output} = "";
		# Reset internal variables
	$self->_resetBatchVariables();

	$self->{options} = {};

	return $self;
}  # new()


#--- HTML::TocGenerator::_deinitializeBatch() ---------------------------------

sub _deinitializeBatch() {
		# Get arguments
	my ($self) = @_;
}  # _deinitializeBatch()


#--- HTML::TocGenerator::_deinitializeExtenderBatch() -------------------------

sub _deinitializeExtenderBatch() {
		# Get arguments
	my ($self) = @_;
		# Do general batch deinitialization
	$self->_deinitializeBatch();
		# Indicate end of ToC generation
	$self->{_doGenerateToc} = 0;
		# Reset batch variables
	$self->_resetBatchVariables();
}  # _deinitializeExtenderBatch()


#--- HTML::TocGenerator::_deinitializeGeneratorBatch() ------------------------

sub _deinitializeGeneratorBatch() {
		# Get arguments
	my ($self) = @_;
		# Do 'extender' batch deinitialization
	$self->_deinitializeExtenderBatch();
}  # _deinitializeBatchGenerator()


#--- HTML::TocGenerator::_doesHashContainHash() -------------------------------
# function: Determines whether hash1 matches regular expressions of hash2.
# args:     - $aHash1
#           - $aHash2
#           - $aContainmentType: 0 (include) or 1 (exclude)
# returns:  True (1) if hash1 satisfies hash2, 0 if not.  For example, with the
#           following hashes:
#
#              %hash1 = {							%hash2 = {
#                 'class' => 'header'				'class' => '^h'
#                 'id'    => 'intro'         }
#              }
#
#           the routine will return 1 if 'aContainmentType' equals 0, cause
#           'hash1' satisfies the conditions of 'hash2'.  The routine will
#           return 0 if 'aContainmentType' equals 1, cause 'hash1' doesn't
#           exclude the conditions of 'hash2'.
# note:     Class function.

sub _doesHashContainHash {
		# Get arguments
	my ($aHash1, $aHash2, $aContainmentType) = @_;
		# Local variables
	my ($key1, $value1, $key2, $value2, $result);
		# Bias to success
	$result = 1;
		# Loop through hash2
	HASH2: while (($key2, $value2) = each %$aHash2) {
		# Yes, values are available;
			# Get value1
		$value1 = $aHash1->{$key2};
			# Does value1 match criteria of value2?
		if (defined($value1) && $value1 =~ m/$value2/) {
			# Yes, value1 matches criteria of value2;
				# Containment type was exclude?
			if ($aContainmentType == CONTAINMENT_EXCLUDE) {
				# Yes, containment type was exclude;
					# Indicate condition fails
				$result = 0;
					# Reset 'each' iterator which we're going to break
				keys %$aHash2;
					# Break loop
				last HASH2;
			}
		}
		else {
			# No, value1 didn't match criteria of value2;
				# Containment type was include?
			if ($aContainmentType == CONTAINMENT_INCLUDE) {
				# Yes, containment type was include;
					# Indicate condition fails
				$result = 0;
					# Reset 'each' iterator which we're going to break
				keys %$aHash2;
					# Break loop
				last HASH2;
			}
		}
	}
		# Return value
	return $result;
}  # _doesHashContainHash()


#--- HTML::TocGenerator::_extend() --------------------------------------------
# function: Extend ToC.
#           - $aString: String to parse.

sub _extend {
		# Get arguments
	my ($self, $aFile) = @_;
		# Local variables
	my ($file);
		# Parse string
	$self->parse($aFile);
		# Flush remaining buffered text
	$self->eof();
}  # _extend()


#--- HTML::TocGenerator::_extendFromFile() ------------------------------------
# function: Extend ToC.
#           - $aFile: (reference to array of) file to parse.

sub _extendFromFile {
		# Get arguments
	my ($self, $aFile) = @_;
		# Local variables
	my ($file, @files);
		# Dereference array reference or make array of file specification
	@files = (ref($aFile) =~ m/ARRAY/) ? @$aFile : ($aFile);
		# Loop through files
	foreach $file (@files) {
			# Store filename
		$self->{_currentFile} = $file;
			# Parse file
		$self->parse_file($file);
			# Flush remaining buffered text
		$self->eof();
	}
}  # _extendFromFile()


#--- HTML::TocGenerator::_formatHeadingLevel() --------------------------------
# function: Format heading level.
# args:     - $aLevel: Level of current heading
#           - $aClass: Class of current heading
#           - $aGroup: Group of current heading
#           - $aToc: Toc of current heading

sub _formatHeadingLevel {
		# Get arguments
	my ($self, $aLevel, $aClass, $aGroup, $aToc) = @_;
		# Local variables
	my ($result, $headingNumber, $numberingStyle);

	$headingNumber = $self->_getGroupIdManager($aToc)->
		{levels}{$aClass}[$aLevel - 1] || 0;

		# Alias numbering style of current group
	$numberingStyle = $aGroup->{numberingStyle};

	SWITCH: {
		if ($numberingStyle eq "decimal") {
			$result = $headingNumber;
			last SWITCH;
		}
		if ($numberingStyle eq "lower-alpha") {
			$result = chr($headingNumber + ord('a') - 1);
			last SWITCH;
		}
		if ($numberingStyle eq "upper-alpha") {
			$result = chr($headingNumber + ord('A') - 1);
			last SWITCH;
		}
		if ($numberingStyle eq "lower-roman") {
			require Roman;
			$result = Roman::roman($headingNumber);
			last SWITCH;
		}
		if ($numberingStyle eq "upper-roman") {
			require Roman;
			$result = Roman::Roman($headingNumber);
			last SWITCH;
		}
		die "Unknown case: $numberingStyle";
	}
		# Return value
	return $result;
}	# _formatHeadingLevel()


#--- HTML::TocGenerator::_formatTocNode() -------------------------------------
# function: Format heading node.
# args:     - $aLevel: Level of current heading
#           - $aClass: Class of current heading
#           - $aGroup: Group of current heading
#           - $aToc: Toc of current heading

sub _formatTocNode {
		# Get arguments
	my ($self, $aLevel, $aClass, $aGroup, $aToc) = @_;
		# Local variables
	my ($result, $level, $levelGroups);

		# Alias 'levelGroups' of right 'groupId'
	$levelGroups = $aToc->{_levelGroups}{$aGroup->{'groupId'}};
		# Loop through levels
	for ($level = 1; $level <= $aLevel; $level++) {
			# If not first level, add dot
		$result = ($result ? $result . "." : $result);
			# Format heading level using argument group
		$result .= $self->_formatHeadingLevel(
			$level, $aClass, @{$levelGroups}[$level - 1], $aToc
		);
	}
		# Return value
	return $result;
}  # _formatTocNode()
     	
     	
#--- HTML::TocGenerator::_generate() ------------------------------------------
# function: Generate ToC.
# args:     - $aString: Reference to string to parse

sub _generate {
		# Get arguments
	my ($self, $aString) = @_;
		# Local variables
	my ($toc);
		# Loop through ToCs
	foreach $toc (@{$self->{_tocs}}) {
			# Clear ToC
		$toc->clear();
	}
		# Extend ToCs
	$self->_extend($aString);
}  # _generate()


#--- HTML::TocGenerator::_generateFromFile() ----------------------------------
# function: Generate ToC.
# args:     - $aFile: (reference to array of) file to parse.

sub _generateFromFile {
		# Get arguments
	my ($self, $aFile) = @_;
		# Local variables
	my ($toc);
		# Loop through ToCs
	foreach $toc (@{$self->{_tocs}}) {
			# Clear ToC
		$toc->clear();
	}
		# Extend ToCs
	$self->_extendFromFile($aFile);
}  # _generateFromFile()


#--- HTML::TocGenerator::_getGroupIdManager() ---------------------------------
# function: Get group id manager.
# args:     - $aToc: Active ToC.
# returns:  Group id levels.

sub _getGroupIdManager {
		# Get arguments
	my ($self, $aToc) = @_;
		# Local variables
	my ($result);
		# Global groups?
	if ($self->{options}{'doUseGroupsGlobal'}) {
		# Yes, global groups;
		$result = $self;
	}
	else {
		# No, local groups;
		$result = $aToc;
	}
		# Return value
	return $result;
}  # _getGroupIdManager()


#--- HTML::TocGenerator::_initializeBatch() -----------------------------------
# function: Initialize batch.  This function is called once when a parse batch
#           is started.
# args:     - $aTocs: Reference to array of tocs.

sub _initializeBatch {
		# Get arguments
	my ($self, $aTocs) = @_;
		# Local variables
	my ($toc);

		# Store reference to tocs
		
		# Is ToC specification reference to array?
	if (ref($aTocs) =~ m/ARRAY/) {
		# Yes, ToC specification is reference to array;
			# Store array reference
		$self->{_tocs} = $aTocs;
	}
	else {
		# No, ToC specification is reference to ToC object;
			# Wrap reference in array reference, containing only one element
		$self->{_tocs} = [$aTocs];
	}
		# Loop through ToCs
	foreach $toc (@{$self->{_tocs}}) {
			# Parse ToC options
		$toc->parseOptions();
	}
}  # _initializeBatch()


#--- HTML::TocGenerator::_initializeExtenderBatch() --------------------------
# function: Initialize 'extender' batch.  This function is called once when a 
#           parse batch is started.
# args:     - $aTocs: Reference to array of tocs.

sub _initializeExtenderBatch {
		# Get arguments
	my ($self, $aTocs) = @_;
		# Do general batch initialization
	$self->_initializeBatch($aTocs);
		# Parse ToC options
	$self->_parseTocOptions();
		# Indicate start of batch
	$self->{_doGenerateToc} = 1;
}  # _initializeExtenderBatch()


#--- HTML::TocGenerator::_initializeGeneratorBatch() --------------------------
# function: Initialize generator batch.  This function is called once when a 
#           parse batch is started.
# args:     - $aTocs: Reference to array of tocs.
#           - $aOptions: optional options

sub _initializeGeneratorBatch {
		# Get arguments
	my ($self, $aTocs, $aOptions) = @_;
		# Add invocation options
	$self->setOptions($aOptions);
		# Option 'doUseGroupsGlobal' specified?
	if (!defined($self->{options}{'doUseGroupsGlobal'})) {
		# No, options 'doUseGroupsGlobal' not specified;
			# Default to no 'doUseGroupsGlobal'
		$self->{options}{'doUseGroupsGlobal'} = 0;
	}
		# Global groups?
	if ($self->{options}{'doUseGroupsGlobal'}) {
		# Yes, global groups;
			# Reset groups and levels
		$self->_resetStackVariables();
	}
		# Do 'extender' batch initialization
	$self->_initializeExtenderBatch($aTocs);
}  # _initializeGeneratorBatch()


#--- HTML::TocGenerator::_linkTocToToken() ------------------------------------
# function: Link ToC to token.
# args:     - $aToc: ToC to add token to.
#           - $aFile
#           - $aGroupId
#           - $aLevel
#           - $aNode
#           - $aGroupLevel
#           - $aLinkType
#           - $aTokenAttributes: reference to hash containing attributes of 
#                currently parsed token

sub _linkTocToToken {
		# Get arguments
	my (
		$self, $aToc, $aFile, $aGroupId, $aLevel, $aNode, $aGroupLevel, 
		$aDoLinkToId, $aTokenAttributes
	) = @_;
		# Local variables
	my ($file, $groupId, $level, $node, $anchorName);
	my ($doInsertAnchor, $doInsertId);

		# Fill local arguments to be used by templates
	$file    = $aFile;
	$groupId = $aGroupId;
	$level   = $aLevel;
	$node    = $aNode;
	
		# Assemble anchor name
	$anchorName = 
		ref($aToc->{_templateAnchorName}) eq "CODE" ?
			&{$aToc->{_templateAnchorName}}(
				$aFile, $aGroupId, $aLevel, $aNode
			) : 
			eval($aToc->{_templateAnchorName});

		# Bias to insert anchor name
	$doInsertAnchor = 1;
	$doInsertId     = 0;
		# Link to 'id'?
	if ($aDoLinkToId) {
		# Yes, link to 'id';
			# Indicate to insert anchor id
		$doInsertAnchor = 0;
		$doInsertId     = 1;
			# Id attribute is available?
		if (defined($aTokenAttributes->{id})) {
			# Yes, id attribute is available;
				# Use existing ids?
			if ($aToc->{options}{'doUseExistingIds'}) {
				# Yes, use existing ids;
					# Use existing id
				$anchorName = $aTokenAttributes->{id};
					# Indicate to not insert id
				$doInsertId = 0;
			}
		}

	}
	else {
		# No, link to 'name';
			# Anchor name is currently active?
		if (defined($self->{_activeAnchorName})) {
			# Yes, anchor name is currently active;
				# Use existing anchors?
			if ($aToc->{options}{'doUseExistingAnchors'}) {
				# Yes, use existing anchors;
					# Use existing anchor name
				$anchorName = $self->{_activeAnchorName};
					# Indicate to not insert anchor name
				$doInsertAnchor = 0;
			}
			else {
				# No, don't use existing anchors; insert new anchor;
					# 
			}
		}
	}

		# Add reference to ToC
	$aToc->{_toc} .= 
		ref($aToc->{_templateAnchorHrefBegin}) eq "CODE" ?
			&{$aToc->{_templateAnchorHrefBegin}}(
				$aFile, $aGroupId, $aLevel, $aNode, $anchorName
			) : 
			eval($aToc->{_templateAnchorHrefBegin});

		# Bias to not output anchor name end
	$self->{_doOutputAnchorNameEnd} = 0;
		# Must anchor be inserted?
	if ($doInsertAnchor) {
		# Yes, anchor must be inserted;
			# Allow adding of anchor name begin token to text by calling 
			# 'anchorNameBegin' method
		$self->anchorNameBegin(
			ref($aToc->{_templateAnchorNameBegin}) eq "CODE" ?
				&{$aToc->{_templateAnchorNameBegin}}(
					$aFile, $aGroupId, $aLevel, $aNode, $anchorName
				) :
				eval($aToc->{_templateAnchorNameBegin}),
			$aToc
		);
	}

		# Must anchorId attribute be inserted?
	if ($doInsertId) {
		# Yes, anchorId attribute must be inserted;
			# Allow adding of anchorId attribute to text by calling 'anchorId'
			# method
		$self->anchorId($anchorName);
	}
}  # _linkTocToToken()


#--- HTML::TocGenerator::_outputAnchorNameEndConditionally() ------------------
# function: Output 'anchor name end' if necessary
# args:     - $aToc: ToC of which 'anchor name end' must be output.

sub _outputAnchorNameEndConditionally {
		# Get arguments
	my ($self, $aToc) = @_;
		# Must anchor name end be output?
	if ($self->{_doOutputAnchorNameEnd}) {
		# Yes, output anchor name end;
			# Allow adding of anchor to text by calling 'anchorNameEnd' 
			# method
		$self->anchorNameEnd(
			ref($aToc->{_templateAnchorNameEnd}) eq "CODE" ?
				&{$aToc->{_templateAnchorNameEnd}} :
				eval($aToc->{_templateAnchorNameEnd}),
			$aToc
		);
	}
}  # _outputAnchorNameEndConditionally()


#--- HTML::TocGenerator::_parseTocOptions() -----------------------------------
# function: Parse ToC options.

sub _parseTocOptions {
		# Get arguments
	my ($self) = @_;
		# Local variables
	my ($toc, $group, $tokens, $tokenType, $i);
		# Create parsers for ToC tokens
	$self->{_tokensTocBegin} = [];
	my $tokenTocBeginParser = HTML::_TokenTocBeginParser->new(
		$self->{_tokensTocBegin}
	);
	my $tokenTocEndParser = HTML::_TokenTocEndParser->new();
		# Loop through ToCs
	foreach $toc (@{$self->{_tocs}}) {
			# Reference parser ToC to current ToC
		$tokenTocBeginParser->setToc($toc);
			# Loop through 'tokenToToc' groups
		foreach $group (@{$toc->{options}{'tokenToToc'}}) {
				# Reference parser group to current group
			$tokenTocBeginParser->setGroup($group);
				# Parse 'tokenToToc' group
			$tokenTocBeginParser->parse($group->{'tokenBegin'});
				# Flush remaining buffered text
			$tokenTocBeginParser->eof();
			$tokenTocEndParser->parse(
				$group->{'tokenEnd'}, 
				$tokenTocBeginParser->{_lastAddedToken},
				$tokenTocBeginParser->{_lastAddedTokenType}
			);
				# Flush remaining buffered text
			$tokenTocEndParser->eof();
		}
	}
}  # _parseTocOptions()


#--- HTML::TocGenerator::_processTocEndingToken() -----------------------------
# function: Process ToC-ending-token.
# args:     - $aTocToken: token which acts as ToC-ending-token.

sub _processTocEndingToken {
		# Get arguments
	my ($self, $aTocToken) = @_;
		# Local variables
	my ($toc);
		# Aliases
	$toc = $aTocToken->[TT_TOC];
		# Link ToC to tokens?
	if ($toc->{options}{'doLinkToToken'}) {
		# Yes, link ToC to tokens;
			# Add anchor href end
		$toc->{_toc} .= 
			(ref($toc->{_templateAnchorHrefEnd}) eq "CODE") ?
				&{$toc->{_templateAnchorHrefEnd}} : 
				eval($toc->{_templateAnchorHrefEnd});

			# Output anchor name end only if necessary
		$self->_outputAnchorNameEndConditionally($toc);
	}
}  # _processTocEndingToken()


#--- HTML::TocGenerator::_processTocStartingToken() ---------------------------
# function: Process ToC-starting-token.
# args:     - $aTocToken: token which acts as ToC-starting-token.
#           - $aTokenType: type of token.  Can be either TT_TOKENTYPE_START,
#                _END, _TEXT, _COMMENT or _DECLARATION.
#           - $aTokenAttributes: reference to hash containing attributes of 
#                currently parsed token
#           - $aTokenOrigText: reference to original token text

sub _processTocStartingToken {
		# Get arguments
	my ($self, $aTocToken, $aTokenType, $aTokenAttributes, $aTokenOrigText) = @_;
		# Local variables
	my ($i, $level, $doLinkToId, $node, $groupLevel);
	my ($file, $tocTokenId, $groupId, $toc, $attribute);
		# Aliases
	$file        = $self->{_currentFile};
	$toc		    = $aTocToken->[TT_TOC];
	$level	    = $aTocToken->[TT_GROUP]{'level'};
	$groupId	    = $aTocToken->[TT_GROUP]{'groupId'};

		# Retrieve 'doLinkToId' setting from either group options or toc options
	$doLinkToId = (defined($aTocToken->[TT_GROUP]{'doLinkToId'})) ?
		$aTocToken->[TT_GROUP]{'doLinkToId'} : $toc->{options}{'doLinkToId'}; 
	
		# Link to 'id' and tokenType isn't 'start'?
	if (($doLinkToId) && ($aTokenType != TT_TOKENTYPE_START)) {
		# Yes, link to 'id' and tokenType isn't 'start';
			# Indicate to *not* link to 'id'
		$doLinkToId = 0;
	}

	if (ref($level) eq "CODE") {
		$level = &$level($self->{_currentFile}, $node);
	}
	if (ref($groupId) eq "CODE") {
		$groupId = &$groupId($self->{_currentFile}, $node);
	}

		# Determine class level

	my $groupIdManager = $self->_getGroupIdManager($toc);
		# Known group?
	if (!exists($groupIdManager->{groupIdLevels}{$groupId})) {
		# No, unknown group;
			# Add group
		$groupIdManager->{groupIdLevels}{$groupId} = keys(
			%{$groupIdManager->{groupIdLevels}}
		) + 1;
	}
	$groupLevel = $groupIdManager->{groupIdLevels}{$groupId};

		# Temporarily allow symbolic references
	#no strict qw(refs);
		# Increase level
	$groupIdManager->{levels}{$groupId}[$level - 1] += 1;
		# Reset remaining levels of same group
	for ($i = $level; $i < @{$groupIdManager->{levels}{$groupId}}; $i++) {
		$groupIdManager->{levels}{$groupId}[$i] = 0;
	}

		# Assemble numeric string indicating current level
	$node = $self->_formatTocNode(
		$level, $groupId, $aTocToken->[TT_GROUP], $toc
	);

		# Add newline if _toc not empty
	if ($toc->{_toc}) { 
		$toc->{_toc} .= "\n";
	}

		# Add toc item info
	$toc->{_toc} .= "$level $groupLevel $groupId $node " .
		$groupIdManager->{levels}{$groupId}[$level - 1] . " ";

		# Add value of 'id' attribute if available
	if (defined($aTokenAttributes->{id})) {
		$toc->{_toc} .= $aTokenAttributes->{id};
	}
	$toc->{_toc} .= " ";
		# Link ToC to tokens?
	if ($toc->{options}{'doLinkToToken'}) {
		# Yes, link ToC to tokens;
			# Link ToC to token
		$self->_linkTocToToken(
			$toc, $file, $groupId, $level, $node, $groupLevel, $doLinkToId,
			$aTokenAttributes
		);
	}

		# Number tokens?
	if (
		$aTocToken->[TT_GROUP]{'doNumberToken'} || 
		(
			! defined($aTocToken->[TT_GROUP]{'doNumberToken'}) && 
			$toc->{options}{'doNumberToken'}
		)
	) {
		# Yes, number tokens;
			# Add number by calling 'number' method
		$self->number(
			ref($toc->{_templateTokenNumber}) eq "CODE" ?
				&{$toc->{_templateTokenNumber}}(
					$node, $groupId, $file, $groupLevel, $level, $toc
				) : 
				eval($toc->{_templateTokenNumber}),
			$toc
		);
	}

		# Must attribute be used as ToC text?
	if (defined($aTocToken->[TT_ATTRIBUTES_TOC])) {
		# Yes, attribute must be used as ToC text;
			# Loop through attributes
		foreach $attribute (@{$aTocToken->[TT_ATTRIBUTES_TOC]}) {
				# Attribute is available?
			if (defined($$aTokenAttributes{$attribute})) {
				# Yes, attribute is available;
					# Add attribute value to ToC
				$self->_processTocText($$aTokenAttributes{$attribute}, $toc);
			}
			else {
				# No, attribute isn't available;
					# Show warning
				$self->_showWarning(
					WARNING_TOC_ATTRIBUTE_PS_NOT_AVAILABLE_WITHIN_PS,
					[$attribute, $$aTokenOrigText]
				);
			}
				# Output anchor name end only if necessary
			#$self->_outputAnchorNameEndConditionally($toc);
				# End attribute
			$self->_processTocEndingToken($aTocToken);
		}
	}
	else {
		# No, attribute mustn't be used as ToC text;
			# Add end token to 'end token array'
		push(
			@{$self->{_tokensTocEnd}[$aTocToken->[TT_TAG_TYPE_END]]}, $aTocToken
		);
	}
}  # _processTocStartingToken()


#--- HTML::TocGenerator::_processTocText() ------------------------------------
# function: This function processes text which must be added to the preliminary
#           ToC.
# args:     - $aText: Text to add to ToC.
#           - $aToc: ToC to add text to.

sub _processTocText {
		# Get arguments
	my ($self, $aText, $aToc) = @_;
		# Add text to ToC
	$aToc->{_toc} .= $aText;
}  # _processTocText()


#--- HTML::TocGenerator::_processTokenAsTocEndingToken() ----------------------
# function: Check for token being a token to use for triggering the end of
#           a ToC line and process it accordingly.
# args:     - $aTokenType: type of token: 'start', 'end', 'comment' or 'text'.
#           - $aTokenId: token id of currently parsed token

sub _processTokenAsTocEndingToken {
		# Get arguments
	my ($self, $aTokenType, $aTokenId) = @_;
		# Local variables
	my ($i, $tokenId, $toc, $tokens);
		# Loop through dirty start tokens
	$i = 0;

		# Alias token array of right type
	$tokens = $self->{_tokensTocEnd}[$aTokenType];
		# Loop through token array
	while ($i < scalar @$tokens) {
			# Aliases
		$tokenId = $tokens->[$i][TT_TAG_END];
			# Does current end tag equals dirty tag?
		if ($aTokenId eq $tokenId) {
			# Yes, current end tag equals dirty tag;
				# Process ToC-ending-token
			$self->_processTocEndingToken($tokens->[$i]);
				# Remove dirty tag from array, automatically advancing to
				# next token
			splice(@$tokens, $i, 1);
		}
		else {
			# No, current end tag doesn't equal dirty tag;
				# Advance to next token
			$i++;
		}
	}
}  # _processTokenAsTocEndingToken()


#--- HTML::TocGenerator::_processTokenAsTocStartingToken() --------------------
# function: Check for token being a ToC-starting-token and process it 
#           accordingly.
# args:     - $aTokenType: type of token.  Can be either TT_TOKENTYPE_START,
#                _END, _TEXT, _COMMENT or _DECLARATION.
#           - $aTokenId: token id of currently parsed token
#           - $aTokenAttributes: reference to hash containing attributes of 
#                currently parsed token
#           - $aTokenOrigText: reference to original text of token
# returns:  1 if successful, i.e. token is processed as ToC-starting-token, 0
#           if not.

sub _processTokenAsTocStartingToken {
		# Get arguments
	my ($self, $aTokenType, $aTokenId, $aTokenAttributes, $aTokenOrigText) = @_;
		# Local variables
	my ($level, $levelToToc, $groupId, $groupToToc);
	my ($result, $tocToken, $tagBegin, @tokensTocBegin, $fileSpec);
		# Bias to token not functioning as ToC-starting-token
	$result = 0;
		# Loop through start tokens of right type
	foreach $tocToken (@{$self->{_tokensTocBegin}[$aTokenType]}) {
			# Alias file filter
		$fileSpec = $tocToken->[TT_GROUP]{'fileSpec'};
			# File matches?
		if (!defined($fileSpec) || (
			defined($fileSpec) &&
			($self->{_currentFile} =~ m/$fileSpec/)
		)) {
			# Yes, file matches;
				# Alias tag begin
			$tagBegin = $tocToken->[TT_TAG_BEGIN];
				# Tag and attributes match?
			if (
				defined($tagBegin) && 
				($aTokenId =~ m/$tagBegin/) && 
				HTML::TocGenerator::_doesHashContainHash(
					$aTokenAttributes, $tocToken->[TT_INCLUDE_ATTRIBUTES_BEGIN], 0
				) &&
				HTML::TocGenerator::_doesHashContainHash(
					$aTokenAttributes, $tocToken->[TT_EXCLUDE_ATTRIBUTES_BEGIN], 1
				)
			) {
				# Yes, tag and attributes match;
					# Aliases
				$level	    = $tocToken->[TT_GROUP]{'level'};
				$levelToToc = $tocToken->[TT_TOC]{options}{'levelToToc'};
				$groupId     = $tocToken->[TT_GROUP]{'groupId'}; 
				$groupToToc = $tocToken->[TT_TOC]{options}{'groupToToc'};
					# Must level and group be processed?
				if (
					($level =~ m/$levelToToc/) &&
					($groupId =~ m/$groupToToc/)
				) {
					# Yes, level and group must be processed;
						# Indicate token acts as ToC-starting-token
					$result = 1;
						# Process ToC-starting-token
					$self->_processTocStartingToken(
						$tocToken, $aTokenType, $aTokenAttributes, $aTokenOrigText
					);
				}
			}
		}
	}
		# Return value
	return $result;
}  # _processTokenAsTocStartingToken()


#--- HTML::TocGenerator::_resetBatchVariables() -------------------------------
# function: Reset variables which are set because of batch invocation.

sub _resetBatchVariables {
		# Get arguments
	my ($self) = @_;

		# Filename of current file being parsed, empty string if not available
	$self->{_currentFile} = "";
		# Arrays containing start, end, comment, text & declaration tokens which 
		# must trigger the ToC assembling.  Each array element may contain a 
		# reference to an array containing the following elements:
		#
      #    TT_TAG_BEGIN                => 0;
      #    TT_TAG_END                  => 1;
      #    TT_TAG_TYPE_END             => 2;
      #    TT_INCLUDE_ATTRIBUTES_BEGIN => 3;
      #    TT_EXCLUDE_ATTRIBUTES_BEGIN => 4;
      #    TT_INCLUDE_ATTRIBUTES_END   => 5;
      #    TT_EXCLUDE_ATTRIBUTES_END   => 6;
      #    TT_GROUP                    => 7;
      #    TT_TOC                      => 8;
		#    TT_ATTRIBUTES_TOC           => 9;
		#
	$self->{_tokensTocBegin} = [
		[],  # TT_TOKENTYPE_START      
		[],  # TT_TOKENTYPE_END        
		[],  # TT_TOKENTYPE_COMMENT    
		[],  # TT_TOKENTYPE_TEXT       
		[]   # TT_TOKENTYPE_DECLARATION
	];
	$self->{_tokensTocEnd} = [
		[],  # TT_TOKENTYPE_START      
		[],  # TT_TOKENTYPE_END        
		[],  # TT_TOKENTYPE_COMMENT    
		[],  # TT_TOKENTYPE_TEXT       
		[]   # TT_TOKENTYPE_DECLARATION
	];
		# TRUE if ToCs have been initialized, FALSE if not.
	$self->{_doneInitializeTocs} = 0;
		# Array of ToCs to process
	$self->{_tocs} = [];
		# Active anchor name
	$self->{_activeAnchorName} = undef;
}  # _resetBatchVariables()


#--- HTML::TocGenerator::_resetStackVariables() -------------------------------
# function: Reset variables which cumulate during ToC generation.

sub _resetStackVariables {
		# Get arguments
	my ($self) = @_;
		# Reset variables
	$self->{levels}        = undef;
	$self->{groupIdLevels} = undef;
}  # _resetStackVariables()


#--- HTML::TocGenerator::_setActiveAnchorName() -------------------------------
# function: Set active anchor name.
# args:     - aAnchorName: Name of anchor name to set active.

sub _setActiveAnchorName {
		# Get arguments
	my ($self, $aAnchorName) = @_;
		# Set active anchor name
	$self->{_activeAnchorName} = $aAnchorName;
}  # _setActiveAnchorName()


#--- HTML::TocGenerator::_showWarning() ---------------------------------------
# function: Show warning.
# args:     - aWarningNr: Number of warning to show.
#           - aWarningArgs: Arguments to display within the warning.

sub _showWarning {
		# Get arguments
	my ($self, $aWarningNr, $aWarningArgs) = @_;
		# Local variables
	my (%warnings);
		# Set warnings
	%warnings = (
		WARNING_NESTED_ANCHOR_PS_WITHIN_PS()               => 
			"Nested anchor '%s' within anchor '%s'.", 
		WARNING_TOC_ATTRIBUTE_PS_NOT_AVAILABLE_WITHIN_PS() =>
			"ToC attribute '%s' not available within token '%s'.",
	);
		# Show warning
	print STDERR "warning ($aWarningNr): " . sprintf($warnings{"$aWarningNr"}, @$aWarningArgs) . "\n";
}  # _showWarning()


#--- HTML::TocGenerator::anchorId() -------------------------------------------
# function: Anchor id processing method.  Leave it up to the descendant to do 
#           something useful with it.
# args:     - $aAnchorId
#           - $aToc: Reference to ToC to which anchorId belongs.

sub anchorId {
}  # anchorId()


#--- HTML::TocGenerator::anchorNameBegin() ------------------------------------
# function: Anchor name begin processing method.  Leave it up to the descendant
#           to do something useful with it.
# args:     - $aAnchorName
#           - $aToc: Reference to ToC to which anchorname belongs.

sub anchorNameBegin {
}  # anchorNameBegin()


#--- HTML::TocGenerator::anchorNameEnd() --------------------------------------
# function: Anchor name end processing method.  Leave it up to the descendant
#           to do something useful with it.
# args:     - $aAnchorName
#           - $aToc: Reference to ToC to which anchorname belongs.

sub anchorNameEnd {
}  # anchorNameEnd()


#--- HTML::TocGenerator::comment() --------------------------------------------
# function: Process comment.
# args:     - $aComment: comment text with '<!--' and '-->' tags stripped off.

sub comment {
		# Get arguments
	my ($self, $aComment) = @_;
		# Must a ToC be generated?
	if ($self->{_doGenerateToc}) {
		# Yes, a ToC must be generated
			# Process end tag as ToC-starting-token
		$self->_processTokenAsTocStartingToken(
			TT_TOKENTYPE_COMMENT, $aComment, undef, \$aComment
		);
			# Process end tag as token which ends ToC registration
		$self->_processTokenAsTocEndingToken(
			TT_TOKENTYPE_COMMENT, $aComment
		);
	}
}  # comment()


#--- HTML::TocGenerator::end() ------------------------------------------------
# function: This function is called every time a closing tag is encountered.
# args:     - $aTag: tag name (in lower case).
#           - $aOrigText: tag name including brackets.

sub end {
		# Get arguments
	my ($self, $aTag, $aOrigText) = @_;
		# Local variables
	my ($tag, $toc, $i);
		# Must a ToC be generated?
	if ($self->{_doGenerateToc}) {
		# Yes, a ToC must be generated
			# Process end tag as ToC-starting-token
		$self->_processTokenAsTocStartingToken(
			TT_TOKENTYPE_END, $aTag, undef, \$aOrigText
		);
			# Process end tag as ToC-ending-token
		$self->_processTokenAsTocEndingToken(
			TT_TOKENTYPE_END, $aTag
		);
			# Tag is of type 'anchor'?
		if (defined($self->{_activeAnchorName}) && ($aTag eq "a")) {
			# Yes, tag is of type 'anchor';
				# Reset dirty anchor
			$self->{_activeAnchorName} = undef;
		}
	}
}  # end()


#--- HTML::TocGenerator::extend() ---------------------------------------------
# function: Extend ToCs.
# args:     - $aTocs: Reference to array of ToC objects
#           - $aString: String to parse.

sub extend {
		# Get arguments
	my ($self, $aTocs, $aString) = @_;
		# Initialize TocGenerator batch
	$self->_initializeExtenderBatch($aTocs);
		# Extend ToCs
	$self->_extend($aString);
		# Deinitialize TocGenerator batch
	$self->_deinitializeExtenderBatch();
}  # extend()


#--- HTML::TocGenerator::extendFromFile() -------------------------------------
# function: Extend ToCs.
# args:     - @aTocs: Reference to array of ToC objects
#           - @aFiles: Reference to array of files to parse.

sub extendFromFile {
		# Get arguments
	my ($self, $aTocs, $aFiles) = @_;
		# Initialize TocGenerator batch
	$self->_initializeExtenderBatch($aTocs);
		# Extend ToCs
	$self->_extendFromFile($aFiles);
		# Deinitialize TocGenerator batch
	$self->_deinitializeExtenderBatch();
}  # extendFromFile()


#--- HTML::TocGenerator::generate() -------------------------------------------
# function: Generate ToC.
# args:     - $aToc: Reference to (array of) ToC object(s)
#           - $aString: Reference to string to parse
#           - $aOptions: optional options

sub generate {
		# Get arguments
	my ($self, $aToc, $aString, $aOptions) = @_;
		# Initialize TocGenerator batch
	$self->_initializeGeneratorBatch($aToc, $aOptions);
		# Do generate ToC
	$self->_generate($aString);
		# Deinitialize TocGenerator batch
	$self->_deinitializeGeneratorBatch();
}  # generate()


#--- HTML::TocGenerator::generateFromFile() -----------------------------------
# function: Generate ToC.
# args:     - $aToc: Reference to (array of) ToC object(s)
#           - $aFile: (reference to array of) file to parse.
#           - $aOptions: optional options

sub generateFromFile {
		# Get arguments
	my ($self, $aToc, $aFile, $aOptions) = @_;
		# Initialize TocGenerator batch
	$self->_initializeGeneratorBatch($aToc, $aOptions);
		# Do generate ToC
	$self->_generateFromFile($aFile);
		# Deinitialize TocGenerator batch
	$self->_deinitializeGeneratorBatch();
}  # generateFromFile()


#--- HTML::TocGenerator::number() ---------------------------------------------
# function: Heading number processing method.  Leave it up to the descendant
#           to do something useful with it.
# args:     - $aNumber
#           - $aToc: Reference to ToC to which anchorname belongs.

sub number {
		# Get arguments
	my ($self, $aNumber, $aToc) = @_;
}  # number()


#--- HTML::TocGenerator::parse() ----------------------------------------------
# function: Parse scalar.
# args:     - $aString: string to parse

sub parse {
		# Get arguments
	my ($self, $aString) = @_;
		# Call ancestor
	$self->SUPER::parse($aString);
}  # parse()


#--- HTML::TocGenerator::parse_file() -----------------------------------------
# function: Parse file.

sub parse_file {
		# Get arguments
	my ($self, $aFile) = @_;
		# Call ancestor
	$self->SUPER::parse_file($aFile);
}  # parse_file()


#--- HTML::TocGenerator::setOptions() -----------------------------------------
# function: Set options.
# args:     - aOptions: Reference to hash containing options.

sub setOptions {
		# Get arguments
	my ($self, $aOptions) = @_;
		# Options are defined?
	if (defined($aOptions)) {
		# Yes, options are defined; add to options
		%{$self->{options}} = (%{$self->{options}}, %$aOptions);
	}
}  # setOptions()


#--- HTML::TocGenerator::start() ----------------------------------------------
# function: This function is called every time an opening tag is encountered.
# args:     - $aTag: tag name (in lower case).
#           - $aAttr: reference to hash containing all tag attributes (in lower
#                case).
#           - $aAttrSeq: reference to array containing all tag attributes (in 
#                lower case) in the original order
#           - $aOrigText: the original HTML text

sub start {
		# Get arguments
	my ($self, $aTag, $aAttr, $aAttrSeq, $aOrigText) = @_;
	$self->{isTocToken} = 0;
		# Start tag is of type 'anchor name'?
	if ($aTag eq "a" && defined($aAttr->{name})) {
		# Yes, start tag is of type 'anchor name';
			# Is another anchor already active?
		if (defined($self->{_activeAnchorName})) {
			# Yes, another anchor is already active;
				# Is the first anchor inserted by 'TocGenerator'?
			if ($self->{_doOutputAnchorNameEnd}) {
				# Yes, the first anchor is inserted by 'TocGenerator';
					# Show warning
				$self->_showWarning(
					WARNING_NESTED_ANCHOR_PS_WITHIN_PS,
					[$aOrigText, $self->{_activeAnchorName}]
				);
			}
		}
			# Set active anchor name
		$self->_setActiveAnchorName($aAttr->{name});
	}
		# Must a ToC be generated?
	if ($self->{_doGenerateToc}) {
		# Yes, a ToC must be generated
			# Process start tag as ToC token
		$self->{isTocToken} = $self->_processTokenAsTocStartingToken(
			TT_TOKENTYPE_START, $aTag, $aAttr, \$aOrigText
		);
			# Process end tag as ToC-ending-token
		$self->_processTokenAsTocEndingToken(
			TT_TOKENTYPE_START, $aTag
		);
	}
}  # start()


#--- HTML::TocGenerator::text() -----------------------------------------------
# function: This function is called every time plain text is encountered.
# args:     - @_: array containing data.

sub text {
		# Get arguments
	my ($self, $aText) = @_;
		# Local variables
	my ($text, $toc, $i, $token, $tokens);
		# Must a ToC be generated?
	if ($self->{_doGenerateToc}) {
		# Yes, a ToC must be generated
			# Are there dirty start tags?

			# Loop through token types
		foreach $tokens (@{$self->{_tokensTocEnd}}) {
				# Loop though tokens
			foreach $token (@$tokens) {
					# Add text to toc

					# Alias
				$toc = $token->[TT_TOC];
					# Remove possible newlines from text
				($text = $aText) =~ s/\s*\n\s*/ /g;
					# Add text to toc
				$self->_processTocText($text, $toc);
			}
		}
	}
}  # text()




#=== HTML::_TokenTocParser ====================================================
# function: Parse 'toc tokens'.  'Toc tokens' mark HTML code which is to be
#           inserted into the ToC.
# note:     Used internally.

package HTML::_TokenTocParser;


BEGIN {
	use vars qw(@ISA);

	@ISA = qw(HTML::Parser);
}


END {}


#--- HTML::_TokenTocParser::new() ---------------------------------------------
# function: Constructor

sub new {
		# Get arguments
	my ($aType) = @_;
		# Create instance
	my $self = $aType->SUPER::new;

		# Return instance
	return $self;
}  # new()


#--- HTML::_TokenTocParser::_parseAttributes() --------------------------------
# function: Parse attributes.
# args:     - $aAttr: Reference to hash containing all tag attributes (in lower
#                case).
#           - $aIncludeAttributes: Reference to hash to which 'include
#                attributes' must be added.
#           - $aExcludeAttributes: Reference to hash to which 'exclude
#                attributes' must be added.
#           - $aTocAttributes: Reference to hash to which 'ToC attributes' 
#                must be added.

sub _parseAttributes {
		# Get arguments
	my (
		$self, $aAttr, $aIncludeAttributes, $aExcludeAttributes,
		$aTocAttributes
	) = @_;
		# Local variables
	my ($key, $value);
	my ($attributeToExcludeToken, $attributeToTocToken);
		# Get token which marks attributes which must be excluded
	$attributeToExcludeToken = $self->{_toc}{options}{'attributeToExcludeToken'};
	$attributeToTocToken     = $self->{_toc}{options}{'attributeToTocToken'};
		# Loop through attributes
	while (($key, $value) = each %$aAttr) {
			# Attribute value equals 'ToC token'?
		if ($value =~ m/$attributeToTocToken/) {
			# Yes, attribute value equals 'ToC token';
				# Add attribute to 'ToC attributes'
			push @$aTocAttributes, $key;
		}
		else {
			# No, attribute isn't 'ToC' token;
				# Attribute value starts with 'exclude token'?
			if ($value =~ m/^$attributeToExcludeToken(.*)/) {
				# Yes, attribute value starts with 'exclude token';
					# Add attribute to 'exclude attributes'
				$$aExcludeAttributes{$key} = "$1";
			}
			else {
				# No, attribute key doesn't start with '-';
					# Add attribute to 'include attributes'
				$$aIncludeAttributes{$key} = $value;
			}
		}
	}
}  # _parseAttributes()




#=== HTML::_TokenTocBeginParser ===============================================
# function: Parse 'toc tokens'.  'Toc tokens' mark HTML code which is to be
#           inserted into the ToC.
# note:     Used internally.

package HTML::_TokenTocBeginParser;


BEGIN {
	use vars qw(@ISA);

	@ISA = qw(HTML::_TokenTocParser);
}

END {}


#--- HTML::_TokenTocBeginParser::new() ----------------------------------------
# function: Constructor

sub new {
		# Get arguments
	my ($aType, $aTokenArray) = @_;
		# Create instance
	my $self = $aType->SUPER::new;
		# Reference token array
	$self->{tokens} = $aTokenArray;
		# Reference to last added token
	$self->{_lastAddedToken}     = undef;
	$self->{_lastAddedTokenType} = undef;
		# Return instance
	return $self;
}  # new()


#--- HTML::_TokenTocBeginParser::_processAttributes() -------------------------
# function: Process attributes.
# args:     - $aAttributes: Attributes to parse.

sub _processAttributes {
		# Get arguments
	my ($self, $aAttributes) = @_;
		# Local variables
	my (%includeAttributes, %excludeAttributes, @tocAttributes);

		# Parse attributes
	$self->_parseAttributes(
		$aAttributes, \%includeAttributes, \%excludeAttributes, \@tocAttributes
	);
		# Include attributes are specified?
	if (keys(%includeAttributes) > 0) {
		# Yes, include attributes are specified;
			# Store include attributes
		@${$self->{_lastAddedToken}}[
			HTML::TocGenerator::TT_INCLUDE_ATTRIBUTES_BEGIN
		] = \%includeAttributes;
	}
		# Exclude attributes are specified?
	if (keys(%excludeAttributes) > 0) {
		# Yes, exclude attributes are specified;
			# Store exclude attributes
		@${$self->{_lastAddedToken}}[
			HTML::TocGenerator::TT_EXCLUDE_ATTRIBUTES_BEGIN
		] = \%excludeAttributes;
	}
		# Toc attributes are specified?
	if (@tocAttributes > 0) {
		# Yes, toc attributes are specified;
			# Store toc attributes
		@${$self->{_lastAddedToken}}[
			HTML::TocGenerator::TT_ATTRIBUTES_TOC
		] = \@tocAttributes;
	}
}  # _processAttributes()


#--- HTML::_TokenTocBeginParser::_processToken() ------------------------------
# function: Process token.
# args:     - $aTokenType: Type of token to process.
#           - $aTag: Tag of token.

sub _processToken {
		# Get arguments
	my ($self, $aTokenType, $aTag) = @_;
		# Local variables
	my ($tokenArray, $index);
		# Push element on array of update tokens
	$index = push(@{$self->{tokens}[$aTokenType]}, []) - 1;
		# Alias token array to add element to
	$tokenArray = $self->{tokens}[$aTokenType];
		# Indicate last updated token array element
	$self->{_lastAddedTokenType} = $aTokenType;
	$self->{_lastAddedToken}     = \$$tokenArray[$index];
		# Add fields
	$$tokenArray[$index][HTML::TocGenerator::TT_TAG_BEGIN] = $aTag;
	$$tokenArray[$index][HTML::TocGenerator::TT_GROUP]     = $self->{_group};
	$$tokenArray[$index][HTML::TocGenerator::TT_TOC]       = $self->{_toc};
}  # _processToken()


#--- HTML::_TokenTocBeginParser::comment() ------------------------------------
# function: Process comment.
# args:     - $aComment: comment text with '<!--' and '-->' tags stripped off.

sub comment {
		# Get arguments
	my ($self, $aComment) = @_;
		# Process token
	$self->_processToken(HTML::TocGenerator::TT_TOKENTYPE_COMMENT, $aComment);
}  # comment()


#--- HTML::_TokenTocBeginParser::declaration() --------------------------------
# function: This function is called every time a markup declaration is
#           encountered by HTML::Parser.
# args:     - $aDeclaration: Markup declaration.

sub declaration {
		# Get arguments
	my ($self, $aDeclaration) = @_;
		# Process token
	$self->_processToken(
		HTML::TocGenerator::TT_TOKENTYPE_DECLARATION, $aDeclaration
	);
}  # declaration()

	
#--- HTML::_TokenTocBeginParser::end() ----------------------------------------
# function: This function is called every time a closing tag is encountered
#           by HTML::Parser.
# args:     - $aTag: tag name (in lower case).

sub end {
		# Get arguments
	my ($self, $aTag, $aOrigText) = @_;
		# Process token
	$self->_processToken(HTML::TocGenerator::TT_TOKENTYPE_END, $aTag);
}  # end()


#--- HTML::_TokenTocBeginParser::parse() --------------------------------------
# function: Parse begin token.
# args:     - $aToken: 'toc token' to parse

sub parse {
		# Get arguments
	my ($self, $aString) = @_;
		# Call ancestor
	$self->SUPER::parse($aString);
}  # parse()


#--- HTML::_TokenTocBeginParser->setGroup() -----------------------------------
# function: Set current 'tokenToToc' group.

sub setGroup {
		# Get arguments
	my ($self, $aGroup) = @_;
		# Set current 'tokenToToc' group
	$self->{_group} = $aGroup;
}  # setGroup()


#--- HTML::_TokenTocBeginParser->setToc() -------------------------------------
# function: Set current ToC.

sub setToc {
		# Get arguments
	my ($self, $aToc) = @_;
		# Set current ToC
	$self->{_toc} = $aToc;
}  # setToc()


#--- HTML::_TokenTocBeginParser::start() --------------------------------------
# function: This function is called every time an opening tag is encountered.
# args:     - $aTag: tag name (in lower case).
#           - $aAttr: reference to hash containing all tag attributes (in lower
#                case).
#           - $aAttrSeq: reference to array containing all attribute keys (in 
#                lower case) in the original order
#           - $aOrigText: the original HTML text

sub start {
		# Get arguments
	my ($self, $aTag, $aAttr, $aAttrSeq, $aOrigText) = @_;
		# Process token
	$self->_processToken(HTML::TocGenerator::TT_TOKENTYPE_START, $aTag);
		# Process attributes
	$self->_processAttributes($aAttr);
}  # start()


#--- HTML::_TokenTocBeginParser::text() ---------------------------------------
# function: This function is called every time plain text is encountered.
# args:     - @_: array containing data.

sub text {
		# Get arguments
	my ($self, $aText) = @_;
		# Was token already created and is last added token of type 'text'?
	if (
		defined($self->{_lastAddedToken}) && 
		$self->{_lastAddedTokenType} == HTML::TocGenerator::TT_TOKENTYPE_TEXT
	) {
		# Yes, token is already created;
			# Add tag to existing token
		@${$self->{_lastAddedToken}}[HTML::TocGenerator::TT_TAG_BEGIN] .= $aText;
	}
	else {
		# No, token isn't created;
			# Process token
		$self->_processToken(HTML::TocGenerator::TT_TOKENTYPE_TEXT, $aText);
	}
}  # text()




#=== HTML::_TokenTocEndParser =================================================
# function: Parse 'toc tokens'.  'Toc tokens' mark HTML code which is to be
#           inserted into the ToC.
# note:     Used internally.

package HTML::_TokenTocEndParser;


BEGIN {
	use vars qw(@ISA);

	@ISA = qw(HTML::_TokenTocParser);
}


END {}


#--- HTML::_TokenTocEndParser::new() ------------------------------------------
# function: Constructor
# args:     - $aType: Class type.

sub new {
		# Get arguments
	my ($aType) = @_;
		# Create instance
	my $self = $aType->SUPER::new;
		# Reference to last added token
	$self->{_lastAddedToken} = undef;
		# Return instance
	return $self;
}  # new()


#--- HTML::_TokenTocEndParser::_processAttributes() ---------------------------
# function: Process attributes.
# args:     - $aAttributes: Attributes to parse.

sub _processAttributes {
		# Get arguments
	my ($self, $aAttributes) = @_;
		# Local variables
	my (%includeAttributes, %excludeAttributes);

		# Parse attributes
	$self->_parseAttributes(
		$aAttributes, \%includeAttributes, \%excludeAttributes
	);
		# Include attributes are specified?
	if (keys(%includeAttributes) > 0) {
		# Yes, include attributes are specified;
			# Store include attributes
		@${$self->{_Token}}[
			HTML::TocGenerator::TT_INCLUDE_ATTRIBUTES_END
		] = \%includeAttributes;
	}
		# Exclude attributes are specified?
	if (keys(%excludeAttributes) > 0) {
		# Yes, exclude attributes are specified;
			# Store exclude attributes
		@${$self->{_Token}}[
			HTML::TocGenerator::TT_EXCLUDE_ATTRIBUTES_END
		] = \%excludeAttributes;
	}
}  # _processAttributes()


#--- HTML::_TokenTocEndParser::_processToken() --------------------------------
# function: Process token.
# args:     - $aTokenType: Type of token to process.
#           - $aTag: Tag of token.

sub _processToken {
		# Get arguments
	my ($self, $aTokenType, $aTag) = @_;
		# Update token
	@${$self->{_token}}[HTML::TocGenerator::TT_TAG_TYPE_END] = $aTokenType;
	@${$self->{_token}}[HTML::TocGenerator::TT_TAG_END]      = $aTag;
		# Indicate token type which has been processed
	$self->{_lastAddedTokenType} = $aTokenType;
}  # _processToken()


#--- HTML::_TokenTocEndParser::comment() --------------------------------------
# function: Process comment.
# args:     - $aComment: comment text with '<!--' and '-->' tags stripped off.

sub comment {
		# Get arguments
	my ($self, $aComment) = @_;
		# Process token
	$self->_processToken(HTML::TocGenerator::TT_TOKENTYPE_COMMENT, $aComment);
}  # comment()


#--- HTML::_TokenTocDeclarationParser::declaration() --------------------------
# function: This function is called every time a markup declaration is
#           encountered by HTML::Parser.
# args:     - $aDeclaration: Markup declaration.

sub declaration {
		# Get arguments
	my ($self, $aDeclaration) = @_;
		# Process token
	$self->_processToken(
		HTML::TocGenerator::TT_TOKENTYPE_DECLARATION, $aDeclaration
	);
}  # declaration()

	
#--- HTML::_TokenTocEndParser::end() ------------------------------------------
# function: This function is called every time a closing tag is encountered
#           by HTML::Parser.
# args:     - $aTag: tag name (in lower case).

sub end {
		# Get arguments
	my ($self, $aTag, $aOrigText) = @_;
		# Process token
	$self->_processToken(HTML::TocGenerator::TT_TOKENTYPE_END, $aTag);
}  # end()


#--- HTML::_TokenTocEndParser::parse() ----------------------------------------
# function: Parse token.
# args:     - $aString: 'toc token' to parse
#           - $aToken: Reference to token
#           - $aTokenTypeBegin: Type of begin token

sub parse {
		# Get arguments
	my ($self, $aString, $aToken, $aTokenTypeBegin) = @_;
		# Token argument specified?
	if (defined($aToken)) {
		# Yes, token argument is specified;
			# Store token reference
		$self->{_token} = $aToken;
	}
		# End tag defined?
	if (! defined($aString)) {
		# No, end tag isn't defined;
			# Last added tokentype was of type 'start'?
		if (
			(defined($aTokenTypeBegin)) &&
			($aTokenTypeBegin == HTML::TocGenerator::TT_TOKENTYPE_START) 
		) {
			# Yes, last added tokentype was of type 'start';
				# Assume end tag
			$self->_processToken(
				HTML::TocGenerator::TT_TAG_END,
				@${$self->{_token}}[HTML::TocGenerator::TT_TAG_BEGIN]
			);
		}
	}
	else {
			# Call ancestor
		$self->SUPER::parse($aString);
	}
}  # parse()


#--- HTML::_TokenTocEndParser::start() ----------------------------------------
# function: This function is called every time an opening tag is encountered.
# args:     - $aTag: tag name (in lower case).
#           - $aAttr: reference to hash containing all tag attributes (in lower
#                case).
#           - $aAttrSeq: reference to array containing all attribute keys (in 
#                lower case) in the original order
#           - $aOrigText: the original HTML text

sub start {
		# Get arguments
	my ($self, $aTag, $aAttr, $aAttrSeq, $aOrigText) = @_;
		# Process token
	$self->_processToken(HTML::TocGenerator::TT_TOKENTYPE_START, $aTag);
		# Process attributes
	$self->_processAttributes($aAttr);
}  # start()


#--- HTML::_TokenTocEndParser::text() -----------------------------------------
# function: This function is called every time plain text is encountered.
# args:     - @_: array containing data.

sub text {
		# Get arguments
	my ($self, $aText) = @_;

		# Is token already created?
	if (defined($self->{_lastAddedTokenType})) {
		# Yes, token is already created;
			# Add tag to existing token
		@${$self->{_token}}[HTML::TocGenerator::TT_TAG_END] .= $aText;
	}
	else {
		# No, token isn't created;
			# Process token
		$self->_processToken(HTML::TocGenerator::TT_TOKENTYPE_TEXT, $aText);
	}
}  # text()


1;