The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11515 hornik 1
#! /bin/sh
6098 pd 2
 
11515 hornik 3
## Rd2dvi -- Convert man pages (*.Rd help files) via LaTeX to DVI/PDF.
4
##
5
## Examples:
6
##  R CMD Rd2dvi /path/to/Rsrc/src/library/base/man/Normal.Rd
7
##  R CMD Rd2dvi `grep -l "\\keyword{distr" \
8
##                  /path/to/Rsrc/src/library/base/man/*.Rd | sort | uniq`
9
 
26719 ripley 10
revision='$Revision: 1.40 $'
7367 hornik 11
version=`set - ${revision}; echo ${2}`
11515 hornik 12
version="Rd2dvi ${version}
7217 hornik 13
 
13529 hornik 14
Copyright (C) 2000-2001 The R Core Development Team.
11515 hornik 15
This is free software; see the GNU General Public Licence version 2
16
or later for copying conditions.  There is NO warranty." 
17
 
7367 hornik 18
usage="Usage: R CMD Rd2dvi [options] files
6098 pd 19
 
7367 hornik 20
Generate DVI (or PDF) output from the Rd sources specified by files, by
21
either giving the paths to the files, or the path to a directory with
22
the sources of a package.
23
 
20002 hornik 24
Unless specified via option '--output', the basename of the output file
25
equals the basename of argument 'files' if this specifies a package
26
(bundle) or a single file, and 'Rd2' otherwise.
15253 hornik 27
 
7367 hornik 28
Options:
29
  -h, --help		print short help message and exit
31770 ripley 30
  -v, --version		print Rd2dvi version info and exit
21964 hornik 31
      --batch		no interaction
11515 hornik 32
      --debug		turn on shell debugging (set -x)
33
      --no-clean	do not remove created temporary files
34
      --no-preview	do not preview generated output file
26719 ripley 35
      --os=NAME		use OS subdir 'NAME' (unix or windows)
20002 hornik 36
      --OS=NAME		the same as '--os'
9940 pd 37
  -o, --output=FILE	write output to FILE
11515 hornik 38
      --pdf		generate PDF output
39
      --title=NAME	use NAME as the title of the document
7367 hornik 40
  -V, --verbose		report on what is done
41
 
10633 hornik 42
Report bugs to <r-bugs@r-project.org>."
7367 hornik 43
 
44
start_dir=`pwd`
45
 
21964 hornik 46
batch=false
7367 hornik 47
clean=true
48
debug=false
9940 pd 49
out_ext="dvi"
7367 hornik 50
output=""
51
preview=xdvi
52
verbose=false
14341 hornik 53
OSdir=${R_OSTYPE-"unix"}
7367 hornik 54
 
34111 ripley 55
: ${R_SHARE_DIR}=${R_HOME}/share
15168 pd 56
## Need a `safe' echo which does not interpret backslash-escaped
57
## characters in SysV style.
34111 ripley 58
echo="sh ${R_SHARE_DIR}/sh/echo.sh"
15168 pd 59
 
6994 pd 60
while test -n "${1}"; do
61
  case ${1} in
7367 hornik 62
    -h|--help)
15168 pd 63
      ${echo} "${usage}"; exit 0 ;;
7367 hornik 64
    -v|--version)
15168 pd 65
      ${echo} "${version}"; exit 0 ;;
21964 hornik 66
    --batch)
67
      batch=true ;;
7367 hornik 68
    --debug)
69
      debug=true ;;
70
    --no-clean)
71
      clean=false ;;
72
    --no-preview)
73
      preview=false ;;
7295 leisch 74
    --pdf)
9940 pd 75
      out_ext="pdf";
7367 hornik 76
      preview=false;
30918 ripley 77
      R_RD4DVI=${R_RD4PDF-"times,hyper"};
13964 hornik 78
      R_LATEXCMD=${PDFLATEX-pdflatex} ;;
7295 leisch 79
    --title=*)
7367 hornik 80
      title=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
9940 pd 81
    -o)
82
      if test -n "`echo ${2} | sed 's/^-.*//'`"; then      
83
	output="${2}"; shift
84
      else
20002 hornik 85
	${echo} "ERROR: option '${1}' requires an argument"
9940 pd 86
	exit 1
87
      fi
88
      ;;
7333 leisch 89
    --output=*)
7367 hornik 90
      output=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
13529 hornik 91
    --OS=*|--os=*)
92
      OSdir=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
7367 hornik 93
    -V|--verbose)
15168 pd 94
      verbose=${echo} ;;
6994 pd 95
    --|*)
96
      break ;;
97
  esac
98
  shift
99
done
100
 
7367 hornik 101
if ${debug}; then set -x; fi
102
 
34111 ripley 103
. ${R_SHARE_DIR}/sh/dcf.sh	# get_dcf_field()
13529 hornik 104
 
105
Rdconv_dir_or_files_to_LaTeX () {
106
  ## Convert Rd files in a dir or a list of Rd files to LaTeX, appending
107
  ## the result to OUTFILE.
108
  ## Usage:
109
  ##   Rdconv_dir_or_files_to_LaTeX OUTFILE DIR
110
  ##   Rdconv_dir_or_files_to_LaTeX OUTFILE FILES
111
 
112
  ${verbose} $@
113
 
114
  out="${1}"
115
  shift
116
 
117
  if test -d ${1}; then
118
    files=`ls ${1}/*.[Rr]d`
119
    if test -d ${1}/${OSdir}; then
120
      files="${files} `ls ${1}/${OSdir}/*.[Rr]d`"
121
    fi
15168 pd 122
    files=`LC_ALL=C ${echo} ${files} | sort`
13529 hornik 123
  else
124
    files="${@}"
125
  fi
126
 
127
  echo "Converting Rd files to LaTeX ..."
128
 
129
  for f in ${files}; do
15168 pd 130
    ${echo} ${f}
13529 hornik 131
    ${R_CMD} Rdconv -t latex ${f} >> ${out}
132
  done
133
 
134
}
135
 
14002 hornik 136
Rd_DESCRIPTION_to_LaTeX () {
137
  ## Typeset the contents of a DESCRIPTION file in a LaTeX description
138
  ## list.
139
  ## Usage:
140
  ##   Rd_DESCRIPTION_to_LaTeX FILE
15168 pd 141
 
14002 hornik 142
  fields=`sed '/^[ 	]/d; s/^\([^:]*\):.*$/\1/' $1`
15168 pd 143
  ${echo} "\\begin{description}"
144
  ${echo} "\\raggedright{}"
145
  for f in `${echo} "${fields}" | sed '/Package/d; /Bundle/d;'`; do
14002 hornik 146
    text=`get_dcf_field ${f} ${1}`
15168 pd 147
    ${echo} "\\item[${f}] \\AsIs{${text}}"
14002 hornik 148
  done
15168 pd 149
  ${echo} "\\end{description}"
14002 hornik 150
}
151
 
13529 hornik 152
is_bundle=no
14544 hornik 153
is_base_package=no
13529 hornik 154
file_sed='s/[_$]/\\&/g'
155
 
9940 pd 156
toc="\\Rdcontents{\\R{} topics documented:}"
13529 hornik 157
if test -d "${1}"; then
158
  if test -f ${1}/DESCRIPTION; then
159
    if test -n "`grep '^Bundle:' ${1}/DESCRIPTION`"; then
160
      echo "Hmm ... looks like a package bundle"
161
      is_bundle=yes
162
      bundle_name=`get_dcf_field Bundle "${1}/DESCRIPTION"`
163
      bundle_pkgs=`get_dcf_field Contains "${1}/DESCRIPTION"`
164
      title=${title-"Bundle \`${bundle_name}'"}
165
    else
166
      echo "Hmm ... looks like a package"
14002 hornik 167
      package_name=`get_dcf_field Package "${1}/DESCRIPTION"`
168
      title=${title-"Package \`${package_name}'"}
13529 hornik 169
      dir=${1}/man
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
    is_base_package=yes
175
    echo "Hmm ... looks like a package from the R distribution"
176
    package_name=`get_dcf_field Package "${1}/DESCRIPTION.in"`
177
    title=${title-"Package \`${package_name}'"}
178
    dir=${1}/man
15234 hornik 179
    test -z "${output}" && output="`basename ${1}`.${out_ext}"    
6994 pd 180
  else
13529 hornik 181
    if test -d ${1}/man; then
182
      dir=${1}/man
183
    else
184
      dir=${1}
185
    fi
15168 pd 186
    subj0=`${echo} ${dir} | sed -e ${file_sed}`
187
    subj="all in \\file{${subj0}}"
6994 pd 188
  fi
4662 pd 189
else
13529 hornik 190
  if test ${#} -gt 1 ; then
6994 pd 191
    subj=" etc.";
192
  else
193
    subj=
7367 hornik 194
    toc=
15234 hornik 195
    if test -z "${output}"; then
196
      output=`basename "${1}"`
197
      output="`echo ${output} | sed 's/[Rr]d$//'`${out_ext}"
198
    fi
6994 pd 199
  fi
15168 pd 200
  subj0=`${echo} ${1} | sed -e ${file_sed}`
201
  subj="\\file{${subj0}}${subj}"
3279 pd 202
fi
13529 hornik 203
title=${title-"\\R{} documentation}} \\par\\bigskip{{\\Large of ${subj}"}
6994 pd 204
 
13529 hornik 205
## Prepare for building the documentation.
15234 hornik 206
if test -z "${output}"; then
207
  output="Rd2.${out_ext}"
208
fi
7367 hornik 209
if test -f ${output}; then
20002 hornik 210
  ${echo} "file '${output}' exists; please remove first"
6994 pd 211
  exit 1
212
fi
7367 hornik 213
build_dir=.Rd2dvi${$}
214
if test -d ${build_dir}; then
215
  rm -rf ${build_dir} || echo "cannot write to build dir" && exit 2
6994 pd 216
fi
7367 hornik 217
mkdir ${build_dir}
6994 pd 218
 
13529 hornik 219
## Rd2.tex part 1: header
21964 hornik 220
if test ${batch}; then
221
  cat > ${build_dir}/Rd2.tex <<EOF
222
\\nonstopmode{}
223
EOF
224
else
225
  cat > ${build_dir}/Rd2.tex <<EOF
226
EOF
227
fi
228
cat >> ${build_dir}/Rd2.tex <<EOF
7208 hornik 229
\\documentclass[${R_PAPERSIZE}paper]{book}
11550 hornik 230
\\usepackage[${R_RD4DVI-ae}]{Rd}
6994 pd 231
\\usepackage{makeidx}
13964 hornik 232
\\makeindex{}
6994 pd 233
\\begin{document}
13964 hornik 234
EOF
15168 pd 235
if test "${is_bundle}" = no; then
13964 hornik 236
  cat >> ${build_dir}/Rd2.tex <<EOF
6994 pd 237
\\chapter*{}
238
\\begin{center}
7367 hornik 239
{\\textbf{\\huge ${title}}}
13529 hornik 240
\\par\\bigskip{\\large \\today}
6994 pd 241
\\end{center}
242
EOF
14002 hornik 243
  if test -f ${1}/DESCRIPTION; then
244
    Rd_DESCRIPTION_to_LaTeX ${1}/DESCRIPTION >> ${build_dir}/Rd2.tex
245
  fi
15168 pd 246
  if test "${is_base_package}" = yes; then
14544 hornik 247
    R_version=unknown
248
    if test -f ${1}/../../../VERSION; then
249
      R_version=`cat ${1}/../../../VERSION`
250
    fi
251
    Rd_DESCRIPTION_to_LaTeX ${1}/DESCRIPTION.in | \
252
      sed "s/@VERSION@/${R_version}/" >> ${build_dir}/Rd2.tex
253
  fi
13964 hornik 254
else
255
  cat >> ${build_dir}/Rd2.tex <<EOF
256
\\pagenumbering{Roman}
257
\\begin{titlepage}
258
\\strut\\vfill
259
\\begin{center}
260
{\\textbf{\\Huge ${title}}}
261
\\par\\bigskip{\\large \\today}
262
\\end{center}
14002 hornik 263
\\par\\bigskip
264
EOF
265
  Rd_DESCRIPTION_to_LaTeX ${1}/DESCRIPTION >> ${build_dir}/Rd2.tex
266
  cat >> ${build_dir}/Rd2.tex <<EOF
13964 hornik 267
\\vfill\\vfill
268
\\end{titlepage}
269
EOF
270
fi
271
 
13529 hornik 272
## Rd2.tex part 2: body
15168 pd 273
if test "${is_bundle}" = no; then
274
  ${echo} ${toc} >> ${build_dir}/Rd2.tex
13529 hornik 275
  Rdconv_dir_or_files_to_LaTeX ${build_dir}/Rd2.tex ${dir-${@}}
276
else
13964 hornik 277
  cat >> ${build_dir}/Rd2.tex <<EOF
15234 hornik 278
\\setcounter{secnumdepth}{-1}
13964 hornik 279
\\pagenumbering{roman}
280
\\tableofcontents{}
281
\\cleardoublepage{}
282
\\pagenumbering{arabic}
283
EOF
13529 hornik 284
  for p in ${bundle_pkgs}; do
20002 hornik 285
    ${echo} "Bundle package: '${p}'"
15168 pd 286
    ${echo} "\\chapter{Package \`${p}'}" >> ${build_dir}/Rd2.tex
14002 hornik 287
    if test -f ${1}/${p}/DESCRIPTION.in; then
288
      Rd_DESCRIPTION_to_LaTeX ${1}/${p}/DESCRIPTION.in \
289
        >> ${build_dir}/Rd2.tex
290
    fi
13529 hornik 291
    Rdconv_dir_or_files_to_LaTeX ${build_dir}/Rd2.tex ${1}/${p}/man
15168 pd 292
    ${echo} "\\clearpage{}" >> ${build_dir}/Rd2.tex
13529 hornik 293
  done
15168 pd 294
  ${echo} "\\cleardoublepage{}" >> ${build_dir}/Rd2.tex
13529 hornik 295
fi
6994 pd 296
 
13529 hornik 297
## Rd2.tex part 3: footer
7367 hornik 298
cat >> ${build_dir}/Rd2.tex <<EOF
13964 hornik 299
\\printindex{}
6994 pd 300
\\end{document}
301
EOF
302
 
21964 hornik 303
## <FIXME>
304
## Need to do something smarter about the exit status in batch mode.
305
status=0
306
## <FIXME>
307
 
9940 pd 308
echo "Creating ${out_ext} output from LaTeX ..."
7367 hornik 309
cd ${build_dir}
21964 hornik 310
${R_LATEXCMD-latex} Rd2 || status=1
11550 hornik 311
${R_MAKEINDEXCMD-makeindex} Rd2
312
${R_LATEXCMD-latex} Rd2
15168 pd 313
if test "${out_ext}" = pdf; then
13964 hornik 314
  ${R_LATEXCMD-latex} Rd2
315
fi
7367 hornik 316
cd ${start_dir}
20002 hornik 317
${echo} "Saving output to '${output}' ..."
9940 pd 318
cp ${build_dir}/Rd2.${out_ext} ${output}
13529 hornik 319
echo "Done"
320
 
7367 hornik 321
if ${clean}; then
322
  rm -rf ${build_dir}
6994 pd 323
else
20002 hornik 324
  ${echo} "You may want to clean up by 'rm -rf ${build_dir}'"
2195 hornik 325
fi
7367 hornik 326
${preview} ${output}
21964 hornik 327
exit ${status}
7218 hornik 328
 
329
### Local Variables: ***
330
### mode: sh ***
331
### sh-indentation: 2 ***
332
### End: ***