The R Project SVN R

Rev

Rev 47905 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 47905 Rev 47912
1
## Rd2dvi -- Convert man pages (*.Rd help files) via LaTeX to DVI/PDF.
1
## Rd2dvi -- Convert man pages (*.Rd help files) via LaTeX to DVI/PDF.
2
##
2
##
3
## Examples:
3
## Examples:
4
##  R CMD Rd2dvi /path/to/Rsrc/src/library/base/man/Normal.Rd
4
##  R CMD Rd2dvi /path/to/Rsrc/src/library/base/man/Normal.Rd
5
##  R CMD Rd2dvi `grep -l "\\keyword{distr" \
5
##  R CMD Rd2dvi `grep -l "\\keyword{distr" \
6
##                  /path/to/Rsrc/src/library/stats/man/*.Rd | sort | uniq`
6
##                  /path/to/Rsrc/src/library/stats/man/*.Rd | sort | uniq`
7
 
7
 
8
revision='$Rev: 47905 $'
8
revision='$Rev: 47912 $'
9
version=`set - ${revision}; echo ${2}`
9
version=`set - ${revision}; echo ${2}`
10
version="Rd2dvi: ${R_VERSION} (r${version})
10
version="Rd2dvi: ${R_VERSION} (r${version})
11
 
11
 
12
Copyright (C) 2000-2009 The R Core Development Team.
12
Copyright (C) 2000-2009 The R Core Development Team.
13
This is free software; see the GNU General Public License version 2
13
This is free software; see the GNU General Public License version 2
14
or later for copying conditions.  There is NO warranty."
14
or later for copying conditions.  There is NO warranty."
15
 
15
 
16
usage="Usage: R CMD Rd2dvi [options] files
16
usage="Usage: R CMD Rd2dvi [options] files
17
 
17
 
18
Generate DVI (or PDF) output from the Rd sources specified by files, by
18
Generate DVI (or PDF) output from the Rd sources specified by files, by
19
either giving the paths to the files, or the path to a directory with
19
either giving the paths to the files, or the path to a directory with
20
the sources of a package/bundle, or an installed package.
20
the sources of a package/bundle, or an installed package.
21
 
21
 
22
Unless specified via option '--output', the basename of the output file
22
Unless specified via option '--output', the basename of the output file
23
equals the basename of argument 'files' if this specifies a package
23
equals the basename of argument 'files' if this specifies a package
24
(bundle) or a single file, and 'Rd2' otherwise.
24
(bundle) or a single file, and 'Rd2' otherwise.
25
 
25
 
26
The Rd sources are assumed to be ASCII unless they contain \encoding
26
The Rd sources are assumed to be ASCII unless they contain \encoding
27
declarations (which take priority) or --encoding is supplied or if using
27
declarations (which take priority) or --encoding is supplied or if using
28
package sources, if the package DESCRIPTION file has an Encoding field.
28
package sources, if the package DESCRIPTION file has an Encoding field.
29
 
29
 
30
Options:
30
Options:
31
  -h, --help		print short help message and exit
31
  -h, --help		print short help message and exit
32
  -v, --version		print version info and exit
32
  -v, --version		print version info and exit
33
      --batch		no interaction
33
      --batch		no interaction
34
      --debug		turn on shell debugging (set -x)
34
      --debug		turn on shell debugging (set -x)
35
      --no-clean	do not remove created temporary files
35
      --no-clean	do not remove created temporary files
36
      --no-preview	do not preview generated DVI file
36
      --no-preview	do not preview generated DVI file
37
      --encoding=enc    use 'enc' as the default encoding
37
      --encoding=enc    use 'enc' as the default encoding
38
      --os=NAME		use OS subdir 'NAME' (unix or windows)
38
      --os=NAME		use OS subdir 'NAME' (unix or windows)
39
      --OS=NAME		the same as '--os'
39
      --OS=NAME		the same as '--os'
40
  -o, --output=FILE	write output to FILE
40
  -o, --output=FILE	write output to FILE
41
      --pdf		generate PDF output
41
      --pdf		generate PDF output
42
      --title=NAME	use NAME as the title of the document
42
      --title=NAME	use NAME as the title of the document
43
  -V, --verbose		report on what is done
43
  -V, --verbose		report on what is done
44
      --no-index	don't index PDF output
44
      --no-index	don't index PDF output
45
      --no-description	don't typeset the description of a package
45
      --no-description	don't typeset the description of a package
46
      --internals	typeset 'internal' documentatiom (usually skipped)
46
      --internals	typeset 'internal' documentatiom (usually skipped)
47
 
47
 
48
The output papersize is set by the environment variable R_PAPERSIZE.
48
The output papersize is set by the environment variable R_PAPERSIZE.
49
The DVI previewer is set by the environment variable xdvi.
49
The DVI previewer is set by the environment variable xdvi.
50
The PDF previewer is set by the environment variable R_PDFVIEWER.
50
The PDF previewer is set by the environment variable R_PDFVIEWER.
51
 
51
 
52
Report bugs to <r-bugs@r-project.org>."
52
Report bugs to <r-bugs@r-project.org>."
53
 
53
 
54
## workaround the export of CDPATH, which may cause 'cd ${build_dir}' to echo
54
## workaround the export of CDPATH, which may cause 'cd ${build_dir}' to echo
55
unset CDPATH
55
unset CDPATH
56
 
56
 
57
start_dir=`pwd`
57
start_dir=`pwd`
58
build_dir=".Rd2dvi${$}"
58
build_dir=".Rd2dvi${$}"
59
 
59
 
-
 
60
title=""
60
batch=false
61
batch=false
61
clean=true
62
clean=true
62
debug=false
63
debug=false
63
only_meta=false
64
only_meta=false
64
out_ext="dvi"
65
out_ext="dvi"
65
output=""
66
output=""
66
verbose=false
67
verbose=false
67
enc=unknown
68
enc=unknown
68
index=true
69
index=true
69
description=true
70
description=true
70
internals=no
71
internals=no
71
 
72
 
72
if test "${R_OSTYPE}" = "windows"; then
73
if test "${R_OSTYPE}" = "windows"; then
73
  echo=echo
74
  echo=echo
74
  preview=${xdvi-open}
75
  preview=${xdvi-open}
75
  OSdir=windows
76
  OSdir=windows
76
else
77
else
77
  ## Need a `safe' echo which does not interpret backslash-escaped
78
  ## Need a `safe' echo which does not interpret backslash-escaped
78
  ## characters in SysV style.
79
  ## characters in SysV style.
79
  ## FIXME: R_SHARE_DIR might contain a space.
80
  ## FIXME: R_SHARE_DIR might contain a space.
80
  echo="sh ${R_SHARE_DIR}/sh/echo.sh"
81
  echo="sh ${R_SHARE_DIR}/sh/echo.sh"
81
  preview=${xdvi-xdvi}
82
  preview=${xdvi-xdvi}
82
  OSdir=unix
83
  OSdir=unix
83
fi
84
fi
84
 
85
 
85
while test -n "${1}"; do
86
while test -n "${1}"; do
86
  case ${1} in
87
  case ${1} in
87
    -h|--help)
88
    -h|--help)
88
      ${echo} "${usage}"; exit 0 ;;
89
      ${echo} "${usage}"; exit 0 ;;
89
    -v|--version)
90
    -v|--version)
90
      ${echo} "${version}"; exit 0 ;;
91
      ${echo} "${version}"; exit 0 ;;
91
    --batch)
92
    --batch)
92
      batch=true ;;
93
      batch=true ;;
93
    --debug)
94
    --debug)
94
      debug=true ;;
95
      debug=true ;;
95
    --no-clean)
96
    --no-clean)
96
      clean=false ;;
97
      clean=false ;;
97
    --no-preview)
98
    --no-preview)
98
      preview=false ;;
99
      preview=false ;;
99
    --pdf)
100
    --pdf)
100
      out_ext="pdf";
101
      out_ext="pdf";
101
      ## allow for --no-preview --pdf
102
      ## allow for --no-preview --pdf
102
      if test "${preview}" != "false"; then
103
      if test "${preview}" != "false"; then
103
	if test -n "${R_PDFVIEWER}"; then
104
	if test -n "${R_PDFVIEWER}"; then
104
	    preview=${R_PDFVIEWER}
105
	    preview=${R_PDFVIEWER}
105
	elif test "${R_OSTYPE}" = "windows"; then
106
	elif test "${R_OSTYPE}" = "windows"; then
106
	    preview=open
107
	    preview=open
107
	else
108
	else
108
	    preview=false
109
	    preview=false
109
	fi
110
	fi
110
      fi
111
      fi
111
      R_RD4DVI=${R_RD4PDF-"times,hyper"}
112
      R_RD4DVI=${R_RD4PDF-"times,hyper"}
112
      R_LATEXCMD="${R_PDFLATEXCMD}"
113
      R_LATEXCMD="${R_PDFLATEXCMD}"
113
      ;;
114
      ;;
114
    --title=*)
115
    --title=*)
115
      title=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
116
      title=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
116
    -o)
117
    -o)
117
      if test -n "`echo ${2} | ${SED} 's/^-.*//'`"; then
118
      if test -n "`echo ${2} | ${SED} 's/^-.*//'`"; then
118
	output="${2}"; shift
119
	output="${2}"; shift
119
      else
120
      else
120
	echo "ERROR: option '${1}' requires an argument"
121
	echo "ERROR: option '${1}' requires an argument"
121
	exit 1
122
	exit 1
122
      fi
123
      fi
123
      ;;
124
      ;;
124
    --only-meta)
125
    --only-meta)
125
      only_meta=true ;;
126
      only_meta=true ;;
126
    --output=*)
127
    --output=*)
127
      output=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
128
      output=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
128
    --OS=*|--os=*)
129
    --OS=*|--os=*)
129
      OSdir=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
130
      OSdir=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
130
    --encoding=*)
131
    --encoding=*)
131
      enc=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
132
      enc=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
132
    --build-dir=*)
133
    --build-dir=*)
133
      build_dir=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
134
      build_dir=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
134
    -V|--verbose)
135
    -V|--verbose)
135
      verbose=${echo} ;;
136
      verbose=${echo} ;;
136
    --no-index)
137
    --no-index)
137
      index=false ;;
138
      index=false ;;
138
    --no-description)
139
    --no-description)
139
      description=false ;;
140
      description=false ;;
140
    --internals)
141
    --internals)
141
      internals="yes" ;;
142
      internals="yes" ;;
142
    --|*)
143
    --|*)
143
      break ;;
144
      break ;;
144
  esac
145
  esac
145
  shift
146
  shift
146
done
147
done
147
 
148
 
148
if ${debug}; then set -x; fi
149
if ${debug}; then set -x; fi
149
 
150
 
150
. ${R_SHARE_DIR}/sh/dcf.sh	# get_dcf_field()
-
 
151
 
-
 
152
 
-
 
153
do_cleanup () {
151
do_cleanup () {
154
  if ${clean}; then
152
  if ${clean}; then
155
    cd ${start_dir}
153
    cd ${start_dir}
156
    rm -rf ${build_dir}
154
    rm -rf ${build_dir}
157
  else
155
  else
158
    echo "You may want to clean up by 'rm -rf ${build_dir}'"
156
    echo "You may want to clean up by 'rm -rf ${build_dir}'"
159
  fi
157
  fi
160
}
158
}
161
 
159
 
162
is_bundle=no
160
is_bundle=no
163
file_sed='s/[_$]/\\&/g'
-
 
164
 
161
 
165
if test -d "${1}"; then
162
if test -d "${1}"; then
166
  if test -f ${1}/DESCRIPTION; then
163
  if test -f ${1}/DESCRIPTION; then
167
    if test -n "`grep '^Bundle:' ${1}/DESCRIPTION`"; then
164
    if test -n "`grep '^Bundle:' ${1}/DESCRIPTION`"; then
168
      echo "Hmm ... looks like a package bundle"
165
      echo "Hmm ... looks like a package bundle"
169
      is_bundle=yes
166
      is_bundle=yes
170
      bundle_name=`get_dcf_field Bundle "${1}/DESCRIPTION"`
-
 
171
      title=${title-"Bundle \`${bundle_name}'"}
-
 
172
    else
167
    else
173
      echo "Hmm ... looks like a package"
168
      echo "Hmm ... looks like a package"
174
      package_name=`get_dcf_field Package "${1}/DESCRIPTION"`
-
 
175
      title=${title-"Package \`${package_name}'"}
-
 
176
      dir=${1}
169
      dir=${1}
177
    fi
170
    fi
178
    test -z "${output}" && output="`basename ${1}`.${out_ext}"
171
    test -z "${output}" && output="`basename ${1}`.${out_ext}"
179
  elif test -f ${1}/DESCRIPTION.in && \
172
  elif test -f ${1}/DESCRIPTION.in && \
180
       test -n "`grep '^Priority: *base' ${1}/DESCRIPTION.in`"; then
173
       test -n "`grep '^Priority: *base' ${1}/DESCRIPTION.in`"; then
181
    echo "Hmm ... looks like a package from the R distribution"
174
    echo "Hmm ... looks like a package from the R distribution"
182
    package_name=`get_dcf_field Package "${1}/DESCRIPTION.in"`
-
 
183
    title=${title-"Package \`${package_name}'"}
-
 
184
    dir=${1}
175
    dir=${1}
185
    test -z "${output}" && output="`basename ${1}`.${out_ext}"
176
    test -z "${output}" && output="`basename ${1}`.${out_ext}"
186
  else
177
  else
187
    if test -d ${1}/man; then
178
    if test -d ${1}/man; then
188
      dir=${1}/man
179
      dir=${1}/man
189
    else
180
    else
190
      dir=${1}
181
      dir=${1}
191
    fi
182
    fi
192
    subj0=`${echo} ${dir} | ${SED} -e ${file_sed}`
-
 
193
    subj="all in \\\\\\\\file{${subj0}}"
-
 
194
  fi
183
  fi
195
else
184
else
196
  if test ${#} -gt 1 ; then
185
  if test ${#} -eq 1 ; then
197
    subj=" etc.";
-
 
198
  else
-
 
199
    subj=
-
 
200
    if test -z "${output}"; then
186
    if test -z "${output}"; then
201
      output=`basename "${1}"`
187
      output=`basename "${1}"`
202
      output="`echo ${output} | ${SED} 's/[Rr]d$//'`${out_ext}"
188
      output="`echo ${output} | ${SED} 's/[Rr]d$//'`${out_ext}"
203
    fi
189
    fi
204
  fi
190
  fi
205
  subj0=`${echo} ${1} | ${SED} -e ${file_sed}`
-
 
206
  subj="\\\\\\\\file{${subj0}}${subj}"
-
 
207
fi
191
fi
208
 
192
 
209
 
-
 
210
## substitution went wrong under ash
-
 
211
title1="\\\\\\\\R{} documentation}} \\\\\\\\par\\\\\\\\bigskip{{''\\\\\\\\Large of ${subj}"
-
 
212
title=${title-$title1}
-
 
213
 
-
 
214
 
-
 
215
## Prepare for building the documentation.
193
## Prepare for building the documentation.
216
if test -z "${output}"; then
194
if test -z "${output}"; then
217
  output="Rd2.${out_ext}"
195
  output="Rd2.${out_ext}"
218
fi
196
fi
219
if test -f ${output}; then
197
if test -f ${output}; then
220
  echo "file '${output}' exists; please remove first"
198
  echo "file '${output}' exists; please remove first"
221
  exit 1
199
  exit 1
222
fi
200
fi
223
if test -d ${build_dir}; then
201
if test -d ${build_dir}; then
224
  rm -rf ${build_dir} || echo "cannot write to build dir" && exit 2
202
  rm -rf ${build_dir} || echo "cannot write to build dir" && exit 2
225
fi
203
fi
226
mkdir ${build_dir}
204
mkdir ${build_dir}
227
 
205
 
228
echo "tools:::.Rd2dvi(\"${1}\",  \"${build_dir}/Rd2.tex\", \"$is_bundle\", \
206
echo "tools:::.Rd2dvi(\"${1}\",  \"${build_dir}/Rd2.tex\", \"$is_bundle\", \
229
\"$title\", \"$batch\", \"$description\", \"$only_meta\",\
207
\"$title\", \"$batch\", \"$description\", \"$only_meta\",\
230
\"$enc\", \"${dir-$@}\", \"OSdir\", \"$internals\", \"$index\")" \
208
\"$enc\", \"${dir-$@}\", \"OSdir\", \"$internals\", \"$index\")" \
231
  | LC_ALL=C ${R_HOME}/bin/R --slave --vanilla || exit 1
209
  | LC_ALL=C ${R_HOME}/bin/R --slave --vanilla || exit 1
232
 
210
 
233
## <FIXME>
211
## <FIXME>
234
## Need to do something smarter about the exit status in batch mode.
212
## Need to do something smarter about the exit status in batch mode.
235
status=0
213
status=0
236
## <FIXME>
214
## <FIXME>
237
 
215
 
238
 
216
 
239
R_TEXOPTS=
217
R_TEXOPTS=
240
if test "${R_OSTYPE}" = "windows"; then
218
if test "${R_OSTYPE}" = "windows"; then
241
  miktex=`latex --version | grep ^MiKTeX | wc -l`
219
  miktex=`latex --version | grep ^MiKTeX | wc -l`
242
  if test "${miktex}" = "1"; then
220
  if test "${miktex}" = "1"; then
243
    R_TEXOPTS=--include-directory=${R_HOME}/share/texmf
221
    R_TEXOPTS=--include-directory=${R_HOME}/share/texmf
244
  fi
222
  fi
245
fi
223
fi
246
 
224
 
247
echo "Creating ${out_ext} output from LaTeX ..."
225
echo "Creating ${out_ext} output from LaTeX ..."
248
cd ${build_dir}
226
cd ${build_dir}
249
if test "${out_ext}" = pdf; then
227
if test "${out_ext}" = pdf; then
250
  ${R_PDFLATEXCMD} ${R_TEXOPTS} Rd2 || status=1
228
  ${R_PDFLATEXCMD} ${R_TEXOPTS} Rd2 || status=1
251
  if test $status -gt 0; then
229
  if test $status -gt 0; then
252
    echo "Error in running pdflatex command ('${R_PDFLATEXCMD}')"
230
    echo "Error in running pdflatex command ('${R_PDFLATEXCMD}')"
253
    do_cleanup
231
    do_cleanup
254
    exit 1
232
    exit 1
255
  fi
233
  fi
256
  if test "${index}" != "false"; then
234
  if test "${index}" != "false"; then
257
    ## OK for R_MAKEINDEXCMD to fail.
235
    ## OK for R_MAKEINDEXCMD to fail.
258
    ${R_MAKEINDEXCMD} Rd2
236
    ${R_MAKEINDEXCMD} Rd2
259
    ${R_PDFLATEXCMD} ${R_TEXOPTS} Rd2 || status=1
237
    ${R_PDFLATEXCMD} ${R_TEXOPTS} Rd2 || status=1
260
    if test $status -gt 0; then
238
    if test $status -gt 0; then
261
      echo "Error in running pdflatex command ('${R_PDFLATEXCMD}')"
239
      echo "Error in running pdflatex command ('${R_PDFLATEXCMD}')"
262
      do_cleanup
240
      do_cleanup
263
      exit 1
241
      exit 1
264
    fi
242
    fi
265
  fi
243
  fi
266
  ${R_PDFLATEXCMD} ${R_TEXOPTS} Rd2 || status=1
244
  ${R_PDFLATEXCMD} ${R_TEXOPTS} Rd2 || status=1
267
  if test $status -gt 0; then
245
  if test $status -gt 0; then
268
    echo "Error in running pdflatex command ('${R_PDFATEXCMD}')"
246
    echo "Error in running pdflatex command ('${R_PDFATEXCMD}')"
269
    do_cleanup
247
    do_cleanup
270
    exit 1
248
    exit 1
271
  fi
249
  fi
272
else
250
else
273
  ${R_LATEXCMD} ${R_TEXOPTS} Rd2 || status=1
251
  ${R_LATEXCMD} ${R_TEXOPTS} Rd2 || status=1
274
  if test $status -gt 0; then
252
  if test $status -gt 0; then
275
    echo "Error in running latex command ('${R_LATEXCMD}')"
253
    echo "Error in running latex command ('${R_LATEXCMD}')"
276
    do_cleanup
254
    do_cleanup
277
    exit 1
255
    exit 1
278
  fi
256
  fi
279
  ## OK for R_MAKEINDEXCMD to fail.
257
  ## OK for R_MAKEINDEXCMD to fail.
280
  ${R_MAKEINDEXCMD} Rd2
258
  ${R_MAKEINDEXCMD} Rd2
281
  ${R_LATEXCMD} ${R_TEXOPTS} Rd2 || status=1
259
  ${R_LATEXCMD} ${R_TEXOPTS} Rd2 || status=1
282
  if test $status -gt 0; then
260
  if test $status -gt 0; then
283
    echo "Error in running latex command ('${R_LATEXCMD}')"
261
    echo "Error in running latex command ('${R_LATEXCMD}')"
284
    do_cleanup
262
    do_cleanup
285
    exit 1
263
    exit 1
286
  fi
264
  fi
287
fi
265
fi
288
if test ! -f Rd2.${out_ext}; then
266
if test ! -f Rd2.${out_ext}; then
289
  echo "Error: no output file was created"
267
  echo "Error: no output file was created"
290
  do_cleanup
268
  do_cleanup
291
  exit 1
269
  exit 1
292
fi
270
fi
293
cd ${start_dir}
271
cd ${start_dir}
294
echo "Saving output to '${output}' ..."
272
echo "Saving output to '${output}' ..."
295
cp ${build_dir}/Rd2.${out_ext} ${output}
273
cp ${build_dir}/Rd2.${out_ext} ${output}
296
echo "Done"
274
echo "Done"
297
 
275
 
298
do_cleanup
276
do_cleanup
299
${preview} ${output}
277
${preview} ${output}
300
exit ${status}
278
exit ${status}
301
 
279
 
302
### Local Variables: ***
280
### Local Variables: ***
303
### mode: sh ***
281
### mode: sh ***
304
### sh-indentation: 2 ***
282
### sh-indentation: 2 ***
305
### End: ***
283
### End: ***