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
|
###############################################################################
#
# Package: NaturalDocs::Parser::JavaDoc
#
###############################################################################
#
# A package for translating JavaDoc topics into Natural Docs.
#
# Supported tags:
#
# - @param
# - @author
# - @deprecated
# - @code, @literal (doesn't change font)
# - @exception, @throws (doesn't link to class)
# - @link, @linkplain (doesn't change font)
# - @return, @returns
# - @see
# - @since
# - @value (shown as link instead of replacement)
# - @version
#
# Stripped tags:
#
# - @inheritDoc
# - @serial, @serialField, @serialData
# - All other block level tags.
#
# Unsupported tags:
#
# These will appear literally in the output because I cannot handle them easily.
#
# - @docRoot
# - Any other tags not mentioned
#
# Supported HTML:
#
# - p
# - b, i, u
# - pre
# - a href
# - ol, ul, li (ol gets converted to ul)
# - gt, lt, amp, quot, nbsp entities
#
# Stripped HTML:
#
# - code
# - HTML comments
#
# Unsupported HTML:
#
# These will appear literally in the output because I cannot handle them easily.
#
# - Any tags with additional properties other than a href. (ex. <p class=Something>)
# - Any other tags not mentioned
#
# Reference:
#
# http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javadoc.html
#
###############################################################################
# This file is part of Natural Docs, which is Copyright (C) 2003-2008 Greg Valure
# Natural Docs is licensed under the GPL
use strict;
use integer;
package NaturalDocs::Parser::JavaDoc;
#
# hash: blockTags
# An existence hash of the all-lowercase JavaDoc block tags, not including the @.
#
my %blockTags = ( 'param' => 1, 'author' => 1, 'deprecated' => 1, 'exception' => 1, 'return' => 1, 'see' => 1,
'serial' => 1, 'serialfield' => 1, 'serialdata' => 1, 'since' => 1, 'throws' => 1, 'version' => 1,
'returns' => 1 );
#
# hash: inlineTags
# An existence hash of the all-lowercase JavaDoc inline tags, not including the @.
#
my %inlineTags = ( 'inheritdoc' => 1, 'docroot' => 1, 'code' => 1, 'literal' => 1, 'link' => 1, 'linkplain' => 1, 'value' => 1 );
##
# Examines the comment and returns whether it is *definitely* JavaDoc content, i.e. is owned by this package.
#
# Parameters:
#
# commentLines - An arrayref of the comment lines. Must have been run through <NaturalDocs::Parser->CleanComment()>.
# isJavaDoc - Whether the comment is JavaDoc styled. This doesn't necessarily mean it has JavaDoc content.
#
# Returns:
#
# Whether the comment is *definitely* JavaDoc content.
#
sub IsMine #(string[] commentLines, bool isJavaDoc)
{
my ($self, $commentLines, $isJavaDoc) = @_;
if (!$isJavaDoc)
{ return undef; };
for (my $line = 0; $line < scalar @$commentLines; $line++)
{
if ($commentLines->[$line] =~ /^ *@([a-z]+) /i && exists $blockTags{$1} ||
$commentLines->[$line] =~ /\{@([a-z]+) /i && exists $inlineTags{$1})
{
return 1;
};
};
return 0;
};
##
# Parses the JavaDoc-syntax comment and adds it to the parsed topic list.
#
# Parameters:
#
# commentLines - An arrayref of the comment lines. Must have been run through <NaturalDocs::Parser->CleanComment()>.
# *The original memory will be changed.*
# isJavaDoc - Whether the comment is JavaDoc styled. This doesn't necessarily mean it has JavaDoc content.
# lineNumber - The line number of the first of the comment lines.
# parsedTopics - A reference to the array where any new <NaturalDocs::Parser::ParsedTopics> should be placed.
#
# Returns:
#
# The number of parsed topics added to the array, which in this case will always be one.
#
sub ParseComment #(string[] commentLines, bool isJavaDoc, int lineNumber, ParsedTopics[]* parsedTopics)
{
my ($self, $commentLines, $isJavaDoc, $lineNumber, $parsedTopics) = @_;
# Stage one: Before block level tags.
my $i = 0;
my $output;
my $unformattedText;
my $inCode;
my $sharedCodeIndent;
while ($i < scalar @$commentLines &&
!($commentLines->[$i] =~ /^ *@([a-z]+) /i && exists $blockTags{$1}) )
{
my $line = $self->ConvertAmpChars($commentLines->[$i]);
my @tokens = split(/(<\/?pre>)/, $line);
foreach my $token (@tokens)
{
if ($token =~ /^<pre>$/i)
{
if (!$inCode && $unformattedText)
{
$output .= '<p>' . $self->FormatText($unformattedText, 1) . '</p>';
};
$inCode = 1;
$unformattedText = undef;
}
elsif ($token =~ /^<\/pre>$/i)
{
if ($inCode && $unformattedText)
{
$unformattedText =~ s/^ {$sharedCodeIndent}//mg;
$unformattedText =~ s/\n{3,}/\n\n/g;
$unformattedText =~ s/\n+$//;
$output .= '<code>' . $unformattedText . '</code>';
$sharedCodeIndent = undef;
};
$inCode = 0;
$unformattedText = undef;
}
elsif (length($token))
{
if (!$inCode)
{
$token =~ s/^ +//;
if ($unformattedText)
{ $unformattedText .= ' '; };
}
else
{
$token =~ /^( *)/;
my $indent = length($1);
if (!defined $sharedCodeIndent || $indent < $sharedCodeIndent)
{ $sharedCodeIndent = $indent; };
};
$unformattedText .= $token;
};
};
if ($inCode && $unformattedText)
{ $unformattedText .= "\n"; };
$i++;
};
if ($unformattedText)
{
if ($inCode)
{
$unformattedText =~ s/^ {$sharedCodeIndent}//mg;
$unformattedText =~ s/\n{3,}/\n\n/g;
$unformattedText =~ s/\n+$//;
$output .= '<code>' . $unformattedText . '</code>';
}
else
{ $output .= '<p>' . $self->FormatText($unformattedText, 1) . '</p>'; };
$unformattedText = undef;
};
# Stage two: Block level tags.
my ($keyword, $value, $unformattedTextPtr, $unformattedTextCloser);
my ($params, $authors, $deprecation, $throws, $returns, $seeAlso, $since, $version);
while ($i < scalar @$commentLines)
{
my $line = $self->ConvertAmpChars($commentLines->[$i]);
$line =~ s/^ +//;
if ($line =~ /^@([a-z]+) ?(.*)$/i)
{
($keyword, $value) = (lc($1), $2);
# Process the previous one, if any.
if ($unformattedText)
{
$$unformattedTextPtr .= $self->FormatText($unformattedText) . $unformattedTextCloser;
$unformattedText = undef;
};
if ($keyword eq 'param')
{
$value =~ /^([a-z0-9_]+) *(.*)$/i;
$params .= '<de>' . $1 . '</de><dd>';
$unformattedText = $2;
$unformattedTextPtr = \$params;
$unformattedTextCloser = '</dd>';
}
elsif ($keyword eq 'exception' || $keyword eq 'throws')
{
$value =~ /^([a-z0-9_]+) *(.*)$/i;
$throws .= '<de>' . $1 . '</de><dd>';
$unformattedText = $2;
$unformattedTextPtr = \$throws;
$unformattedTextCloser = '</dd>';
}
elsif ($keyword eq 'return' || $keyword eq 'returns')
{
if ($returns)
{ $returns .= ' '; };
$unformattedText = $value;
$unformattedTextPtr = \$returns;
$unformattedTextCloser = undef;
}
elsif ($keyword eq 'author')
{
if ($authors)
{ $authors .= ', '; };
$unformattedText = $value;
$unformattedTextPtr = \$authors;
$unformattedTextCloser = undef;
}
elsif ($keyword eq 'deprecated')
{
if ($deprecation)
{ $deprecation .= ' '; };
$unformattedText = $value;
$unformattedTextPtr = \$deprecation;
$unformattedTextCloser = undef;
}
elsif ($keyword eq 'since')
{
if ($since)
{ $since .= ', '; };
$unformattedText = $value;
$unformattedTextPtr = \$since;
$unformattedTextCloser = undef;
}
elsif ($keyword eq 'version')
{
if ($version)
{ $version .= ', '; };
$unformattedText = $value;
$unformattedTextPtr = \$version;
$unformattedTextCloser = undef;
}
elsif ($keyword eq 'see')
{
if ($seeAlso)
{ $seeAlso .= ', '; };
$unformattedText = undef;
if ($value =~ /^&(?:quot|lt);/i)
{ $seeAlso .= $self->FormatText($value); }
else
{ $seeAlso .= $self->ConvertLink($value); };
};
# Everything else will be skipped.
}
elsif ($unformattedText)
{
$unformattedText .= ' ' . $line;
};
$i++;
};
if ($unformattedText)
{
$$unformattedTextPtr .= $self->FormatText($unformattedText) . $unformattedTextCloser;
$unformattedText = undef;
};
if ($params)
{ $output .= '<h>Parameters</h><dl>' . $params . '</dl>'; };
if ($returns)
{ $output .= '<h>Returns</h><p>' . $returns . '</p>'; };
if ($throws)
{ $output .= '<h>Throws</h><dl>' . $throws . '</dl>'; };
if ($since)
{ $output .= '<h>Since</h><p>' . $since . '</p>'; };
if ($version)
{ $output .= '<h>Version</h><p>' . $version . '</p>'; };
if ($deprecation)
{ $output .= '<h>Deprecated</h><p>' . $deprecation . '</p>'; };
if ($authors)
{ $output .= '<h>Author</h><p>' . $authors . '</p>'; };
if ($seeAlso)
{ $output .= '<h>See Also</h><p>' . $seeAlso . '</p>'; };
# Stage three: Build the parsed topic.
my $summary = NaturalDocs::Parser->GetSummaryFromBody($output);
push @$parsedTopics, NaturalDocs::Parser::ParsedTopic->New(undef, undef, undef, undef, undef, $summary,
$output, $lineNumber, undef);
return 1;
};
##
# Translates any inline tags or HTML codes to <NDMarkup> and returns it.
#
sub FormatText #(string text, bool inParagraph)
{
my ($self, $text, $inParagraph) = @_;
# JavaDoc Literal
$text =~ s/\{\@(?:code|literal) ([^\}]*)\}/$self->ConvertAmpChars($1)/gie;
# HTML
$text =~ s/<b>(.*?)<\/b>/<b>$1<\/b>/gi;
$text =~ s/<i>(.*?)<\/i>/<i>$1<\/i>/gi;
$text =~ s/<u>(.*?)<\/u>/<u>$1<\/u>/gi;
$text =~ s/<code>(.*?)<\/code>/$1/gi;
$text =~ s/<ul.*?>(.*?)<\/ul>/<ul>$1<\/ul>/gi;
$text =~ s/<ol.*?>(.*?)<\/ol>/<ul>$1<\/ul>/gi;
$text =~ s/<li.*?>(.*?)<\/li>/<li>$1<\/li>/gi;
$text =~ s/<!--.*?-->//gi;
$text =~ s/<\/p>//gi;
$text =~ s/^<p>//i;
if ($inParagraph)
{ $text =~ s/<p>/<\/p><p>/gi; }
else
{ $text =~ s/<p>//gi; };
$text =~ s/<a href="mailto:(.*?)".*?>(.*?)<\/a>/$self->MakeEMailLink($1, $2)/gie;
$text =~ s/<a href="(.*?)".*?>(.*?)<\/a>/$self->MakeURLLink($1, $2)/gie;
$text =~ s/&nbsp;/ /gi;
$text =~ s/&amp;/&/gi;
$text =~ s/&gt;/>/gi;
$text =~ s/&lt;/</gi;
$text =~ s/&quot;/"/gi;
# JavaDoc
$text =~ s/\{\@inheritdoc\}//gi;
$text =~ s/\{\@(?:linkplain|link|value) ([^\}]*)\}/$self->ConvertLink($1)/gie;
return $text;
};
sub ConvertAmpChars #(text)
{
my ($self, $text) = @_;
$text =~ s/&/&/g;
$text =~ s/</</g;
$text =~ s/>/>/g;
$text =~ s/"/"/g;
return $text;
};
sub ConvertLink #(text)
{
my ($self, $text) = @_;
$text =~ /^ *([a-z0-9\_\.\:\#]+(?:\([^\)]*\))?) *(.*)$/i;
my ($target, $label) = ($1, $2);
# Convert the anchor to part of the link, but remove it altogether if it's the beginning of the link.
$target =~ s/^\#//;
$target =~ s/\#/\./;
$label =~ s/ +$//;
if (!length $label)
{ return '<link target="' . $target . '" name="' . $target . '" original="' . $target . '">'; }
else
{ return '<link target="' . $target . '" name="' . $label . '" original="' . $label . ' (' . $target . ')">'; };
};
sub MakeURLLink #(target, text)
{
my ($self, $target, $text) = @_;
return '<url target="' . $target . '" name="' . $text . '">';
};
sub MakeEMailLink #(target, text)
{
my ($self, $target, $text) = @_;
return '<email target="' . $target . '" name="' . $text . '">';
};
1;
|