Skip to content

Commit 56df3af

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 94fa8b6 commit 56df3af

File tree

1 file changed

+101
-16
lines changed

1 file changed

+101
-16
lines changed

autodoc.pl

+101-16
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,65 @@ ($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+
# Find the longest element name
2371+
my %this_index = $index{$podname}->%*;
2372+
my $max_element_width = 0;
2373+
2374+
foreach my $element (keys %this_index) {
2375+
$max_element_width = length $element
2376+
if length $element > $max_element_width;
2377+
}
2378+
2379+
my $space = " ";
2380+
my $column2_indent = $max_element_width + 1 + 1; # 1 space between; +1
2381+
# verbatim indent
2382+
my $column2_width = $max_width - $column2_indent;
2383+
2384+
my %column2_text; # Maps section names to their formatted texts, so only
2385+
# have to calculate once
2386+
2387+
foreach my $element (sort dictionary_order keys %this_index) {
2388+
print $fh " ", $element, " " x ($column2_indent - length($element) - 1);
2389+
my $section_name = $this_index{$element};
2390+
2391+
# Calculate the formatted 2nd column if haven't already done so.
2392+
if (! $column2_text{$section_name}) {
2393+
my $remaining = $section_name;
2394+
2395+
# Split into as many rows as necessary so each fits in the
2396+
# available space
2397+
while (length $remaining > $column2_width) {
2398+
2399+
# Find the rightmost space in the portion that fits
2400+
my $split_pos = rindex $remaining, " ", $column2_width;
2401+
2402+
# And split off a row there.
2403+
my $this_row = substr $remaining, 0, $split_pos;
2404+
$remaining = substr $remaining, $split_pos + 1;
2405+
2406+
# Add this row to the formatted text, and prepare for
2407+
# indenting the next line
2408+
$column2_text{$section_name} .= $this_row
2409+
. "\n"
2410+
. " " x $column2_indent;
2411+
}
2412+
2413+
# The remaining fits in the final row.
2414+
$column2_text{$section_name} .= $remaining . "\n";
2415+
}
2416+
2417+
print $fh $column2_text{$section_name};
2418+
}
2419+
23392420
print $fh "\n$destpod->{footer}\n=cut\n";
23402421

23412422
read_only_bottom_close_and_rename($fh);
@@ -2350,7 +2431,10 @@ ($destpod)
23502431
@{$embed}{qw(flags return_type name args)};
23512432
check_and_add_proto_defn($func, $file,
23522433
# embed.fnc data doesn't currently furnish the
2353-
# line number
2434+
# line number; and we have no section in the
2435+
# documente yet
2436+
undef,
2437+
undef,
23542438
undef,
23552439

23562440
$flags, $ret_type, $args,
@@ -2593,7 +2677,8 @@ ($destpod)
25932677

25942678
my $section_list = join "\n\n", map { "=item L</$_>" }
25952679
sort(dictionary_order keys %valid_sections),
2596-
$undocumented_scn; # Keep last
2680+
$undocumented_scn, # Keep next-to-last
2681+
$index_scn; # Keep last
25972682

25982683
# Leading '|' is to hide these lines from pod checkers. khw is unsure if this
25992684
# is still needed.
@@ -2613,9 +2698,9 @@ ($destpod)
26132698
|L<perlintern> and F<$config_h>, some items are listed here as being actually
26142699
|documented in another pod.
26152700
|
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.
2701+
|L<Just before the index|/$undocumented_scn> is a list of functions which have
2702+
|yet to be documented. Patches welcome! The interfaces of these are subject
2703+
|to change without notice.
26192704
|
26202705
|Some of the functions documented here are consolidated so that a single entry
26212706
|serves for multiple functions which all do basically the same thing, but have

0 commit comments

Comments
 (0)