The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

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