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" \
34818 ripley 8
##                  /path/to/Rsrc/src/library/stats/man/*.Rd | sort | uniq`
11515 hornik 9
 
34818 ripley 10
revision='$Revision: 1.41 $'
7367 hornik 11
version=`set - ${revision}; echo ${2}`
11515 hornik 12
version="Rd2dvi ${version}
7217 hornik 13
 
34818 ripley 14
Copyright (C) 2000-2005 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
34431 hornik 146
    text=`get_dcf_field ${f} ${1} | \
147
      tr '\n' ' ' | \
148
      sed "s/\"\([^\"]*\)\"/\\\`\\\`\\1''/g"`
15168 pd 149
    ${echo} "\\item[${f}] \\AsIs{${text}}"
14002 hornik 150
  done
15168 pd 151
  ${echo} "\\end{description}"
14002 hornik 152
}
153
 
13529 hornik 154
is_bundle=no
14544 hornik 155
is_base_package=no
13529 hornik 156
file_sed='s/[_$]/\\&/g'
157
 
9940 pd 158
toc="\\Rdcontents{\\R{} topics documented:}"
13529 hornik 159
if test -d "${1}"; then
160
  if test -f ${1}/DESCRIPTION; then
161
    if test -n "`grep '^Bundle:' ${1}/DESCRIPTION`"; then
162
      echo "Hmm ... looks like a package bundle"
163
      is_bundle=yes
164
      bundle_name=`get_dcf_field Bundle "${1}/DESCRIPTION"`
165
      bundle_pkgs=`get_dcf_field Contains "${1}/DESCRIPTION"`
166
      title=${title-"Bundle \`${bundle_name}'"}
167
    else
168
      echo "Hmm ... looks like a package"
14002 hornik 169
      package_name=`get_dcf_field Package "${1}/DESCRIPTION"`
170
      title=${title-"Package \`${package_name}'"}
13529 hornik 171
      dir=${1}/man
172
    fi
15234 hornik 173
    test -z "${output}" && output="`basename ${1}`.${out_ext}"
14544 hornik 174
  elif test -f ${1}/DESCRIPTION.in && \
175
       test -n "`grep '^Priority: *base' ${1}/DESCRIPTION.in`"; then
176
    is_base_package=yes
177
    echo "Hmm ... looks like a package from the R distribution"
178
    package_name=`get_dcf_field Package "${1}/DESCRIPTION.in"`
179
    title=${title-"Package \`${package_name}'"}
180
    dir=${1}/man
15234 hornik 181
    test -z "${output}" && output="`basename ${1}`.${out_ext}"    
6994 pd 182
  else
13529 hornik 183
    if test -d ${1}/man; then
184
      dir=${1}/man
185
    else
186
      dir=${1}
187
    fi
15168 pd 188
    subj0=`${echo} ${dir} | sed -e ${file_sed}`
189
    subj="all in \\file{${subj0}}"
6994 pd 190
  fi
4662 pd 191
else
13529 hornik 192
  if test ${#} -gt 1 ; then
6994 pd 193
    subj=" etc.";
194
  else
195
    subj=
7367 hornik 196
    toc=
15234 hornik 197
    if test -z "${output}"; then
198
      output=`basename "${1}"`
199
      output="`echo ${output} | sed 's/[Rr]d$//'`${out_ext}"
200
    fi
6994 pd 201
  fi
15168 pd 202
  subj0=`${echo} ${1} | sed -e ${file_sed}`
203
  subj="\\file{${subj0}}${subj}"
3279 pd 204
fi
13529 hornik 205
title=${title-"\\R{} documentation}} \\par\\bigskip{{\\Large of ${subj}"}
6994 pd 206
 
13529 hornik 207
## Prepare for building the documentation.
15234 hornik 208
if test -z "${output}"; then
209
  output="Rd2.${out_ext}"
210
fi
7367 hornik 211
if test -f ${output}; then
20002 hornik 212
  ${echo} "file '${output}' exists; please remove first"
6994 pd 213
  exit 1
214
fi
7367 hornik 215
build_dir=.Rd2dvi${$}
216
if test -d ${build_dir}; then
217
  rm -rf ${build_dir} || echo "cannot write to build dir" && exit 2
6994 pd 218
fi
7367 hornik 219
mkdir ${build_dir}
6994 pd 220
 
13529 hornik 221
## Rd2.tex part 1: header
21964 hornik 222
if test ${batch}; then
223
  cat > ${build_dir}/Rd2.tex <<EOF
224
\\nonstopmode{}
225
EOF
226
else
227
  cat > ${build_dir}/Rd2.tex <<EOF
228
EOF
229
fi
230
cat >> ${build_dir}/Rd2.tex <<EOF
7208 hornik 231
\\documentclass[${R_PAPERSIZE}paper]{book}
11550 hornik 232
\\usepackage[${R_RD4DVI-ae}]{Rd}
6994 pd 233
\\usepackage{makeidx}
34818 ripley 234
\\usepackage[@ENC@]{inputenc}
13964 hornik 235
\\makeindex{}
6994 pd 236
\\begin{document}
13964 hornik 237
EOF
15168 pd 238
if test "${is_bundle}" = no; then
13964 hornik 239
  cat >> ${build_dir}/Rd2.tex <<EOF
6994 pd 240
\\chapter*{}
241
\\begin{center}
7367 hornik 242
{\\textbf{\\huge ${title}}}
13529 hornik 243
\\par\\bigskip{\\large \\today}
6994 pd 244
\\end{center}
245
EOF
14002 hornik 246
  if test -f ${1}/DESCRIPTION; then
247
    Rd_DESCRIPTION_to_LaTeX ${1}/DESCRIPTION >> ${build_dir}/Rd2.tex
248
  fi
15168 pd 249
  if test "${is_base_package}" = yes; then
14544 hornik 250
    R_version=unknown
251
    if test -f ${1}/../../../VERSION; then
252
      R_version=`cat ${1}/../../../VERSION`
253
    fi
254
    Rd_DESCRIPTION_to_LaTeX ${1}/DESCRIPTION.in | \
255
      sed "s/@VERSION@/${R_version}/" >> ${build_dir}/Rd2.tex
256
  fi
13964 hornik 257
else
258
  cat >> ${build_dir}/Rd2.tex <<EOF
259
\\pagenumbering{Roman}
260
\\begin{titlepage}
261
\\strut\\vfill
262
\\begin{center}
263
{\\textbf{\\Huge ${title}}}
264
\\par\\bigskip{\\large \\today}
265
\\end{center}
14002 hornik 266
\\par\\bigskip
267
EOF
268
  Rd_DESCRIPTION_to_LaTeX ${1}/DESCRIPTION >> ${build_dir}/Rd2.tex
269
  cat >> ${build_dir}/Rd2.tex <<EOF
13964 hornik 270
\\vfill\\vfill
271
\\end{titlepage}
272
EOF
273
fi
274
 
13529 hornik 275
## Rd2.tex part 2: body
15168 pd 276
if test "${is_bundle}" = no; then
277
  ${echo} ${toc} >> ${build_dir}/Rd2.tex
13529 hornik 278
  Rdconv_dir_or_files_to_LaTeX ${build_dir}/Rd2.tex ${dir-${@}}
279
else
13964 hornik 280
  cat >> ${build_dir}/Rd2.tex <<EOF
15234 hornik 281
\\setcounter{secnumdepth}{-1}
13964 hornik 282
\\pagenumbering{roman}
283
\\tableofcontents{}
284
\\cleardoublepage{}
285
\\pagenumbering{arabic}
286
EOF
13529 hornik 287
  for p in ${bundle_pkgs}; do
20002 hornik 288
    ${echo} "Bundle package: '${p}'"
15168 pd 289
    ${echo} "\\chapter{Package \`${p}'}" >> ${build_dir}/Rd2.tex
14002 hornik 290
    if test -f ${1}/${p}/DESCRIPTION.in; then
291
      Rd_DESCRIPTION_to_LaTeX ${1}/${p}/DESCRIPTION.in \
292
        >> ${build_dir}/Rd2.tex
293
    fi
13529 hornik 294
    Rdconv_dir_or_files_to_LaTeX ${build_dir}/Rd2.tex ${1}/${p}/man
15168 pd 295
    ${echo} "\\clearpage{}" >> ${build_dir}/Rd2.tex
13529 hornik 296
  done
15168 pd 297
  ${echo} "\\cleardoublepage{}" >> ${build_dir}/Rd2.tex
13529 hornik 298
fi
6994 pd 299
 
13529 hornik 300
## Rd2.tex part 3: footer
7367 hornik 301
cat >> ${build_dir}/Rd2.tex <<EOF
13964 hornik 302
\\printindex{}
6994 pd 303
\\end{document}
304
EOF
305
 
34818 ripley 306
## Look for encodings
307
ENCS=`grep '^\\\\inputencoding' ${build_dir}/Rd2.tex | uniq |\
308
  sed -e 's/^\\\\inputencoding{\(.*\)}/\1/'`
309
ENCS=`grep '^\\\\\inputencoding' ${build_dir}/Rd2.tex |  uniq | \
310
  sed -e 's/^\\\\inputencoding{\(.*\)}/\1/' | \
311
  tr '\na-z0-9' ',a-z0-9' | sed -e s/,$//`
312
echo "ENCS is ${ENCS}"
313
 
314
## substitute for the encodings used
315
mv ${build_dir}/Rd2.tex ${build_dir}/Rd2.tex.pre
316
if test -z "${ENCS}"; then
317
  sed -e '/^\\usepackage\[@ENC@\]{inputenc}$/d' \
318
    ${build_dir}/Rd2.tex.pre > ${build_dir}/Rd2.tex
319
else
320
  sed -e s/^\\\\usepackage\\[@ENC@\\]/\\\\usepackage[${ENCS}]/ \
321
    ${build_dir}/Rd2.tex.pre > ${build_dir}/Rd2.tex
322
fi
323
 
21964 hornik 324
## <FIXME>
325
## Need to do something smarter about the exit status in batch mode.
326
status=0
327
## <FIXME>
328
 
9940 pd 329
echo "Creating ${out_ext} output from LaTeX ..."
7367 hornik 330
cd ${build_dir}
21964 hornik 331
${R_LATEXCMD-latex} Rd2 || status=1
11550 hornik 332
${R_MAKEINDEXCMD-makeindex} Rd2
333
${R_LATEXCMD-latex} Rd2
15168 pd 334
if test "${out_ext}" = pdf; then
13964 hornik 335
  ${R_LATEXCMD-latex} Rd2
336
fi
7367 hornik 337
cd ${start_dir}
20002 hornik 338
${echo} "Saving output to '${output}' ..."
9940 pd 339
cp ${build_dir}/Rd2.${out_ext} ${output}
13529 hornik 340
echo "Done"
341
 
7367 hornik 342
if ${clean}; then
343
  rm -rf ${build_dir}
6994 pd 344
else
20002 hornik 345
  ${echo} "You may want to clean up by 'rm -rf ${build_dir}'"
2195 hornik 346
fi
7367 hornik 347
${preview} ${output}
21964 hornik 348
exit ${status}
7218 hornik 349
 
350
### Local Variables: ***
351
### mode: sh ***
352
### sh-indentation: 2 ***
353
### End: ***