/
Sd2Rd.in
263 lines (246 loc) · 6.53 KB
/
Sd2Rd.in
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
#! @PERL@
# Convert S documentation to R documentation format
# Usage: Sd2Rd [-n] [-x] file
# The option `-x' makes the program greedily interpret all single-quoted
# names intended as code names.
# The option `-n' renders examples non-executable by wrapping them into
# a \dontrun{} environment.
# Copyright (C) 1997, 1998, 1999 Kurt Hornik
#
# This document 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, 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.
#
# A copy of the GNU General Public License is available via WWW at
# http://www.gnu.org/copyleft/gpl.html. You can also obtain it by
# writing to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA.
# Bug reports to Kurt.Hornik@ci.tuwien.ac.at
$VERSION = "0.3-3";
($NAME = $0) =~ s|.*/||;
$\ = "\n";
$braceLevel = 0;
$inVerbatim = 0;
$inSeeAlso = 0;
$doprint = 1;
$needArg = 1;
$needVal = 0;
$output = "";
use Getopt::Long;
GetOptions(("n", "x")) || &usage();
while (<>) {
chop;
&substitute unless /^\./;
@word = split;
if (/^\s*$/) { &output("\n"); }
if (/^[^.]/) { &output($_); }
## Added by BDR 1998/08/27
if (/^\.\\\"/o) {
s/^\.\\\"/%/;
&output($_);
}
## End
if (/^\.AG/) {
if ($needArg) {
§ion(0, "\\arguments\{");
$needArg = 0;
}
§ion(1, "\\item\{$word[1]\}\{");
}
if (/^\.CS/) {
§ion(0, "\\usage\{");
$inVerbatim = 1;
}
if (/^\.DN/) { §ion(0, "\\description\{"); }
if (/^\.DT/) { §ion(0, "\\details\{"); }
if (/^\.EX/) {
if ($opt_n) {
§ion(1, "\\examples\{\\dontrun\{");
} else {
§ion(0, "\\examples\{");
}
$inVerbatim = 1;
}
if (/^\.FN/) {
unless($fun) { $fun = $word[1]; }
push(@aliases, $word[1]);
}
if (/^\.(IP|PP)/) { &output("\n"); }
if (/^\.KW/) {
if ($braceLevel > 0) {
§ion(0, "");
$braceLevel = 0;
}
if ($word[1] =~ /sysdata/) {
&output("\\keyword\{datasets\}");
} else {
&output("\\keyword\{$word[1]\}");
}
}
if (/^\.RC/) {
if ($needVal) {
$needVal = 0;
§ion(0, "\\value\{\n$output\n");
$doprint = 1;
}
§ion(1, "\\item\{$word[1]\}\{");
}
if (/^\.RT/) {
$needVal = 1;
$doprint = 0;
$output = "";
}
if (/^\.SA/) {
§ion(0, "\\seealso\{");
$inSeeAlso = 1;
}
if (/^\.SE/) { §ion(0, "\\section\{Side Effects\}\{"); }
if (/^\.SH/) {
if ($word[1] =~ /REFERENCE/) {
§ion(0, "\\references\{");
} elsif ($word[1] =~ /SOURCE/) {
§ion(0, "\\source\{");
} elsif ($word[1] =~ /SUMMARY/) {
§ion(0, "\\description\{");
} elsif (join(" ", @word[1..2]) =~ /DATA DESCRIPTION/) {
§ion(0, "\\format\{");
} else {
# This line may be of the form .SH "A B C"
($tmp = join(" ", @word[1..$#word])) =~ s/\"(.*)\"/$1/;
§ion(0, "\\section\{$tmp\}\{");
}
}
if (/^\.sp/) { output("\n"); }
if (/^\.TL/) {
print("\\name\{$fun\}");
print("\\alias\{", join("\}\n\\alias\{", @aliases), "\}");
§ion(0, "\\title\{");
$inVerbatim = 1;
}
if (/^\.WR/) {
§ion(0, "");
print("% Converted by $NAME version $VERSION.");
}
if (/^\.AO/) {
output("Arguments for function \\code\{$word[1]()\} can also be");
output("supplied to this function.");
}
if (/^\.GE/) {
output("This is a generic function.");
output("Functions with names beginning in \\code\{$fun.\} will be");
output("methods for this function.");
}
if (/^\.GR/) {
output("Graphical parameters (see \\code\{\\link\{par\}\}) may also");
output("be supplied as arguments to this function.");
}
if (/^\.ME/) {
output("This function is a method for the generic function");
output("\\code\{$word[1]()\} for class \\code\{$word[2]\}.");
output("It can be invoked by calling \\code\{$word[1](x)\} for an");
output("object \\code\{x\} of the appropriate class, or directly by");
output("calling \\code\{$word[1].$word[2](x)\} regardless of the");
output("class of the object.");
}
if (/^\.NA/) { output("Missing values (\\code\{NA\}s) are allowed."); }
if (/^\.Tl/) {
output("In addition, the high-level graphics control arguments");
output("described under \\code\{\\link\{par\}\} and the arguments to");
output("\\code\{\\link\{title\}\} may be supplied to this function.");
}
## Added by BDR 1998/06/20
if (/^\.ul/) {
$_ = <>;
&substitute;
chomp;
output("\\emph{".$_."\}");
}
## End
}
sub substitute {
if (!$inVerbatim) {
s/\{/\\\{/g;
s/\}/\\\}/g;
s/&/\\&/g;
## Added by BDR 1998/06/20
s/\\\(aa/'/g; # extra ' for highlight matching
s/\\\(em/--/g; # em dash
s/\\\(tm/ (TM) /g; # Trademark
s/\\\(mu/ x /g; # multiply sign
s/\\\(\*a/\alpha/g; # greek
s/\\\(\*b/\beta/g;
s/\\\(\*e/\epsilon/g;
s/\\\(\*l/\lambda/g;
s/\\\(\*m/\mu/g;
s/\\\(\*p/\pi/g;
s/\\\(\*s/\sigma/g;
## End
}
if ($inVerbatim) {
s/\.\.\./\\dots/g;
} else {
s/\.\.\./\\dots\{\}/g;
}
s/\\fB/\\bold\{/g;
s/\\fR/\}/g;
## Added by BDR 1998/06/20
s/\\fI/\\emph\{/g;
s/\\fP/\}/g;
## End
s/\%/\\%/g;
s/\\\.(.*)$/# $1)/g;
if ($inSeeAlso) {
if ($opt_x) {
s/\`?([\.\w]*\w+)\'?/\\code{\\link{$1}}/g;
} else {
s/\`([^\']*)\'/\\code{\\link{$1}}/g;
}
} elsif (!$inVerbatim) {
s/\`([^\']*)\'/\\code{$1}/g;
}
}
sub section {
local($level, $text) = @_;
$n = $braceLevel - $level;
print("\}" x $n) if ($n > 0);
if ($needVal) {
print("\\value\{\n$output\n\}");
$needVal = 0;
}
print("$text") if $text;
$braceLevel = $level + 1;
$inVerbatim = 0;
$inSeeAlso = 0;
$doprint = 1;
}
sub paragraph {
local($name) = @_;
&output("\n\\bold\{$name.\} ");
}
sub output {
local($text) = @_;
if ($doprint) {
print($text);
} elsif ($output) {
$output .= "\n$text";
} else {
$output = $text;
}
}
sub usage {
print("\n$NAME version $VERSION\n\nusage: $NAME [-n] [-x] file\n");
print("options:");
print(" -x interpret all single-quoted names as code names");
print(" -n render examples non-executable");
exit;
}
### Local Variables: ***
### mode: perl ***
### perl-indent-level: 2 ***
### End: ***