-
Notifications
You must be signed in to change notification settings - Fork 38
/
doc.pm
339 lines (297 loc) · 10.4 KB
/
doc.pm
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
# See bottom of file for license and copyright information
=begin TML
---+ package Foswiki::IncludeHandlers::doc
This package is designed to be lazy-loaded when Foswiki sees
an INCLUDE macro with the doc: protocol. It implements a single
method INCLUDE which generates perl documentation for a Foswiki class.
=cut
package Foswiki::IncludeHandlers::doc;
use v5.14;
use strict;
use warnings;
use File::Spec ();
use Foswiki ();
BEGIN {
if ( $Foswiki::cfg{UseLocale} ) {
require locale;
import locale();
}
}
use constant PUBLISHED_API_TOPIC => 'PublishedAPI';
# Include embedded doc in a core module
sub INCLUDE {
my ( $includeMacro, $control, $params ) = @_;
my $app = $includeMacro->app;
my %removedblocks = ();
my $class = $control->{_DEFAULT} || 'doc:Foswiki';
my $publicOnly = ( $params->{publicOnly} || '' ) eq 'on';
$app->prefs->setSessionPreferences( 'SMELLS', '' );
# SMELL This is no longer being used in PerlDoc ...
# $app->prefs->setSessionPreferences( 'DOC_PARENT', '' );
$app->prefs->setSessionPreferences( 'DOC_CHILDREN', '' );
$app->prefs->setSessionPreferences( 'DOC_TITLE', '---++ !! !%TOPIC%' );
$class =~ s/[a-z]+://; # remove protocol
$class ||= 'Foswiki'; # provide a reasonable default
# return '' unless $class && $class =~ m/^Foswiki/;
$class =~ s/[^\w:]//g;
my %publicPackages = map { $_ => 1 } _loadPublishedAPI($app);
my $visibility = exists $publicPackages{$class} ? 'public' : 'internal';
_setNavigation( $app, $class, $publicOnly, \%publicPackages );
$app->prefs->setSessionPreferences( 'DOC_TITLE',
"---++ !! =$visibility package= " . _renderTitle( $app, $class ) );
my $pmfile = _getPmFile( $app, $class );
return '' unless $pmfile;
my $PMFILE;
open( $PMFILE, '<', $pmfile ) || return '';
my $inPod = 0;
my $pod = '';
my $howSmelly = 0;
my $showSmells = !$app->isGuest();
local $/ = undef;
my $perl = <$PMFILE>;
my $isa;
my $inSuppressedMethod;
if ( $perl =~ m/our\s+\@ISA\s*=\s*\(\s*['"](.*?)['"]\s*\)/
|| $perl =~ m/extends\s+(?:qw\(|'|")(.+?)(?:\)|'|");/ )
{
$isa = " ==is a== $1";
$isa =~ s#\s(\w+?(?:::[A-Z]\w+)+)#' ' . _doclink($app, $1)#ge;
}
$perl = Foswiki::takeOutBlocks( $perl, 'verbatim', \%removedblocks );
foreach my $line ( split( /\r?\n/, $perl ) ) {
if ( $line =~ m/^=(begin (twiki|TML|html)|pod)/ ) {
$inPod = 1;
$inSuppressedMethod = 0;
}
elsif ( $line =~ m/^=cut/ ) {
$inPod = 0;
}
elsif ($inPod) {
if ( $line =~ m/^---\+(!!)?\s+(?i:package|class)\s+\S+\s*$/ ) {
if ($isa) {
$line .= $isa;
$isa = undef;
}
$line =~
s/^---\+(?:!!)?\s+((?i)package|class)\s*(.*)/---+ =$visibility $1= $2/;
$app->prefs->setSessionPreferences( 'DOC_TITLE',
"---++ !! =$visibility $1= "
. _renderTitle( $app, $class ) );
}
else {
# Check for module names not prefixed with colon or left square
# bracket.
$line =~
s#(?<![\[:])\b(\w+?(?:::[A-Z]\w+)+)#_doclink($app, $1)#ge;
}
if ( $line =~ s/^(---\++\s+)(\w+(?:Method|Attribute))\s+/$1=$2= / )
{
$line =~ s/\s+[-=]>\s+/ → /;
if ( $publicOnly && $line =~ m/Method=\s+_/ ) {
$inSuppressedMethod = 1;
}
}
elsif ( $line =~ m/^---/ ) {
$inSuppressedMethod = 0;
}
$pod .= "$line\n"
unless $inSuppressedMethod;
}
if ( !$inSuppressedMethod
&& $line =~ m/(SMELL|FIXME|TODO)/
&& $showSmells )
{
$howSmelly++;
$pod .=
"<blockquote class=\"foswikiAlert\">"
. Foswiki::entityEncode($line)
. "</blockquote>";
}
}
close($PMFILE);
Foswiki::putBackBlocks( \$pod, \%removedblocks, 'verbatim', 'verbatim' );
$pod =~ s/.*?%STARTINCLUDE%//s;
$pod =~ s/%(?:END|STOP)INCLUDE%.*//s;
if ($howSmelly) {
my $podSmell =
'<blockquote class="foswikiAlert">'
. " *SMELL / FIX / TODO count: $howSmelly*\n"
. '</blockquote>';
$pod .= $podSmell;
$app->prefs->setSessionPreferences( 'SMELLS', $podSmell );
}
$pod =
$includeMacro->applyPatternToIncludedText( $pod, $control->{pattern} )
if ( $control->{pattern} );
# Adjust the root heading level
if ( $params->{level} ) {
my $minhead = '+' x 100;
$pod =~ s/^---(\++)/
$minhead = $1 if length($1) < length($minhead); "---$1"/gem;
return $pod if length($minhead) == 100;
my $newroot = '+' x $params->{level};
$minhead =~ s/\+/\\+/g;
$pod =~ s/^---$minhead/---$newroot/gm;
}
return $pod;
}
sub _getPmFile {
my ( $app, $class ) = @_;
state %cachedPMs;
state $fwPath;
unless ( defined $fwPath ) {
$fwPath = ( File::Spec->splitpath( $INC{'Foswiki.pm'} ) )[1];
}
return $cachedPMs{$class} if $cachedPMs{$class};
my $pmfile = '';
( my $classFile = $class ) =~ s#::#/#g;
$classFile = File::Spec->catfile( $fwPath, "$classFile.pm" );
$pmfile = $classFile if ( -f $classFile );
$cachedPMs{$class} = $pmfile;
return $pmfile;
}
# set DOC_CHILDREN preference value to a list of sub-packages.
sub _setNavigation {
my ( $app, $class, $publicOnly, $publicPackages ) = @_;
my @children;
my %childrenDesc;
my $classPrefix = $class . '::';
# my $classParent = $class;
# $classParent =~ s/::[^:]+$//;
# $app->prefs->setSessionPreferences( 'DOC_PARENT', _doclink($app, $classParent) );
$class =~ s#::#/#g;
foreach my $inc (@INC) {
if ( -d "$inc/$class" and opendir my $dh, "$inc/$class" ) {
my @dir = grep { !/^\./ } readdir($dh);
push @children,
map { -d "$inc/$class/$_" ? "$classPrefix$_" : () } @dir;
for my $d (@dir) {
if ( $d =~ s/\.pm$// ) {
push @children, "$classPrefix$d";
$childrenDesc{"$classPrefix$d"} =
_getPackSummary("$inc/$class/$d.pm");
}
}
closedir $dh;
}
}
if ($publicOnly) {
@children = grep { exists $publicPackages->{$_} } @children;
}
my $children = '<ul>';
if (@children) {
my %children = map { $_ => 1 } @children;
@children = sort keys %children;
foreach my $child (@children) {
my $desc =
$childrenDesc{$child} ? ' - ' . $childrenDesc{$child} : '';
$children .= '<li>' . _doclink( $app, $child ) . "$desc</li>\n";
}
}
$children .= '</ul>';
$app->prefs->setSessionPreferences( 'DOC_CHILDREN', $children );
}
# get a summary of the pod documentation by looking directly after the ---+ package TML.
sub _getPackSummary ($) {
my $pmfile = $_[0];
my @summary;
my $PMFILE;
open( $PMFILE, '<', $pmfile ) || return '';
my $inPod = 0;
my $inPackage = 0;
while ( my $line = <$PMFILE> ) {
if ( $line =~ m/^=(begin (twiki|TML|html)|pod)/ ) {
$inPod = 1;
}
elsif ( $line =~ m/^=cut/ ) {
@summary
and last;
$inPod = 0;
}
elsif ($inPod) {
if ($inPackage) {
chomp($line);
push @summary, $line;
}
if ( $line =~ m/^---\+(!!)?\s+(?i:package|class)\s+\S+\s*$/ ) {
$inPackage = 1;
}
}
}
close($PMFILE);
while (@summary) {
if ( $summary[0] =~ m/^\s*$/ ) {
shift @summary;
}
else {
last;
}
}
if ( !@summary ) {
return '';
}
my $emptyLine = 0;
while ( $emptyLine < @summary && $summary[$emptyLine] !~ /^\s*$/ ) {
$emptyLine++;
}
return join ' ', @summary[ 0 .. $emptyLine - 1 ];
}
sub _loadPublishedAPI {
my $app = shift;
my ( $meta, $text ) =
$app->readTopic( $app->cfg->data->{SystemWebName}, PUBLISHED_API_TOPIC );
my @ret;
for my $line ( split /\r?\n/, $text ) {
#| [[%SYSTEMWEB%.PerlDoc?module=Foswiki::Func][Foswiki::Func]] | 1.1.5 | Main API. |
$line =~ m/^\|\s*\[\[.*?PerlDoc.*?\]\[(.*?)\]\]/
and push @ret, $1;
}
return @ret;
}
# Make each intermediate package into a doc link.
sub _renderTitle {
my $app = shift;
my $pack = $_[0];
my @packComps = split '::', $pack;
my @packLinks =
map {
_doclink( $app, ( join '::', @packComps[ 0 .. $_ ] ), $packComps[$_] )
} 0 .. $#packComps - 1;
my $packageTitle = join '::', @packLinks, $packComps[$#packComps];
return $packageTitle;
}
sub _doclink ($) {
my $app = shift;
my $module = $_[0];
$module =~ /^/; # Do it to reset $n match variables.
$module =~ s/^_(.+)(_)$/$1/;
my $formatChar = $2 // '';
my $title = $_[1] || $module;
my $pmfile = _getPmFile( $app, $module );
# SMELL relying on TML to set publicOnly
return $formatChar
. (
$pmfile
? ( "[[%SCRIPTURL{view}%/%SYSTEMWEB%/PerlDoc?module="
. $module
. "%IF{\"\$publicOnly = 'on'\" then=\";publicOnly=on\"}%]["
. $title
. "]]" )
: "[[CPAN:$module][$title]]"
) . $formatChar;
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2012 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.