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
21964 hornik 30
  -v, --version		print version info and exit
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
 
15168 pd 55
## Need a `safe' echo which does not interpret backslash-escaped
56
## characters in SysV style.
57
echo="sh ${R_HOME}/share/sh/echo.sh"
58
 
6994 pd 59
while test -n "${1}"; do
60
  case ${1} in
7367 hornik 61
    -h|--help)
15168 pd 62
      ${echo} "${usage}"; exit 0 ;;
7367 hornik 63
    -v|--version)
15168 pd 64
      ${echo} "${version}"; exit 0 ;;
21964 hornik 65
    --batch)
66
      batch=true ;;
7367 hornik 67
    --debug)
68
      debug=true ;;
69
    --no-clean)
70
      clean=false ;;
71
    --no-preview)
72
      preview=false ;;
7295 leisch 73
    --pdf)
9940 pd 74
      out_ext="pdf";
7367 hornik 75
      preview=false;
11550 hornik 76
      R_RD4DVI=${R_RD4PDF-"ae,hyper"};
13964 hornik 77
      R_LATEXCMD=${PDFLATEX-pdflatex} ;;
7295 leisch 78
    --title=*)
7367 hornik 79
      title=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
9940 pd 80
    -o)
81
      if test -n "`echo ${2} | sed 's/^-.*//'`"; then      
82
	output="${2}"; shift
83
      else
20002 hornik 84
	${echo} "ERROR: option '${1}' requires an argument"
9940 pd 85
	exit 1
86
      fi
87
      ;;
7333 leisch 88
    --output=*)
7367 hornik 89
      output=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
13529 hornik 90
    --OS=*|--os=*)
91
      OSdir=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
7367 hornik 92
    -V|--verbose)
15168 pd 93
      verbose=${echo} ;;
6994 pd 94
    --|*)
95
      break ;;
96
  esac
97
  shift
98
done
99
 
7367 hornik 100
if ${debug}; then set -x; fi
101
 
13964 hornik 102
. ${R_HOME}/share/sh/dcf.sh	# get_dcf_field()
13529 hornik 103
 
104
Rdconv_dir_or_files_to_LaTeX () {
105
  ## Convert Rd files in a dir or a list of Rd files to LaTeX, appending
106
  ## the result to OUTFILE.
107
  ## Usage:
108
  ##   Rdconv_dir_or_files_to_LaTeX OUTFILE DIR
109
  ##   Rdconv_dir_or_files_to_LaTeX OUTFILE FILES
110
 
111
  ${verbose} $@
112
 
113
  out="${1}"
114
  shift
115
 
116
  if test -d ${1}; then
117
    files=`ls ${1}/*.[Rr]d`
118
    if test -d ${1}/${OSdir}; then
119
      files="${files} `ls ${1}/${OSdir}/*.[Rr]d`"
120
    fi
15168 pd 121
    files=`LC_ALL=C ${echo} ${files} | sort`
13529 hornik 122
  else
123
    files="${@}"
124
  fi
125
 
126
  echo "Converting Rd files to LaTeX ..."
127
 
128
  for f in ${files}; do
15168 pd 129
    ${echo} ${f}
13529 hornik 130
    ${R_CMD} Rdconv -t latex ${f} >> ${out}
131
  done
132
 
133
}
134
 
14002 hornik 135
Rd_DESCRIPTION_to_LaTeX () {
136
  ## Typeset the contents of a DESCRIPTION file in a LaTeX description
137
  ## list.
138
  ## Usage:
139
  ##   Rd_DESCRIPTION_to_LaTeX FILE
15168 pd 140
 
14002 hornik 141
  fields=`sed '/^[ 	]/d; s/^\([^:]*\):.*$/\1/' $1`
15168 pd 142
  ${echo} "\\begin{description}"
143
  ${echo} "\\raggedright{}"
144
  for f in `${echo} "${fields}" | sed '/Package/d; /Bundle/d;'`; do
14002 hornik 145
    text=`get_dcf_field ${f} ${1}`
15168 pd 146
    ${echo} "\\item[${f}] \\AsIs{${text}}"
14002 hornik 147
  done
15168 pd 148
  ${echo} "\\end{description}"
14002 hornik 149
}
150
 
13529 hornik 151
is_bundle=no
14544 hornik 152
is_base_package=no
13529 hornik 153
file_sed='s/[_$]/\\&/g'
154
 
9940 pd 155
toc="\\Rdcontents{\\R{} topics documented:}"
13529 hornik 156
if test -d "${1}"; then
157
  if test -f ${1}/DESCRIPTION; then
158
    if test -n "`grep '^Bundle:' ${1}/DESCRIPTION`"; then
159
      echo "Hmm ... looks like a package bundle"
160
      is_bundle=yes
161
      bundle_name=`get_dcf_field Bundle "${1}/DESCRIPTION"`
162
      bundle_pkgs=`get_dcf_field Contains "${1}/DESCRIPTION"`
163
      title=${title-"Bundle \`${bundle_name}'"}
164
    else
165
      echo "Hmm ... looks like a package"
14002 hornik 166
      package_name=`get_dcf_field Package "${1}/DESCRIPTION"`
167
      title=${title-"Package \`${package_name}'"}
13529 hornik 168
      dir=${1}/man
169
    fi
15234 hornik 170
    test -z "${output}" && output="`basename ${1}`.${out_ext}"
14544 hornik 171
  elif test -f ${1}/DESCRIPTION.in && \
172
       test -n "`grep '^Priority: *base' ${1}/DESCRIPTION.in`"; then
173
    is_base_package=yes
174
    echo "Hmm ... looks like a package from the R distribution"
175
    package_name=`get_dcf_field Package "${1}/DESCRIPTION.in"`
176
    title=${title-"Package \`${package_name}'"}
177
    dir=${1}/man
15234 hornik 178
    test -z "${output}" && output="`basename ${1}`.${out_ext}"    
6994 pd 179
  else
13529 hornik 180
    if test -d ${1}/man; then
181
      dir=${1}/man
182
    else
183
      dir=${1}
184
    fi
15168 pd 185
    subj0=`${echo} ${dir} | sed -e ${file_sed}`
186
    subj="all in \\file{${subj0}}"
6994 pd 187
  fi
4662 pd 188
else
13529 hornik 189
  if test ${#} -gt 1 ; then
6994 pd 190
    subj=" etc.";
191
  else
192
    subj=
7367 hornik 193
    toc=
15234 hornik 194
    if test -z "${output}"; then
195
      output=`basename "${1}"`
196
      output="`echo ${output} | sed 's/[Rr]d$//'`${out_ext}"
197
    fi
6994 pd 198
  fi
15168 pd 199
  subj0=`${echo} ${1} | sed -e ${file_sed}`
200
  subj="\\file{${subj0}}${subj}"
3279 pd 201
fi
13529 hornik 202
title=${title-"\\R{} documentation}} \\par\\bigskip{{\\Large of ${subj}"}
6994 pd 203
 
13529 hornik 204
## Prepare for building the documentation.
15234 hornik 205
if test -z "${output}"; then
206
  output="Rd2.${out_ext}"
207
fi
7367 hornik 208
if test -f ${output}; then
20002 hornik 209
  ${echo} "file '${output}' exists; please remove first"
6994 pd 210
  exit 1
211
fi
7367 hornik 212
build_dir=.Rd2dvi${$}
213
if test -d ${build_dir}; then
214
  rm -rf ${build_dir} || echo "cannot write to build dir" && exit 2
6994 pd 215
fi
7367 hornik 216
mkdir ${build_dir}
6994 pd 217
 
13529 hornik 218
## Rd2.tex part 1: header
21964 hornik 219
if test ${batch}; then
220
  cat > ${build_dir}/Rd2.tex <<EOF
221
\\nonstopmode{}
222
EOF
223
else
224
  cat > ${build_dir}/Rd2.tex <<EOF
225
EOF
226
fi
227
cat >> ${build_dir}/Rd2.tex <<EOF
7208 hornik 228
\\documentclass[${R_PAPERSIZE}paper]{book}
11550 hornik 229
\\usepackage[${R_RD4DVI-ae}]{Rd}
6994 pd 230
\\usepackage{makeidx}
13964 hornik 231
\\makeindex{}
6994 pd 232
\\begin{document}
13964 hornik 233
EOF
15168 pd 234
if test "${is_bundle}" = no; then
13964 hornik 235
  cat >> ${build_dir}/Rd2.tex <<EOF
6994 pd 236
\\chapter*{}
237
\\begin{center}
7367 hornik 238
{\\textbf{\\huge ${title}}}
13529 hornik 239
\\par\\bigskip{\\large \\today}
6994 pd 240
\\end{center}
241
EOF
14002 hornik 242
  if test -f ${1}/DESCRIPTION; then
243
    Rd_DESCRIPTION_to_LaTeX ${1}/DESCRIPTION >> ${build_dir}/Rd2.tex
244
  fi
15168 pd 245
  if test "${is_base_package}" = yes; then
14544 hornik 246
    R_version=unknown
247
    if test -f ${1}/../../../VERSION; then
248
      R_version=`cat ${1}/../../../VERSION`
249
    fi
250
    Rd_DESCRIPTION_to_LaTeX ${1}/DESCRIPTION.in | \
251
      sed "s/@VERSION@/${R_version}/" >> ${build_dir}/Rd2.tex
252
  fi
13964 hornik 253
else
254
  cat >> ${build_dir}/Rd2.tex <<EOF
255
\\pagenumbering{Roman}
256
\\begin{titlepage}
257
\\strut\\vfill
258
\\begin{center}
259
{\\textbf{\\Huge ${title}}}
260
\\par\\bigskip{\\large \\today}
261
\\end{center}
14002 hornik 262
\\par\\bigskip
263
EOF
264
  Rd_DESCRIPTION_to_LaTeX ${1}/DESCRIPTION >> ${build_dir}/Rd2.tex
265
  cat >> ${build_dir}/Rd2.tex <<EOF
13964 hornik 266
\\vfill\\vfill
267
\\end{titlepage}
268
EOF
269
fi
270
 
13529 hornik 271
## Rd2.tex part 2: body
15168 pd 272
if test "${is_bundle}" = no; then
273
  ${echo} ${toc} >> ${build_dir}/Rd2.tex
13529 hornik 274
  Rdconv_dir_or_files_to_LaTeX ${build_dir}/Rd2.tex ${dir-${@}}
275
else
13964 hornik 276
  cat >> ${build_dir}/Rd2.tex <<EOF
15234 hornik 277
\\setcounter{secnumdepth}{-1}
13964 hornik 278
\\pagenumbering{roman}
279
\\tableofcontents{}
280
\\cleardoublepage{}
281
\\pagenumbering{arabic}
282
EOF
13529 hornik 283
  for p in ${bundle_pkgs}; do
20002 hornik 284
    ${echo} "Bundle package: '${p}'"
15168 pd 285
    ${echo} "\\chapter{Package \`${p}'}" >> ${build_dir}/Rd2.tex
14002 hornik 286
    if test -f ${1}/${p}/DESCRIPTION.in; then
287
      Rd_DESCRIPTION_to_LaTeX ${1}/${p}/DESCRIPTION.in \
288
        >> ${build_dir}/Rd2.tex
289
    fi
13529 hornik 290
    Rdconv_dir_or_files_to_LaTeX ${build_dir}/Rd2.tex ${1}/${p}/man
15168 pd 291
    ${echo} "\\clearpage{}" >> ${build_dir}/Rd2.tex
13529 hornik 292
  done
15168 pd 293
  ${echo} "\\cleardoublepage{}" >> ${build_dir}/Rd2.tex
13529 hornik 294
fi
6994 pd 295
 
13529 hornik 296
## Rd2.tex part 3: footer
7367 hornik 297
cat >> ${build_dir}/Rd2.tex <<EOF
13964 hornik 298
\\printindex{}
6994 pd 299
\\end{document}
300
EOF
301
 
21964 hornik 302
## <FIXME>
303
## Need to do something smarter about the exit status in batch mode.
304
status=0
305
## <FIXME>
306
 
9940 pd 307
echo "Creating ${out_ext} output from LaTeX ..."
7367 hornik 308
cd ${build_dir}
21964 hornik 309
${R_LATEXCMD-latex} Rd2 || status=1
11550 hornik 310
${R_MAKEINDEXCMD-makeindex} Rd2
311
${R_LATEXCMD-latex} Rd2
15168 pd 312
if test "${out_ext}" = pdf; then
13964 hornik 313
  ${R_LATEXCMD-latex} Rd2
314
fi
7367 hornik 315
cd ${start_dir}
20002 hornik 316
${echo} "Saving output to '${output}' ..."
9940 pd 317
cp ${build_dir}/Rd2.${out_ext} ${output}
13529 hornik 318
echo "Done"
319
 
7367 hornik 320
if ${clean}; then
321
  rm -rf ${build_dir}
6994 pd 322
else
20002 hornik 323
  ${echo} "You may want to clean up by 'rm -rf ${build_dir}'"
2195 hornik 324
fi
7367 hornik 325
${preview} ${output}
21964 hornik 326
exit ${status}
7218 hornik 327
 
328
### Local Variables: ***
329
### mode: sh ***
330
### sh-indentation: 2 ***
331
### End: ***