-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGenToc.pm
More file actions
1926 lines (1579 loc) · 46.3 KB
/
GenToc.pm
File metadata and controls
1926 lines (1579 loc) · 46.3 KB
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
package HTML::GenToc;
use strict;
=head1 NAME
HTML::GenToc - Generate a Table of Contents for HTML documents.
=head1 SYNOPSIS
use HTML::GenToc;
# create a new object
my $toc = new HTML::GenToc();
my $toc = new HTML::GenToc(title=>"Table of Contents",
toc_entry=>{
H1=>1,
H2=>2
},
toc_end=>{
H1=>'/H1',
H2=>'/H2'
}
);
# generate a ToC from a file
$toc->generate_toc(input=>$html_file,
footer=>$footer_file,
header=>$header_file
);
=head1 DESCRIPTION
HTML::GenToc generates anchors and a table of contents for
HTML documents. Depending on the arguments, it will insert
the information it generates, or output to a string, a separate file
or STDOUT.
While it defaults to taking H1 and H2 elements as the significant
elements to put into the table of contents, any tag can be defined
as a significant element. Also, it doesn't matter if the input
HTML code is complete, pure HTML, one can input pseudo-html
or page-fragments, which makes it suitable for using on templates
and HTML meta-languages such as WML.
Also included in the distrubution is hypertoc, a script which uses the
module so that one can process files on the command-line in a
user-friendly manner.
=head1 DETAILS
The ToC generated is a multi-level level list containing links to the
significant elements. HTML::GenToc inserts the links into the ToC to
significant elements at a level specified by the user.
B<Example:>
If H1s are specified as level 1, than they appear in the first
level list of the ToC. If H2s are specified as a level 2, than
they appear in a second level list in the ToC.
Information on the significant elements and what level they should occur
are passed in to the methods used by this object, or one can use the
defaults.
There are two phases to the ToC generation. The first phase is to
put suitable anchors into the HTML documents, and the second phase
is to generate the ToC from HTML documents which have anchors
in them for the ToC to link to.
For more information on controlling the contents of the created ToC, see
L</Formatting the ToC>.
HTML::GenToc also supports the ability to incorporate the ToC into the HTML
document itself via the B<inline> option. See L</Inlining the ToC> for more
information.
In order for HTML::GenToc to support linking to significant elements,
HTML::GenToc inserts anchors into the significant elements. One can
use HTML::GenToc as a filter, outputing the result to another file,
or one can overwrite the original file, with the original backed
up with a suffix (default: "org") appended to the filename.
One can also output the result to a string.
=head1 METHODS
Default arguments can be set when the object is created, and overridden
by setting arguments when the generate_toc method is called.
Arguments are given as a hash of arguments.
=cut
use Data::Dumper;
use HTML::SimpleParse;
use HTML::Entities;
use HTML::LinkList;
#################################################################
#---------------------------------------------------------------#
# Object interface
#---------------------------------------------------------------#
=head2 Method -- new
$toc = new HTML::GenToc();
$toc = new HTML::GenToc(toc_entry=>\%my_toc_entry,
toc_end=>\%my_toc_end,
bak=>'bak',
...
);
Creates a new HTML::GenToc object.
These arguments will be used as defaults in invocations of other methods.
See L<generate_tod> for possible arguments.
=cut
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant; # Object or class name
my $self = {
debug => 0,
bak => 'org',
entrysep => ', ',
footer => '',
inline => 0,
header => '',
input => '',
notoc_match => 'class="notoc"',
ol => 0,
ol_num_levels => 1,
overwrite => 0,
outfile => '-',
quiet => 0,
textonly => 0,
title => 'Table of Contents',
toclabel => '<h1>Table of Contents</h1>',
toc_tag => '^BODY',
toc_tag_replace => 0,
toc_only => 0,
# define TOC entry elements
toc_entry => {
'H1'=>1,
'H2'=>2,
},
# TOC entry element terminators
toc_end => {
'H1'=>'/H1',
'H2'=>'/H2',
},
useorg => 0,
@_
};
# bless self
bless($self, $class);
if ($self->{debug})
{
print STDERR Dumper($self);
}
return $self;
} # new
=head2 generate_toc
$toc->generate_toc(outfile=>"index2.html");
my $result_str = $toc->generate_toc(to_string=>1);
Generates a table of contents for the significant elements in the HTML
documents, optionally generating anchors for them first.
B<Options>
=over
=item bak
bak => I<string>
If the input file/files is/are being overwritten (B<overwrite> is on), copy
the original file to "I<filename>.I<string>". If the value is empty, B<no>
backup file will be created.
(default:org)
=item debug
debug => 1
Enable verbose debugging output. Used for debugging this module;
in other words, don't bother.
(default:off)
=item entrysep
entrysep => I<string>
Separator string for non-<li> item entries
(default: ", ")
=item filenames
filenames => \@filenames
The filenames to use when creating table-of-contents links.
This overrides the filenames given in the B<input> option,
and is expected to have exactly the same number of elements.
This can also be used when passing in string-content to the B<input>
option, to give a (fake) filename to use for the links relating
to that content.
=item footer
footer => I<file_or_string>
Either the filename of the file containing footer text for ToC;
or a string containing the footer text.
=item header
header => I<file_or_string>
Either the filename of the file containing header text for ToC;
or a string containing the header text.
=item ignore_only_one
ignore_only_one => 1
If there would be only one item in the ToC, don't make a ToC.
=item ignore_sole_first
ignore_sole_first => 1
If the first item in the ToC is of the highest level,
AND it is the only one of that level, ignore it.
This is useful in web-pages where there is only one H1 header
but one doesn't know beforehand whether there will be only one.
=item inline
inline => 1
Put ToC in document at a given point.
See L</Inlining the ToC> for more information.
=item input
input => \@filenames
input => $content
This is expected to be either a reference to an array of filenames,
or a string containing content to process.
The three main uses would be:
=over
=item (a)
you have more than one file to process, so pass in multiple filenames
=item (b)
you have one file to process, so pass in its filename as the only array item
=item (c)
you have HTML content to process, so pass in just the content as a string
=back
(default:undefined)
=item notoc_match
notoc_match => I<string>
If there are certain individual tags you don't wish to include in the
table of contents, even though they match the "significant elements",
then if this pattern matches contents inside the tag (not the body),
then that tag will not be included, either in generating anchors nor in
generating the ToC. (default: C<class="notoc">)
=item ol
ol => 1
Use an ordered list for level 1 ToC entries.
=item ol_num_levels
ol_num_levels => 2
The number of levels deep the OL listing will go if B<ol> is true.
If set to zero, will use an ordered list for all levels.
(default:1)
=item overwrite
overwrite => 1
Overwrite the input file with the output.
(default:off)
=item outfile
outfile => I<file>
File to write the output to. This is where the modified HTML
output goes to. Note that it doesn't make sense to use this option if you
are processing more than one file. If you give '-' as the filename, then
output will go to STDOUT.
(default: STDOUT)
=item quiet
quiet => 1
Suppress informative messages. (default: off)
=item textonly
textonly => 1
Use only text content in significant elements.
=item title
title => I<string>
Title for ToC page (if not using B<header> or B<inline> or B<toc_only>)
(default: "Table of Contents")
=item toc_after
toc_after => \%toc_after_data
%toc_after_data = { I<tag1> => I<suffix1>,
I<tag2> => I<suffix2>
};
toc_after => { H2=>'</em>' }
For defining layout of significant elements in the ToC.
This expects a reference to a hash of
tag=>suffix pairs.
The I<tag> is the HTML tag which marks the start of the element. The
I<suffix> is what is required to be appended to the Table of Contents
entry generated for that tag.
(default: undefined)
=item toc_before
toc_before => \%toc_before_data
%toc_before_data = { I<tag1> => I<prefix1>,
I<tag2> => I<prefix2>
};
toc_before=>{ H2=>'<em>' }
For defining the layout of significant elements in the ToC. The I<tag>
is the HTML tag which marks the start of the element. The I<prefix> is
what is required to be prepended to the Table of Contents entry
generated for that tag.
(default: undefined)
=item toc_end
toc_end => \%toc_end_data
%toc_end_data = { I<tag1> => I<endtag1>,
I<tag2> => I<endtag2>
};
toc_end => { H1 => '/H1', H2 => '/H2' }
For defining significant elements. The I<tag> is the HTML tag which
marks the start of the element. The I<endtag> the HTML tag which marks
the end of the element. When matching in the input file, case is
ignored (but make sure that all your I<tag> options referring to the
same tag are exactly the same!).
=item toc_entry
toc_entry => \%toc_entry_data
%toc_entry_data = { I<tag1> => I<level1>,
I<tag2> => I<level2>
};
toc_entry => { H1 => 1, H2 => 2 }
For defining significant elements. The I<tag> is the HTML tag which marks
the start of the element. The I<level> is what level the tag is considered
to be. The value of I<level> must be numeric, and non-zero. If the value
is negative, consective entries represented by the significant_element will
be separated by the value set by B<entrysep> option.
=item toclabel
toclabel => I<string>
HTML text that labels the ToC. Always used.
(default: "<h1>Table of Contents</h1>")
=item toc_tag
toc_tag => I<string>
If a ToC is to be included inline, this is the pattern which is used to
match the tag where the ToC should be put. This can be a start-tag, an
end-tag or a comment, but the E<lt> should be left out; that is, if you
want the ToC to be placed after the BODY tag, then give "BODY". If you
want a special comment tag to make where the ToC should go, then include
the comment marks, for example: "!--toc--" (default:BODY)
=item toc_tag_replace
toc_tag_replace => 1
In conjunction with B<toc_tag>, this is a flag to say whether the given tag
should be replaced, or if the ToC should be put after the tag.
This can be useful if your toc_tag is a comment and you don't need it
after you have the ToC in place.
(default:false)
=item toc_only
toc_only => 1
Output only the Table of Contents, that is, the Table of Contents plus
the toclabel. If there is a B<header> or a B<footer>, these will also be
output.
If B<toc_only> is false then if there is no B<header>, and B<inline> is
not true, then a suitable HTML page header will be output, and if there
is no B<footer> and B<inline> is not true, then a HTML page footer will
be output.
(default:false)
=item to_string
to_string => 1
Return the modified HTML output as a string. This I<does> override
other methods of output (unlike version 3.00). If I<to_string> is false,
the method will return 1 rather than a string.
=item use_id
use_id => 1
Use <a id="I<name>"/> for anchors rather than <a name="I<name>"/>.
However if an anchor already exists for a Significant Element, this
won't make an id for that particular element.
=item useorg
useorg => 1
Use pre-existing backup files as the input source; that is, files of the
form I<infile>.I<bak> (see B<input> and B<bak>).
=back
=cut
sub generate_toc ($%) {
my $self = shift;
my %args = (
make_anchors=>1,
make_toc=>1,
input=>undef,
filenames=>undef,
bak=>$self->{bak},
debug=>$self->{debug},
useorg=>$self->{useorg},
use_id=>$self->{use_id},
notoc_match=>$self->{notoc_match},
toc_entry=>$self->{toc_entry},
toc_end=>$self->{toc_end},
overwrite=>$self->{overwrite},
ol=>$self->{ol},
ol_num_levels=>$self->{ol_num_levels},
entrysep=>$self->{entrysep},
ignore_only_one=>$self->{ignore_only_one},
@_
);
if ($args{debug})
{
print STDERR Dumper(\%args);
}
if (!$args{input})
{
warn "generate_toc: no input given\n";
return '';
}
#
# get the input
#
my @filenames = ();
my @input = ();
if (ref $args{input} eq "ARRAY")
{
@filenames = @{$args{input}};
my $i = 0;
my $fh_needs_closing = 0;
foreach my $fn (@filenames)
{
my $infn = $fn;
my $bakfile = $fn . "." . $args{bak};
if ($args{useorg}
&& $args{bak}
&& -e $bakfile)
{
# use the old backup files as source
$infn = $bakfile;
}
my $fh = undef;
# using '-' means STDIN
if ($infn eq '-')
{
$fh = *STDIN;
$fh_needs_closing = 0;
}
else
{
open ($fh, $infn) ||
die "Error: unable to open ", $infn, ": $!\n";
$fh_needs_closing = 1;
}
my $content = '';
{
local $/; # slurp entire file
$content = <$fh>;
close ($fh) if ($fh_needs_closing);
}
$input[$i] = $content;
$i++;
}
}
else
{
$filenames[0] = '';
$input[0] = $args{input};
}
# overwrite the filenames array if a replacement
# was passed in and has the same length
if (defined $args{filenames}
&& @{$args{filenames}}
&& $#{$args{filenames}} == $#{filenames}
)
{
@filenames = @{$args{filenames}};
}
#
# make the anchors
#
if ($args{make_anchors})
{
my $i = 0;
foreach my $fn (@filenames)
{
my $html_str = $input[$i];
$input[$i] = $self->make_anchors(%args,
filename=>$fn,
input=>$html_str);
$i++;
}
}
#
# make the ToC
#
my $toc_str = '';
if ($args{make_toc})
{
my %labels = ();
my @list_of_lists = ();
my $i = 0;
for (my $i = 0; $i < @filenames; $i++)
{
my @the_list = $self->make_toc_list(%args,
first_file=>$filenames[0],
labels=>\%labels,
filename=>$filenames[$i],
input=>$input[$i]);
if (!($args{ignore_only_one}
and @the_list <= 1))
{
push @list_of_lists, @the_list;
}
}
if (@list_of_lists > 0)
{
#
# create the appropriate format
#
my %formats = ();
# check for non-list entries, flagged by negative levels
while (my ($key, $val) = each %{$args{toc_entry}})
{
if ($val < 0)
{
$formats{abs($val) - 1} = {};
$formats{abs($val) - 1}->{tree_head} = '<ul><li>';
$formats{abs($val) - 1}->{tree_foot} = "\n</li></ul>\n";
$formats{abs($val) - 1}->{item_sep} = $args{entrysep};
$formats{abs($val) - 1}->{pre_item} = '';
$formats{abs($val) - 1}->{post_item} = '';
}
}
# check for OL
if ($args{ol})
{
$formats{0} = {};
$formats{0}->{tree_head} = '<ol>';
$formats{0}->{tree_foot} = "\n</ol>";
if ($args{ol_num_levels} > 0)
{
$formats{$args{ol_num_levels}} = {};
$formats{$args{ol_num_levels}}->{tree_head} = '<ul>';
$formats{$args{ol_num_levels}}->{tree_foot} = "\n</ul>";
}
}
$toc_str = HTML::LinkList::link_tree(
%args,
link_tree=>\@list_of_lists,
labels=>\%labels,
formats=>\%formats,
);
}
}
#
# put the output
#
my $ret = $self->output_toc(
%args,
toc=>$toc_str,
input=>\@input,
filenames=>\@filenames,
);
return $ret;
} # generate_toc
=head1 INTERNAL METHODS
These methods are documented for developer purposes and aren't intended
to be used externally.
=head2 make_anchor_name
$toc->make_anchor_name(content=>$content,
anchors=>\%anchors);
Makes the anchor-name for one anchor.
Bases the anchor on the content of the significant element.
Ensures that anchors are unique.
=cut
sub make_anchor_name ($%) {
my $self = shift;
my %args = (
content=>'', # will be overwritten by one of @_
anchors=>undef,
@_
);
my $name = $args{content}; # the anchor name will most often be very close to the token content
if ($name !~ /^\s*$/) {
# generate a SEO-friendly anchor right from the token content
# The allowed character set is limited first by the URI specification
# for fragments, http://tools.ietf.org/html/rfc3986#section-2:
# characters then by the limitations of the values of 'id' and 'name'
# attributes: http://www.w3.org/TR/REC-html40/types.html#type-name
# Eventually, the only punctuation allowed in id values is [_.:-]
# we need to replace [#&;] only when they are NOT part of an HTML
# entity. decode_entities saves us from crafting a nasty regexp
decode_entities($name);
# MediaWiki also uses the period, see
# http://en.wikipedia.org/wiki/Hierarchies#Ethics.2C_behavioral_psychology.2C_philosophies_of_identity
$name =~ s/([^\s\w_.:-])/'.'.sprintf('%02X', ord($1))/eg;
$name =~ s/\s+/_/g;
# "ID and NAME tokens must begin with a letter ([A-Za-z])"
$name =~ s/^[^a-zA-Z]+//;
}
else
{
$name = 'id';
}
$name = 'id' if $name eq '';
# check if it already exists; if so, add a number
my $anch_num = 1;
my $word_name = $name;
my $name_key = lc $name;
# Reference: http://www.w3.org/TR/REC-html40/struct/links.html#h-12.2.1
# Anchor names must be unique within a document. Anchor names that differ
# only in case may not appear in the same document.
while (defined $args{anchors}->{$name_key}
&& $args{anchors}->{$name_key})
{
$name = $word_name . "_$anch_num";
$name_key = lc $name;
$anch_num++;
}
return $name;
} # make_anchor_name
=head2 make_anchors
my $new_html = $toc->make_anchors(input=>$html,
notoc_match=>$notoc_match,
use_id=>$use_id,
toc_entry=>\%toc_entries,
toc_end=>\%toc_ends,
);
Makes the anchors the given input string.
Returns a string.
=cut
sub make_anchors ($%) {
my $self = shift;
my %args = (
input=>'',
notoc_match=>$self->{notoc_match},
use_id=>$self->{use_id},
toc_entry=>$self->{toc_entry},
toc_end=>$self->{toc_end},
debug=>$self->{debug},
quiet=>$self->{quiet},
@_
);
my $html_str = $args{input};
print STDERR "Making anchors for ", $args{filename}, "...\n"
if (!$args{quiet} && $args{filename});
my @newhtml = ();
my %anchors = ();
# Note that the keys to the anchors hash should be lower-cased
# since anchor names that differ only in case are not allowed.
# parse the HTML
my $hp = new HTML::SimpleParse();
$hp->text($html_str);
$hp->parse();
my $tag;
my $endtag;
my $level = 0;
my $tmp;
my $adone = 0;
my $name = '';
# go through the HTML
my $tok;
my $next_tok;
my $i;
my $notoc = $args{notoc_match};
my @tree = $hp->tree();
while (@tree) {
$tok = shift @tree;
$next_tok = $tree[0];
if ($tok->{type} ne 'starttag')
{
push @newhtml, $hp->execute($tok);
next;
}
# assert: we have a start tag
$level = 0;
# check if tag included in TOC (significant element)
foreach my $key (keys %{$args{toc_entry}}) {
if ($tok->{content} =~ /^$key/i
&& (!$notoc
|| $tok->{content} !~ /$notoc/)) {
$tag = $key;
# level of significant element
$level = abs($args{toc_entry}->{$key});
# End tag of significant element
$endtag = $args{toc_end}->{$key};
last;
}
}
# if $level is not set, we didn't find a Significant tag
if (!$level) {
push @newhtml, $hp->execute($tok);
next;
}
# assert: current tag is a Significant tag
#
# Add A element or ID to document
#
my $name_in_anchor = 0;
$adone = 0;
$name = '';
my $sig_tok = $tok;
if ($tag =~ /title/i) { # TITLE tag is a special case
$adone = 1;
}
if ($args{use_id})
{
# is there an existing ID?
if ($sig_tok->{content} =~ /ID\s*=\s*(['"])/i) {
my $q = $1;
($name) = $sig_tok->{content} =~ m/ID\s*=\s*$q([^$q]*)$q/i;
if ($name)
{
$anchors{lc $name} = $name;
push @newhtml, $hp->execute($sig_tok);
$adone = 1;
}
else # if the ID has no name, remove it!
{
$sig_tok->{content} =~ s/ID\s*=\s*$q$q//i;
}
}
}
else # not adding ID, move right along
{
push @newhtml, $hp->execute($tok);
}
# Find the "name" of the significant element
# Don't consume the tree, because ID behaves differently from A
my $i = 0;
while (!$name && $i < @tree)
{
$tok = $tree[$i];
$next_tok = $tree[$i + 1];
if ($tok->{type} eq 'text') {
$name = $self->make_anchor_name(content=>$tok->{content},
anchors=>\%anchors);
# Anchor
} elsif (!$adone && $tok->{type} eq 'starttag'
&& $tok->{content} =~ /^A/i)
{
if ($tok->{content} =~ /NAME\s*=\s*(['"])/i) {
my $q = $1;
($name) = $tok->{content} =~ m/NAME\s*=\s*$q([^$q]*)$q/i;
$name_in_anchor = 1;
} elsif ($next_tok->{type} eq 'text') {
$name = $self->make_anchor_name(content=>$next_tok->{content},
anchors=>\%anchors);
}
} elsif ($tok->{type} eq 'starttag'
|| $tok->{type} eq 'endtag')
{ # Tag
last if $tok->{content} =~ m|$endtag|i;
}
$i++;
}
# assert: there is a name, or there is no name to be found
if (!$name)
{
# make up a name
$name = $self->make_anchor_name(content=>"TOC",
anchors=>\%anchors);
}
if (!$adone && $args{use_id})
{
if (!$name_in_anchor)
{
$anchors{lc $name} = $name;
# add the ID
$sig_tok->{content} .= " id='$name'";
push @newhtml, $hp->execute($sig_tok);
$adone = 1;
}
else
{
# we have an already-named anchor, so don't add an ID
push @newhtml, $hp->execute($sig_tok);
}
}
while (@tree) {
$tok = shift @tree;
$next_tok = $tree[0];
# Text
if ($tok->{type} eq 'text') {
if (!$adone && $tok->{content} !~ /^\s*$/) {
$anchors{lc $name} = $name;
# replace the text with an anchor containing the text
push(@newhtml, qq|<a name="$name">$tok->{content}</a>|);
$adone = 1;
} else {
push @newhtml, $hp->execute($tok);
}
# Anchor
} elsif (!$adone && $tok->{type} eq 'starttag'
&& $tok->{content} =~ /^A/i)
{
# is there an existing NAME anchor?
if ($name_in_anchor) {
$anchors{lc $name} = $name;
push @newhtml, $hp->execute($tok);
} else {
# add the current name anchor
$tmp = $hp->execute($tok);
$tmp =~ s/^(<A)(.*)$/$1 name="$name" $2/i;
push @newhtml, $tmp;
$anchors{lc $name} = $name;
}
$adone = 1;
} elsif ($tok->{type} eq 'starttag'
|| $tok->{type} eq 'endtag')
{ # Tag
push @newhtml, $hp->execute($tok);
last if $tok->{content} =~ m|$endtag|i;
}
else {
push @newhtml, $hp->execute($tok);
}
}
}
my $out = join('', @newhtml);
return $out;
} # make_anchors
=head2 make_toc_list
my @toc_list = $toc->make_toc_list(input=>$html,
labels=>\%labels,
notoc_match=>$notoc_match,
toc_entry=>\%toc_entry,
toc_end=>\%toc_end,
filename=>$filename);
Makes a list of lists which represents the structure and content
of (a portion of) the ToC from one file.
Also updates a list of labels for the ToC entries.
=cut
sub make_toc_list ($%) {
my $self = shift;
my %args = (
input=>'',
filename=>'',
labels=>undef,
notoc_match=>$self->{notoc_match},
toc_entry=>$self->{toc_entry},
toc_end=>$self->{toc_end},
inline=>$self->{inline},
debug=>$self->{debug},
toc_before=>$self->{toc_before},
toc_after=>$self->{toc_after},
textonly=>$self->{textonly},
ignore_sole_first=>$self->{ignore_sole_first},
ignore_only_one=>$self->{ignore_only_one},
@_
);
my $html_str = $args{input};
my $infile = $args{filename};
my $labels = $args{labels};
my $toc_str = "";
my @toc = ();
my @list_of_paths = ();
my %level_count = ();
# parse the HTML
my $hp = new HTML::SimpleParse();
$hp->text($html_str);
$hp->parse();
my $noli;
my $prevnoli;
my $before = "";