-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrcdoc.pas
530 lines (502 loc) · 14.7 KB
/
srcdoc.pas
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
(* This file is a part of the srcdoc program for creating
documentation of Delphi/Pascal programs from comments in source
files.
Copyright (C) 2005 by Lukasz Czajka
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.
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. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA *)
program srcdoc;
{$apptype console }
{$linklib pascaladt-1.0.2 }
uses
SysUtils, Classes, Getopts, adtqueue,
commonclasses, defdriver, htmlwriter, languages;
const
skipSeparators = [optIgnoreHandMadeSeparators,
optDiscardParagraphOnHandMadeSeparators];
joinComments = [optJoinComments, optNewParagraphBeforeJoinedComment];
srcdoc_rawCommentParser = [optLinkPlurals, optMakeSynopsis];
srcdocCommentParser = [optExplicitLocalSymbols, optEmphasis] +
skipSeparators + srcdoc_rawCommentParser;
beautifyComments = [optBeautifyComments];
verboseOptions = [optVerbose, optShowWarnings];
otherOptions = [optTableBorder, optShowWarnings,
optFetchCommentsFromAncestors, optGenerateClassTree,
optGenerateInterfaceTree, optGenerateSymbolIndex,
optGenerateContents] + joinComments;
userProfile = [optShowProtected];
developerProfile = userProfile + [optShowPrivate, optShowImplementationInfo];
VERSION_CHAR = '0';
PROTECTED_CHAR = '1';
PRIVATE_CHAR = '2';
SKIP_SEPARATORS_CHAR = '3';
SYNOPSIS_CHAR = '4';
FETCH_COMMENTS_CHAR = '5';
TABLE_BORDER_CHAR = '6';
var
writer : TDocWriter;
deque : TStringDeque;
sourceDirectories : TStrings;
options : TOptions;
title, profileStr : String;
procedure CleanExit(exitCode : Byte);
begin
writer.Free;
sourceDirectories.Free;
deque.Free;
Halt(exitCode);
end;
{ used to exit when an invalid command line parameter specified }
procedure ErrorExit;
begin
CleanExit(3);
end;
procedure PrintUsage;
begin
WriteLn('Usage: srcdoc [options] files_to_parse');
WriteLn('For help type srcdoc --help');
ErrorExit;
end;
procedure PrintHelp;
begin
WriteLn('Usage: srcdoc [options] files_to_parse');
WriteLn('Creates documentation of programs by parsing comments in source files.');
WriteLn;
WriteLn('Mandatory options to long arguments are mandatory for short options, too');
WriteLn('-o, --output-directory=DIR Sets the output directory to DIR.');
WriteLn('-s, --source-directory=DIR Adds DIR to the list of directories searched');
WriteLn(' for source files.');
WriteLn('-f, --format=FORMAT Sets the output format to FORMAT. For now,');
WriteLn(' FORMAT may be only ''html''.');
WriteLn('-l, --language=LANG Sets the (human) language for output files.');
WriteLn(' For now, LANG may be only ''en'' or ''pl''.');
WriteLn('-p, --profile=PROF Sets the output profile. PROF may be either');
WriteLn(' ''user'' (default) or ''devel''. In the');
WriteLn(' ''user'' profile only globally visible');
WriteLn(' declarations and only public and protected');
WriteLn(' class members are included. In the ''devel''');
WriteLn(' profile local declarations and private members');
WriteLn(' are also included. You may override this');
WriteLn(' with --protected, --private and -i.');
WriteLn('-c, --comment-parser=PARSER Sets the comment parser. PARSER may be');
WriteLn(' ''srcdoc'' (default) or ''srcdoc_raw''. The');
WriteLn(' raw parser just does not support several');
WriteLn(' features and thus leaves things like <name>');
WriteLn(' or _name_ intact.');
WriteLn('-d, --dependencies Try find and parse the modules (units) on');
WriteLn(' which a given module depends first.');
WriteLn('-b, --beautify-comments ''Beautify'' the comments. When beautifying');
WriteLn(' all '';'' are changed to dots and all words');
WriteLn(' beginning a sentence are capitalized.');
WriteLn('-t, --title=TITLE Sets the title for the whole documentation.');
WriteLn(' This should be the name of the program or');
WriteLn(' the library being documented.');
WriteLn('-i, --implementation-info=on/off');
WriteLn(' Indicates whether to include local declarations');
WriteLn(' in the generated documentation.');
WriteLn('--protected=on/off Indicates whether to show protected members.');
WriteLn('--private=on/off Indicates whether to show private members.');
WriteLn('--table-border=on/off Indicates whether to draw borders for tables.');
WriteLn(' On by default.');
WriteLn('--skip-separators=on/off If on then any sequence consisting of one');
WriteLn(' non-alphanumeric character repeated more than');
WriteLn(' 7 times is skipped. On by default.');
WriteLn('-j, --join-comments=on/off If on several comments one after another,');
WriteLn(' possibly separated with whitespace, are');
WriteLn(' joined into one comment. On by default.');
WriteLn('--fetch-comments=on/pff Indicates whether to fetch comments from');
WriteLn(' ancestor classes if no comment is present');
WriteLn(' for a given member.');
WriteLn('--synopsis=on/off If on then if a comment does not contain the');
WriteLn(' @synopsis command, the first sentence of the');
WriteLn(' comment is assumed to be the synopsis.');
WriteLn('-v, --verbose Be verbose.');
WriteLn('-q, --quiet, --silent Suppress warnings.');
WriteLn('-h, --help Show this screen.');
WriteLn('--version Show the version information.');
CleanExit(0);
end;
procedure PrintVersion;
begin
WriteLn(srcdocVersion);
WriteLn(srcdocCopyright);
CleanExit(0);
end;
{ should set the <writer>, <title> and <options> variables }
procedure ParseCommandLine;
const
OPTS_NUM = 22;
var
opts : array[0..OPTS_NUM] of getopts.TOption;
commentParser, profile, exclude_options : commonclasses.TOptions;
unused : Integer;
outputDirectory : String;
c : Char;
i : Integer;
begin
with opts[OPTS_NUM] do
begin
name := '';
has_arg := 0;
flag := nil;
value := #0;
end;
with opts[0] do
begin
name := 'language';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := 'l';
end;
with opts[1] do
begin
name := 'source-directory';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := 's';
end;
with opts[2] do
begin
name := 'output-directory';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := 'o';
end;
with opts[3] do
begin
name := 'comment-parser';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := 'c';
end;
with opts[4] do
begin
name := 'format';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := 'f';
end;
with opts[5] do
begin
name := 'profile';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := 'p';
end;
with opts[6] do
begin
name := 'title';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := 't';
end;
{ options without args }
with opts[7] do
begin
name := 'dependencies';
has_arg := 0;
flag := nil;
value := 'd';
end;
with opts[8] do
begin
name := 'beautify-comments';
has_arg := 0;
flag := nil;
value := 'b';
end;
with opts[9] do
begin
name := 'help';
has_arg := 0;
flag := nil;
value := 'h';
end;
with opts[10] do
begin
name := 'version';
has_arg := 0;
flag := nil;
value := VERSION_CHAR;
end;
{ on/off options }
with opts[11] do
begin
name := 'table-border';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := TABLE_BORDER_CHAR;
end;
with opts[12] do
begin
name := 'implementation-info';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := 'i';
end;
with opts[13] do
begin
name := 'join-comments';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := 'j';
end;
with opts[14] do
begin
name := 'protected';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := PROTECTED_CHAR;
end;
with opts[15] do
begin
name := 'private';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := PRIVATE_CHAR;
end;
with opts[16] do
begin
name := 'skip-separators';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := SKIP_SEPARATORS_CHAR;
end;
with opts[17] do
begin
name := 'synopsis';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := SYNOPSIS_CHAR;
end;
with opts[18] do
begin
name := 'fetch-comments';
has_arg := REQUIRED_ARGUMENT;
flag := nil;
value := FETCH_COMMENTS_CHAR;
end;
{ no args again }
with opts[19] do
begin
name := 'verbose';
has_arg := 0;
flag := nil;
value := 'v';
end;
with opts[20] do
begin
name := 'silent';
has_arg := 0;
flag := nil;
value := 'q';
end;
with opts[21] do
begin
name := 'quiet';
has_arg := 0;
flag := nil;
value := 'q';
end;
commentParser := srcdocCommentParser;
profile := userProfile;
profileStr := 'user';
exclude_options := [];
options := otherOptions;
writer := nil;
deque := TStringDeque.Create();
sourceDirectories := TStringList.Create;
sourceDirectories.Add('.');
outputDirectory := '.';
title := '';
repeat
c := GetLongOpts('l:s:o:c:f:p:dbht:i:j:vq', @opts[0], unused);
case c of
'l':
begin
if not SetLanguage(OptArg) then
PrintUsage;
end;
's':
begin
sourceDirectories.Add(OptArg);
end;
'o':
begin
outputDirectory := OptArg;
end;
'c':
begin
if OptArg = 'srcdoc' then
commentParser := srcdocCommentParser
else if OptArg = 'srcdoc_raw' then
commentParser := srcdoc_rawCommentParser
else
PrintUsage;
end;
'f':
begin
if OptArg <> 'html' then
PrintUsage;
end;
'p':
begin
if OptArg = 'user' then
profile := userProfile
else if OptArg = 'devel' then
profile := developerProfile
else
PrintUsage;
profileStr := OptArg;
end;
'd':
begin
Include(options, optDependencies);
end;
'b':
begin
options := options + beautifyComments;
end;
'h':
begin
PrintHelp;
end;
't':
begin
title := OptArg;
end;
'i':
begin
if OptArg = 'on' then
Include(options, optShowImplementationInfo)
else if OptArg = 'off' then
Include(exclude_options, optShowImplementationInfo)
else
PrintUsage;
end;
'j':
begin
if OptArg = 'on' then
options := options + joinComments
else if OptArg = 'off' then
exclude_options := exclude_options + joinComments
else
PrintUsage;
end;
'v':
begin
options := options + verboseOptions;
end;
FETCH_COMMENTS_CHAR:
begin
if OptArg = 'on' then
Include(options, optFetchCommentsFromAncestors)
else if OptArg = 'off' then
Include(exclude_options, optFetchCommentsFromAncestors)
else
PrintUsage;
end;
VERSION_CHAR:
begin
PrintVersion;
end;
PROTECTED_CHAR:
begin
if OptArg = 'on' then
Include(options, optShowProtected)
else if OptArg = 'off' then
Include(exclude_options, optShowProtected)
else
PrintUsage;
end;
PRIVATE_CHAR:
begin
if OptArg = 'on' then
Include(options, optShowPrivate)
else if OptArg = 'off' then
Include(exclude_options, optShowPrivate)
else
PrintUsage;
end;
SKIP_SEPARATORS_CHAR:
begin
if OptArg = 'on' then
options := options + skipSeparators
else if OptArg = 'off' then
exclude_options := exclude_options + skipSeparators
else
PrintUsage;
end;
SYNOPSIS_CHAR:
begin
if OptArg = 'on' then
Include(options, optMakeSynopsis)
else if OptArg = 'off' then
Include(exclude_options, optMakeSynopsis)
else
PrintUsage;
end;
TABLE_BORDER_CHAR:
begin
if OptArg = 'on' then
Include(options, optTableBorder)
else if OptArg = 'off' then
Include(exclude_options, optTableBorder)
else
PrintUsage;
end;
'?':
begin
PrintUsage;
end;
end;
until c = EndOfOptions;
if OptInd <= ParamCount then
begin
for i := OptInd to ParamCount do
deque.PushBack(ParamStr(i));
end else
PrintUsage;
options := options + commentParser + profile;
options := options - exclude_options;
if not DirectoryExists(outputDirectory) then
MkDir(outputDirectory);
if writer = nil then
writer := THtmlWriter.Create(outputDirectory);
end;
begin
ParseCommandLine;
try
Driver := TDefaultDriver.Create(options, writer, sourceDirectories,
title, profileStr);
Driver.Run(deque);
except
on err1 : ESrcDocError do
begin
WriteLn(err1.Message);
{$ifdef DEBUG }
raise;
{$else }
halt(3);
{$endif }
end;
on err2 : Exception do
begin
WriteLn('srcdoc: Fatal error: ' + err2.Message);
{$ifdef DEBUG }
raise;
{$else }
halt(3);
{$endif }
end;
end;
end.