-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapidocs.perl
268 lines (209 loc) · 5.74 KB
/
apidocs.perl
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
#!perl -w
use strict;
use ExtUtils::Manifest 'maniread';
my $outname = shift || '-';
my @funcs = make_func_list();
my %funcs = map { $_ => 1 } @funcs;
# look for files to parse
my $mani = maniread;
my @files = sort grep /\.(c|im|h)$/, keys %$mani;
# scan each file for =item <func>\b
my $func;
my $start;
my %alldocs;
my @funcdocs;
my %from;
my $category;
my %funccats;
my %cats;
my $synopsis = '';
my %funcsyns;
my $order;
my %order;
for my $file (@files) {
open SRC, "< $file"
or die "Cannot open $file for documentation: $!\n";
while (<SRC>) {
if (/^=item (\w+)\b/ && $funcs{$1}) {
$func = $1;
$start = $.;
@funcdocs = $_;
}
elsif ($func && /^=(cut|head)/) {
if ($funcs{$func}) { # only save the API functions
$alldocs{$func} = [ @funcdocs ];
$from{$func} = "File $file";
if ($category) {
$funccats{$func} = $category;
push @{$cats{$category}}, $func;
}
if ($synopsis) {
$funcsyns{$func} = $synopsis;
}
defined $order or $order = 50;
$order{$func} = $order;
}
undef $func;
undef $category;
undef $order;
$synopsis = '';
}
elsif ($func) {
if (/^=category (.*)/) {
$category = $1;
}
elsif (/^=synopsis (.*)/) {
unless (length $synopsis) {
push @funcdocs, "\n";
}
$synopsis .= "$1\n";
push @funcdocs, " $1\n";
}
elsif (/^=order (.*)$/) {
$order = $1;
$order =~ /^\d+$/
or die "=order must specify a number for $func in $file\n";
}
else {
push @funcdocs, $_;
}
}
}
$func and
die "Documentation for $func not followed by =cut or =head in $file\n";
close SRC;
}
open OUT, "> $outname"
or die "Cannot open $outname: $!";
# I keep this file in git and as part of the dist, make sure newlines
# don't mess me up
binmode OUT;
print OUT <<'EOS';
Do not edit this file, it is generated automatically by apidocs.perl
from Imager's source files.
Each function description has a comment listing the source file where
you can find the documentation.
=head1 NAME
Imager::APIRef - Imager's C API - reference.
=head1 SYNOPSIS
i_color color;
color.rgba.r = 255; color.rgba.g = 0; color.rgba.b = 255;
double x[] = { ... };
double y[] = { ... };
i_polygon_t poly;
poly.count = sizeof(x) / sizeof(*x);
poly.x = x;
poly.y = y;
EOS
for my $cat (sort { lc $a cmp lc $b } keys %cats) {
print OUT "\n # $cat\n";
my @funcs = @{$cats{$cat}};
my %orig;
@orig{@funcs} = 0 .. $#funcs;
@funcs = sort { $order{$a} <=> $order{$b} || $orig{$a} <=> $orig{$b} } @funcs;
for my $func (grep $funcsyns{$_}, @funcs) {
my $syn = $funcsyns{$func};
$syn =~ s/^/ /gm;
print OUT $syn;
}
}
print OUT <<'EOS';
=head1 DESCRIPTION
EOS
my %undoc = %funcs;
for my $cat (sort { lc $a cmp lc $b } keys %cats) {
print OUT "=head2 $cat\n\n=over\n\n";
my @ordered_funcs = sort {
$order{$a} <=> $order{$b}
|| lc $a cmp lc $b
} @{$cats{$cat}};
for my $func (@ordered_funcs) {
print OUT @{$alldocs{$func}}, "\n";
print OUT "=for comment\nFrom: $from{$func}\n\n";
delete $undoc{$func};
}
print OUT "\n=back\n\n";
}
# see if we have an uncategorised section
if (grep $alldocs{$_}, keys %undoc) {
print OUT "=head2 Uncategorized functions\n\n=over\n\n";
#print join(",", grep !exists $order{$_}, @funcs), "\n";
for my $func (sort { $order{$a} <=> $order{$b} || $a cmp $b }
grep $undoc{$_} && $alldocs{$_}, @funcs) {
print OUT @{$alldocs{$func}}, "\n";
print OUT "=for comment\nFrom: $from{$func}\n\n";
delete $undoc{$func};
}
print OUT "\n\n=back\n\n";
}
if (keys %undoc) {
print OUT <<'EOS';
=head1 UNDOCUMENTED
The following API functions are undocumented so far, hopefully this
will change:
=over
EOS
print OUT "=item *\n\nB<$_>\n\n" for sort keys %undoc;
print OUT "\n\n=back\n\n";
}
print OUT <<'EOS';
=head1 AUTHOR
Tony Cook <tonyc@cpan.org>
=head1 SEE ALSO
Imager, Imager::API, Imager::ExtUtils, Imager::Inline
=cut
EOS
close OUT;
sub make_func_list {
my @funcs =
qw(i_img i_color i_fcolor i_fill_t mm_log mm_log i_color_model_t
im_context_t i_img_dim i_img_dim_u im_slot_t
i_polygon_t i_poly_fill_mode_t i_mutex_t
i_img_has_alpha i_DF i_DFc i_DFp i_DFcp i_psamp_bits i_gsamp_bits
i_psamp i_psampf);
open FUNCS, "< imexttypes.h"
or die "Cannot open imexttypes.h: $!\n";
my $in_struct;
while (<FUNCS>) {
/^typedef struct/ && ++$in_struct;
if ($in_struct && !/SKIP/ && /\(\*f_(i[om]?_\w+)/) {
my $name = $1;
$name =~ s/_imp$//;
push @funcs, $name;
}
if (/^\} im_ext_funcs;$/) {
$in_struct
or die "Found end of functions structure but not the start";
close FUNCS;
return @funcs;
}
}
if ($in_struct) {
die "Found start of the functions structure but not the end\n";
}
else {
die "Found neither the start nor end of the functions structure\n";
}
}
=head1 NAME
apidocs.perl - parse Imager's source for POD documenting the C API
=head1 SYNOPSIS
perl apidocs.perl lib/Imager/APIRef.pod
=head1 DESCRIPTION
Parses Imager's C sources, including .c, .h and .im files searching
for function documentation.
Besides the normal POD markup, the following can be included:
=over
=item =category I<category-name>
The category the function should be in.
=item =synopsis I<sample-code>
Sample code using the function to include in the Imager::APIRef SYNOPSIS
=item =order I<integer>
Allows a function to be listed out of order. If this isn't specified
it defaults to 50, so a value of 10 will cause the function to be
listed at the beginning of its category, or 90 to list at the end.
Functions with equal order are otherwise ordered by name.
=back
=head1 AUTHOR
Tony Cook <tonyc@cpan.org>
=cut