Skip to content

Commit 2306c73

Browse files
committed
autodoc: Add an Index section
perlapi and perlintern are divided into sections that partition the API elements into rough major categories of related functions. This commit causes an Index to be generated, a mapping from function name to the section it's documented in.
1 parent db77faf commit 2306c73

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed

autodoc.pl

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@
158158
# unlooked at
159159
my %protos;
160160

161+
# For outputting an index of the API elements. Each key is an element; its
162+
# value is the section the element is in.
163+
my %index;
164+
161165
my $described_in = "Described in";
162166

163167
my $description_indent = 4;
@@ -231,6 +235,7 @@
231235

232236
# Kept separate at end
233237
my $undocumented_scn = 'Undocumented elements';
238+
my $index_scn = 'INDEX';
234239

235240
my %valid_sections = (
236241
$AV_scn => {},
@@ -479,10 +484,11 @@ ($file, $line_num = 0)
479484
return "at $file, line $line_num";
480485
}
481486

482-
sub check_and_add_proto_defn {
483-
my ($element, $file, $line_num, $raw_flags, $ret_type, $args_ref,
484-
$definition_type
485-
) = @_;
487+
sub check_and_add_proto_defn($element, $file, $line_num, $destpod, $section,
488+
$raw_flags, $ret_type, $args_ref,
489+
$definition_type)
490+
{
491+
486492

487493
# This function constructs a hash describing what we know so far about the
488494
# API element '$element', as determined by 'apidoc'-type lines scattered
@@ -620,6 +626,16 @@ sub check_and_add_proto_defn {
620626
else {
621627
$elements{$element}{docs_found}{file} = $file;
622628
$elements{$element}{docs_found}{line_num} = $line_num // 0;
629+
$elements{$element}{docs_found}{section} = $section;
630+
631+
if (! $destpod) {
632+
my $this_flags = $elements{$element}{flags}
633+
|| $flags_sans_d;
634+
$destpod = destination_pod($this_flags);
635+
}
636+
637+
$elements{$element}{destpod} = $destpod;
638+
$index{$destpod}{$element} = $section;
623639
}
624640

625641
if ($definition_type == PLAIN_APIDOC) {
@@ -758,7 +774,7 @@ ($file, $line_num, $input, $is_file_C)
758774
EOS
759775
}
760776

761-
sub handle_apidoc_line ($file, $line_num, $type, $arg) {
777+
sub handle_apidoc_line ($file, $line_num, $destpod, $section, $type, $arg) {
762778

763779
# This just does a couple of checks that would otherwise have to be
764780
# duplicated in the calling code, and calls check_and_add_proto_defn() to
@@ -792,10 +808,12 @@ ($file, $line_num, $type, $arg)
792808
# this definition (which has the side effect of cleaning up any NN or
793809
# NULLOK in @args)
794810
my $updated = check_and_add_proto_defn($name, $file, $line_num,
811+
$destpod, $section,
795812

796813
# The fact that we have this line somewhere in the source
797814
# code means we implicitly have the 'd' flag
798815
$flags . "d",
816+
799817
$ret_type, \@args,
800818
$type);
801819

@@ -899,7 +917,8 @@ ($fh, $file)
899917
. " '$arg' " . where_from_string($file, $line_num);
900918
}
901919
elsif ($outer_line_type == APIDOC_DEFN) {
902-
handle_apidoc_line($file, $line_num, $outer_line_type, $arg);
920+
handle_apidoc_line($file, $line_num, $destpod, $section,
921+
$outer_line_type, $arg);
903922
next; # 'handle_apidoc_line' handled everything for this type
904923
}
905924
elsif ($outer_line_type == APIDOC_SECTION) {
@@ -920,9 +939,10 @@ ($fh, $file)
920939
# Drop down to accumulate the heading text for this section.
921940
}
922941
elsif ($outer_line_type == PLAIN_APIDOC) {
923-
my $leader_ref =
924-
handle_apidoc_line($file, $line_num, $outer_line_type, $arg);
925-
$destpod = destination_pod($$leader_ref->{flags});
942+
my $leader_ref = handle_apidoc_line($file, $line_num,
943+
undef, $section,
944+
$outer_line_type, $arg);
945+
$destpod = $$leader_ref->{destpod};
926946

927947
push @items, $leader_ref;
928948

@@ -944,8 +964,9 @@ ($fh, $file)
944964
# separating 'apidoc_item' lines
945965
$text = "";
946966

947-
my $item_ref =
948-
handle_apidoc_line($file, $line_num, $item_line_type, $arg);
967+
my $item_ref = handle_apidoc_line($file, $line_num,
968+
$destpod, $section,
969+
$item_line_type, $arg);
949970

950971
push @items, $item_ref;
951972
}
@@ -1563,6 +1584,7 @@ sub parse_config_h {
15631584
$flags .= 'U' unless defined $configs{$name}{usage};
15641585
my $data = check_and_add_proto_defn($name, $config_h,
15651586
$configs{$name}{defn_line_num},
1587+
$api, $section,
15661588
$flags,
15671589
"void", # No return type
15681590
[],
@@ -2336,6 +2358,24 @@ ($destpod)
23362358
print $fh construct_missings_section($hdr, $ref);
23372359
}
23382360

2361+
print $fh <<~EOT;
2362+
2363+
=head1 $index_scn
2364+
2365+
Below is an alphabetical list of all API elements in this file,
2366+
cross referenced to the section in which they are documented.
2367+
2368+
EOT
2369+
2370+
my %this_index = $index{$podname}->%*;
2371+
my $space = "E<nbsp>" x 4;
2372+
2373+
print $fh "=over 1\n\n";
2374+
foreach my $element (sort dictionary_order keys %this_index) {
2375+
print $fh "=item L</$element>${space}L</$this_index{$element}>\n\n";
2376+
}
2377+
print $fh "=back\n\n";
2378+
23392379
print $fh "\n$destpod->{footer}\n=cut\n";
23402380

23412381
read_only_bottom_close_and_rename($fh);
@@ -2350,7 +2390,10 @@ ($destpod)
23502390
@{$embed}{qw(flags return_type name args)};
23512391
check_and_add_proto_defn($func, $file,
23522392
# embed.fnc data doesn't currently furnish the
2353-
# line number
2393+
# line number; and we have no section in the
2394+
# documente yet
2395+
undef,
2396+
undef,
23542397
undef,
23552398

23562399
$flags, $ret_type, $args,
@@ -2593,7 +2636,8 @@ ($destpod)
25932636

25942637
my $section_list = join "\n\n", map { "=item L</$_>" }
25952638
sort(dictionary_order keys %valid_sections),
2596-
$undocumented_scn; # Keep last
2639+
$undocumented_scn, # Keep next-to-last
2640+
$index_scn; # Keep last
25972641

25982642
# Leading '|' is to hide these lines from pod checkers. khw is unsure if this
25992643
# is still needed.
@@ -2613,9 +2657,9 @@ ($destpod)
26132657
|L<perlintern> and F<$config_h>, some items are listed here as being actually
26142658
|documented in another pod.
26152659
|
2616-
|L<At the end|/$undocumented_scn> is a list of functions which have yet
2617-
|to be documented. Patches welcome! The interfaces of these are subject to
2618-
|change without notice.
2660+
|L<Just before the index|/$undocumented_scn> is a list of functions which have
2661+
|yet to be documented. Patches welcome! The interfaces of these are subject
2662+
|to change without notice.
26192663
|
26202664
|Some of the functions documented here are consolidated so that a single entry
26212665
|serves for multiple functions which all do basically the same thing, but have

0 commit comments

Comments
 (0)