1 | #!/usr/local/bin/perl |
---|
2 | # $Id: pl2doc.pl,v 1.3 1999-07-22 14:51:32 obachman Exp $ |
---|
3 | ################################################################### |
---|
4 | # Computer Algebra System SINGULAR |
---|
5 | # |
---|
6 | # pl2doc.pl: utility to generate doc file out of pl for singular library |
---|
7 | # |
---|
8 | #### |
---|
9 | $Usage = <<EOT; |
---|
10 | Usage: |
---|
11 | $0 [-o out_file -db db_file -no_ex -no_fun] library_perl_file.doc |
---|
12 | Convert library_perl_file.pl to library_perl_file.doc |
---|
13 | EOT |
---|
14 | |
---|
15 | ################################################################### |
---|
16 | # parse command line options |
---|
17 | # |
---|
18 | while (@ARGV && $ARGV[0] =~ /^-/) |
---|
19 | { |
---|
20 | $_ = shift(@ARGV); |
---|
21 | if (/^-o$/) { $out_file = shift(@ARGV); next;} |
---|
22 | if (/^-db$/) { $db_file = shift(@ARGV); next;} |
---|
23 | if (/^-no_fun$/) { $no_fun = 1;next;} |
---|
24 | if (/^-h(elp)?$/) { print $Usage; exit;} |
---|
25 | die "Error: Unknown option: $_:$Usage\n"; |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | ################################################################### |
---|
30 | # Execute perl file |
---|
31 | # |
---|
32 | $pl_file = pop(@ARGV); |
---|
33 | die "Error: No perl file specified: $Usage\n" unless $pl_file; |
---|
34 | die "Error: Can't find perl file $pl_file: $Usage\n" unless -r $pl_file; |
---|
35 | require $pl_file; |
---|
36 | ($lib = $library) =~ s|.*/(.*)\.lib$|$1|; |
---|
37 | |
---|
38 | ################################################################### |
---|
39 | # print summary |
---|
40 | # |
---|
41 | unless ($out_file) |
---|
42 | { |
---|
43 | ($out_file = $pl_file) =~ s/(.*)\..*$/$1/; |
---|
44 | $out_file .= "_noFun" if ($no_fun); |
---|
45 | $out_file .= ".doc"; |
---|
46 | } |
---|
47 | open(LDOC, ">$out_file") || die"Error: can't open $out_file for writing: $!\n"; |
---|
48 | print LDOC "\@c library version: $version\n"; |
---|
49 | print LDOC "\@c library file: $library\n"; |
---|
50 | |
---|
51 | undef @procs; # will be again defined by OutLibInfo |
---|
52 | $ref = OutLibInfo(\*LDOC, $info, ! $no_fun); |
---|
53 | OutRef(\*LDOC, $ref) if $ref; |
---|
54 | |
---|
55 | |
---|
56 | ################################################################### |
---|
57 | # print summary |
---|
58 | # |
---|
59 | # print subsections for help of procs |
---|
60 | unless ($no_fun) |
---|
61 | { |
---|
62 | if ($db_file) |
---|
63 | { |
---|
64 | $db_file = $1 if ($db_file =~ /(.*)\..*$/); |
---|
65 | dbmopen(%CHECKSUMS, $db_file, oct(755)) || |
---|
66 | die "Error: can't open chksum data base $db_file"; |
---|
67 | } |
---|
68 | # print help and example of each function |
---|
69 | for ($i = 0; $i <= $#procs; $i++) |
---|
70 | { |
---|
71 | # print node and section heading |
---|
72 | print LDOC "\n\@c ------------------- " . $procs[$i]." -------------\n"; |
---|
73 | print LDOC "\@node " . $procs[$i].","; |
---|
74 | print LDOC " ".$procs[$i+1] if ($i < $#procs); |
---|
75 | print LDOC ","; |
---|
76 | print LDOC " ".$procs[$i-1] if ($i > 0); |
---|
77 | print LDOC ", " . $lib ."_lib\n"; |
---|
78 | print LDOC "\@subsection " . $procs[$i] . "\n"; |
---|
79 | print LDOC "\@cindex ". $procs[$i] . "\n"; |
---|
80 | $CHECKSUMS{$procs[$i]} = $chksum{$procs[$i]} if ($db_file); |
---|
81 | print LDOC "\@c ---content $procs[$i]---\n"; |
---|
82 | print LDOC "Procedure from library \@code{$lib.lib} (\@pxref{${lib}_lib}).\n\n"; |
---|
83 | if ($help{$procs[$i]} =~ /^\@/) |
---|
84 | { |
---|
85 | print LDOC $help{$procs[$i]}; |
---|
86 | $ref = ''; |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | print LDOC "\@table \@asis\n"; |
---|
91 | $table_is_open = 1; |
---|
92 | # print help |
---|
93 | $ref = OutInfo(\*LDOC, $help{$procs[$i]}); |
---|
94 | print LDOC "\@end table\n"; |
---|
95 | } |
---|
96 | # print example |
---|
97 | if ($ex = &CleanUpExample($lib, $example{$procs[$i]})) |
---|
98 | { |
---|
99 | print LDOC "\@strong{Example:}\n"; |
---|
100 | print LDOC "\@smallexample\n\@c example\n"; |
---|
101 | print LDOC $ex; |
---|
102 | print LDOC "\n\@c example\n\@end smallexample\n"; |
---|
103 | } |
---|
104 | OutRef(\*LDOC, $ref) if $ref; |
---|
105 | print LDOC "\@c ---end content $procs[$i]---\n"; |
---|
106 | } |
---|
107 | dbmclose(%CHECKSUMS); |
---|
108 | } |
---|
109 | |
---|
110 | # |
---|
111 | # und Tschuess |
---|
112 | # |
---|
113 | close(LDOC); |
---|
114 | exit(0); |
---|
115 | |
---|
116 | ########################################################################### |
---|
117 | # |
---|
118 | # parse and print-out libinfo |
---|
119 | # |
---|
120 | sub OutLibInfo |
---|
121 | { |
---|
122 | my ($FH, $info, $l_fun) = @_; |
---|
123 | print $FH "\@c ---content LibInfo---\n"; |
---|
124 | if ($info =~ /^\@/) |
---|
125 | { |
---|
126 | print $FH $info; |
---|
127 | return; |
---|
128 | } |
---|
129 | print $FH "\@table \@asis\n"; |
---|
130 | $table_is_open = 1; |
---|
131 | |
---|
132 | my ($ref) = OutInfo($FH, $info, $l_fun); |
---|
133 | |
---|
134 | print $FH "\@end table\n" if $table_is_open; |
---|
135 | print $FH "\@c ---end content LibInfo---\n"; |
---|
136 | $table_is_open = 0; |
---|
137 | return $ref; |
---|
138 | } |
---|
139 | |
---|
140 | sub OutInfo |
---|
141 | { |
---|
142 | my ($FH, $info, $l_fun) = @_; |
---|
143 | if ($info =~ /^\s*\@/) |
---|
144 | { |
---|
145 | print $FH $info; |
---|
146 | return; |
---|
147 | } |
---|
148 | $info =~ s/^\s*//; |
---|
149 | $info =~ s/\s*$//; |
---|
150 | $info .= "\n"; |
---|
151 | |
---|
152 | my ($item, $text, $line, $ref); |
---|
153 | while ($info =~ m/(.*\n)/g) |
---|
154 | { |
---|
155 | $line = $1; |
---|
156 | if ($1 =~ /^(\w.+?):(.*\n)/) |
---|
157 | { |
---|
158 | $ref .= OutInfoItem($FH, $item, $text, $l_fun) if $item && $text; |
---|
159 | $item = $1; |
---|
160 | $text = $2; |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | $text .= $line; |
---|
165 | } |
---|
166 | } |
---|
167 | $ref .= OutInfoItem($FH, $item, $text, $l_fun) if $item && $text; |
---|
168 | return $ref; |
---|
169 | } |
---|
170 | |
---|
171 | sub FormatInfoText |
---|
172 | { |
---|
173 | my $length = shift; |
---|
174 | $length = 0 unless $length; |
---|
175 | # insert @* infront of all lines whose previous line is shorter than |
---|
176 | # 60 characters |
---|
177 | $_ = ' ' x $length . $_; |
---|
178 | if (/^(.*)\n/) |
---|
179 | { |
---|
180 | $_ .= "\n"; |
---|
181 | my $pline; |
---|
182 | my $line; |
---|
183 | my $ptext = $_; |
---|
184 | my $text = ''; |
---|
185 | while ($ptext =~ /(.*)\n/g) |
---|
186 | { |
---|
187 | $line = $1; |
---|
188 | # break line if |
---|
189 | $text .= '@*' |
---|
190 | if ($line =~ /\w/ && $pline =~ /\w/ # line and prev line are not empty |
---|
191 | && $line !~ /^\s*\@\*/ # line does not start with @* |
---|
192 | && $pline !~ /\@\*\s*/ # prev line does not end with @* |
---|
193 | && |
---|
194 | ((length($pline) < 60 && # prev line is shorter than 60 chars |
---|
195 | $pline !~ /\@code{.*?}/ # and does not contain @code, @math |
---|
196 | && $pline !~ /\@math{.*?}/) |
---|
197 | || |
---|
198 | $line =~ /^\s*\w*\(.*?\)/ # $line starts with \w*(..) |
---|
199 | || |
---|
200 | $pline =~ /^\s*\w*\(.*?\)[\s;:]*$/)); # prev line is only \w(..) |
---|
201 | $line =~ s/\s*$//; |
---|
202 | $text .= "$line\n"; |
---|
203 | $pline = $line; |
---|
204 | } |
---|
205 | $_ = $text; |
---|
206 | } |
---|
207 | s/\t/ /g; |
---|
208 | s/\n +/\n/g; |
---|
209 | s/\s*$//g; |
---|
210 | s/ +/ /g; # replace double whitespaces by one |
---|
211 | s/(\w+\(.*?\))/\@code{$1}/g; |
---|
212 | s/\@\*\s*/\@\*/g; |
---|
213 | s/(\@[^\*])/\@$1/g; # escape @ signs, except @* |
---|
214 | s/{/\@{/g; # escape {} |
---|
215 | s/}/\@}/g; |
---|
216 | # unprotect @@math@{@}, @code@{@} |
---|
217 | while (s/\@\@math\@{(.*?)\@}/\@math{$1}/g) {} |
---|
218 | while (s/\@\@code\@{(.*?)\@}/\@code{$1}/g) {} |
---|
219 | # remove @code{} inside @code{} and inside @math{} |
---|
220 | while (s/\@math{([^}]*)\@code{(.*?)}(.*)?}/\@math{$1$2$3}/g) {} |
---|
221 | while (s/\@code{([^}]*)\@code{(.*?)}(.*)?}/\@code{$1$2$3}/g) {} |
---|
222 | } |
---|
223 | |
---|
224 | sub OutInfoItem |
---|
225 | { |
---|
226 | my ($FH, $item, $text, $l_fun) = @_; |
---|
227 | |
---|
228 | $item = lc $item; |
---|
229 | $item = ucfirst $item; |
---|
230 | |
---|
231 | if ($item =~ /see also/i) |
---|
232 | { |
---|
233 | # return references |
---|
234 | return $text; |
---|
235 | } |
---|
236 | elsif ($item =~ m/example/i) |
---|
237 | { |
---|
238 | # forget about example, since it comes explicitely |
---|
239 | return ''; |
---|
240 | } |
---|
241 | elsif ($item =~ m/procedure/i) |
---|
242 | { |
---|
243 | if ($l_fun && $table_is_open) |
---|
244 | { |
---|
245 | print $FH "\@end table\n\n"; |
---|
246 | $table_is_open = 0; |
---|
247 | } |
---|
248 | $text =~ s/^\s*//; |
---|
249 | $text =~ s/\s*$//; |
---|
250 | $text =~ s/.*$// if ($text=~/parameters.*brackets.*are.*optional.*$/); |
---|
251 | $text .= "\n"; |
---|
252 | |
---|
253 | my ($proc, $pargs, $pinfo, $line); |
---|
254 | if ($l_fun) |
---|
255 | { |
---|
256 | print $FH "\@strong{$item:}\n\@menu\n"; |
---|
257 | } |
---|
258 | else |
---|
259 | { |
---|
260 | print $FH "\@item \@strong{$item:}\n\@table \@asis\n"; |
---|
261 | } |
---|
262 | while ($text =~ /(.*\n)/g) |
---|
263 | { |
---|
264 | $line = $1; |
---|
265 | if ($1 =~ /^\s*(\w+)\((.*?)\)/) |
---|
266 | { |
---|
267 | OutProcInfo($FH, $proc, $procargs, $pinfo, $l_fun) if $proc && $pinfo; |
---|
268 | $proc = $1; |
---|
269 | $procargs = $2; |
---|
270 | $pinfo = $'; |
---|
271 | } |
---|
272 | else |
---|
273 | { |
---|
274 | $pinfo .= $line; |
---|
275 | } |
---|
276 | } |
---|
277 | OutProcInfo($FH, $proc, $procargs, $pinfo, $l_fun) if $proc && $pinfo; |
---|
278 | print $FH ($l_fun ? "\@end menu\n" : "\@end table\n"); |
---|
279 | return ''; |
---|
280 | } |
---|
281 | |
---|
282 | if (! $table_is_open) |
---|
283 | { |
---|
284 | print $FH "\@table \@asis\n"; |
---|
285 | $table_is_open = 1; |
---|
286 | } |
---|
287 | print $FH '@item @strong{'. "$item:}\n"; |
---|
288 | # prepare text: |
---|
289 | local $_ = $text; |
---|
290 | if (($item =~ m/^library/i) && m/\s*(\w*)\.lib/) |
---|
291 | { |
---|
292 | print $FH "$1.lib\n"; |
---|
293 | $text = $'; |
---|
294 | if ($text =~ /\w/) |
---|
295 | { |
---|
296 | print $FH '@item @strong{Purpose:'."}\n"; |
---|
297 | print $FH lc $text; |
---|
298 | } |
---|
299 | } |
---|
300 | else |
---|
301 | { |
---|
302 | # just print the text |
---|
303 | FormatInfoText(length($item) + 1); |
---|
304 | print $FH "$_\n"; |
---|
305 | } |
---|
306 | return ''; |
---|
307 | } |
---|
308 | |
---|
309 | sub OutProcInfo |
---|
310 | { |
---|
311 | my($FH, $proc, $procargs, $pinfo, $l_fun) = @_; |
---|
312 | local $_ = $pinfo; |
---|
313 | s/^[;\s]*//; |
---|
314 | s/\n/ /g; |
---|
315 | FormatInfoText(); |
---|
316 | |
---|
317 | if ($l_fun) |
---|
318 | { |
---|
319 | print $FH "* ${proc}:: $_\n"; |
---|
320 | push @procs, $proc; |
---|
321 | } |
---|
322 | else |
---|
323 | { |
---|
324 | print $FH "\@item \@code{$proc($procargs)} "; |
---|
325 | print $FH "\n\@cindex $proc\n$_\n"; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | sub OutRef |
---|
330 | { |
---|
331 | my ($FH, $refs) = @_; |
---|
332 | $refs =~ s/^\s*//; |
---|
333 | $refs =~ s/\s*$//; |
---|
334 | $refs =~ s/\n/,/g; |
---|
335 | my @refs = split (/[,;\.]+/, $refs); |
---|
336 | my $ref; |
---|
337 | |
---|
338 | print $FH "\@c ref\nSee also:\n"; |
---|
339 | $ref = shift @refs; |
---|
340 | print $FH "\@ref{$ref}"; |
---|
341 | for $ref (@refs) |
---|
342 | { |
---|
343 | $ref =~ s/^\s*//; |
---|
344 | $ref =~ s/\s*$//; |
---|
345 | print $FH ", \@ref{$ref}" if ($ref =~ /\w/); |
---|
346 | } |
---|
347 | print $FH "\n\@c ref\n"; |
---|
348 | } |
---|
349 | |
---|
350 | sub CleanUpExample |
---|
351 | { |
---|
352 | local($lib, $example) = @_; |
---|
353 | |
---|
354 | # find portion in {} |
---|
355 | $example =~ s/^[^{]*{(.*)}[^}]*$/$1/s; |
---|
356 | |
---|
357 | if ($example =~ /EXAMPLE: \(not executed\)/) |
---|
358 | { |
---|
359 | # erase first three lines |
---|
360 | $example =~ s/^.*\n.*\n.*\n/\n/; |
---|
361 | # erase enclosing " " in every line |
---|
362 | $example =~ s/\n\s*"/\n/g; |
---|
363 | $example =~ s/";\n/\n/g; |
---|
364 | } |
---|
365 | # erase EXAMPLE, echo and pause statements |
---|
366 | $example =~ s/"EXAMPLE.*"[^;]*;//g; |
---|
367 | $example .= "\n"; |
---|
368 | my ($mexample, $line); |
---|
369 | while ($example =~ m/(.*)\n/g) |
---|
370 | { |
---|
371 | $line = $1; |
---|
372 | $line =~ s|echo[^;]*;||g if $line !~ m|(.*)//(.*)echo[^;]*;|; |
---|
373 | $line =~ s|pause\(.*?\)[^;]*;||g if $line !~ m|(.*)//(.*)pause\(.*?\)[^;]*;|; |
---|
374 | $mexample .= "$line\n"; |
---|
375 | } |
---|
376 | $example = $mexample; |
---|
377 | |
---|
378 | # prepend LIB command |
---|
379 | $example = "LIB \"$lib.lib\";\n".$example |
---|
380 | if ($example && $lib ne "standard"); |
---|
381 | # erase empty lines |
---|
382 | $example =~ s/^\s*\n//g; |
---|
383 | # erase spaces from beginning of lines |
---|
384 | $example =~ s/\n\s*/\n/g; |
---|
385 | $example =~ s/\s*$//g; |
---|
386 | return $example; |
---|
387 | } |
---|
388 | |
---|